blob: de3a0cdad6f401ff63a829cf95c1f06e495ed806 [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
Eugene Zelenkofb69e662017-06-06 22:22:41 +000014#include "MIParser.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000015#include "MILexer.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000016#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/APSInt.h"
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/None.h"
21#include "llvm/ADT/Optional.h"
22#include "llvm/ADT/SmallVector.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000023#include "llvm/ADT/StringMap.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000024#include "llvm/ADT/StringRef.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000025#include "llvm/ADT/StringSwitch.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000026#include "llvm/ADT/Twine.h"
Alex Lorenzad156fb2015-07-31 20:49:21 +000027#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000028#include "llvm/AsmParser/SlotMapping.h"
Matthias Braun89401142017-05-05 21:09:30 +000029#include "llvm/CodeGen/MIRPrinter.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
Alex Lorenz7feaf7c2015-07-16 23:37:45 +000031#include "llvm/CodeGen/MachineFrameInfo.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000032#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000033#include "llvm/CodeGen/MachineInstr.h"
Alex Lorenzcb268d42015-07-06 23:07:26 +000034#include "llvm/CodeGen/MachineInstrBuilder.h"
Alex Lorenz4af7e612015-08-03 23:08:19 +000035#include "llvm/CodeGen/MachineMemOperand.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000036#include "llvm/CodeGen/MachineOperand.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000037#include "llvm/CodeGen/MachineRegisterInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000038#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000039#include "llvm/CodeGen/TargetRegisterInfo.h"
40#include "llvm/CodeGen/TargetSubtargetInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000041#include "llvm/IR/BasicBlock.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000042#include "llvm/IR/Constants.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000043#include "llvm/IR/DataLayout.h"
Reid Kleckner6d353342017-08-23 20:31:27 +000044#include "llvm/IR/DebugInfoMetadata.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000045#include "llvm/IR/DebugLoc.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/InstrTypes.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000048#include "llvm/IR/Instructions.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000049#include "llvm/IR/Intrinsics.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000050#include "llvm/IR/Metadata.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000051#include "llvm/IR/Module.h"
Alex Lorenz8a1915b2015-07-27 22:42:41 +000052#include "llvm/IR/ModuleSlotTracker.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000053#include "llvm/IR/Type.h"
54#include "llvm/IR/Value.h"
Alex Lorenzdeb53492015-07-28 17:28:03 +000055#include "llvm/IR/ValueSymbolTable.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000056#include "llvm/MC/LaneBitmask.h"
57#include "llvm/MC/MCDwarf.h"
58#include "llvm/MC/MCInstrDesc.h"
59#include "llvm/MC/MCRegisterInfo.h"
60#include "llvm/Support/AtomicOrdering.h"
61#include "llvm/Support/BranchProbability.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/ErrorHandling.h"
64#include "llvm/Support/LowLevelTypeImpl.h"
65#include "llvm/Support/MemoryBuffer.h"
66#include "llvm/Support/SMLoc.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000067#include "llvm/Support/SourceMgr.h"
Quentin Colombet2a831fb2016-03-07 21:48:43 +000068#include "llvm/Support/raw_ostream.h"
Tim Northover6b3bd612016-07-29 20:32:59 +000069#include "llvm/Target/TargetIntrinsicInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000070#include "llvm/Target/TargetMachine.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000071#include <algorithm>
72#include <cassert>
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +000073#include <cctype>
Eugene Zelenkofb69e662017-06-06 22:22:41 +000074#include <cstddef>
75#include <cstdint>
76#include <limits>
77#include <string>
78#include <utility>
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000079
80using namespace llvm;
81
Matthias Braune35861d2016-07-13 23:27:50 +000082PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
Matthias Braunde5fea22017-01-18 00:59:19 +000083 SourceMgr &SM, const SlotMapping &IRSlots,
84 const Name2RegClassMap &Names2RegClasses,
85 const Name2RegBankMap &Names2RegBanks)
86 : MF(MF), SM(&SM), IRSlots(IRSlots), Names2RegClasses(Names2RegClasses),
87 Names2RegBanks(Names2RegBanks) {
Matthias Braun83947862016-07-13 22:23:23 +000088}
89
Matthias Braun74ad41c2016-10-11 03:13:01 +000090VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) {
91 auto I = VRegInfos.insert(std::make_pair(Num, nullptr));
92 if (I.second) {
93 MachineRegisterInfo &MRI = MF.getRegInfo();
94 VRegInfo *Info = new (Allocator) VRegInfo;
95 Info->VReg = MRI.createIncompleteVirtualRegister();
96 I.first->second = Info;
97 }
98 return *I.first->second;
99}
100
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000101VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) {
102 assert(RegName != "" && "Expected named reg.");
103
104 auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr));
105 if (I.second) {
106 VRegInfo *Info = new (Allocator) VRegInfo;
107 Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName);
108 I.first->second = Info;
109 }
110 return *I.first->second;
111}
112
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000113namespace {
114
Alex Lorenz36962cd2015-07-07 02:08:46 +0000115/// A wrapper struct around the 'MachineOperand' struct that includes a source
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000116/// range and other attributes.
117struct ParsedMachineOperand {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000118 MachineOperand Operand;
119 StringRef::iterator Begin;
120 StringRef::iterator End;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000121 Optional<unsigned> TiedDefIdx;
Alex Lorenz36962cd2015-07-07 02:08:46 +0000122
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000123 ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin,
124 StringRef::iterator End, Optional<unsigned> &TiedDefIdx)
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000125 : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) {
126 if (TiedDefIdx)
127 assert(Operand.isReg() && Operand.isUse() &&
128 "Only used register operands can be tied");
129 }
Alex Lorenz36962cd2015-07-07 02:08:46 +0000130};
131
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000132class MIParser {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000133 MachineFunction &MF;
134 SMDiagnostic &Error;
Alex Lorenz91370c52015-06-22 20:37:46 +0000135 StringRef Source, CurrentSource;
136 MIToken Token;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000137 PerFunctionMIParsingState &PFS;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000138 /// Maps from instruction names to op codes.
139 StringMap<unsigned> Names2InstrOpCodes;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000140 /// Maps from register names to registers.
141 StringMap<unsigned> Names2Regs;
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000142 /// Maps from register mask names to register masks.
143 StringMap<const uint32_t *> Names2RegMasks;
Alex Lorenz2eacca82015-07-13 23:24:34 +0000144 /// Maps from subregister names to subregister indices.
145 StringMap<unsigned> Names2SubRegIndices;
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000146 /// Maps from slot numbers to function's unnamed basic blocks.
147 DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks;
Alex Lorenzdd13be02015-08-19 23:31:05 +0000148 /// Maps from slot numbers to function's unnamed values.
149 DenseMap<unsigned, const Value *> Slots2Values;
Alex Lorenzef5c1962015-07-28 23:02:45 +0000150 /// Maps from target index names to target indices.
151 StringMap<int> Names2TargetIndices;
Alex Lorenz49873a82015-08-06 00:44:07 +0000152 /// Maps from direct target flag names to the direct target flag values.
153 StringMap<unsigned> Names2DirectTargetFlags;
Alex Lorenzf3630112015-08-18 22:52:15 +0000154 /// Maps from direct target flag names to the bitmask target flag values.
155 StringMap<unsigned> Names2BitmaskTargetFlags;
Geoff Berry6748abe2017-07-13 02:28:54 +0000156 /// Maps from MMO target flag names to MMO target flag values.
157 StringMap<MachineMemOperand::Flags> Names2MMOTargetFlags;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000158
159public:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000160 MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000161 StringRef Source);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000162
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000163 /// \p SkipChar gives the number of characters to skip before looking
164 /// for the next token.
165 void lex(unsigned SkipChar = 0);
Alex Lorenz91370c52015-06-22 20:37:46 +0000166
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000167 /// Report an error at the current location with the given message.
168 ///
169 /// This function always return true.
170 bool error(const Twine &Msg);
171
Alex Lorenz91370c52015-06-22 20:37:46 +0000172 /// Report an error at the given location with the given message.
173 ///
174 /// This function always return true.
175 bool error(StringRef::iterator Loc, const Twine &Msg);
176
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000177 bool
178 parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
179 bool parseBasicBlocks();
Alex Lorenz3708a642015-06-30 17:47:50 +0000180 bool parse(MachineInstr *&MI);
Alex Lorenz1ea60892015-07-27 20:29:27 +0000181 bool parseStandaloneMBB(MachineBasicBlock *&MBB);
182 bool parseStandaloneNamedRegister(unsigned &Reg);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000183 bool parseStandaloneVirtualRegister(VRegInfo *&Info);
Tom Stellard9c884e42016-11-15 00:03:14 +0000184 bool parseStandaloneRegister(unsigned &Reg);
Alex Lorenza314d812015-08-18 22:26:26 +0000185 bool parseStandaloneStackObject(int &FI);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000186 bool parseStandaloneMDNode(MDNode *&Node);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000187
188 bool
189 parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots);
Matthias Braun89401142017-05-05 21:09:30 +0000190 bool parseBasicBlock(MachineBasicBlock &MBB,
191 MachineBasicBlock *&AddFalthroughFrom);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000192 bool parseBasicBlockLiveins(MachineBasicBlock &MBB);
193 bool parseBasicBlockSuccessors(MachineBasicBlock &MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000194
Matthias Braun74ad41c2016-10-11 03:13:01 +0000195 bool parseNamedRegister(unsigned &Reg);
196 bool parseVirtualRegister(VRegInfo *&Info);
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000197 bool parseNamedVirtualRegister(VRegInfo *&Info);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000198 bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo);
Alex Lorenzcb268d42015-07-06 23:07:26 +0000199 bool parseRegisterFlag(unsigned &Flags);
Matthias Braunde5fea22017-01-18 00:59:19 +0000200 bool parseRegisterClassOrBank(VRegInfo &RegInfo);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000201 bool parseSubRegisterIndex(unsigned &SubReg);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000202 bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx);
203 bool parseRegisterOperand(MachineOperand &Dest,
204 Optional<unsigned> &TiedDefIdx, bool IsDef = false);
Alex Lorenz240fc1e2015-06-23 23:42:28 +0000205 bool parseImmediateOperand(MachineOperand &Dest);
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +0000206 bool parseIRConstant(StringRef::iterator Loc, StringRef Source,
207 const Constant *&C);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +0000208 bool parseIRConstant(StringRef::iterator Loc, const Constant *&C);
Ahmed Bougachad760de02016-07-28 17:15:12 +0000209 bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty);
Alex Lorenz05e38822015-08-05 18:52:21 +0000210 bool parseTypedImmediateOperand(MachineOperand &Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +0000211 bool parseFPImmediateOperand(MachineOperand &Dest);
Alex Lorenzf09df002015-06-30 18:16:42 +0000212 bool parseMBBReference(MachineBasicBlock *&MBB);
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000213 bool parseMBBOperand(MachineOperand &Dest);
Alex Lorenzdc9dadf2015-08-18 22:18:52 +0000214 bool parseStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000215 bool parseStackObjectOperand(MachineOperand &Dest);
Alex Lorenzea882122015-08-12 21:17:02 +0000216 bool parseFixedStackFrameIndex(int &FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000217 bool parseFixedStackObjectOperand(MachineOperand &Dest);
Alex Lorenz41df7d32015-07-28 17:09:52 +0000218 bool parseGlobalValue(GlobalValue *&GV);
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000219 bool parseGlobalAddressOperand(MachineOperand &Dest);
Alex Lorenzab980492015-07-20 20:51:18 +0000220 bool parseConstantPoolIndexOperand(MachineOperand &Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +0000221 bool parseSubRegisterIndexOperand(MachineOperand &Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +0000222 bool parseJumpTableIndexOperand(MachineOperand &Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +0000223 bool parseExternalSymbolOperand(MachineOperand &Dest);
Alex Lorenz44f29252015-07-22 21:07:04 +0000224 bool parseMDNode(MDNode *&Node);
Reid Kleckner6d353342017-08-23 20:31:27 +0000225 bool parseDIExpression(MDNode *&Node);
Alex Lorenz35e44462015-07-22 17:58:46 +0000226 bool parseMetadataOperand(MachineOperand &Dest);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000227 bool parseCFIOffset(int &Offset);
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000228 bool parseCFIRegister(unsigned &Reg);
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +0000229 bool parseCFIEscapeValues(std::string& Values);
Alex Lorenzf4baeb52015-07-21 22:28:27 +0000230 bool parseCFIOperand(MachineOperand &Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +0000231 bool parseIRBlock(BasicBlock *&BB, const Function &F);
232 bool parseBlockAddressOperand(MachineOperand &Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +0000233 bool parseIntrinsicOperand(MachineOperand &Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +0000234 bool parsePredicateOperand(MachineOperand &Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000235 bool parseTargetIndexOperand(MachineOperand &Dest);
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +0000236 bool parseCustomRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +0000237 bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000238 bool parseMachineOperand(MachineOperand &Dest,
239 Optional<unsigned> &TiedDefIdx);
240 bool parseMachineOperandAndTargetFlags(MachineOperand &Dest,
241 Optional<unsigned> &TiedDefIdx);
Alex Lorenzdc24c172015-08-07 20:21:00 +0000242 bool parseOffset(int64_t &Offset);
Alex Lorenz620f8912015-08-13 20:33:33 +0000243 bool parseAlignment(unsigned &Alignment);
Francis Visoiu Mistrihe4718e82018-01-26 11:47:28 +0000244 bool parseAddrspace(unsigned &Addrspace);
Alex Lorenz5672a892015-08-05 22:26:15 +0000245 bool parseOperandsOffset(MachineOperand &Op);
Alex Lorenz36593ac2015-08-19 23:27:07 +0000246 bool parseIRValue(const Value *&V);
Justin Lebar0af80cd2016-07-15 18:26:59 +0000247 bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags);
Alex Lorenz91097a32015-08-12 20:33:26 +0000248 bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV);
249 bool parseMachinePointerInfo(MachinePointerInfo &Dest);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000250 bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID);
Tim Northoverb73e3092017-02-13 22:14:08 +0000251 bool parseOptionalAtomicOrdering(AtomicOrdering &Order);
Alex Lorenz4af7e612015-08-03 23:08:19 +0000252 bool parseMachineMemoryOperand(MachineMemOperand *&Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000253
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000254private:
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000255 /// Convert the integer literal in the current token into an unsigned integer.
256 ///
257 /// Return true if an error occurred.
258 bool getUnsigned(unsigned &Result);
259
Alex Lorenz4af7e612015-08-03 23:08:19 +0000260 /// Convert the integer literal in the current token into an uint64.
261 ///
262 /// Return true if an error occurred.
263 bool getUint64(uint64_t &Result);
264
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +0000265 /// Convert the hexadecimal literal in the current token into an unsigned
266 /// APInt with a minimum bitwidth required to represent the value.
267 ///
268 /// Return true if the literal does not represent an integer value.
269 bool getHexUint(APInt &Result);
270
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000271 /// If the current token is of the given kind, consume it and return false.
272 /// Otherwise report an error and return true.
273 bool expectAndConsume(MIToken::TokenKind TokenKind);
274
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000275 /// If the current token is of the given kind, consume it and return true.
276 /// Otherwise return false.
277 bool consumeIfPresent(MIToken::TokenKind TokenKind);
278
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000279 void initNames2InstrOpCodes();
280
281 /// Try to convert an instruction name to an opcode. Return true if the
282 /// instruction name is invalid.
283 bool parseInstrName(StringRef InstrName, unsigned &OpCode);
Alex Lorenz91370c52015-06-22 20:37:46 +0000284
Alex Lorenze5a44662015-07-17 00:24:15 +0000285 bool parseInstruction(unsigned &OpCode, unsigned &Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000286
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000287 bool assignRegisterTies(MachineInstr &MI,
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000288 ArrayRef<ParsedMachineOperand> Operands);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000289
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000290 bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000291 const MCInstrDesc &MCID);
292
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000293 void initNames2Regs();
294
295 /// Try to convert a register name to a register number. Return true if the
296 /// register name is invalid.
297 bool getRegisterByName(StringRef RegName, unsigned &Reg);
Alex Lorenz8f6f4282015-06-29 16:57:06 +0000298
299 void initNames2RegMasks();
300
301 /// Check if the given identifier is a name of a register mask.
302 ///
303 /// Return null if the identifier isn't a register mask.
304 const uint32_t *getRegMask(StringRef Identifier);
Alex Lorenz2eacca82015-07-13 23:24:34 +0000305
306 void initNames2SubRegIndices();
307
308 /// Check if the given identifier is a name of a subregister index.
309 ///
310 /// Return 0 if the name isn't a subregister index class.
311 unsigned getSubRegIndex(StringRef Name);
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000312
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000313 const BasicBlock *getIRBlock(unsigned Slot);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +0000314 const BasicBlock *getIRBlock(unsigned Slot, const Function &F);
Alex Lorenzef5c1962015-07-28 23:02:45 +0000315
Alex Lorenzdd13be02015-08-19 23:31:05 +0000316 const Value *getIRValue(unsigned Slot);
317
Alex Lorenzef5c1962015-07-28 23:02:45 +0000318 void initNames2TargetIndices();
319
320 /// Try to convert a name of target index to the corresponding target index.
321 ///
322 /// Return true if the name isn't a name of a target index.
323 bool getTargetIndex(StringRef Name, int &Index);
Alex Lorenz49873a82015-08-06 00:44:07 +0000324
325 void initNames2DirectTargetFlags();
326
327 /// Try to convert a name of a direct target flag to the corresponding
328 /// target flag.
329 ///
330 /// Return true if the name isn't a name of a direct flag.
331 bool getDirectTargetFlag(StringRef Name, unsigned &Flag);
Alex Lorenzf3630112015-08-18 22:52:15 +0000332
333 void initNames2BitmaskTargetFlags();
334
335 /// Try to convert a name of a bitmask target flag to the corresponding
336 /// target flag.
337 ///
338 /// Return true if the name isn't a name of a bitmask target flag.
339 bool getBitmaskTargetFlag(StringRef Name, unsigned &Flag);
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000340
Geoff Berry6748abe2017-07-13 02:28:54 +0000341 void initNames2MMOTargetFlags();
342
343 /// Try to convert a name of a MachineMemOperand target flag to the
344 /// corresponding target flag.
345 ///
346 /// Return true if the name isn't a name of a target MMO flag.
347 bool getMMOTargetFlag(StringRef Name, MachineMemOperand::Flags &Flag);
348
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000349 /// parseStringConstant
350 /// ::= StringConstant
351 bool parseStringConstant(std::string &Result);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000352};
353
354} // end anonymous namespace
355
Matthias Braun74ad41c2016-10-11 03:13:01 +0000356MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
Matthias Braune35861d2016-07-13 23:27:50 +0000357 StringRef Source)
358 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
359{}
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000360
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000361void MIParser::lex(unsigned SkipChar) {
Alex Lorenz91370c52015-06-22 20:37:46 +0000362 CurrentSource = lexMIToken(
Quentin Colombet287c6bb2016-03-08 00:57:31 +0000363 CurrentSource.data() + SkipChar, Token,
Alex Lorenz91370c52015-06-22 20:37:46 +0000364 [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); });
365}
366
367bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
368
369bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) {
Matthias Braune35861d2016-07-13 23:27:50 +0000370 const SourceMgr &SM = *PFS.SM;
Alex Lorenz91370c52015-06-22 20:37:46 +0000371 assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000372 const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID());
373 if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) {
374 // Create an ordinary diagnostic when the source manager's buffer is the
375 // source string.
376 Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
377 return true;
378 }
379 // Create a diagnostic for a YAML string literal.
380 Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1,
381 Loc - Source.data(), SourceMgr::DK_Error, Msg.str(),
382 Source, None, None);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000383 return true;
384}
385
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000386static const char *toString(MIToken::TokenKind TokenKind) {
387 switch (TokenKind) {
388 case MIToken::comma:
389 return "','";
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000390 case MIToken::equal:
391 return "'='";
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000392 case MIToken::colon:
393 return "':'";
Alex Lorenzdeb53492015-07-28 17:28:03 +0000394 case MIToken::lparen:
395 return "'('";
396 case MIToken::rparen:
397 return "')'";
Alex Lorenz8cfc6862015-07-23 23:09:07 +0000398 default:
399 return "<unknown token>";
400 }
401}
402
403bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
404 if (Token.isNot(TokenKind))
405 return error(Twine("expected ") + toString(TokenKind));
406 lex();
407 return false;
408}
409
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000410bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
411 if (Token.isNot(TokenKind))
412 return false;
Alex Lorenz91370c52015-06-22 20:37:46 +0000413 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000414 return true;
415}
Alex Lorenz91370c52015-06-22 20:37:46 +0000416
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000417bool MIParser::parseBasicBlockDefinition(
418 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
419 assert(Token.is(MIToken::MachineBasicBlockLabel));
420 unsigned ID = 0;
421 if (getUnsigned(ID))
422 return true;
423 auto Loc = Token.location();
424 auto Name = Token.stringValue();
425 lex();
426 bool HasAddressTaken = false;
427 bool IsLandingPad = false;
428 unsigned Alignment = 0;
429 BasicBlock *BB = nullptr;
430 if (consumeIfPresent(MIToken::lparen)) {
431 do {
432 // TODO: Report an error when multiple same attributes are specified.
433 switch (Token.kind()) {
434 case MIToken::kw_address_taken:
435 HasAddressTaken = true;
436 lex();
437 break;
438 case MIToken::kw_landing_pad:
439 IsLandingPad = true;
440 lex();
441 break;
442 case MIToken::kw_align:
443 if (parseAlignment(Alignment))
444 return true;
445 break;
446 case MIToken::IRBlock:
447 // TODO: Report an error when both name and ir block are specified.
Matthias Braunf1caa282017-12-15 22:22:58 +0000448 if (parseIRBlock(BB, MF.getFunction()))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000449 return true;
450 lex();
451 break;
452 default:
453 break;
454 }
455 } while (consumeIfPresent(MIToken::comma));
456 if (expectAndConsume(MIToken::rparen))
457 return true;
458 }
459 if (expectAndConsume(MIToken::colon))
460 return true;
461
462 if (!Name.empty()) {
463 BB = dyn_cast_or_null<BasicBlock>(
Matthias Braunf1caa282017-12-15 22:22:58 +0000464 MF.getFunction().getValueSymbolTable()->lookup(Name));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000465 if (!BB)
466 return error(Loc, Twine("basic block '") + Name +
467 "' is not defined in the function '" +
468 MF.getName() + "'");
469 }
470 auto *MBB = MF.CreateMachineBasicBlock(BB);
471 MF.insert(MF.end(), MBB);
472 bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second;
473 if (!WasInserted)
474 return error(Loc, Twine("redefinition of machine basic block with id #") +
475 Twine(ID));
476 if (Alignment)
477 MBB->setAlignment(Alignment);
478 if (HasAddressTaken)
479 MBB->setHasAddressTaken();
Reid Kleckner0e288232015-08-27 23:27:47 +0000480 MBB->setIsEHPad(IsLandingPad);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000481 return false;
482}
483
484bool MIParser::parseBasicBlockDefinitions(
485 DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) {
486 lex();
487 // Skip until the first machine basic block.
488 while (Token.is(MIToken::Newline))
489 lex();
490 if (Token.isErrorOrEOF())
491 return Token.isError();
492 if (Token.isNot(MIToken::MachineBasicBlockLabel))
493 return error("expected a basic block definition before instructions");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000494 unsigned BraceDepth = 0;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000495 do {
496 if (parseBasicBlockDefinition(MBBSlots))
497 return true;
498 bool IsAfterNewline = false;
499 // Skip until the next machine basic block.
500 while (true) {
501 if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) ||
502 Token.isErrorOrEOF())
503 break;
504 else if (Token.is(MIToken::MachineBasicBlockLabel))
505 return error("basic block definition should be located at the start of "
506 "the line");
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000507 else if (consumeIfPresent(MIToken::Newline)) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000508 IsAfterNewline = true;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000509 continue;
510 }
511 IsAfterNewline = false;
512 if (Token.is(MIToken::lbrace))
513 ++BraceDepth;
514 if (Token.is(MIToken::rbrace)) {
515 if (!BraceDepth)
516 return error("extraneous closing brace ('}')");
517 --BraceDepth;
518 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000519 lex();
520 }
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000521 // Verify that we closed all of the '{' at the end of a file or a block.
522 if (!Token.isError() && BraceDepth)
523 return error("expected '}'"); // FIXME: Report a note that shows '{'.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000524 } while (!Token.isErrorOrEOF());
525 return Token.isError();
526}
527
528bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) {
529 assert(Token.is(MIToken::kw_liveins));
530 lex();
531 if (expectAndConsume(MIToken::colon))
532 return true;
533 if (Token.isNewlineOrEOF()) // Allow an empty list of liveins.
534 return false;
535 do {
536 if (Token.isNot(MIToken::NamedRegister))
537 return error("expected a named register");
538 unsigned Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000539 if (parseNamedRegister(Reg))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000540 return true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000541 lex();
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000542 LaneBitmask Mask = LaneBitmask::getAll();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000543 if (consumeIfPresent(MIToken::colon)) {
544 // Parse lane mask.
545 if (Token.isNot(MIToken::IntegerLiteral) &&
546 Token.isNot(MIToken::HexLiteral))
547 return error("expected a lane mask");
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000548 static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned),
549 "Use correct get-function for lane mask");
550 LaneBitmask::Type V;
551 if (getUnsigned(V))
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000552 return error("invalid lane mask value");
Krzysztof Parzyszek91b5cf82016-12-15 14:36:06 +0000553 Mask = LaneBitmask(V);
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +0000554 lex();
555 }
556 MBB.addLiveIn(Reg, Mask);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000557 } while (consumeIfPresent(MIToken::comma));
558 return false;
559}
560
561bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) {
562 assert(Token.is(MIToken::kw_successors));
563 lex();
564 if (expectAndConsume(MIToken::colon))
565 return true;
566 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
567 return false;
568 do {
569 if (Token.isNot(MIToken::MachineBasicBlock))
570 return error("expected a machine basic block reference");
571 MachineBasicBlock *SuccMBB = nullptr;
572 if (parseMBBReference(SuccMBB))
573 return true;
574 lex();
575 unsigned Weight = 0;
576 if (consumeIfPresent(MIToken::lparen)) {
Geoff Berryb51774a2016-11-18 19:37:24 +0000577 if (Token.isNot(MIToken::IntegerLiteral) &&
578 Token.isNot(MIToken::HexLiteral))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000579 return error("expected an integer literal after '('");
580 if (getUnsigned(Weight))
581 return true;
582 lex();
583 if (expectAndConsume(MIToken::rparen))
584 return true;
585 }
Cong Houd97c1002015-12-01 05:29:22 +0000586 MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight));
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000587 } while (consumeIfPresent(MIToken::comma));
Cong Houd97c1002015-12-01 05:29:22 +0000588 MBB.normalizeSuccProbs();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000589 return false;
590}
591
Matthias Braun89401142017-05-05 21:09:30 +0000592bool MIParser::parseBasicBlock(MachineBasicBlock &MBB,
593 MachineBasicBlock *&AddFalthroughFrom) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000594 // Skip the definition.
595 assert(Token.is(MIToken::MachineBasicBlockLabel));
596 lex();
597 if (consumeIfPresent(MIToken::lparen)) {
598 while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF())
599 lex();
600 consumeIfPresent(MIToken::rparen);
601 }
602 consumeIfPresent(MIToken::colon);
603
604 // Parse the liveins and successors.
605 // N.B: Multiple lists of successors and liveins are allowed and they're
606 // merged into one.
607 // Example:
608 // liveins: %edi
609 // liveins: %esi
610 //
611 // is equivalent to
612 // liveins: %edi, %esi
Hiroshi Inoue6a391bb2017-06-27 10:35:37 +0000613 bool ExplicitSuccessors = false;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000614 while (true) {
615 if (Token.is(MIToken::kw_successors)) {
616 if (parseBasicBlockSuccessors(MBB))
617 return true;
Hiroshi Inoue6a391bb2017-06-27 10:35:37 +0000618 ExplicitSuccessors = true;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000619 } else if (Token.is(MIToken::kw_liveins)) {
620 if (parseBasicBlockLiveins(MBB))
621 return true;
622 } else if (consumeIfPresent(MIToken::Newline)) {
623 continue;
624 } else
625 break;
626 if (!Token.isNewlineOrEOF())
627 return error("expected line break at the end of a list");
628 lex();
629 }
630
631 // Parse the instructions.
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000632 bool IsInBundle = false;
633 MachineInstr *PrevMI = nullptr;
Matthias Braun89401142017-05-05 21:09:30 +0000634 while (!Token.is(MIToken::MachineBasicBlockLabel) &&
635 !Token.is(MIToken::Eof)) {
636 if (consumeIfPresent(MIToken::Newline))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000637 continue;
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000638 if (consumeIfPresent(MIToken::rbrace)) {
639 // The first parsing pass should verify that all closing '}' have an
640 // opening '{'.
641 assert(IsInBundle);
642 IsInBundle = false;
643 continue;
644 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000645 MachineInstr *MI = nullptr;
646 if (parse(MI))
647 return true;
648 MBB.insert(MBB.end(), MI);
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000649 if (IsInBundle) {
650 PrevMI->setFlag(MachineInstr::BundledSucc);
651 MI->setFlag(MachineInstr::BundledPred);
652 }
653 PrevMI = MI;
654 if (Token.is(MIToken::lbrace)) {
655 if (IsInBundle)
656 return error("nested instruction bundles are not allowed");
657 lex();
658 // This instruction is the start of the bundle.
659 MI->setFlag(MachineInstr::BundledSucc);
660 IsInBundle = true;
661 if (!Token.is(MIToken::Newline))
662 // The next instruction can be on the same line.
663 continue;
664 }
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000665 assert(Token.isNewlineOrEOF() && "MI is not fully parsed");
666 lex();
667 }
Matthias Braun89401142017-05-05 21:09:30 +0000668
669 // Construct successor list by searching for basic block machine operands.
Hiroshi Inoue6a391bb2017-06-27 10:35:37 +0000670 if (!ExplicitSuccessors) {
Matthias Braun89401142017-05-05 21:09:30 +0000671 SmallVector<MachineBasicBlock*,4> Successors;
672 bool IsFallthrough;
673 guessSuccessors(MBB, Successors, IsFallthrough);
674 for (MachineBasicBlock *Succ : Successors)
675 MBB.addSuccessor(Succ);
676
677 if (IsFallthrough) {
678 AddFalthroughFrom = &MBB;
679 } else {
680 MBB.normalizeSuccProbs();
681 }
682 }
683
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000684 return false;
685}
686
687bool MIParser::parseBasicBlocks() {
688 lex();
689 // Skip until the first machine basic block.
690 while (Token.is(MIToken::Newline))
691 lex();
692 if (Token.isErrorOrEOF())
693 return Token.isError();
694 // The first parsing pass should have verified that this token is a MBB label
695 // in the 'parseBasicBlockDefinitions' method.
696 assert(Token.is(MIToken::MachineBasicBlockLabel));
Matthias Braun89401142017-05-05 21:09:30 +0000697 MachineBasicBlock *AddFalthroughFrom = nullptr;
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000698 do {
699 MachineBasicBlock *MBB = nullptr;
700 if (parseMBBReference(MBB))
701 return true;
Matthias Braun89401142017-05-05 21:09:30 +0000702 if (AddFalthroughFrom) {
703 if (!AddFalthroughFrom->isSuccessor(MBB))
704 AddFalthroughFrom->addSuccessor(MBB);
705 AddFalthroughFrom->normalizeSuccProbs();
706 AddFalthroughFrom = nullptr;
707 }
708 if (parseBasicBlock(*MBB, AddFalthroughFrom))
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000709 return true;
710 // The method 'parseBasicBlock' should parse the whole block until the next
711 // block or the end of file.
712 assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof));
713 } while (Token.isNot(MIToken::Eof));
714 return false;
715}
716
717bool MIParser::parse(MachineInstr *&MI) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000718 // Parse any register operands before '='
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000719 MachineOperand MO = MachineOperand::CreateImm(0);
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000720 SmallVector<ParsedMachineOperand, 8> Operands;
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000721 while (Token.isRegister() || Token.isRegisterFlag()) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000722 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000723 Optional<unsigned> TiedDefIdx;
724 if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true))
Alex Lorenz3708a642015-06-30 17:47:50 +0000725 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000726 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000727 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000728 if (Token.isNot(MIToken::comma))
729 break;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000730 lex();
731 }
Alex Lorenzfbe9c042015-07-29 18:51:21 +0000732 if (!Operands.empty() && expectAndConsume(MIToken::equal))
733 return true;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000734
Alex Lorenze5a44662015-07-17 00:24:15 +0000735 unsigned OpCode, Flags = 0;
736 if (Token.isError() || parseInstruction(OpCode, Flags))
Alex Lorenz3708a642015-06-30 17:47:50 +0000737 return true;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000738
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000739 // Parse the remaining machine operands.
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000740 while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_debug_location) &&
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000741 Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000742 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000743 Optional<unsigned> TiedDefIdx;
744 if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx))
Alex Lorenz3708a642015-06-30 17:47:50 +0000745 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000746 Operands.push_back(
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000747 ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx));
Alex Lorenzf9a2b122015-08-14 18:57:24 +0000748 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
749 Token.is(MIToken::lbrace))
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000750 break;
Alex Lorenz3708a642015-06-30 17:47:50 +0000751 if (Token.isNot(MIToken::comma))
752 return error("expected ',' before the next machine operand");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000753 lex();
754 }
755
Alex Lorenz46d760d2015-07-22 21:15:11 +0000756 DebugLoc DebugLocation;
757 if (Token.is(MIToken::kw_debug_location)) {
758 lex();
759 if (Token.isNot(MIToken::exclaim))
760 return error("expected a metadata node after 'debug-location'");
761 MDNode *Node = nullptr;
762 if (parseMDNode(Node))
763 return true;
764 DebugLocation = DebugLoc(Node);
765 }
766
Alex Lorenz4af7e612015-08-03 23:08:19 +0000767 // Parse the machine memory operands.
768 SmallVector<MachineMemOperand *, 2> MemOperands;
769 if (Token.is(MIToken::coloncolon)) {
770 lex();
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000771 while (!Token.isNewlineOrEOF()) {
Alex Lorenz4af7e612015-08-03 23:08:19 +0000772 MachineMemOperand *MemOp = nullptr;
773 if (parseMachineMemoryOperand(MemOp))
774 return true;
775 MemOperands.push_back(MemOp);
Alex Lorenz5022f6b2015-08-13 23:10:16 +0000776 if (Token.isNewlineOrEOF())
Alex Lorenz4af7e612015-08-03 23:08:19 +0000777 break;
778 if (Token.isNot(MIToken::comma))
779 return error("expected ',' before the next machine memory operand");
780 lex();
781 }
782 }
783
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000784 const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode);
Alex Lorenz36962cd2015-07-07 02:08:46 +0000785 if (!MCID.isVariadic()) {
786 // FIXME: Move the implicit operand verification to the machine verifier.
787 if (verifyImplicitOperands(Operands, MCID))
788 return true;
789 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000790
Alex Lorenzcb268d42015-07-06 23:07:26 +0000791 // TODO: Check for extraneous machine operands.
Alex Lorenz46d760d2015-07-22 21:15:11 +0000792 MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true);
Alex Lorenze5a44662015-07-17 00:24:15 +0000793 MI->setFlags(Flags);
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000794 for (const auto &Operand : Operands)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000795 MI->addOperand(MF, Operand.Operand);
Alex Lorenz5ef93b02015-08-19 19:05:34 +0000796 if (assignRegisterTies(*MI, Operands))
797 return true;
Alex Lorenz4af7e612015-08-03 23:08:19 +0000798 if (MemOperands.empty())
799 return false;
800 MachineInstr::mmo_iterator MemRefs =
801 MF.allocateMemRefsArray(MemOperands.size());
802 std::copy(MemOperands.begin(), MemOperands.end(), MemRefs);
803 MI->setMemRefs(MemRefs, MemRefs + MemOperands.size());
Alex Lorenz3708a642015-06-30 17:47:50 +0000804 return false;
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000805}
806
Alex Lorenz1ea60892015-07-27 20:29:27 +0000807bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) {
Alex Lorenzf09df002015-06-30 18:16:42 +0000808 lex();
809 if (Token.isNot(MIToken::MachineBasicBlock))
810 return error("expected a machine basic block reference");
811 if (parseMBBReference(MBB))
812 return true;
813 lex();
814 if (Token.isNot(MIToken::Eof))
815 return error(
816 "expected end of string after the machine basic block reference");
817 return false;
818}
819
Alex Lorenz1ea60892015-07-27 20:29:27 +0000820bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) {
Alex Lorenz9fab3702015-07-14 21:24:41 +0000821 lex();
822 if (Token.isNot(MIToken::NamedRegister))
823 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000824 if (parseNamedRegister(Reg))
Alex Lorenz607efb62015-08-18 22:57:36 +0000825 return true;
Alex Lorenz9fab3702015-07-14 21:24:41 +0000826 lex();
827 if (Token.isNot(MIToken::Eof))
828 return error("expected end of string after the register reference");
829 return false;
830}
831
Matthias Braun74ad41c2016-10-11 03:13:01 +0000832bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) {
Alex Lorenz12045a42015-07-27 17:42:45 +0000833 lex();
834 if (Token.isNot(MIToken::VirtualRegister))
835 return error("expected a virtual register");
Matthias Braun74ad41c2016-10-11 03:13:01 +0000836 if (parseVirtualRegister(Info))
Alex Lorenz607efb62015-08-18 22:57:36 +0000837 return true;
Alex Lorenz12045a42015-07-27 17:42:45 +0000838 lex();
839 if (Token.isNot(MIToken::Eof))
840 return error("expected end of string after the register reference");
841 return false;
842}
843
Tom Stellard9c884e42016-11-15 00:03:14 +0000844bool MIParser::parseStandaloneRegister(unsigned &Reg) {
845 lex();
846 if (Token.isNot(MIToken::NamedRegister) &&
847 Token.isNot(MIToken::VirtualRegister))
848 return error("expected either a named or virtual register");
849
850 VRegInfo *Info;
851 if (parseRegister(Reg, Info))
852 return true;
853
854 lex();
855 if (Token.isNot(MIToken::Eof))
856 return error("expected end of string after the register reference");
857 return false;
858}
859
Alex Lorenza314d812015-08-18 22:26:26 +0000860bool MIParser::parseStandaloneStackObject(int &FI) {
861 lex();
862 if (Token.isNot(MIToken::StackObject))
863 return error("expected a stack object");
864 if (parseStackFrameIndex(FI))
865 return true;
866 if (Token.isNot(MIToken::Eof))
867 return error("expected end of string after the stack object reference");
868 return false;
869}
870
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000871bool MIParser::parseStandaloneMDNode(MDNode *&Node) {
872 lex();
Reid Kleckner6d353342017-08-23 20:31:27 +0000873 if (Token.is(MIToken::exclaim)) {
874 if (parseMDNode(Node))
875 return true;
876 } else if (Token.is(MIToken::md_diexpr)) {
877 if (parseDIExpression(Node))
878 return true;
879 } else
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000880 return error("expected a metadata node");
Alex Lorenzdf9e3c62015-08-19 00:13:25 +0000881 if (Token.isNot(MIToken::Eof))
882 return error("expected end of string after the metadata node");
883 return false;
884}
885
Alex Lorenz36962cd2015-07-07 02:08:46 +0000886static const char *printImplicitRegisterFlag(const MachineOperand &MO) {
887 assert(MO.isImplicit());
888 return MO.isDef() ? "implicit-def" : "implicit";
889}
890
891static std::string getRegisterName(const TargetRegisterInfo *TRI,
892 unsigned Reg) {
893 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg");
894 return StringRef(TRI->getName(Reg)).lower();
895}
896
Alex Lorenz0153e592015-09-10 14:04:34 +0000897/// Return true if the parsed machine operands contain a given machine operand.
898static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand,
899 ArrayRef<ParsedMachineOperand> Operands) {
900 for (const auto &I : Operands) {
901 if (ImplicitOperand.isIdenticalTo(I.Operand))
902 return true;
903 }
904 return false;
905}
906
Alex Lorenzfeb6b432015-08-19 19:19:16 +0000907bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands,
908 const MCInstrDesc &MCID) {
Alex Lorenz36962cd2015-07-07 02:08:46 +0000909 if (MCID.isCall())
910 // We can't verify call instructions as they can contain arbitrary implicit
911 // register and register mask operands.
912 return false;
913
914 // Gather all the expected implicit operands.
915 SmallVector<MachineOperand, 4> ImplicitOperands;
916 if (MCID.ImplicitDefs)
Craig Toppere5e035a32015-12-05 07:13:35 +0000917 for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000918 ImplicitOperands.push_back(
919 MachineOperand::CreateReg(*ImpDefs, true, true));
920 if (MCID.ImplicitUses)
Craig Toppere5e035a32015-12-05 07:13:35 +0000921 for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses)
Alex Lorenz36962cd2015-07-07 02:08:46 +0000922 ImplicitOperands.push_back(
923 MachineOperand::CreateReg(*ImpUses, false, true));
924
925 const auto *TRI = MF.getSubtarget().getRegisterInfo();
926 assert(TRI && "Expected target register info");
Alex Lorenz0153e592015-09-10 14:04:34 +0000927 for (const auto &I : ImplicitOperands) {
928 if (isImplicitOperandIn(I, Operands))
929 continue;
930 return error(Operands.empty() ? Token.location() : Operands.back().End,
Alex Lorenz36962cd2015-07-07 02:08:46 +0000931 Twine("missing implicit register operand '") +
Alex Lorenz0153e592015-09-10 14:04:34 +0000932 printImplicitRegisterFlag(I) + " %" +
933 getRegisterName(TRI, I.getReg()) + "'");
Alex Lorenz36962cd2015-07-07 02:08:46 +0000934 }
935 return false;
936}
937
Alex Lorenze5a44662015-07-17 00:24:15 +0000938bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) {
Francis Visoiu Mistrih3abf05732018-03-13 19:53:16 +0000939 // Allow both:
940 // * frame-setup frame-destroy OPCODE
941 // * frame-destroy frame-setup OPCODE
942 while (Token.is(MIToken::kw_frame_setup) ||
943 Token.is(MIToken::kw_frame_destroy)) {
944 Flags |= Token.is(MIToken::kw_frame_setup) ? MachineInstr::FrameSetup
945 : MachineInstr::FrameDestroy;
Francis Visoiu Mistrihdbf2c482018-01-09 11:33:22 +0000946 lex();
Alex Lorenze5a44662015-07-17 00:24:15 +0000947 }
Alex Lorenz91370c52015-06-22 20:37:46 +0000948 if (Token.isNot(MIToken::Identifier))
949 return error("expected a machine instruction");
950 StringRef InstrName = Token.stringValue();
951 if (parseInstrName(InstrName, OpCode))
952 return error(Twine("unknown machine instruction name '") + InstrName + "'");
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000953 lex();
954 return false;
955}
956
Matthias Braun74ad41c2016-10-11 03:13:01 +0000957bool MIParser::parseNamedRegister(unsigned &Reg) {
958 assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token");
959 StringRef Name = Token.stringValue();
960 if (getRegisterByName(Name, Reg))
961 return error(Twine("unknown register name '") + Name + "'");
962 return false;
963}
964
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000965bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) {
966 assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token");
967 StringRef Name = Token.stringValue();
968 // TODO: Check that the VReg name is not the same as a physical register name.
969 // If it is, then print a warning (when warnings are implemented).
970 Info = &PFS.getVRegInfoNamed(Name);
971 return false;
972}
973
Matthias Braun74ad41c2016-10-11 03:13:01 +0000974bool MIParser::parseVirtualRegister(VRegInfo *&Info) {
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000975 if (Token.is(MIToken::NamedVirtualRegister))
976 return parseNamedVirtualRegister(Info);
Matthias Braun74ad41c2016-10-11 03:13:01 +0000977 assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token");
978 unsigned ID;
979 if (getUnsigned(ID))
980 return true;
981 Info = &PFS.getVRegInfo(ID);
982 return false;
983}
984
985bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000986 switch (Token.kind()) {
Alex Lorenz12b554e2015-06-24 17:34:58 +0000987 case MIToken::underscore:
988 Reg = 0;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000989 return false;
990 case MIToken::NamedRegister:
991 return parseNamedRegister(Reg);
Puyan Lotfi399b46c2018-03-30 18:15:54 +0000992 case MIToken::NamedVirtualRegister:
Matthias Braun74ad41c2016-10-11 03:13:01 +0000993 case MIToken::VirtualRegister:
994 if (parseVirtualRegister(Info))
Alex Lorenz53464512015-07-10 22:51:20 +0000995 return true;
Matthias Braun74ad41c2016-10-11 03:13:01 +0000996 Reg = Info->VReg;
997 return false;
Alex Lorenzf3db51de2015-06-23 16:35:26 +0000998 // TODO: Parse other register kinds.
999 default:
1000 llvm_unreachable("The current token should be a register");
1001 }
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001002}
1003
Matthias Braunde5fea22017-01-18 00:59:19 +00001004bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) {
Ahmed Bougachabf480552017-01-20 00:29:59 +00001005 if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore))
1006 return error("expected '_', register class, or register bank name");
Matthias Braunde5fea22017-01-18 00:59:19 +00001007 StringRef::iterator Loc = Token.location();
1008 StringRef Name = Token.stringValue();
1009
1010 // Was it a register class?
1011 auto RCNameI = PFS.Names2RegClasses.find(Name);
1012 if (RCNameI != PFS.Names2RegClasses.end()) {
1013 lex();
1014 const TargetRegisterClass &RC = *RCNameI->getValue();
1015
1016 switch (RegInfo.Kind) {
1017 case VRegInfo::UNKNOWN:
1018 case VRegInfo::NORMAL:
1019 RegInfo.Kind = VRegInfo::NORMAL;
1020 if (RegInfo.Explicit && RegInfo.D.RC != &RC) {
1021 const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
1022 return error(Loc, Twine("conflicting register classes, previously: ") +
1023 Twine(TRI.getRegClassName(RegInfo.D.RC)));
1024 }
1025 RegInfo.D.RC = &RC;
1026 RegInfo.Explicit = true;
1027 return false;
1028
1029 case VRegInfo::GENERIC:
1030 case VRegInfo::REGBANK:
1031 return error(Loc, "register class specification on generic register");
1032 }
1033 llvm_unreachable("Unexpected register kind");
1034 }
1035
Ahmed Bougachabf480552017-01-20 00:29:59 +00001036 // Should be a register bank or a generic register.
1037 const RegisterBank *RegBank = nullptr;
1038 if (Name != "_") {
1039 auto RBNameI = PFS.Names2RegBanks.find(Name);
1040 if (RBNameI == PFS.Names2RegBanks.end())
1041 return error(Loc, "expected '_', register class, or register bank name");
1042 RegBank = RBNameI->getValue();
1043 }
Matthias Braunde5fea22017-01-18 00:59:19 +00001044
Ahmed Bougachabf480552017-01-20 00:29:59 +00001045 lex();
1046
Matthias Braunde5fea22017-01-18 00:59:19 +00001047 switch (RegInfo.Kind) {
1048 case VRegInfo::UNKNOWN:
1049 case VRegInfo::GENERIC:
1050 case VRegInfo::REGBANK:
Ahmed Bougachabf480552017-01-20 00:29:59 +00001051 RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC;
1052 if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank)
1053 return error(Loc, "conflicting generic register banks");
1054 RegInfo.D.RegBank = RegBank;
Matthias Braunde5fea22017-01-18 00:59:19 +00001055 RegInfo.Explicit = true;
1056 return false;
1057
1058 case VRegInfo::NORMAL:
Ahmed Bougachabf480552017-01-20 00:29:59 +00001059 return error(Loc, "register bank specification on normal register");
Matthias Braunde5fea22017-01-18 00:59:19 +00001060 }
1061 llvm_unreachable("Unexpected register kind");
1062}
1063
Alex Lorenzcb268d42015-07-06 23:07:26 +00001064bool MIParser::parseRegisterFlag(unsigned &Flags) {
Alex Lorenz2b3cf192015-08-05 18:09:03 +00001065 const unsigned OldFlags = Flags;
Alex Lorenzcb268d42015-07-06 23:07:26 +00001066 switch (Token.kind()) {
1067 case MIToken::kw_implicit:
1068 Flags |= RegState::Implicit;
1069 break;
1070 case MIToken::kw_implicit_define:
1071 Flags |= RegState::ImplicitDefine;
1072 break;
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001073 case MIToken::kw_def:
1074 Flags |= RegState::Define;
1075 break;
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001076 case MIToken::kw_dead:
1077 Flags |= RegState::Dead;
1078 break;
Alex Lorenz495ad872015-07-08 21:23:34 +00001079 case MIToken::kw_killed:
1080 Flags |= RegState::Kill;
1081 break;
Alex Lorenz4d026b892015-07-08 23:58:31 +00001082 case MIToken::kw_undef:
1083 Flags |= RegState::Undef;
1084 break;
Alex Lorenz1039fd12015-08-14 19:07:07 +00001085 case MIToken::kw_internal:
1086 Flags |= RegState::InternalRead;
1087 break;
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001088 case MIToken::kw_early_clobber:
1089 Flags |= RegState::EarlyClobber;
1090 break;
Alex Lorenz90752582015-08-05 17:41:17 +00001091 case MIToken::kw_debug_use:
1092 Flags |= RegState::Debug;
1093 break;
Geoff Berry60c43102017-12-12 17:53:59 +00001094 case MIToken::kw_renamable:
1095 Flags |= RegState::Renamable;
1096 break;
Alex Lorenzcb268d42015-07-06 23:07:26 +00001097 default:
1098 llvm_unreachable("The current token should be a register flag");
1099 }
Alex Lorenz2b3cf192015-08-05 18:09:03 +00001100 if (OldFlags == Flags)
1101 // We know that the same flag is specified more than once when the flags
1102 // weren't modified.
1103 return error("duplicate '" + Token.stringValue() + "' register flag");
Alex Lorenzcb268d42015-07-06 23:07:26 +00001104 lex();
1105 return false;
1106}
1107
Alex Lorenz2eacca82015-07-13 23:24:34 +00001108bool MIParser::parseSubRegisterIndex(unsigned &SubReg) {
Matthias Braun333e4682016-07-26 21:49:34 +00001109 assert(Token.is(MIToken::dot));
Alex Lorenz2eacca82015-07-13 23:24:34 +00001110 lex();
1111 if (Token.isNot(MIToken::Identifier))
Matthias Braun333e4682016-07-26 21:49:34 +00001112 return error("expected a subregister index after '.'");
Alex Lorenz2eacca82015-07-13 23:24:34 +00001113 auto Name = Token.stringValue();
1114 SubReg = getSubRegIndex(Name);
1115 if (!SubReg)
1116 return error(Twine("use of unknown subregister index '") + Name + "'");
1117 lex();
1118 return false;
1119}
1120
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001121bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) {
1122 if (!consumeIfPresent(MIToken::kw_tied_def))
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001123 return true;
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001124 if (Token.isNot(MIToken::IntegerLiteral))
1125 return error("expected an integer literal after 'tied-def'");
1126 if (getUnsigned(TiedDefIdx))
1127 return true;
1128 lex();
1129 if (expectAndConsume(MIToken::rparen))
1130 return true;
1131 return false;
1132}
1133
Alex Lorenzfeb6b432015-08-19 19:19:16 +00001134bool MIParser::assignRegisterTies(MachineInstr &MI,
1135 ArrayRef<ParsedMachineOperand> Operands) {
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001136 SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs;
1137 for (unsigned I = 0, E = Operands.size(); I != E; ++I) {
1138 if (!Operands[I].TiedDefIdx)
1139 continue;
1140 // The parser ensures that this operand is a register use, so we just have
1141 // to check the tied-def operand.
1142 unsigned DefIdx = Operands[I].TiedDefIdx.getValue();
1143 if (DefIdx >= E)
1144 return error(Operands[I].Begin,
1145 Twine("use of invalid tied-def operand index '" +
1146 Twine(DefIdx) + "'; instruction has only ") +
1147 Twine(E) + " operands");
1148 const auto &DefOperand = Operands[DefIdx].Operand;
1149 if (!DefOperand.isReg() || !DefOperand.isDef())
1150 // FIXME: add note with the def operand.
1151 return error(Operands[I].Begin,
1152 Twine("use of invalid tied-def operand index '") +
1153 Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) +
1154 " isn't a defined register");
1155 // Check that the tied-def operand wasn't tied elsewhere.
1156 for (const auto &TiedPair : TiedRegisterPairs) {
1157 if (TiedPair.first == DefIdx)
1158 return error(Operands[I].Begin,
1159 Twine("the tied-def operand #") + Twine(DefIdx) +
1160 " is already tied with another register operand");
1161 }
1162 TiedRegisterPairs.push_back(std::make_pair(DefIdx, I));
1163 }
1164 // FIXME: Verify that for non INLINEASM instructions, the def and use tied
1165 // indices must be less than tied max.
1166 for (const auto &TiedPair : TiedRegisterPairs)
1167 MI.tieOperands(TiedPair.first, TiedPair.second);
1168 return false;
1169}
1170
1171bool MIParser::parseRegisterOperand(MachineOperand &Dest,
1172 Optional<unsigned> &TiedDefIdx,
1173 bool IsDef) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001174 unsigned Flags = IsDef ? RegState::Define : 0;
1175 while (Token.isRegisterFlag()) {
1176 if (parseRegisterFlag(Flags))
1177 return true;
1178 }
1179 if (!Token.isRegister())
1180 return error("expected a register after register flags");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001181 unsigned Reg;
1182 VRegInfo *RegInfo;
1183 if (parseRegister(Reg, RegInfo))
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001184 return true;
1185 lex();
Alex Lorenz2eacca82015-07-13 23:24:34 +00001186 unsigned SubReg = 0;
Matthias Braun333e4682016-07-26 21:49:34 +00001187 if (Token.is(MIToken::dot)) {
Alex Lorenz2eacca82015-07-13 23:24:34 +00001188 if (parseSubRegisterIndex(SubReg))
1189 return true;
Matthias Braun5d00b322016-07-16 01:36:18 +00001190 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1191 return error("subregister index expects a virtual register");
Alex Lorenz2eacca82015-07-13 23:24:34 +00001192 }
Matthias Braunde5fea22017-01-18 00:59:19 +00001193 if (Token.is(MIToken::colon)) {
1194 if (!TargetRegisterInfo::isVirtualRegister(Reg))
1195 return error("register class specification expects a virtual register");
1196 lex();
1197 if (parseRegisterClassOrBank(*RegInfo))
1198 return true;
1199 }
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001200 MachineRegisterInfo &MRI = MF.getRegInfo();
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001201 if ((Flags & RegState::Define) == 0) {
1202 if (consumeIfPresent(MIToken::lparen)) {
1203 unsigned Idx;
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001204 if (!parseRegisterTiedDefIndex(Idx))
1205 TiedDefIdx = Idx;
1206 else {
1207 // Try a redundant low-level type.
1208 LLT Ty;
1209 if (parseLowLevelType(Token.location(), Ty))
1210 return error("expected tied-def or low-level type after '('");
1211
1212 if (expectAndConsume(MIToken::rparen))
1213 return true;
1214
1215 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1216 return error("inconsistent type for generic virtual register");
1217
1218 MRI.setType(Reg, Ty);
1219 }
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001220 }
1221 } else if (consumeIfPresent(MIToken::lparen)) {
Quentin Colombete08cc592016-12-22 21:56:35 +00001222 // Virtual registers may have a tpe with GlobalISel.
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001223 if (!TargetRegisterInfo::isVirtualRegister(Reg))
Quentin Colombete08cc592016-12-22 21:56:35 +00001224 return error("unexpected type on physical register");
Ahmed Bougacha5a59b242016-07-19 19:48:36 +00001225
Tim Northover0f140c72016-09-09 11:46:34 +00001226 LLT Ty;
1227 if (parseLowLevelType(Token.location(), Ty))
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001228 return true;
Quentin Colombet2a831fb2016-03-07 21:48:43 +00001229
Tim Northover0f140c72016-09-09 11:46:34 +00001230 if (expectAndConsume(MIToken::rparen))
1231 return true;
1232
Tim Northoverd28d3cc2016-09-12 11:20:10 +00001233 if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty)
1234 return error("inconsistent type for generic virtual register");
1235
Tim Northover0f140c72016-09-09 11:46:34 +00001236 MRI.setType(Reg, Ty);
Matthias Braun74ad41c2016-10-11 03:13:01 +00001237 } else if (TargetRegisterInfo::isVirtualRegister(Reg)) {
Quentin Colombet3749f332016-12-22 22:50:34 +00001238 // Generic virtual registers must have a type.
1239 // If we end up here this means the type hasn't been specified and
Quentin Colombet2c646962016-06-08 23:27:46 +00001240 // this is bad!
Matthias Braun74ad41c2016-10-11 03:13:01 +00001241 if (RegInfo->Kind == VRegInfo::GENERIC ||
1242 RegInfo->Kind == VRegInfo::REGBANK)
Quentin Colombet3749f332016-12-22 22:50:34 +00001243 return error("generic virtual registers must have a type");
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001244 }
Alex Lorenz495ad872015-07-08 21:23:34 +00001245 Dest = MachineOperand::CreateReg(
1246 Reg, Flags & RegState::Define, Flags & RegState::Implicit,
Alex Lorenz2eacca82015-07-13 23:24:34 +00001247 Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef,
Alex Lorenz1039fd12015-08-14 19:07:07 +00001248 Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug,
Geoff Berry60c43102017-12-12 17:53:59 +00001249 Flags & RegState::InternalRead, Flags & RegState::Renamable);
1250
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001251 return false;
1252}
1253
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001254bool MIParser::parseImmediateOperand(MachineOperand &Dest) {
1255 assert(Token.is(MIToken::IntegerLiteral));
1256 const APSInt &Int = Token.integerValue();
1257 if (Int.getMinSignedBits() > 64)
Alex Lorenz3f2058d2015-08-05 19:03:42 +00001258 return error("integer literal is too large to be an immediate operand");
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001259 Dest = MachineOperand::CreateImm(Int.getExtValue());
1260 lex();
1261 return false;
1262}
1263
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001264bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue,
1265 const Constant *&C) {
1266 auto Source = StringValue.str(); // The source has to be null terminated.
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001267 SMDiagnostic Err;
Matthias Braunf1caa282017-12-15 22:22:58 +00001268 C = parseConstantValue(Source, Err, *MF.getFunction().getParent(),
Matthias Braune35861d2016-07-13 23:27:50 +00001269 &PFS.IRSlots);
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001270 if (!C)
1271 return error(Loc + Err.getColumnNo(), Err.getMessage());
1272 return false;
1273}
1274
Alex Lorenz5d8b0bd2015-08-21 21:48:22 +00001275bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) {
1276 if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C))
1277 return true;
1278 lex();
1279 return false;
1280}
1281
Ahmed Bougachad760de02016-07-28 17:15:12 +00001282bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) {
Tim Northover32a078a2016-09-15 10:09:59 +00001283 if (Token.is(MIToken::ScalarType)) {
Tim Northover62ae5682016-07-20 19:09:30 +00001284 Ty = LLT::scalar(APSInt(Token.range().drop_front()).getZExtValue());
1285 lex();
1286 return false;
Tim Northoverbd505462016-07-22 16:59:52 +00001287 } else if (Token.is(MIToken::PointerType)) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001288 const DataLayout &DL = MF.getDataLayout();
Tim Northover5ae83502016-09-15 09:20:34 +00001289 unsigned AS = APSInt(Token.range().drop_front()).getZExtValue();
1290 Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS));
Tim Northoverbd505462016-07-22 16:59:52 +00001291 lex();
1292 return false;
Tim Northover62ae5682016-07-20 19:09:30 +00001293 }
Quentin Colombet85199672016-03-08 00:20:48 +00001294
Tim Northover62ae5682016-07-20 19:09:30 +00001295 // Now we're looking for a vector.
1296 if (Token.isNot(MIToken::less))
Tim Northoverbd505462016-07-22 16:59:52 +00001297 return error(Loc,
1298 "expected unsized, pN, sN or <N x sM> for GlobalISel type");
1299
Tim Northover62ae5682016-07-20 19:09:30 +00001300 lex();
1301
1302 if (Token.isNot(MIToken::IntegerLiteral))
1303 return error(Loc, "expected <N x sM> for vctor type");
1304 uint64_t NumElements = Token.integerValue().getZExtValue();
1305 lex();
1306
1307 if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x")
1308 return error(Loc, "expected '<N x sM>' for vector type");
1309 lex();
1310
1311 if (Token.isNot(MIToken::ScalarType))
1312 return error(Loc, "expected '<N x sM>' for vector type");
1313 uint64_t ScalarSize = APSInt(Token.range().drop_front()).getZExtValue();
1314 lex();
1315
1316 if (Token.isNot(MIToken::greater))
1317 return error(Loc, "expected '<N x sM>' for vector type");
1318 lex();
1319
1320 Ty = LLT::vector(NumElements, ScalarSize);
Quentin Colombet85199672016-03-08 00:20:48 +00001321 return false;
1322}
1323
Alex Lorenz05e38822015-08-05 18:52:21 +00001324bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) {
1325 assert(Token.is(MIToken::IntegerType));
1326 auto Loc = Token.location();
1327 lex();
1328 if (Token.isNot(MIToken::IntegerLiteral))
1329 return error("expected an integer literal");
1330 const Constant *C = nullptr;
1331 if (parseIRConstant(Loc, C))
1332 return true;
1333 Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C));
1334 return false;
1335}
1336
Alex Lorenzad156fb2015-07-31 20:49:21 +00001337bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) {
1338 auto Loc = Token.location();
1339 lex();
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001340 if (Token.isNot(MIToken::FloatingPointLiteral) &&
1341 Token.isNot(MIToken::HexLiteral))
Alex Lorenzad156fb2015-07-31 20:49:21 +00001342 return error("expected a floating point literal");
Alex Lorenz7eaff4c2015-08-05 18:44:00 +00001343 const Constant *C = nullptr;
1344 if (parseIRConstant(Loc, C))
1345 return true;
Alex Lorenzad156fb2015-07-31 20:49:21 +00001346 Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C));
1347 return false;
1348}
1349
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001350bool MIParser::getUnsigned(unsigned &Result) {
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001351 if (Token.hasIntegerValue()) {
1352 const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1;
1353 uint64_t Val64 = Token.integerValue().getLimitedValue(Limit);
1354 if (Val64 == Limit)
1355 return error("expected 32-bit integer (too large)");
1356 Result = Val64;
1357 return false;
1358 }
1359 if (Token.is(MIToken::HexLiteral)) {
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001360 APInt A;
1361 if (getHexUint(A))
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001362 return true;
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00001363 if (A.getBitWidth() > 32)
Krzysztof Parzyszekd62669d2016-10-12 21:06:45 +00001364 return error("expected 32-bit integer (too large)");
1365 Result = A.getZExtValue();
1366 return false;
1367 }
1368 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001369}
1370
Alex Lorenzf09df002015-06-30 18:16:42 +00001371bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) {
Alex Lorenz5022f6b2015-08-13 23:10:16 +00001372 assert(Token.is(MIToken::MachineBasicBlock) ||
1373 Token.is(MIToken::MachineBasicBlockLabel));
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001374 unsigned Number;
1375 if (getUnsigned(Number))
1376 return true;
Alex Lorenz7a503fa2015-07-07 17:46:43 +00001377 auto MBBInfo = PFS.MBBSlots.find(Number);
1378 if (MBBInfo == PFS.MBBSlots.end())
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001379 return error(Twine("use of undefined machine basic block #") +
1380 Twine(Number));
Alex Lorenzf09df002015-06-30 18:16:42 +00001381 MBB = MBBInfo->second;
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +00001382 // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once
1383 // we drop the <irname> from the bb.<id>.<irname> format.
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001384 if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName())
1385 return error(Twine("the name of machine basic block #") + Twine(Number) +
1386 " isn't '" + Token.stringValue() + "'");
Alex Lorenzf09df002015-06-30 18:16:42 +00001387 return false;
1388}
1389
1390bool MIParser::parseMBBOperand(MachineOperand &Dest) {
1391 MachineBasicBlock *MBB;
1392 if (parseMBBReference(MBB))
1393 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001394 Dest = MachineOperand::CreateMBB(MBB);
1395 lex();
1396 return false;
1397}
1398
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001399bool MIParser::parseStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001400 assert(Token.is(MIToken::StackObject));
1401 unsigned ID;
1402 if (getUnsigned(ID))
1403 return true;
1404 auto ObjectInfo = PFS.StackObjectSlots.find(ID);
1405 if (ObjectInfo == PFS.StackObjectSlots.end())
1406 return error(Twine("use of undefined stack object '%stack.") + Twine(ID) +
1407 "'");
1408 StringRef Name;
1409 if (const auto *Alloca =
Matthias Braun941a7052016-07-28 18:40:00 +00001410 MF.getFrameInfo().getObjectAllocation(ObjectInfo->second))
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001411 Name = Alloca->getName();
1412 if (!Token.stringValue().empty() && Token.stringValue() != Name)
1413 return error(Twine("the name of the stack object '%stack.") + Twine(ID) +
1414 "' isn't '" + Token.stringValue() + "'");
1415 lex();
Alex Lorenzdc9dadf2015-08-18 22:18:52 +00001416 FI = ObjectInfo->second;
1417 return false;
1418}
1419
1420bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1421 int FI;
1422 if (parseStackFrameIndex(FI))
1423 return true;
1424 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001425 return false;
1426}
1427
Alex Lorenzea882122015-08-12 21:17:02 +00001428bool MIParser::parseFixedStackFrameIndex(int &FI) {
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001429 assert(Token.is(MIToken::FixedStackObject));
1430 unsigned ID;
1431 if (getUnsigned(ID))
1432 return true;
1433 auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID);
1434 if (ObjectInfo == PFS.FixedStackObjectSlots.end())
1435 return error(Twine("use of undefined fixed stack object '%fixed-stack.") +
1436 Twine(ID) + "'");
1437 lex();
Alex Lorenzea882122015-08-12 21:17:02 +00001438 FI = ObjectInfo->second;
1439 return false;
1440}
1441
1442bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1443 int FI;
1444 if (parseFixedStackFrameIndex(FI))
1445 return true;
1446 Dest = MachineOperand::CreateFI(FI);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001447 return false;
1448}
1449
Alex Lorenz41df7d32015-07-28 17:09:52 +00001450bool MIParser::parseGlobalValue(GlobalValue *&GV) {
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001451 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001452 case MIToken::NamedGlobalValue: {
Matthias Braunf1caa282017-12-15 22:22:58 +00001453 const Module *M = MF.getFunction().getParent();
Alex Lorenz970c12e2015-08-05 17:35:55 +00001454 GV = M->getNamedValue(Token.stringValue());
Alex Lorenz41df7d32015-07-28 17:09:52 +00001455 if (!GV)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001456 return error(Twine("use of undefined global value '") + Token.range() +
1457 "'");
Alex Lorenz41df7d32015-07-28 17:09:52 +00001458 break;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001459 }
1460 case MIToken::GlobalValue: {
1461 unsigned GVIdx;
1462 if (getUnsigned(GVIdx))
1463 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001464 if (GVIdx >= PFS.IRSlots.GlobalValues.size())
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001465 return error(Twine("use of undefined global value '@") + Twine(GVIdx) +
1466 "'");
Matthias Braune35861d2016-07-13 23:27:50 +00001467 GV = PFS.IRSlots.GlobalValues[GVIdx];
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001468 break;
1469 }
1470 default:
1471 llvm_unreachable("The current token should be a global value");
1472 }
Alex Lorenz41df7d32015-07-28 17:09:52 +00001473 return false;
1474}
1475
1476bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) {
1477 GlobalValue *GV = nullptr;
1478 if (parseGlobalValue(GV))
1479 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001480 lex();
Alex Lorenz5672a892015-08-05 22:26:15 +00001481 Dest = MachineOperand::CreateGA(GV, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001482 if (parseOperandsOffset(Dest))
1483 return true;
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001484 return false;
1485}
1486
Alex Lorenzab980492015-07-20 20:51:18 +00001487bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) {
1488 assert(Token.is(MIToken::ConstantPoolItem));
1489 unsigned ID;
1490 if (getUnsigned(ID))
1491 return true;
1492 auto ConstantInfo = PFS.ConstantPoolSlots.find(ID);
1493 if (ConstantInfo == PFS.ConstantPoolSlots.end())
1494 return error("use of undefined constant '%const." + Twine(ID) + "'");
1495 lex();
Alex Lorenzab980492015-07-20 20:51:18 +00001496 Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001497 if (parseOperandsOffset(Dest))
1498 return true;
Alex Lorenzab980492015-07-20 20:51:18 +00001499 return false;
1500}
1501
Alex Lorenz31d70682015-07-15 23:38:35 +00001502bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) {
1503 assert(Token.is(MIToken::JumpTableIndex));
1504 unsigned ID;
1505 if (getUnsigned(ID))
1506 return true;
1507 auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID);
1508 if (JumpTableEntryInfo == PFS.JumpTableSlots.end())
1509 return error("use of undefined jump table '%jump-table." + Twine(ID) + "'");
1510 lex();
Alex Lorenz31d70682015-07-15 23:38:35 +00001511 Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second);
1512 return false;
1513}
1514
Alex Lorenz6ede3742015-07-21 16:59:53 +00001515bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001516 assert(Token.is(MIToken::ExternalSymbol));
1517 const char *Symbol = MF.createExternalSymbolName(Token.stringValue());
Alex Lorenz6ede3742015-07-21 16:59:53 +00001518 lex();
Alex Lorenz6ede3742015-07-21 16:59:53 +00001519 Dest = MachineOperand::CreateES(Symbol);
Alex Lorenz5672a892015-08-05 22:26:15 +00001520 if (parseOperandsOffset(Dest))
1521 return true;
Alex Lorenz6ede3742015-07-21 16:59:53 +00001522 return false;
1523}
1524
Matthias Braunb74eb412016-03-28 18:18:46 +00001525bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) {
1526 assert(Token.is(MIToken::SubRegisterIndex));
1527 StringRef Name = Token.stringValue();
1528 unsigned SubRegIndex = getSubRegIndex(Token.stringValue());
1529 if (SubRegIndex == 0)
1530 return error(Twine("unknown subregister index '") + Name + "'");
1531 lex();
1532 Dest = MachineOperand::CreateImm(SubRegIndex);
1533 return false;
1534}
1535
Alex Lorenz44f29252015-07-22 21:07:04 +00001536bool MIParser::parseMDNode(MDNode *&Node) {
Alex Lorenz35e44462015-07-22 17:58:46 +00001537 assert(Token.is(MIToken::exclaim));
Reid Kleckner6d353342017-08-23 20:31:27 +00001538
Alex Lorenz35e44462015-07-22 17:58:46 +00001539 auto Loc = Token.location();
1540 lex();
1541 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
1542 return error("expected metadata id after '!'");
1543 unsigned ID;
1544 if (getUnsigned(ID))
1545 return true;
Matthias Braune35861d2016-07-13 23:27:50 +00001546 auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID);
1547 if (NodeInfo == PFS.IRSlots.MetadataNodes.end())
Alex Lorenz35e44462015-07-22 17:58:46 +00001548 return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'");
1549 lex();
Alex Lorenz44f29252015-07-22 21:07:04 +00001550 Node = NodeInfo->second.get();
1551 return false;
1552}
1553
Reid Kleckner6d353342017-08-23 20:31:27 +00001554bool MIParser::parseDIExpression(MDNode *&Expr) {
1555 assert(Token.is(MIToken::md_diexpr));
1556 lex();
1557
1558 // FIXME: Share this parsing with the IL parser.
1559 SmallVector<uint64_t, 8> Elements;
1560
1561 if (expectAndConsume(MIToken::lparen))
1562 return true;
1563
1564 if (Token.isNot(MIToken::rparen)) {
1565 do {
1566 if (Token.is(MIToken::Identifier)) {
1567 if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) {
1568 lex();
1569 Elements.push_back(Op);
1570 continue;
1571 }
1572 return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1573 }
1574
1575 if (Token.isNot(MIToken::IntegerLiteral) ||
1576 Token.integerValue().isSigned())
1577 return error("expected unsigned integer");
1578
1579 auto &U = Token.integerValue();
1580 if (U.ugt(UINT64_MAX))
1581 return error("element too large, limit is " + Twine(UINT64_MAX));
1582 Elements.push_back(U.getZExtValue());
1583 lex();
1584
1585 } while (consumeIfPresent(MIToken::comma));
1586 }
1587
1588 if (expectAndConsume(MIToken::rparen))
1589 return true;
1590
Matthias Braunf1caa282017-12-15 22:22:58 +00001591 Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
Reid Kleckner6d353342017-08-23 20:31:27 +00001592 return false;
1593}
1594
Alex Lorenz44f29252015-07-22 21:07:04 +00001595bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1596 MDNode *Node = nullptr;
Reid Kleckner6d353342017-08-23 20:31:27 +00001597 if (Token.is(MIToken::exclaim)) {
1598 if (parseMDNode(Node))
1599 return true;
1600 } else if (Token.is(MIToken::md_diexpr)) {
1601 if (parseDIExpression(Node))
1602 return true;
1603 }
Alex Lorenz44f29252015-07-22 21:07:04 +00001604 Dest = MachineOperand::CreateMetadata(Node);
Alex Lorenz35e44462015-07-22 17:58:46 +00001605 return false;
1606}
1607
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001608bool MIParser::parseCFIOffset(int &Offset) {
1609 if (Token.isNot(MIToken::IntegerLiteral))
1610 return error("expected a cfi offset");
1611 if (Token.integerValue().getMinSignedBits() > 32)
1612 return error("expected a 32 bit integer (the cfi offset is too large)");
1613 Offset = (int)Token.integerValue().getExtValue();
1614 lex();
1615 return false;
1616}
1617
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001618bool MIParser::parseCFIRegister(unsigned &Reg) {
1619 if (Token.isNot(MIToken::NamedRegister))
1620 return error("expected a cfi register");
1621 unsigned LLVMReg;
Matthias Braun74ad41c2016-10-11 03:13:01 +00001622 if (parseNamedRegister(LLVMReg))
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001623 return true;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001624 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1625 assert(TRI && "Expected target register info");
1626 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
1627 if (DwarfReg < 0)
1628 return error("invalid DWARF register");
1629 Reg = (unsigned)DwarfReg;
1630 lex();
1631 return false;
1632}
1633
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00001634bool MIParser::parseCFIEscapeValues(std::string &Values) {
1635 do {
1636 if (Token.isNot(MIToken::HexLiteral))
1637 return error("expected a hexadecimal literal");
1638 unsigned Value;
1639 if (getUnsigned(Value))
1640 return true;
1641 if (Value > UINT8_MAX)
1642 return error("expected a 8-bit integer (too large)");
1643 Values.push_back(static_cast<uint8_t>(Value));
1644 lex();
1645 } while (consumeIfPresent(MIToken::comma));
1646 return false;
1647}
1648
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001649bool MIParser::parseCFIOperand(MachineOperand &Dest) {
1650 auto Kind = Token.kind();
1651 lex();
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001652 int Offset;
1653 unsigned Reg;
1654 unsigned CFIIndex;
1655 switch (Kind) {
Alex Lorenz577d2712015-08-14 21:55:58 +00001656 case MIToken::kw_cfi_same_value:
1657 if (parseCFIRegister(Reg))
1658 return true;
Matthias Braunf23ef432016-11-30 23:48:42 +00001659 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
Alex Lorenz577d2712015-08-14 21:55:58 +00001660 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001661 case MIToken::kw_cfi_offset:
1662 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1663 parseCFIOffset(Offset))
1664 return true;
1665 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001666 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001667 break;
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00001668 case MIToken::kw_cfi_rel_offset:
1669 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1670 parseCFIOffset(Offset))
1671 return true;
1672 CFIIndex = MF.addFrameInst(
1673 MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
1674 break;
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001675 case MIToken::kw_cfi_def_cfa_register:
1676 if (parseCFIRegister(Reg))
1677 return true;
1678 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001679 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00001680 break;
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001681 case MIToken::kw_cfi_def_cfa_offset:
1682 if (parseCFIOffset(Offset))
1683 return true;
1684 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
Matthias Braunf23ef432016-11-30 23:48:42 +00001685 CFIIndex = MF.addFrameInst(
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001686 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
1687 break;
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00001688 case MIToken::kw_cfi_adjust_cfa_offset:
1689 if (parseCFIOffset(Offset))
1690 return true;
1691 CFIIndex = MF.addFrameInst(
1692 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
1693 break;
Alex Lorenzb1393232015-07-29 18:57:23 +00001694 case MIToken::kw_cfi_def_cfa:
1695 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1696 parseCFIOffset(Offset))
1697 return true;
1698 // NB: MCCFIInstruction::createDefCfa negates the offset.
1699 CFIIndex =
Matthias Braunf23ef432016-11-30 23:48:42 +00001700 MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
Alex Lorenzb1393232015-07-29 18:57:23 +00001701 break;
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00001702 case MIToken::kw_cfi_remember_state:
1703 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
1704 break;
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00001705 case MIToken::kw_cfi_restore:
1706 if (parseCFIRegister(Reg))
1707 return true;
1708 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
1709 break;
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00001710 case MIToken::kw_cfi_restore_state:
1711 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
1712 break;
1713 case MIToken::kw_cfi_undefined:
1714 if (parseCFIRegister(Reg))
1715 return true;
1716 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
1717 break;
1718 case MIToken::kw_cfi_register: {
1719 unsigned Reg2;
1720 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
1721 parseCFIRegister(Reg2))
1722 return true;
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00001723
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00001724 CFIIndex =
1725 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
1726 break;
1727 }
1728 case MIToken::kw_cfi_window_save:
1729 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
1730 break;
1731 case MIToken::kw_cfi_escape: {
1732 std::string Values;
1733 if (parseCFIEscapeValues(Values))
1734 return true;
1735 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
1736 break;
1737 }
Alex Lorenz8cfc6862015-07-23 23:09:07 +00001738 default:
1739 // TODO: Parse the other CFI operands.
1740 llvm_unreachable("The current token should be a cfi operand");
1741 }
1742 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
Alex Lorenzf4baeb52015-07-21 22:28:27 +00001743 return false;
1744}
1745
Alex Lorenzdeb53492015-07-28 17:28:03 +00001746bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
1747 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00001748 case MIToken::NamedIRBlock: {
1749 BB = dyn_cast_or_null<BasicBlock>(
Mehdi Aminia53d49e2016-09-17 06:00:02 +00001750 F.getValueSymbolTable()->lookup(Token.stringValue()));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001751 if (!BB)
Alex Lorenz3fb77682015-08-06 23:17:42 +00001752 return error(Twine("use of undefined IR block '") + Token.range() + "'");
Alex Lorenzdeb53492015-07-28 17:28:03 +00001753 break;
1754 }
1755 case MIToken::IRBlock: {
1756 unsigned SlotNumber = 0;
1757 if (getUnsigned(SlotNumber))
1758 return true;
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00001759 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
Alex Lorenzdeb53492015-07-28 17:28:03 +00001760 if (!BB)
1761 return error(Twine("use of undefined IR block '%ir-block.") +
1762 Twine(SlotNumber) + "'");
1763 break;
1764 }
1765 default:
1766 llvm_unreachable("The current token should be an IR block reference");
1767 }
1768 return false;
1769}
1770
1771bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
1772 assert(Token.is(MIToken::kw_blockaddress));
1773 lex();
1774 if (expectAndConsume(MIToken::lparen))
1775 return true;
1776 if (Token.isNot(MIToken::GlobalValue) &&
Alex Lorenz970c12e2015-08-05 17:35:55 +00001777 Token.isNot(MIToken::NamedGlobalValue))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001778 return error("expected a global value");
1779 GlobalValue *GV = nullptr;
1780 if (parseGlobalValue(GV))
1781 return true;
1782 auto *F = dyn_cast<Function>(GV);
1783 if (!F)
1784 return error("expected an IR function reference");
1785 lex();
1786 if (expectAndConsume(MIToken::comma))
1787 return true;
1788 BasicBlock *BB = nullptr;
Alex Lorenz970c12e2015-08-05 17:35:55 +00001789 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
Alex Lorenzdeb53492015-07-28 17:28:03 +00001790 return error("expected an IR block reference");
1791 if (parseIRBlock(BB, *F))
1792 return true;
1793 lex();
1794 if (expectAndConsume(MIToken::rparen))
1795 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001796 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001797 if (parseOperandsOffset(Dest))
1798 return true;
Alex Lorenzdeb53492015-07-28 17:28:03 +00001799 return false;
1800}
1801
Tim Northover6b3bd612016-07-29 20:32:59 +00001802bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
1803 assert(Token.is(MIToken::kw_intrinsic));
1804 lex();
1805 if (expectAndConsume(MIToken::lparen))
1806 return error("expected syntax intrinsic(@llvm.whatever)");
1807
1808 if (Token.isNot(MIToken::NamedGlobalValue))
1809 return error("expected syntax intrinsic(@llvm.whatever)");
1810
1811 std::string Name = Token.stringValue();
1812 lex();
1813
1814 if (expectAndConsume(MIToken::rparen))
1815 return error("expected ')' to terminate intrinsic name");
1816
1817 // Find out what intrinsic we're dealing with, first try the global namespace
1818 // and then the target's private intrinsics if that fails.
1819 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
1820 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
1821 if (ID == Intrinsic::not_intrinsic && TII)
1822 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
1823
1824 if (ID == Intrinsic::not_intrinsic)
1825 return error("unknown intrinsic name");
1826 Dest = MachineOperand::CreateIntrinsicID(ID);
1827
1828 return false;
1829}
1830
Tim Northoverde3aea0412016-08-17 20:25:25 +00001831bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
1832 assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred));
1833 bool IsFloat = Token.is(MIToken::kw_floatpred);
1834 lex();
1835
1836 if (expectAndConsume(MIToken::lparen))
1837 return error("expected syntax intpred(whatever) or floatpred(whatever");
1838
1839 if (Token.isNot(MIToken::Identifier))
1840 return error("whatever");
1841
1842 CmpInst::Predicate Pred;
1843 if (IsFloat) {
1844 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1845 .Case("false", CmpInst::FCMP_FALSE)
1846 .Case("oeq", CmpInst::FCMP_OEQ)
1847 .Case("ogt", CmpInst::FCMP_OGT)
1848 .Case("oge", CmpInst::FCMP_OGE)
1849 .Case("olt", CmpInst::FCMP_OLT)
1850 .Case("ole", CmpInst::FCMP_OLE)
1851 .Case("one", CmpInst::FCMP_ONE)
1852 .Case("ord", CmpInst::FCMP_ORD)
1853 .Case("uno", CmpInst::FCMP_UNO)
1854 .Case("ueq", CmpInst::FCMP_UEQ)
1855 .Case("ugt", CmpInst::FCMP_UGT)
1856 .Case("uge", CmpInst::FCMP_UGE)
1857 .Case("ult", CmpInst::FCMP_ULT)
1858 .Case("ule", CmpInst::FCMP_ULE)
1859 .Case("une", CmpInst::FCMP_UNE)
1860 .Case("true", CmpInst::FCMP_TRUE)
1861 .Default(CmpInst::BAD_FCMP_PREDICATE);
1862 if (!CmpInst::isFPPredicate(Pred))
1863 return error("invalid floating-point predicate");
1864 } else {
1865 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
1866 .Case("eq", CmpInst::ICMP_EQ)
1867 .Case("ne", CmpInst::ICMP_NE)
1868 .Case("sgt", CmpInst::ICMP_SGT)
1869 .Case("sge", CmpInst::ICMP_SGE)
1870 .Case("slt", CmpInst::ICMP_SLT)
1871 .Case("sle", CmpInst::ICMP_SLE)
1872 .Case("ugt", CmpInst::ICMP_UGT)
1873 .Case("uge", CmpInst::ICMP_UGE)
1874 .Case("ult", CmpInst::ICMP_ULT)
1875 .Case("ule", CmpInst::ICMP_ULE)
1876 .Default(CmpInst::BAD_ICMP_PREDICATE);
1877 if (!CmpInst::isIntPredicate(Pred))
1878 return error("invalid integer predicate");
1879 }
1880
1881 lex();
1882 Dest = MachineOperand::CreatePredicate(Pred);
Tim Northover6cd4b232016-08-23 21:01:26 +00001883 if (expectAndConsume(MIToken::rparen))
Tim Northoverde3aea0412016-08-17 20:25:25 +00001884 return error("predicate should be terminated by ')'.");
1885
1886 return false;
1887}
1888
Alex Lorenzef5c1962015-07-28 23:02:45 +00001889bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
1890 assert(Token.is(MIToken::kw_target_index));
1891 lex();
1892 if (expectAndConsume(MIToken::lparen))
1893 return true;
1894 if (Token.isNot(MIToken::Identifier))
1895 return error("expected the name of the target index");
1896 int Index = 0;
1897 if (getTargetIndex(Token.stringValue(), Index))
1898 return error("use of undefined target index '" + Token.stringValue() + "'");
1899 lex();
1900 if (expectAndConsume(MIToken::rparen))
1901 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001902 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
Alex Lorenz5672a892015-08-05 22:26:15 +00001903 if (parseOperandsOffset(Dest))
1904 return true;
Alex Lorenzef5c1962015-07-28 23:02:45 +00001905 return false;
1906}
1907
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00001908bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
1909 assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask");
1910 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
1911 assert(TRI && "Expected target register info");
1912 lex();
1913 if (expectAndConsume(MIToken::lparen))
1914 return true;
1915
1916 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1917 while (true) {
1918 if (Token.isNot(MIToken::NamedRegister))
1919 return error("expected a named register");
1920 unsigned Reg;
1921 if (parseNamedRegister(Reg))
1922 return true;
1923 lex();
1924 Mask[Reg / 32] |= 1U << (Reg % 32);
1925 // TODO: Report an error if the same register is used more than once.
1926 if (Token.isNot(MIToken::comma))
1927 break;
1928 lex();
1929 }
1930
1931 if (expectAndConsume(MIToken::rparen))
1932 return true;
1933 Dest = MachineOperand::CreateRegMask(Mask);
1934 return false;
1935}
1936
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001937bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
1938 assert(Token.is(MIToken::kw_liveout));
1939 const auto *TRI = MF.getSubtarget().getRegisterInfo();
1940 assert(TRI && "Expected target register info");
1941 uint32_t *Mask = MF.allocateRegisterMask(TRI->getNumRegs());
1942 lex();
1943 if (expectAndConsume(MIToken::lparen))
1944 return true;
1945 while (true) {
1946 if (Token.isNot(MIToken::NamedRegister))
1947 return error("expected a named register");
Matthias Braun74ad41c2016-10-11 03:13:01 +00001948 unsigned Reg;
1949 if (parseNamedRegister(Reg))
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00001950 return true;
1951 lex();
1952 Mask[Reg / 32] |= 1U << (Reg % 32);
1953 // TODO: Report an error if the same register is used more than once.
1954 if (Token.isNot(MIToken::comma))
1955 break;
1956 lex();
1957 }
1958 if (expectAndConsume(MIToken::rparen))
1959 return true;
1960 Dest = MachineOperand::CreateRegLiveOut(Mask);
1961 return false;
1962}
1963
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001964bool MIParser::parseMachineOperand(MachineOperand &Dest,
1965 Optional<unsigned> &TiedDefIdx) {
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001966 switch (Token.kind()) {
Alex Lorenzcb268d42015-07-06 23:07:26 +00001967 case MIToken::kw_implicit:
1968 case MIToken::kw_implicit_define:
Alex Lorenze66a7cc2015-08-19 18:55:47 +00001969 case MIToken::kw_def:
Alex Lorenzcbbfd0b2015-07-07 20:34:53 +00001970 case MIToken::kw_dead:
Alex Lorenz495ad872015-07-08 21:23:34 +00001971 case MIToken::kw_killed:
Alex Lorenz4d026b892015-07-08 23:58:31 +00001972 case MIToken::kw_undef:
Alex Lorenz1039fd12015-08-14 19:07:07 +00001973 case MIToken::kw_internal:
Alex Lorenz01c1a5e2015-08-05 17:49:03 +00001974 case MIToken::kw_early_clobber:
Alex Lorenz90752582015-08-05 17:41:17 +00001975 case MIToken::kw_debug_use:
Geoff Berry60c43102017-12-12 17:53:59 +00001976 case MIToken::kw_renamable:
Alex Lorenz12b554e2015-06-24 17:34:58 +00001977 case MIToken::underscore:
Alex Lorenzf3db51de2015-06-23 16:35:26 +00001978 case MIToken::NamedRegister:
Alex Lorenz53464512015-07-10 22:51:20 +00001979 case MIToken::VirtualRegister:
Puyan Lotfi399b46c2018-03-30 18:15:54 +00001980 case MIToken::NamedVirtualRegister:
Alex Lorenz5ef93b02015-08-19 19:05:34 +00001981 return parseRegisterOperand(Dest, TiedDefIdx);
Alex Lorenz240fc1e2015-06-23 23:42:28 +00001982 case MIToken::IntegerLiteral:
1983 return parseImmediateOperand(Dest);
Alex Lorenz05e38822015-08-05 18:52:21 +00001984 case MIToken::IntegerType:
1985 return parseTypedImmediateOperand(Dest);
Alex Lorenzad156fb2015-07-31 20:49:21 +00001986 case MIToken::kw_half:
1987 case MIToken::kw_float:
1988 case MIToken::kw_double:
1989 case MIToken::kw_x86_fp80:
1990 case MIToken::kw_fp128:
1991 case MIToken::kw_ppc_fp128:
1992 return parseFPImmediateOperand(Dest);
Alex Lorenz33f0aef2015-06-26 16:46:11 +00001993 case MIToken::MachineBasicBlock:
1994 return parseMBBOperand(Dest);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +00001995 case MIToken::StackObject:
1996 return parseStackObjectOperand(Dest);
1997 case MIToken::FixedStackObject:
1998 return parseFixedStackObjectOperand(Dest);
Alex Lorenz5d6108e2015-06-26 22:56:48 +00001999 case MIToken::GlobalValue:
2000 case MIToken::NamedGlobalValue:
2001 return parseGlobalAddressOperand(Dest);
Alex Lorenzab980492015-07-20 20:51:18 +00002002 case MIToken::ConstantPoolItem:
2003 return parseConstantPoolIndexOperand(Dest);
Alex Lorenz31d70682015-07-15 23:38:35 +00002004 case MIToken::JumpTableIndex:
2005 return parseJumpTableIndexOperand(Dest);
Alex Lorenz6ede3742015-07-21 16:59:53 +00002006 case MIToken::ExternalSymbol:
Alex Lorenz6ede3742015-07-21 16:59:53 +00002007 return parseExternalSymbolOperand(Dest);
Matthias Braunb74eb412016-03-28 18:18:46 +00002008 case MIToken::SubRegisterIndex:
2009 return parseSubRegisterIndexOperand(Dest);
Reid Kleckner6d353342017-08-23 20:31:27 +00002010 case MIToken::md_diexpr:
Alex Lorenz35e44462015-07-22 17:58:46 +00002011 case MIToken::exclaim:
2012 return parseMetadataOperand(Dest);
Alex Lorenz577d2712015-08-14 21:55:58 +00002013 case MIToken::kw_cfi_same_value:
Alex Lorenz8cfc6862015-07-23 23:09:07 +00002014 case MIToken::kw_cfi_offset:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00002015 case MIToken::kw_cfi_rel_offset:
Alex Lorenz5b0d5f62015-07-27 20:39:03 +00002016 case MIToken::kw_cfi_def_cfa_register:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00002017 case MIToken::kw_cfi_def_cfa_offset:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00002018 case MIToken::kw_cfi_adjust_cfa_offset:
2019 case MIToken::kw_cfi_escape:
Alex Lorenzb1393232015-07-29 18:57:23 +00002020 case MIToken::kw_cfi_def_cfa:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00002021 case MIToken::kw_cfi_register:
2022 case MIToken::kw_cfi_remember_state:
Francis Visoiu Mistrih66d2c262017-11-02 12:00:58 +00002023 case MIToken::kw_cfi_restore:
Francis Visoiu Mistrih5de20e02017-12-15 15:17:18 +00002024 case MIToken::kw_cfi_restore_state:
2025 case MIToken::kw_cfi_undefined:
2026 case MIToken::kw_cfi_window_save:
Alex Lorenzf4baeb52015-07-21 22:28:27 +00002027 return parseCFIOperand(Dest);
Alex Lorenzdeb53492015-07-28 17:28:03 +00002028 case MIToken::kw_blockaddress:
2029 return parseBlockAddressOperand(Dest);
Tim Northover6b3bd612016-07-29 20:32:59 +00002030 case MIToken::kw_intrinsic:
2031 return parseIntrinsicOperand(Dest);
Alex Lorenzef5c1962015-07-28 23:02:45 +00002032 case MIToken::kw_target_index:
2033 return parseTargetIndexOperand(Dest);
Alex Lorenzb97c9ef2015-08-10 23:24:42 +00002034 case MIToken::kw_liveout:
2035 return parseLiveoutRegisterMaskOperand(Dest);
Tim Northoverde3aea0412016-08-17 20:25:25 +00002036 case MIToken::kw_floatpred:
2037 case MIToken::kw_intpred:
2038 return parsePredicateOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002039 case MIToken::Error:
2040 return true;
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002041 case MIToken::Identifier:
2042 if (const auto *RegMask = getRegMask(Token.stringValue())) {
2043 Dest = MachineOperand::CreateRegMask(RegMask);
2044 lex();
2045 break;
Oren Ben Simhon0ef61ec2017-03-19 08:14:18 +00002046 } else
2047 return parseCustomRegisterMaskOperand(Dest);
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002048 default:
Alex Lorenzf22ca8a2015-08-21 21:12:44 +00002049 // FIXME: Parse the MCSymbol machine operand.
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002050 return error("expected a machine operand");
2051 }
Alex Lorenz91370c52015-06-22 20:37:46 +00002052 return false;
2053}
2054
Alex Lorenz5ef93b02015-08-19 19:05:34 +00002055bool MIParser::parseMachineOperandAndTargetFlags(
2056 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
Alex Lorenz49873a82015-08-06 00:44:07 +00002057 unsigned TF = 0;
2058 bool HasTargetFlags = false;
2059 if (Token.is(MIToken::kw_target_flags)) {
2060 HasTargetFlags = true;
2061 lex();
2062 if (expectAndConsume(MIToken::lparen))
2063 return true;
2064 if (Token.isNot(MIToken::Identifier))
2065 return error("expected the name of the target flag");
Alex Lorenzf3630112015-08-18 22:52:15 +00002066 if (getDirectTargetFlag(Token.stringValue(), TF)) {
2067 if (getBitmaskTargetFlag(Token.stringValue(), TF))
2068 return error("use of undefined target flag '" + Token.stringValue() +
2069 "'");
2070 }
Alex Lorenz49873a82015-08-06 00:44:07 +00002071 lex();
Alex Lorenzf3630112015-08-18 22:52:15 +00002072 while (Token.is(MIToken::comma)) {
2073 lex();
2074 if (Token.isNot(MIToken::Identifier))
2075 return error("expected the name of the target flag");
2076 unsigned BitFlag = 0;
2077 if (getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2078 return error("use of undefined target flag '" + Token.stringValue() +
2079 "'");
2080 // TODO: Report an error when using a duplicate bit target flag.
2081 TF |= BitFlag;
2082 lex();
2083 }
Alex Lorenz49873a82015-08-06 00:44:07 +00002084 if (expectAndConsume(MIToken::rparen))
2085 return true;
2086 }
2087 auto Loc = Token.location();
Alex Lorenz5ef93b02015-08-19 19:05:34 +00002088 if (parseMachineOperand(Dest, TiedDefIdx))
Alex Lorenz49873a82015-08-06 00:44:07 +00002089 return true;
2090 if (!HasTargetFlags)
2091 return false;
2092 if (Dest.isReg())
2093 return error(Loc, "register operands can't have target flags");
2094 Dest.setTargetFlags(TF);
2095 return false;
2096}
2097
Alex Lorenzdc24c172015-08-07 20:21:00 +00002098bool MIParser::parseOffset(int64_t &Offset) {
Alex Lorenz5672a892015-08-05 22:26:15 +00002099 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
2100 return false;
Alex Lorenz3fb77682015-08-06 23:17:42 +00002101 StringRef Sign = Token.range();
Alex Lorenz5672a892015-08-05 22:26:15 +00002102 bool IsNegative = Token.is(MIToken::minus);
2103 lex();
2104 if (Token.isNot(MIToken::IntegerLiteral))
2105 return error("expected an integer literal after '" + Sign + "'");
2106 if (Token.integerValue().getMinSignedBits() > 64)
2107 return error("expected 64-bit integer (too large)");
Alex Lorenzdc24c172015-08-07 20:21:00 +00002108 Offset = Token.integerValue().getExtValue();
Alex Lorenz5672a892015-08-05 22:26:15 +00002109 if (IsNegative)
2110 Offset = -Offset;
2111 lex();
Alex Lorenzdc24c172015-08-07 20:21:00 +00002112 return false;
2113}
2114
Alex Lorenz620f8912015-08-13 20:33:33 +00002115bool MIParser::parseAlignment(unsigned &Alignment) {
2116 assert(Token.is(MIToken::kw_align));
2117 lex();
Alex Lorenz68661042015-08-13 20:55:01 +00002118 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
Alex Lorenz620f8912015-08-13 20:33:33 +00002119 return error("expected an integer literal after 'align'");
2120 if (getUnsigned(Alignment))
2121 return true;
2122 lex();
2123 return false;
2124}
2125
Francis Visoiu Mistrihe4718e82018-01-26 11:47:28 +00002126bool MIParser::parseAddrspace(unsigned &Addrspace) {
2127 assert(Token.is(MIToken::kw_addrspace));
2128 lex();
2129 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2130 return error("expected an integer literal after 'addrspace'");
2131 if (getUnsigned(Addrspace))
2132 return true;
2133 lex();
2134 return false;
2135}
2136
Alex Lorenzdc24c172015-08-07 20:21:00 +00002137bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2138 int64_t Offset = 0;
2139 if (parseOffset(Offset))
2140 return true;
Alex Lorenz5672a892015-08-05 22:26:15 +00002141 Op.setOffset(Offset);
2142 return false;
2143}
2144
Alex Lorenz36593ac2015-08-19 23:27:07 +00002145bool MIParser::parseIRValue(const Value *&V) {
Alex Lorenz4af7e612015-08-03 23:08:19 +00002146 switch (Token.kind()) {
Alex Lorenz970c12e2015-08-05 17:35:55 +00002147 case MIToken::NamedIRValue: {
Matthias Braunf1caa282017-12-15 22:22:58 +00002148 V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
Alex Lorenz4af7e612015-08-03 23:08:19 +00002149 break;
2150 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00002151 case MIToken::IRValue: {
2152 unsigned SlotNumber = 0;
2153 if (getUnsigned(SlotNumber))
2154 return true;
2155 V = getIRValue(SlotNumber);
2156 break;
2157 }
Alex Lorenz36efd382015-08-20 00:20:03 +00002158 case MIToken::NamedGlobalValue:
2159 case MIToken::GlobalValue: {
2160 GlobalValue *GV = nullptr;
2161 if (parseGlobalValue(GV))
2162 return true;
2163 V = GV;
2164 break;
2165 }
Alex Lorenzc1136ef32015-08-21 21:54:12 +00002166 case MIToken::QuotedIRValue: {
2167 const Constant *C = nullptr;
2168 if (parseIRConstant(Token.location(), Token.stringValue(), C))
2169 return true;
2170 V = C;
2171 break;
2172 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002173 default:
2174 llvm_unreachable("The current token should be an IR block reference");
2175 }
Alex Lorenzdd13be02015-08-19 23:31:05 +00002176 if (!V)
2177 return error(Twine("use of undefined IR value '") + Token.range() + "'");
Alex Lorenz4af7e612015-08-03 23:08:19 +00002178 return false;
2179}
2180
2181bool MIParser::getUint64(uint64_t &Result) {
Krzysztof Parzyszek36ef5dc2016-12-16 13:58:01 +00002182 if (Token.hasIntegerValue()) {
2183 if (Token.integerValue().getActiveBits() > 64)
2184 return error("expected 64-bit integer (too large)");
2185 Result = Token.integerValue().getZExtValue();
2186 return false;
2187 }
2188 if (Token.is(MIToken::HexLiteral)) {
2189 APInt A;
2190 if (getHexUint(A))
2191 return true;
2192 if (A.getBitWidth() > 64)
2193 return error("expected 64-bit integer (too large)");
2194 Result = A.getZExtValue();
2195 return false;
2196 }
2197 return true;
2198}
2199
2200bool MIParser::getHexUint(APInt &Result) {
2201 assert(Token.is(MIToken::HexLiteral));
2202 StringRef S = Token.range();
2203 assert(S[0] == '0' && tolower(S[1]) == 'x');
2204 // This could be a floating point literal with a special prefix.
2205 if (!isxdigit(S[2]))
2206 return true;
2207 StringRef V = S.substr(2);
2208 APInt A(V.size()*4, V, 16);
Jessica Paquetteb0d17d92017-09-01 22:17:14 +00002209
2210 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2211 // sure it isn't the case before constructing result.
2212 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2213 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
Alex Lorenz4af7e612015-08-03 23:08:19 +00002214 return false;
2215}
2216
Justin Lebar0af80cd2016-07-15 18:26:59 +00002217bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2218 const auto OldFlags = Flags;
Alex Lorenza518b792015-08-04 00:24:45 +00002219 switch (Token.kind()) {
2220 case MIToken::kw_volatile:
2221 Flags |= MachineMemOperand::MOVolatile;
2222 break;
Alex Lorenz10fd0382015-08-06 16:49:30 +00002223 case MIToken::kw_non_temporal:
2224 Flags |= MachineMemOperand::MONonTemporal;
2225 break;
Justin Lebaradbf09e2016-09-11 01:38:58 +00002226 case MIToken::kw_dereferenceable:
2227 Flags |= MachineMemOperand::MODereferenceable;
2228 break;
Alex Lorenzdc8de2a2015-08-06 16:55:53 +00002229 case MIToken::kw_invariant:
2230 Flags |= MachineMemOperand::MOInvariant;
2231 break;
Geoff Berry6748abe2017-07-13 02:28:54 +00002232 case MIToken::StringConstant: {
2233 MachineMemOperand::Flags TF;
2234 if (getMMOTargetFlag(Token.stringValue(), TF))
2235 return error("use of undefined target MMO flag '" + Token.stringValue() +
2236 "'");
2237 Flags |= TF;
2238 break;
2239 }
Alex Lorenza518b792015-08-04 00:24:45 +00002240 default:
2241 llvm_unreachable("The current token should be a memory operand flag");
2242 }
Alex Lorenze86d5152015-08-06 18:26:36 +00002243 if (OldFlags == Flags)
2244 // We know that the same flag is specified more than once when the flags
2245 // weren't modified.
2246 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
Alex Lorenza518b792015-08-04 00:24:45 +00002247 lex();
2248 return false;
2249}
2250
Alex Lorenz91097a32015-08-12 20:33:26 +00002251bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2252 switch (Token.kind()) {
Alex Lorenz46e95582015-08-12 20:44:16 +00002253 case MIToken::kw_stack:
2254 PSV = MF.getPSVManager().getStack();
2255 break;
Alex Lorenzd858f872015-08-12 21:00:22 +00002256 case MIToken::kw_got:
2257 PSV = MF.getPSVManager().getGOT();
2258 break;
Alex Lorenz4be56e92015-08-12 21:11:08 +00002259 case MIToken::kw_jump_table:
2260 PSV = MF.getPSVManager().getJumpTable();
2261 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00002262 case MIToken::kw_constant_pool:
2263 PSV = MF.getPSVManager().getConstantPool();
2264 break;
Alex Lorenz0cc671b2015-08-12 21:23:17 +00002265 case MIToken::FixedStackObject: {
2266 int FI;
2267 if (parseFixedStackFrameIndex(FI))
2268 return true;
2269 PSV = MF.getPSVManager().getFixedStack(FI);
2270 // The token was already consumed, so use return here instead of break.
2271 return false;
2272 }
Matthias Braun3ef7df92016-06-08 00:47:07 +00002273 case MIToken::StackObject: {
2274 int FI;
2275 if (parseStackFrameIndex(FI))
2276 return true;
2277 PSV = MF.getPSVManager().getFixedStack(FI);
2278 // The token was already consumed, so use return here instead of break.
2279 return false;
2280 }
Eugene Zelenkofb69e662017-06-06 22:22:41 +00002281 case MIToken::kw_call_entry:
Alex Lorenz0d009642015-08-20 00:12:57 +00002282 lex();
2283 switch (Token.kind()) {
2284 case MIToken::GlobalValue:
2285 case MIToken::NamedGlobalValue: {
2286 GlobalValue *GV = nullptr;
2287 if (parseGlobalValue(GV))
2288 return true;
2289 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2290 break;
2291 }
2292 case MIToken::ExternalSymbol:
2293 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2294 MF.createExternalSymbolName(Token.stringValue()));
2295 break;
2296 default:
2297 return error(
2298 "expected a global value or an external symbol after 'call-entry'");
2299 }
Alex Lorenz50b826f2015-08-14 21:08:30 +00002300 break;
Alex Lorenz91097a32015-08-12 20:33:26 +00002301 default:
2302 llvm_unreachable("The current token should be pseudo source value");
2303 }
2304 lex();
2305 return false;
2306}
2307
2308bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
Alex Lorenzd858f872015-08-12 21:00:22 +00002309 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
Alex Lorenz0cc671b2015-08-12 21:23:17 +00002310 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
Matthias Braun3ef7df92016-06-08 00:47:07 +00002311 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2312 Token.is(MIToken::kw_call_entry)) {
Alex Lorenz91097a32015-08-12 20:33:26 +00002313 const PseudoSourceValue *PSV = nullptr;
2314 if (parseMemoryPseudoSourceValue(PSV))
2315 return true;
2316 int64_t Offset = 0;
2317 if (parseOffset(Offset))
2318 return true;
2319 Dest = MachinePointerInfo(PSV, Offset);
2320 return false;
2321 }
Alex Lorenz36efd382015-08-20 00:20:03 +00002322 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2323 Token.isNot(MIToken::GlobalValue) &&
Alex Lorenzc1136ef32015-08-21 21:54:12 +00002324 Token.isNot(MIToken::NamedGlobalValue) &&
2325 Token.isNot(MIToken::QuotedIRValue))
Alex Lorenz91097a32015-08-12 20:33:26 +00002326 return error("expected an IR value reference");
Alex Lorenz36593ac2015-08-19 23:27:07 +00002327 const Value *V = nullptr;
Alex Lorenz91097a32015-08-12 20:33:26 +00002328 if (parseIRValue(V))
2329 return true;
2330 if (!V->getType()->isPointerTy())
2331 return error("expected a pointer IR value");
2332 lex();
2333 int64_t Offset = 0;
2334 if (parseOffset(Offset))
2335 return true;
2336 Dest = MachinePointerInfo(V, Offset);
2337 return false;
2338}
2339
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002340bool MIParser::parseOptionalScope(LLVMContext &Context,
2341 SyncScope::ID &SSID) {
2342 SSID = SyncScope::System;
2343 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
2344 lex();
2345 if (expectAndConsume(MIToken::lparen))
2346 return error("expected '(' in syncscope");
2347
2348 std::string SSN;
2349 if (parseStringConstant(SSN))
2350 return true;
2351
2352 SSID = Context.getOrInsertSyncScopeID(SSN);
2353 if (expectAndConsume(MIToken::rparen))
2354 return error("expected ')' in syncscope");
2355 }
2356
2357 return false;
2358}
2359
Tim Northoverb73e3092017-02-13 22:14:08 +00002360bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2361 Order = AtomicOrdering::NotAtomic;
2362 if (Token.isNot(MIToken::Identifier))
2363 return false;
2364
2365 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2366 .Case("unordered", AtomicOrdering::Unordered)
2367 .Case("monotonic", AtomicOrdering::Monotonic)
2368 .Case("acquire", AtomicOrdering::Acquire)
2369 .Case("release", AtomicOrdering::Release)
2370 .Case("acq_rel", AtomicOrdering::AcquireRelease)
2371 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2372 .Default(AtomicOrdering::NotAtomic);
2373
2374 if (Order != AtomicOrdering::NotAtomic) {
2375 lex();
2376 return false;
2377 }
2378
2379 return error("expected an atomic scope, ordering or a size integer literal");
2380}
2381
Alex Lorenz4af7e612015-08-03 23:08:19 +00002382bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2383 if (expectAndConsume(MIToken::lparen))
2384 return true;
Justin Lebar0af80cd2016-07-15 18:26:59 +00002385 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
Alex Lorenza518b792015-08-04 00:24:45 +00002386 while (Token.isMemoryOperandFlag()) {
2387 if (parseMemoryOperandFlag(Flags))
2388 return true;
2389 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002390 if (Token.isNot(MIToken::Identifier) ||
2391 (Token.stringValue() != "load" && Token.stringValue() != "store"))
2392 return error("expected 'load' or 'store' memory operation");
Alex Lorenz4af7e612015-08-03 23:08:19 +00002393 if (Token.stringValue() == "load")
2394 Flags |= MachineMemOperand::MOLoad;
2395 else
2396 Flags |= MachineMemOperand::MOStore;
2397 lex();
2398
Daniel Sanders17d277b2017-11-28 18:57:02 +00002399 // Optional 'store' for operands that both load and store.
2400 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
2401 Flags |= MachineMemOperand::MOStore;
2402 lex();
2403 }
2404
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002405 // Optional synchronization scope.
2406 SyncScope::ID SSID;
Matthias Braunf1caa282017-12-15 22:22:58 +00002407 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002408 return true;
Tim Northoverb73e3092017-02-13 22:14:08 +00002409
2410 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2411 AtomicOrdering Order, FailureOrder;
2412 if (parseOptionalAtomicOrdering(Order))
2413 return true;
2414
2415 if (parseOptionalAtomicOrdering(FailureOrder))
2416 return true;
2417
Alex Lorenz4af7e612015-08-03 23:08:19 +00002418 if (Token.isNot(MIToken::IntegerLiteral))
2419 return error("expected the size integer literal after memory operation");
2420 uint64_t Size;
2421 if (getUint64(Size))
2422 return true;
2423 lex();
2424
Alex Lorenz91097a32015-08-12 20:33:26 +00002425 MachinePointerInfo Ptr = MachinePointerInfo();
Matthias Braunc25c9cc2016-06-04 00:06:31 +00002426 if (Token.is(MIToken::Identifier)) {
Daniel Sanders17d277b2017-11-28 18:57:02 +00002427 const char *Word =
2428 ((Flags & MachineMemOperand::MOLoad) &&
2429 (Flags & MachineMemOperand::MOStore))
2430 ? "on"
2431 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
Matthias Braunc25c9cc2016-06-04 00:06:31 +00002432 if (Token.stringValue() != Word)
2433 return error(Twine("expected '") + Word + "'");
2434 lex();
2435
2436 if (parseMachinePointerInfo(Ptr))
2437 return true;
2438 }
Alex Lorenz61420f72015-08-07 20:48:30 +00002439 unsigned BaseAlignment = Size;
Alex Lorenza617c912015-08-17 22:05:15 +00002440 AAMDNodes AAInfo;
Alex Lorenzeb625682015-08-17 22:09:52 +00002441 MDNode *Range = nullptr;
Alex Lorenza617c912015-08-17 22:05:15 +00002442 while (consumeIfPresent(MIToken::comma)) {
2443 switch (Token.kind()) {
2444 case MIToken::kw_align:
2445 if (parseAlignment(BaseAlignment))
2446 return true;
2447 break;
Francis Visoiu Mistrihe4718e82018-01-26 11:47:28 +00002448 case MIToken::kw_addrspace:
2449 if (parseAddrspace(Ptr.AddrSpace))
2450 return true;
2451 break;
Alex Lorenza617c912015-08-17 22:05:15 +00002452 case MIToken::md_tbaa:
2453 lex();
2454 if (parseMDNode(AAInfo.TBAA))
2455 return true;
2456 break;
Alex Lorenza16f6242015-08-17 22:06:40 +00002457 case MIToken::md_alias_scope:
2458 lex();
2459 if (parseMDNode(AAInfo.Scope))
2460 return true;
2461 break;
Alex Lorenz03e940d2015-08-17 22:08:02 +00002462 case MIToken::md_noalias:
2463 lex();
2464 if (parseMDNode(AAInfo.NoAlias))
2465 return true;
2466 break;
Alex Lorenzeb625682015-08-17 22:09:52 +00002467 case MIToken::md_range:
2468 lex();
2469 if (parseMDNode(Range))
2470 return true;
2471 break;
Alex Lorenza617c912015-08-17 22:05:15 +00002472 // TODO: Report an error on duplicate metadata nodes.
2473 default:
Alex Lorenzeb625682015-08-17 22:09:52 +00002474 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2475 "'!noalias' or '!range'");
Alex Lorenza617c912015-08-17 22:05:15 +00002476 }
Alex Lorenz61420f72015-08-07 20:48:30 +00002477 }
Alex Lorenz4af7e612015-08-03 23:08:19 +00002478 if (expectAndConsume(MIToken::rparen))
2479 return true;
Tim Northoverb73e3092017-02-13 22:14:08 +00002480 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002481 SSID, Order, FailureOrder);
Alex Lorenz4af7e612015-08-03 23:08:19 +00002482 return false;
2483}
2484
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002485void MIParser::initNames2InstrOpCodes() {
2486 if (!Names2InstrOpCodes.empty())
2487 return;
2488 const auto *TII = MF.getSubtarget().getInstrInfo();
2489 assert(TII && "Expected target instruction info");
2490 for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I)
2491 Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I));
2492}
2493
2494bool MIParser::parseInstrName(StringRef InstrName, unsigned &OpCode) {
2495 initNames2InstrOpCodes();
2496 auto InstrInfo = Names2InstrOpCodes.find(InstrName);
2497 if (InstrInfo == Names2InstrOpCodes.end())
2498 return true;
2499 OpCode = InstrInfo->getValue();
2500 return false;
2501}
2502
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002503void MIParser::initNames2Regs() {
2504 if (!Names2Regs.empty())
2505 return;
Alex Lorenz12b554e2015-06-24 17:34:58 +00002506 // The '%noreg' register is the register 0.
2507 Names2Regs.insert(std::make_pair("noreg", 0));
Alex Lorenzf3db51de2015-06-23 16:35:26 +00002508 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2509 assert(TRI && "Expected target register info");
2510 for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) {
2511 bool WasInserted =
2512 Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I))
2513 .second;
2514 (void)WasInserted;
2515 assert(WasInserted && "Expected registers to be unique case-insensitively");
2516 }
2517}
2518
2519bool MIParser::getRegisterByName(StringRef RegName, unsigned &Reg) {
2520 initNames2Regs();
2521 auto RegInfo = Names2Regs.find(RegName);
2522 if (RegInfo == Names2Regs.end())
2523 return true;
2524 Reg = RegInfo->getValue();
2525 return false;
2526}
2527
Alex Lorenz8f6f4282015-06-29 16:57:06 +00002528void MIParser::initNames2RegMasks() {
2529 if (!Names2RegMasks.empty())
2530 return;
2531 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2532 assert(TRI && "Expected target register info");
2533 ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks();
2534 ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames();
2535 assert(RegMasks.size() == RegMaskNames.size());
2536 for (size_t I = 0, E = RegMasks.size(); I < E; ++I)
2537 Names2RegMasks.insert(
2538 std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I]));
2539}
2540
2541const uint32_t *MIParser::getRegMask(StringRef Identifier) {
2542 initNames2RegMasks();
2543 auto RegMaskInfo = Names2RegMasks.find(Identifier);
2544 if (RegMaskInfo == Names2RegMasks.end())
2545 return nullptr;
2546 return RegMaskInfo->getValue();
2547}
2548
Alex Lorenz2eacca82015-07-13 23:24:34 +00002549void MIParser::initNames2SubRegIndices() {
2550 if (!Names2SubRegIndices.empty())
2551 return;
2552 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
2553 for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I)
2554 Names2SubRegIndices.insert(
2555 std::make_pair(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
2556}
2557
2558unsigned MIParser::getSubRegIndex(StringRef Name) {
2559 initNames2SubRegIndices();
2560 auto SubRegInfo = Names2SubRegIndices.find(Name);
2561 if (SubRegInfo == Names2SubRegIndices.end())
2562 return 0;
2563 return SubRegInfo->getValue();
2564}
2565
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002566static void initSlots2BasicBlocks(
2567 const Function &F,
2568 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2569 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002570 MST.incorporateFunction(F);
2571 for (auto &BB : F) {
2572 if (BB.hasName())
2573 continue;
2574 int Slot = MST.getLocalSlot(&BB);
2575 if (Slot == -1)
2576 continue;
2577 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2578 }
2579}
2580
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002581static const BasicBlock *getIRBlockFromSlot(
2582 unsigned Slot,
2583 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
Alex Lorenz8a1915b2015-07-27 22:42:41 +00002584 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2585 if (BlockInfo == Slots2BasicBlocks.end())
2586 return nullptr;
2587 return BlockInfo->second;
2588}
2589
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002590const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2591 if (Slots2BasicBlocks.empty())
Matthias Braunf1caa282017-12-15 22:22:58 +00002592 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002593 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2594}
2595
2596const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
Matthias Braunf1caa282017-12-15 22:22:58 +00002597 if (&F == &MF.getFunction())
Alex Lorenzcba8c5f2015-08-06 23:57:04 +00002598 return getIRBlock(Slot);
2599 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2600 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2601 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2602}
2603
Alex Lorenzdd13be02015-08-19 23:31:05 +00002604static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2605 DenseMap<unsigned, const Value *> &Slots2Values) {
2606 int Slot = MST.getLocalSlot(V);
2607 if (Slot == -1)
2608 return;
2609 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2610}
2611
2612/// Creates the mapping from slot numbers to function's unnamed IR values.
2613static void initSlots2Values(const Function &F,
2614 DenseMap<unsigned, const Value *> &Slots2Values) {
2615 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2616 MST.incorporateFunction(F);
2617 for (const auto &Arg : F.args())
2618 mapValueToSlot(&Arg, MST, Slots2Values);
2619 for (const auto &BB : F) {
2620 mapValueToSlot(&BB, MST, Slots2Values);
2621 for (const auto &I : BB)
2622 mapValueToSlot(&I, MST, Slots2Values);
2623 }
2624}
2625
2626const Value *MIParser::getIRValue(unsigned Slot) {
2627 if (Slots2Values.empty())
Matthias Braunf1caa282017-12-15 22:22:58 +00002628 initSlots2Values(MF.getFunction(), Slots2Values);
Alex Lorenzdd13be02015-08-19 23:31:05 +00002629 auto ValueInfo = Slots2Values.find(Slot);
2630 if (ValueInfo == Slots2Values.end())
2631 return nullptr;
2632 return ValueInfo->second;
2633}
2634
Alex Lorenzef5c1962015-07-28 23:02:45 +00002635void MIParser::initNames2TargetIndices() {
2636 if (!Names2TargetIndices.empty())
2637 return;
2638 const auto *TII = MF.getSubtarget().getInstrInfo();
2639 assert(TII && "Expected target instruction info");
2640 auto Indices = TII->getSerializableTargetIndices();
2641 for (const auto &I : Indices)
2642 Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first));
2643}
2644
2645bool MIParser::getTargetIndex(StringRef Name, int &Index) {
2646 initNames2TargetIndices();
2647 auto IndexInfo = Names2TargetIndices.find(Name);
2648 if (IndexInfo == Names2TargetIndices.end())
2649 return true;
2650 Index = IndexInfo->second;
2651 return false;
2652}
2653
Alex Lorenz49873a82015-08-06 00:44:07 +00002654void MIParser::initNames2DirectTargetFlags() {
2655 if (!Names2DirectTargetFlags.empty())
2656 return;
2657 const auto *TII = MF.getSubtarget().getInstrInfo();
2658 assert(TII && "Expected target instruction info");
2659 auto Flags = TII->getSerializableDirectMachineOperandTargetFlags();
2660 for (const auto &I : Flags)
2661 Names2DirectTargetFlags.insert(
2662 std::make_pair(StringRef(I.second), I.first));
2663}
2664
2665bool MIParser::getDirectTargetFlag(StringRef Name, unsigned &Flag) {
2666 initNames2DirectTargetFlags();
2667 auto FlagInfo = Names2DirectTargetFlags.find(Name);
2668 if (FlagInfo == Names2DirectTargetFlags.end())
2669 return true;
2670 Flag = FlagInfo->second;
2671 return false;
2672}
2673
Alex Lorenzf3630112015-08-18 22:52:15 +00002674void MIParser::initNames2BitmaskTargetFlags() {
2675 if (!Names2BitmaskTargetFlags.empty())
2676 return;
2677 const auto *TII = MF.getSubtarget().getInstrInfo();
2678 assert(TII && "Expected target instruction info");
2679 auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags();
2680 for (const auto &I : Flags)
2681 Names2BitmaskTargetFlags.insert(
2682 std::make_pair(StringRef(I.second), I.first));
2683}
2684
2685bool MIParser::getBitmaskTargetFlag(StringRef Name, unsigned &Flag) {
2686 initNames2BitmaskTargetFlags();
2687 auto FlagInfo = Names2BitmaskTargetFlags.find(Name);
2688 if (FlagInfo == Names2BitmaskTargetFlags.end())
2689 return true;
2690 Flag = FlagInfo->second;
2691 return false;
2692}
2693
Geoff Berry6748abe2017-07-13 02:28:54 +00002694void MIParser::initNames2MMOTargetFlags() {
2695 if (!Names2MMOTargetFlags.empty())
2696 return;
2697 const auto *TII = MF.getSubtarget().getInstrInfo();
2698 assert(TII && "Expected target instruction info");
2699 auto Flags = TII->getSerializableMachineMemOperandTargetFlags();
2700 for (const auto &I : Flags)
2701 Names2MMOTargetFlags.insert(
2702 std::make_pair(StringRef(I.second), I.first));
2703}
2704
2705bool MIParser::getMMOTargetFlag(StringRef Name,
2706 MachineMemOperand::Flags &Flag) {
2707 initNames2MMOTargetFlags();
2708 auto FlagInfo = Names2MMOTargetFlags.find(Name);
2709 if (FlagInfo == Names2MMOTargetFlags.end())
2710 return true;
2711 Flag = FlagInfo->second;
2712 return false;
2713}
2714
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +00002715bool MIParser::parseStringConstant(std::string &Result) {
2716 if (Token.isNot(MIToken::StringConstant))
2717 return error("expected string constant");
2718 Result = Token.stringValue();
2719 lex();
2720 return false;
2721}
2722
Matthias Braun83947862016-07-13 22:23:23 +00002723bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2724 StringRef Src,
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002725 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002726 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
Alex Lorenz5022f6b2015-08-13 23:10:16 +00002727}
2728
Matthias Braun74ad41c2016-10-11 03:13:01 +00002729bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002730 StringRef Src, SMDiagnostic &Error) {
2731 return MIParser(PFS, Error, Src).parseBasicBlocks();
Alex Lorenz8e0a1b42015-06-22 17:02:30 +00002732}
Alex Lorenzf09df002015-06-30 18:16:42 +00002733
Matthias Braun74ad41c2016-10-11 03:13:01 +00002734bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002735 MachineBasicBlock *&MBB, StringRef Src,
Matthias Braun83947862016-07-13 22:23:23 +00002736 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002737 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
Alex Lorenzf09df002015-06-30 18:16:42 +00002738}
Alex Lorenz9fab3702015-07-14 21:24:41 +00002739
Tom Stellard9c884e42016-11-15 00:03:14 +00002740bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
2741 unsigned &Reg, StringRef Src,
2742 SMDiagnostic &Error) {
2743 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
2744}
2745
Matthias Braun74ad41c2016-10-11 03:13:01 +00002746bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002747 unsigned &Reg, StringRef Src,
Alex Lorenz9fab3702015-07-14 21:24:41 +00002748 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002749 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
Alex Lorenz9fab3702015-07-14 21:24:41 +00002750}
Alex Lorenz12045a42015-07-27 17:42:45 +00002751
Matthias Braun74ad41c2016-10-11 03:13:01 +00002752bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
2753 VRegInfo *&Info, StringRef Src,
Alex Lorenz12045a42015-07-27 17:42:45 +00002754 SMDiagnostic &Error) {
Matthias Braun74ad41c2016-10-11 03:13:01 +00002755 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
Alex Lorenz12045a42015-07-27 17:42:45 +00002756}
Alex Lorenza314d812015-08-18 22:26:26 +00002757
Matthias Braun74ad41c2016-10-11 03:13:01 +00002758bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002759 int &FI, StringRef Src,
Alex Lorenza314d812015-08-18 22:26:26 +00002760 SMDiagnostic &Error) {
Matthias Braune35861d2016-07-13 23:27:50 +00002761 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
Alex Lorenza314d812015-08-18 22:26:26 +00002762}
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002763
Matthias Braun74ad41c2016-10-11 03:13:01 +00002764bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
Matthias Braune35861d2016-07-13 23:27:50 +00002765 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
2766 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
Alex Lorenzdf9e3c62015-08-19 00:13:25 +00002767}