File: | lib/CodeGen/MIRParser/MIParser.cpp |
Warning: | line 2844, column 34 The left operand of '!=' is a garbage value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- MIParser.cpp - Machine instructions parser implementation ----------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file implements the parsing of machine instructions. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #include "llvm/CodeGen/MIRParser/MIParser.h" | |||
14 | #include "MILexer.h" | |||
15 | #include "llvm/ADT/APInt.h" | |||
16 | #include "llvm/ADT/APSInt.h" | |||
17 | #include "llvm/ADT/ArrayRef.h" | |||
18 | #include "llvm/ADT/DenseMap.h" | |||
19 | #include "llvm/ADT/None.h" | |||
20 | #include "llvm/ADT/Optional.h" | |||
21 | #include "llvm/ADT/SmallVector.h" | |||
22 | #include "llvm/ADT/StringMap.h" | |||
23 | #include "llvm/ADT/StringRef.h" | |||
24 | #include "llvm/ADT/StringSwitch.h" | |||
25 | #include "llvm/ADT/Twine.h" | |||
26 | #include "llvm/Analysis/MemoryLocation.h" | |||
27 | #include "llvm/AsmParser/Parser.h" | |||
28 | #include "llvm/AsmParser/SlotMapping.h" | |||
29 | #include "llvm/CodeGen/GlobalISel/RegisterBank.h" | |||
30 | #include "llvm/CodeGen/GlobalISel/RegisterBankInfo.h" | |||
31 | #include "llvm/CodeGen/MIRPrinter.h" | |||
32 | #include "llvm/CodeGen/MachineBasicBlock.h" | |||
33 | #include "llvm/CodeGen/MachineFrameInfo.h" | |||
34 | #include "llvm/CodeGen/MachineFunction.h" | |||
35 | #include "llvm/CodeGen/MachineInstr.h" | |||
36 | #include "llvm/CodeGen/MachineInstrBuilder.h" | |||
37 | #include "llvm/CodeGen/MachineMemOperand.h" | |||
38 | #include "llvm/CodeGen/MachineOperand.h" | |||
39 | #include "llvm/CodeGen/MachineRegisterInfo.h" | |||
40 | #include "llvm/CodeGen/TargetInstrInfo.h" | |||
41 | #include "llvm/CodeGen/TargetRegisterInfo.h" | |||
42 | #include "llvm/CodeGen/TargetSubtargetInfo.h" | |||
43 | #include "llvm/IR/BasicBlock.h" | |||
44 | #include "llvm/IR/Constants.h" | |||
45 | #include "llvm/IR/DataLayout.h" | |||
46 | #include "llvm/IR/DebugInfoMetadata.h" | |||
47 | #include "llvm/IR/DebugLoc.h" | |||
48 | #include "llvm/IR/Function.h" | |||
49 | #include "llvm/IR/InstrTypes.h" | |||
50 | #include "llvm/IR/Instructions.h" | |||
51 | #include "llvm/IR/Intrinsics.h" | |||
52 | #include "llvm/IR/Metadata.h" | |||
53 | #include "llvm/IR/Module.h" | |||
54 | #include "llvm/IR/ModuleSlotTracker.h" | |||
55 | #include "llvm/IR/Type.h" | |||
56 | #include "llvm/IR/Value.h" | |||
57 | #include "llvm/IR/ValueSymbolTable.h" | |||
58 | #include "llvm/MC/LaneBitmask.h" | |||
59 | #include "llvm/MC/MCContext.h" | |||
60 | #include "llvm/MC/MCDwarf.h" | |||
61 | #include "llvm/MC/MCInstrDesc.h" | |||
62 | #include "llvm/MC/MCRegisterInfo.h" | |||
63 | #include "llvm/Support/AtomicOrdering.h" | |||
64 | #include "llvm/Support/BranchProbability.h" | |||
65 | #include "llvm/Support/Casting.h" | |||
66 | #include "llvm/Support/ErrorHandling.h" | |||
67 | #include "llvm/Support/LowLevelTypeImpl.h" | |||
68 | #include "llvm/Support/MemoryBuffer.h" | |||
69 | #include "llvm/Support/SMLoc.h" | |||
70 | #include "llvm/Support/SourceMgr.h" | |||
71 | #include "llvm/Support/raw_ostream.h" | |||
72 | #include "llvm/Target/TargetIntrinsicInfo.h" | |||
73 | #include "llvm/Target/TargetMachine.h" | |||
74 | #include <algorithm> | |||
75 | #include <cassert> | |||
76 | #include <cctype> | |||
77 | #include <cstddef> | |||
78 | #include <cstdint> | |||
79 | #include <limits> | |||
80 | #include <string> | |||
81 | #include <utility> | |||
82 | ||||
83 | using namespace llvm; | |||
84 | ||||
85 | void PerTargetMIParsingState::setTarget( | |||
86 | const TargetSubtargetInfo &NewSubtarget) { | |||
87 | ||||
88 | // If the subtarget changed, over conservatively assume everything is invalid. | |||
89 | if (&Subtarget == &NewSubtarget) | |||
90 | return; | |||
91 | ||||
92 | Names2InstrOpCodes.clear(); | |||
93 | Names2Regs.clear(); | |||
94 | Names2RegMasks.clear(); | |||
95 | Names2SubRegIndices.clear(); | |||
96 | Names2TargetIndices.clear(); | |||
97 | Names2DirectTargetFlags.clear(); | |||
98 | Names2BitmaskTargetFlags.clear(); | |||
99 | Names2MMOTargetFlags.clear(); | |||
100 | ||||
101 | initNames2RegClasses(); | |||
102 | initNames2RegBanks(); | |||
103 | } | |||
104 | ||||
105 | void PerTargetMIParsingState::initNames2Regs() { | |||
106 | if (!Names2Regs.empty()) | |||
107 | return; | |||
108 | ||||
109 | // The '%noreg' register is the register 0. | |||
110 | Names2Regs.insert(std::make_pair("noreg", 0)); | |||
111 | const auto *TRI = Subtarget.getRegisterInfo(); | |||
112 | assert(TRI && "Expected target register info")((TRI && "Expected target register info") ? static_cast <void> (0) : __assert_fail ("TRI && \"Expected target register info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 112, __PRETTY_FUNCTION__)); | |||
113 | ||||
114 | for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) { | |||
115 | bool WasInserted = | |||
116 | Names2Regs.insert(std::make_pair(StringRef(TRI->getName(I)).lower(), I)) | |||
117 | .second; | |||
118 | (void)WasInserted; | |||
119 | assert(WasInserted && "Expected registers to be unique case-insensitively")((WasInserted && "Expected registers to be unique case-insensitively" ) ? static_cast<void> (0) : __assert_fail ("WasInserted && \"Expected registers to be unique case-insensitively\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 119, __PRETTY_FUNCTION__)); | |||
120 | } | |||
121 | } | |||
122 | ||||
123 | bool PerTargetMIParsingState::getRegisterByName(StringRef RegName, | |||
124 | unsigned &Reg) { | |||
125 | initNames2Regs(); | |||
126 | auto RegInfo = Names2Regs.find(RegName); | |||
127 | if (RegInfo == Names2Regs.end()) | |||
128 | return true; | |||
129 | Reg = RegInfo->getValue(); | |||
130 | return false; | |||
131 | } | |||
132 | ||||
133 | void PerTargetMIParsingState::initNames2InstrOpCodes() { | |||
134 | if (!Names2InstrOpCodes.empty()) | |||
135 | return; | |||
136 | const auto *TII = Subtarget.getInstrInfo(); | |||
137 | assert(TII && "Expected target instruction info")((TII && "Expected target instruction info") ? static_cast <void> (0) : __assert_fail ("TII && \"Expected target instruction info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 137, __PRETTY_FUNCTION__)); | |||
138 | for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I) | |||
139 | Names2InstrOpCodes.insert(std::make_pair(StringRef(TII->getName(I)), I)); | |||
140 | } | |||
141 | ||||
142 | bool PerTargetMIParsingState::parseInstrName(StringRef InstrName, | |||
143 | unsigned &OpCode) { | |||
144 | initNames2InstrOpCodes(); | |||
145 | auto InstrInfo = Names2InstrOpCodes.find(InstrName); | |||
146 | if (InstrInfo == Names2InstrOpCodes.end()) | |||
147 | return true; | |||
148 | OpCode = InstrInfo->getValue(); | |||
149 | return false; | |||
150 | } | |||
151 | ||||
152 | void PerTargetMIParsingState::initNames2RegMasks() { | |||
153 | if (!Names2RegMasks.empty()) | |||
154 | return; | |||
155 | const auto *TRI = Subtarget.getRegisterInfo(); | |||
156 | assert(TRI && "Expected target register info")((TRI && "Expected target register info") ? static_cast <void> (0) : __assert_fail ("TRI && \"Expected target register info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 156, __PRETTY_FUNCTION__)); | |||
157 | ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks(); | |||
158 | ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames(); | |||
159 | assert(RegMasks.size() == RegMaskNames.size())((RegMasks.size() == RegMaskNames.size()) ? static_cast<void > (0) : __assert_fail ("RegMasks.size() == RegMaskNames.size()" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 159, __PRETTY_FUNCTION__)); | |||
160 | for (size_t I = 0, E = RegMasks.size(); I < E; ++I) | |||
161 | Names2RegMasks.insert( | |||
162 | std::make_pair(StringRef(RegMaskNames[I]).lower(), RegMasks[I])); | |||
163 | } | |||
164 | ||||
165 | const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier) { | |||
166 | initNames2RegMasks(); | |||
167 | auto RegMaskInfo = Names2RegMasks.find(Identifier); | |||
168 | if (RegMaskInfo == Names2RegMasks.end()) | |||
169 | return nullptr; | |||
170 | return RegMaskInfo->getValue(); | |||
171 | } | |||
172 | ||||
173 | void PerTargetMIParsingState::initNames2SubRegIndices() { | |||
174 | if (!Names2SubRegIndices.empty()) | |||
175 | return; | |||
176 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
177 | for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I) | |||
178 | Names2SubRegIndices.insert( | |||
179 | std::make_pair(TRI->getSubRegIndexName(I), I)); | |||
180 | } | |||
181 | ||||
182 | unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name) { | |||
183 | initNames2SubRegIndices(); | |||
184 | auto SubRegInfo = Names2SubRegIndices.find(Name); | |||
185 | if (SubRegInfo == Names2SubRegIndices.end()) | |||
186 | return 0; | |||
187 | return SubRegInfo->getValue(); | |||
188 | } | |||
189 | ||||
190 | void PerTargetMIParsingState::initNames2TargetIndices() { | |||
191 | if (!Names2TargetIndices.empty()) | |||
192 | return; | |||
193 | const auto *TII = Subtarget.getInstrInfo(); | |||
194 | assert(TII && "Expected target instruction info")((TII && "Expected target instruction info") ? static_cast <void> (0) : __assert_fail ("TII && \"Expected target instruction info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 194, __PRETTY_FUNCTION__)); | |||
195 | auto Indices = TII->getSerializableTargetIndices(); | |||
196 | for (const auto &I : Indices) | |||
197 | Names2TargetIndices.insert(std::make_pair(StringRef(I.second), I.first)); | |||
198 | } | |||
199 | ||||
200 | bool PerTargetMIParsingState::getTargetIndex(StringRef Name, int &Index) { | |||
201 | initNames2TargetIndices(); | |||
202 | auto IndexInfo = Names2TargetIndices.find(Name); | |||
203 | if (IndexInfo == Names2TargetIndices.end()) | |||
204 | return true; | |||
205 | Index = IndexInfo->second; | |||
206 | return false; | |||
207 | } | |||
208 | ||||
209 | void PerTargetMIParsingState::initNames2DirectTargetFlags() { | |||
210 | if (!Names2DirectTargetFlags.empty()) | |||
211 | return; | |||
212 | ||||
213 | const auto *TII = Subtarget.getInstrInfo(); | |||
214 | assert(TII && "Expected target instruction info")((TII && "Expected target instruction info") ? static_cast <void> (0) : __assert_fail ("TII && \"Expected target instruction info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 214, __PRETTY_FUNCTION__)); | |||
215 | auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); | |||
216 | for (const auto &I : Flags) | |||
217 | Names2DirectTargetFlags.insert( | |||
218 | std::make_pair(StringRef(I.second), I.first)); | |||
219 | } | |||
220 | ||||
221 | bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name, | |||
222 | unsigned &Flag) { | |||
223 | initNames2DirectTargetFlags(); | |||
224 | auto FlagInfo = Names2DirectTargetFlags.find(Name); | |||
225 | if (FlagInfo == Names2DirectTargetFlags.end()) | |||
226 | return true; | |||
227 | Flag = FlagInfo->second; | |||
228 | return false; | |||
229 | } | |||
230 | ||||
231 | void PerTargetMIParsingState::initNames2BitmaskTargetFlags() { | |||
232 | if (!Names2BitmaskTargetFlags.empty()) | |||
233 | return; | |||
234 | ||||
235 | const auto *TII = Subtarget.getInstrInfo(); | |||
236 | assert(TII && "Expected target instruction info")((TII && "Expected target instruction info") ? static_cast <void> (0) : __assert_fail ("TII && \"Expected target instruction info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 236, __PRETTY_FUNCTION__)); | |||
237 | auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags(); | |||
238 | for (const auto &I : Flags) | |||
239 | Names2BitmaskTargetFlags.insert( | |||
240 | std::make_pair(StringRef(I.second), I.first)); | |||
241 | } | |||
242 | ||||
243 | bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name, | |||
244 | unsigned &Flag) { | |||
245 | initNames2BitmaskTargetFlags(); | |||
246 | auto FlagInfo = Names2BitmaskTargetFlags.find(Name); | |||
247 | if (FlagInfo == Names2BitmaskTargetFlags.end()) | |||
248 | return true; | |||
249 | Flag = FlagInfo->second; | |||
250 | return false; | |||
251 | } | |||
252 | ||||
253 | void PerTargetMIParsingState::initNames2MMOTargetFlags() { | |||
254 | if (!Names2MMOTargetFlags.empty()) | |||
255 | return; | |||
256 | ||||
257 | const auto *TII = Subtarget.getInstrInfo(); | |||
258 | assert(TII && "Expected target instruction info")((TII && "Expected target instruction info") ? static_cast <void> (0) : __assert_fail ("TII && \"Expected target instruction info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 258, __PRETTY_FUNCTION__)); | |||
259 | auto Flags = TII->getSerializableMachineMemOperandTargetFlags(); | |||
260 | for (const auto &I : Flags) | |||
261 | Names2MMOTargetFlags.insert(std::make_pair(StringRef(I.second), I.first)); | |||
262 | } | |||
263 | ||||
264 | bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name, | |||
265 | MachineMemOperand::Flags &Flag) { | |||
266 | initNames2MMOTargetFlags(); | |||
267 | auto FlagInfo = Names2MMOTargetFlags.find(Name); | |||
268 | if (FlagInfo == Names2MMOTargetFlags.end()) | |||
269 | return true; | |||
270 | Flag = FlagInfo->second; | |||
271 | return false; | |||
272 | } | |||
273 | ||||
274 | void PerTargetMIParsingState::initNames2RegClasses() { | |||
275 | if (!Names2RegClasses.empty()) | |||
276 | return; | |||
277 | ||||
278 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
279 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { | |||
280 | const auto *RC = TRI->getRegClass(I); | |||
281 | Names2RegClasses.insert( | |||
282 | std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC)); | |||
283 | } | |||
284 | } | |||
285 | ||||
286 | void PerTargetMIParsingState::initNames2RegBanks() { | |||
287 | if (!Names2RegBanks.empty()) | |||
288 | return; | |||
289 | ||||
290 | const RegisterBankInfo *RBI = Subtarget.getRegBankInfo(); | |||
291 | // If the target does not support GlobalISel, we may not have a | |||
292 | // register bank info. | |||
293 | if (!RBI) | |||
294 | return; | |||
295 | ||||
296 | for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) { | |||
297 | const auto &RegBank = RBI->getRegBank(I); | |||
298 | Names2RegBanks.insert( | |||
299 | std::make_pair(StringRef(RegBank.getName()).lower(), &RegBank)); | |||
300 | } | |||
301 | } | |||
302 | ||||
303 | const TargetRegisterClass * | |||
304 | PerTargetMIParsingState::getRegClass(StringRef Name) { | |||
305 | auto RegClassInfo = Names2RegClasses.find(Name); | |||
306 | if (RegClassInfo == Names2RegClasses.end()) | |||
307 | return nullptr; | |||
308 | return RegClassInfo->getValue(); | |||
309 | } | |||
310 | ||||
311 | const RegisterBank *PerTargetMIParsingState::getRegBank(StringRef Name) { | |||
312 | auto RegBankInfo = Names2RegBanks.find(Name); | |||
313 | if (RegBankInfo == Names2RegBanks.end()) | |||
314 | return nullptr; | |||
315 | return RegBankInfo->getValue(); | |||
316 | } | |||
317 | ||||
318 | PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF, | |||
319 | SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T) | |||
320 | : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) { | |||
321 | } | |||
322 | ||||
323 | VRegInfo &PerFunctionMIParsingState::getVRegInfo(unsigned Num) { | |||
324 | auto I = VRegInfos.insert(std::make_pair(Num, nullptr)); | |||
325 | if (I.second) { | |||
326 | MachineRegisterInfo &MRI = MF.getRegInfo(); | |||
327 | VRegInfo *Info = new (Allocator) VRegInfo; | |||
328 | Info->VReg = MRI.createIncompleteVirtualRegister(); | |||
329 | I.first->second = Info; | |||
330 | } | |||
331 | return *I.first->second; | |||
332 | } | |||
333 | ||||
334 | VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) { | |||
335 | assert(RegName != "" && "Expected named reg.")((RegName != "" && "Expected named reg.") ? static_cast <void> (0) : __assert_fail ("RegName != \"\" && \"Expected named reg.\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 335, __PRETTY_FUNCTION__)); | |||
336 | ||||
337 | auto I = VRegInfosNamed.insert(std::make_pair(RegName.str(), nullptr)); | |||
338 | if (I.second) { | |||
339 | VRegInfo *Info = new (Allocator) VRegInfo; | |||
340 | Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(RegName); | |||
341 | I.first->second = Info; | |||
342 | } | |||
343 | return *I.first->second; | |||
344 | } | |||
345 | ||||
346 | namespace { | |||
347 | ||||
348 | /// A wrapper struct around the 'MachineOperand' struct that includes a source | |||
349 | /// range and other attributes. | |||
350 | struct ParsedMachineOperand { | |||
351 | MachineOperand Operand; | |||
352 | StringRef::iterator Begin; | |||
353 | StringRef::iterator End; | |||
354 | Optional<unsigned> TiedDefIdx; | |||
355 | ||||
356 | ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin, | |||
357 | StringRef::iterator End, Optional<unsigned> &TiedDefIdx) | |||
358 | : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) { | |||
359 | if (TiedDefIdx) | |||
360 | assert(Operand.isReg() && Operand.isUse() &&((Operand.isReg() && Operand.isUse() && "Only used register operands can be tied" ) ? static_cast<void> (0) : __assert_fail ("Operand.isReg() && Operand.isUse() && \"Only used register operands can be tied\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 361, __PRETTY_FUNCTION__)) | |||
361 | "Only used register operands can be tied")((Operand.isReg() && Operand.isUse() && "Only used register operands can be tied" ) ? static_cast<void> (0) : __assert_fail ("Operand.isReg() && Operand.isUse() && \"Only used register operands can be tied\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 361, __PRETTY_FUNCTION__)); | |||
362 | } | |||
363 | }; | |||
364 | ||||
365 | class MIParser { | |||
366 | MachineFunction &MF; | |||
367 | SMDiagnostic &Error; | |||
368 | StringRef Source, CurrentSource; | |||
369 | MIToken Token; | |||
370 | PerFunctionMIParsingState &PFS; | |||
371 | /// Maps from slot numbers to function's unnamed basic blocks. | |||
372 | DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks; | |||
373 | /// Maps from slot numbers to function's unnamed values. | |||
374 | DenseMap<unsigned, const Value *> Slots2Values; | |||
375 | ||||
376 | public: | |||
377 | MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error, | |||
378 | StringRef Source); | |||
379 | ||||
380 | /// \p SkipChar gives the number of characters to skip before looking | |||
381 | /// for the next token. | |||
382 | void lex(unsigned SkipChar = 0); | |||
383 | ||||
384 | /// Report an error at the current location with the given message. | |||
385 | /// | |||
386 | /// This function always return true. | |||
387 | bool error(const Twine &Msg); | |||
388 | ||||
389 | /// Report an error at the given location with the given message. | |||
390 | /// | |||
391 | /// This function always return true. | |||
392 | bool error(StringRef::iterator Loc, const Twine &Msg); | |||
393 | ||||
394 | bool | |||
395 | parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); | |||
396 | bool parseBasicBlocks(); | |||
397 | bool parse(MachineInstr *&MI); | |||
398 | bool parseStandaloneMBB(MachineBasicBlock *&MBB); | |||
399 | bool parseStandaloneNamedRegister(unsigned &Reg); | |||
400 | bool parseStandaloneVirtualRegister(VRegInfo *&Info); | |||
401 | bool parseStandaloneRegister(unsigned &Reg); | |||
402 | bool parseStandaloneStackObject(int &FI); | |||
403 | bool parseStandaloneMDNode(MDNode *&Node); | |||
404 | ||||
405 | bool | |||
406 | parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); | |||
407 | bool parseBasicBlock(MachineBasicBlock &MBB, | |||
408 | MachineBasicBlock *&AddFalthroughFrom); | |||
409 | bool parseBasicBlockLiveins(MachineBasicBlock &MBB); | |||
410 | bool parseBasicBlockSuccessors(MachineBasicBlock &MBB); | |||
411 | ||||
412 | bool parseNamedRegister(unsigned &Reg); | |||
413 | bool parseVirtualRegister(VRegInfo *&Info); | |||
414 | bool parseNamedVirtualRegister(VRegInfo *&Info); | |||
415 | bool parseRegister(unsigned &Reg, VRegInfo *&VRegInfo); | |||
416 | bool parseRegisterFlag(unsigned &Flags); | |||
417 | bool parseRegisterClassOrBank(VRegInfo &RegInfo); | |||
418 | bool parseSubRegisterIndex(unsigned &SubReg); | |||
419 | bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx); | |||
420 | bool parseRegisterOperand(MachineOperand &Dest, | |||
421 | Optional<unsigned> &TiedDefIdx, bool IsDef = false); | |||
422 | bool parseImmediateOperand(MachineOperand &Dest); | |||
423 | bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue, | |||
424 | const Constant *&C); | |||
425 | bool parseIRConstant(StringRef::iterator Loc, const Constant *&C); | |||
426 | bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty); | |||
427 | bool parseTypedImmediateOperand(MachineOperand &Dest); | |||
428 | bool parseFPImmediateOperand(MachineOperand &Dest); | |||
429 | bool parseMBBReference(MachineBasicBlock *&MBB); | |||
430 | bool parseMBBOperand(MachineOperand &Dest); | |||
431 | bool parseStackFrameIndex(int &FI); | |||
432 | bool parseStackObjectOperand(MachineOperand &Dest); | |||
433 | bool parseFixedStackFrameIndex(int &FI); | |||
434 | bool parseFixedStackObjectOperand(MachineOperand &Dest); | |||
435 | bool parseGlobalValue(GlobalValue *&GV); | |||
436 | bool parseGlobalAddressOperand(MachineOperand &Dest); | |||
437 | bool parseConstantPoolIndexOperand(MachineOperand &Dest); | |||
438 | bool parseSubRegisterIndexOperand(MachineOperand &Dest); | |||
439 | bool parseJumpTableIndexOperand(MachineOperand &Dest); | |||
440 | bool parseExternalSymbolOperand(MachineOperand &Dest); | |||
441 | bool parseMCSymbolOperand(MachineOperand &Dest); | |||
442 | bool parseMDNode(MDNode *&Node); | |||
443 | bool parseDIExpression(MDNode *&Expr); | |||
444 | bool parseDILocation(MDNode *&Expr); | |||
445 | bool parseMetadataOperand(MachineOperand &Dest); | |||
446 | bool parseCFIOffset(int &Offset); | |||
447 | bool parseCFIRegister(unsigned &Reg); | |||
448 | bool parseCFIEscapeValues(std::string& Values); | |||
449 | bool parseCFIOperand(MachineOperand &Dest); | |||
450 | bool parseIRBlock(BasicBlock *&BB, const Function &F); | |||
451 | bool parseBlockAddressOperand(MachineOperand &Dest); | |||
452 | bool parseIntrinsicOperand(MachineOperand &Dest); | |||
453 | bool parsePredicateOperand(MachineOperand &Dest); | |||
454 | bool parseTargetIndexOperand(MachineOperand &Dest); | |||
455 | bool parseCustomRegisterMaskOperand(MachineOperand &Dest); | |||
456 | bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest); | |||
457 | bool parseMachineOperand(MachineOperand &Dest, | |||
458 | Optional<unsigned> &TiedDefIdx); | |||
459 | bool parseMachineOperandAndTargetFlags(MachineOperand &Dest, | |||
460 | Optional<unsigned> &TiedDefIdx); | |||
461 | bool parseOffset(int64_t &Offset); | |||
462 | bool parseAlignment(unsigned &Alignment); | |||
463 | bool parseAddrspace(unsigned &Addrspace); | |||
464 | bool parseOperandsOffset(MachineOperand &Op); | |||
465 | bool parseIRValue(const Value *&V); | |||
466 | bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags); | |||
467 | bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV); | |||
468 | bool parseMachinePointerInfo(MachinePointerInfo &Dest); | |||
469 | bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID); | |||
470 | bool parseOptionalAtomicOrdering(AtomicOrdering &Order); | |||
471 | bool parseMachineMemoryOperand(MachineMemOperand *&Dest); | |||
472 | bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol); | |||
473 | ||||
474 | private: | |||
475 | /// Convert the integer literal in the current token into an unsigned integer. | |||
476 | /// | |||
477 | /// Return true if an error occurred. | |||
478 | bool getUnsigned(unsigned &Result); | |||
479 | ||||
480 | /// Convert the integer literal in the current token into an uint64. | |||
481 | /// | |||
482 | /// Return true if an error occurred. | |||
483 | bool getUint64(uint64_t &Result); | |||
484 | ||||
485 | /// Convert the hexadecimal literal in the current token into an unsigned | |||
486 | /// APInt with a minimum bitwidth required to represent the value. | |||
487 | /// | |||
488 | /// Return true if the literal does not represent an integer value. | |||
489 | bool getHexUint(APInt &Result); | |||
490 | ||||
491 | /// If the current token is of the given kind, consume it and return false. | |||
492 | /// Otherwise report an error and return true. | |||
493 | bool expectAndConsume(MIToken::TokenKind TokenKind); | |||
494 | ||||
495 | /// If the current token is of the given kind, consume it and return true. | |||
496 | /// Otherwise return false. | |||
497 | bool consumeIfPresent(MIToken::TokenKind TokenKind); | |||
498 | ||||
499 | bool parseInstruction(unsigned &OpCode, unsigned &Flags); | |||
500 | ||||
501 | bool assignRegisterTies(MachineInstr &MI, | |||
502 | ArrayRef<ParsedMachineOperand> Operands); | |||
503 | ||||
504 | bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, | |||
505 | const MCInstrDesc &MCID); | |||
506 | ||||
507 | const BasicBlock *getIRBlock(unsigned Slot); | |||
508 | const BasicBlock *getIRBlock(unsigned Slot, const Function &F); | |||
509 | ||||
510 | const Value *getIRValue(unsigned Slot); | |||
511 | ||||
512 | /// Get or create an MCSymbol for a given name. | |||
513 | MCSymbol *getOrCreateMCSymbol(StringRef Name); | |||
514 | ||||
515 | /// parseStringConstant | |||
516 | /// ::= StringConstant | |||
517 | bool parseStringConstant(std::string &Result); | |||
518 | }; | |||
519 | ||||
520 | } // end anonymous namespace | |||
521 | ||||
522 | MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error, | |||
523 | StringRef Source) | |||
524 | : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS) | |||
525 | {} | |||
526 | ||||
527 | void MIParser::lex(unsigned SkipChar) { | |||
528 | CurrentSource = lexMIToken( | |||
529 | CurrentSource.data() + SkipChar, Token, | |||
530 | [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); }); | |||
531 | } | |||
532 | ||||
533 | bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); } | |||
534 | ||||
535 | bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { | |||
536 | const SourceMgr &SM = *PFS.SM; | |||
537 | assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size()))((Loc >= Source.data() && Loc <= (Source.data() + Source.size())) ? static_cast<void> (0) : __assert_fail ("Loc >= Source.data() && Loc <= (Source.data() + Source.size())" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 537, __PRETTY_FUNCTION__)); | |||
538 | const MemoryBuffer &Buffer = *SM.getMemoryBuffer(SM.getMainFileID()); | |||
539 | if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) { | |||
540 | // Create an ordinary diagnostic when the source manager's buffer is the | |||
541 | // source string. | |||
542 | Error = SM.GetMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); | |||
543 | return true; | |||
544 | } | |||
545 | // Create a diagnostic for a YAML string literal. | |||
546 | Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1, | |||
547 | Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), | |||
548 | Source, None, None); | |||
549 | return true; | |||
550 | } | |||
551 | ||||
552 | static const char *toString(MIToken::TokenKind TokenKind) { | |||
553 | switch (TokenKind) { | |||
554 | case MIToken::comma: | |||
555 | return "','"; | |||
556 | case MIToken::equal: | |||
557 | return "'='"; | |||
558 | case MIToken::colon: | |||
559 | return "':'"; | |||
560 | case MIToken::lparen: | |||
561 | return "'('"; | |||
562 | case MIToken::rparen: | |||
563 | return "')'"; | |||
564 | default: | |||
565 | return "<unknown token>"; | |||
566 | } | |||
567 | } | |||
568 | ||||
569 | bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) { | |||
570 | if (Token.isNot(TokenKind)) | |||
571 | return error(Twine("expected ") + toString(TokenKind)); | |||
572 | lex(); | |||
573 | return false; | |||
574 | } | |||
575 | ||||
576 | bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) { | |||
577 | if (Token.isNot(TokenKind)) | |||
578 | return false; | |||
579 | lex(); | |||
580 | return true; | |||
581 | } | |||
582 | ||||
583 | bool MIParser::parseBasicBlockDefinition( | |||
584 | DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { | |||
585 | assert(Token.is(MIToken::MachineBasicBlockLabel))((Token.is(MIToken::MachineBasicBlockLabel)) ? static_cast< void> (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 585, __PRETTY_FUNCTION__)); | |||
586 | unsigned ID = 0; | |||
587 | if (getUnsigned(ID)) | |||
588 | return true; | |||
589 | auto Loc = Token.location(); | |||
590 | auto Name = Token.stringValue(); | |||
591 | lex(); | |||
592 | bool HasAddressTaken = false; | |||
593 | bool IsLandingPad = false; | |||
594 | unsigned Alignment = 0; | |||
595 | BasicBlock *BB = nullptr; | |||
596 | if (consumeIfPresent(MIToken::lparen)) { | |||
597 | do { | |||
598 | // TODO: Report an error when multiple same attributes are specified. | |||
599 | switch (Token.kind()) { | |||
600 | case MIToken::kw_address_taken: | |||
601 | HasAddressTaken = true; | |||
602 | lex(); | |||
603 | break; | |||
604 | case MIToken::kw_landing_pad: | |||
605 | IsLandingPad = true; | |||
606 | lex(); | |||
607 | break; | |||
608 | case MIToken::kw_align: | |||
609 | if (parseAlignment(Alignment)) | |||
610 | return true; | |||
611 | break; | |||
612 | case MIToken::IRBlock: | |||
613 | // TODO: Report an error when both name and ir block are specified. | |||
614 | if (parseIRBlock(BB, MF.getFunction())) | |||
615 | return true; | |||
616 | lex(); | |||
617 | break; | |||
618 | default: | |||
619 | break; | |||
620 | } | |||
621 | } while (consumeIfPresent(MIToken::comma)); | |||
622 | if (expectAndConsume(MIToken::rparen)) | |||
623 | return true; | |||
624 | } | |||
625 | if (expectAndConsume(MIToken::colon)) | |||
626 | return true; | |||
627 | ||||
628 | if (!Name.empty()) { | |||
629 | BB = dyn_cast_or_null<BasicBlock>( | |||
630 | MF.getFunction().getValueSymbolTable()->lookup(Name)); | |||
631 | if (!BB) | |||
632 | return error(Loc, Twine("basic block '") + Name + | |||
633 | "' is not defined in the function '" + | |||
634 | MF.getName() + "'"); | |||
635 | } | |||
636 | auto *MBB = MF.CreateMachineBasicBlock(BB); | |||
637 | MF.insert(MF.end(), MBB); | |||
638 | bool WasInserted = MBBSlots.insert(std::make_pair(ID, MBB)).second; | |||
639 | if (!WasInserted) | |||
640 | return error(Loc, Twine("redefinition of machine basic block with id #") + | |||
641 | Twine(ID)); | |||
642 | if (Alignment) | |||
643 | MBB->setAlignment(Alignment); | |||
644 | if (HasAddressTaken) | |||
645 | MBB->setHasAddressTaken(); | |||
646 | MBB->setIsEHPad(IsLandingPad); | |||
647 | return false; | |||
648 | } | |||
649 | ||||
650 | bool MIParser::parseBasicBlockDefinitions( | |||
651 | DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { | |||
652 | lex(); | |||
653 | // Skip until the first machine basic block. | |||
654 | while (Token.is(MIToken::Newline)) | |||
655 | lex(); | |||
656 | if (Token.isErrorOrEOF()) | |||
657 | return Token.isError(); | |||
658 | if (Token.isNot(MIToken::MachineBasicBlockLabel)) | |||
659 | return error("expected a basic block definition before instructions"); | |||
660 | unsigned BraceDepth = 0; | |||
661 | do { | |||
662 | if (parseBasicBlockDefinition(MBBSlots)) | |||
663 | return true; | |||
664 | bool IsAfterNewline = false; | |||
665 | // Skip until the next machine basic block. | |||
666 | while (true) { | |||
667 | if ((Token.is(MIToken::MachineBasicBlockLabel) && IsAfterNewline) || | |||
668 | Token.isErrorOrEOF()) | |||
669 | break; | |||
670 | else if (Token.is(MIToken::MachineBasicBlockLabel)) | |||
671 | return error("basic block definition should be located at the start of " | |||
672 | "the line"); | |||
673 | else if (consumeIfPresent(MIToken::Newline)) { | |||
674 | IsAfterNewline = true; | |||
675 | continue; | |||
676 | } | |||
677 | IsAfterNewline = false; | |||
678 | if (Token.is(MIToken::lbrace)) | |||
679 | ++BraceDepth; | |||
680 | if (Token.is(MIToken::rbrace)) { | |||
681 | if (!BraceDepth) | |||
682 | return error("extraneous closing brace ('}')"); | |||
683 | --BraceDepth; | |||
684 | } | |||
685 | lex(); | |||
686 | } | |||
687 | // Verify that we closed all of the '{' at the end of a file or a block. | |||
688 | if (!Token.isError() && BraceDepth) | |||
689 | return error("expected '}'"); // FIXME: Report a note that shows '{'. | |||
690 | } while (!Token.isErrorOrEOF()); | |||
691 | return Token.isError(); | |||
692 | } | |||
693 | ||||
694 | bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) { | |||
695 | assert(Token.is(MIToken::kw_liveins))((Token.is(MIToken::kw_liveins)) ? static_cast<void> (0 ) : __assert_fail ("Token.is(MIToken::kw_liveins)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 695, __PRETTY_FUNCTION__)); | |||
696 | lex(); | |||
697 | if (expectAndConsume(MIToken::colon)) | |||
698 | return true; | |||
699 | if (Token.isNewlineOrEOF()) // Allow an empty list of liveins. | |||
700 | return false; | |||
701 | do { | |||
702 | if (Token.isNot(MIToken::NamedRegister)) | |||
703 | return error("expected a named register"); | |||
704 | unsigned Reg = 0; | |||
705 | if (parseNamedRegister(Reg)) | |||
706 | return true; | |||
707 | lex(); | |||
708 | LaneBitmask Mask = LaneBitmask::getAll(); | |||
709 | if (consumeIfPresent(MIToken::colon)) { | |||
710 | // Parse lane mask. | |||
711 | if (Token.isNot(MIToken::IntegerLiteral) && | |||
712 | Token.isNot(MIToken::HexLiteral)) | |||
713 | return error("expected a lane mask"); | |||
714 | static_assert(sizeof(LaneBitmask::Type) == sizeof(unsigned), | |||
715 | "Use correct get-function for lane mask"); | |||
716 | LaneBitmask::Type V; | |||
717 | if (getUnsigned(V)) | |||
718 | return error("invalid lane mask value"); | |||
719 | Mask = LaneBitmask(V); | |||
720 | lex(); | |||
721 | } | |||
722 | MBB.addLiveIn(Reg, Mask); | |||
723 | } while (consumeIfPresent(MIToken::comma)); | |||
724 | return false; | |||
725 | } | |||
726 | ||||
727 | bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) { | |||
728 | assert(Token.is(MIToken::kw_successors))((Token.is(MIToken::kw_successors)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::kw_successors)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 728, __PRETTY_FUNCTION__)); | |||
729 | lex(); | |||
730 | if (expectAndConsume(MIToken::colon)) | |||
731 | return true; | |||
732 | if (Token.isNewlineOrEOF()) // Allow an empty list of successors. | |||
733 | return false; | |||
734 | do { | |||
735 | if (Token.isNot(MIToken::MachineBasicBlock)) | |||
736 | return error("expected a machine basic block reference"); | |||
737 | MachineBasicBlock *SuccMBB = nullptr; | |||
738 | if (parseMBBReference(SuccMBB)) | |||
739 | return true; | |||
740 | lex(); | |||
741 | unsigned Weight = 0; | |||
742 | if (consumeIfPresent(MIToken::lparen)) { | |||
743 | if (Token.isNot(MIToken::IntegerLiteral) && | |||
744 | Token.isNot(MIToken::HexLiteral)) | |||
745 | return error("expected an integer literal after '('"); | |||
746 | if (getUnsigned(Weight)) | |||
747 | return true; | |||
748 | lex(); | |||
749 | if (expectAndConsume(MIToken::rparen)) | |||
750 | return true; | |||
751 | } | |||
752 | MBB.addSuccessor(SuccMBB, BranchProbability::getRaw(Weight)); | |||
753 | } while (consumeIfPresent(MIToken::comma)); | |||
754 | MBB.normalizeSuccProbs(); | |||
755 | return false; | |||
756 | } | |||
757 | ||||
758 | bool MIParser::parseBasicBlock(MachineBasicBlock &MBB, | |||
759 | MachineBasicBlock *&AddFalthroughFrom) { | |||
760 | // Skip the definition. | |||
761 | assert(Token.is(MIToken::MachineBasicBlockLabel))((Token.is(MIToken::MachineBasicBlockLabel)) ? static_cast< void> (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 761, __PRETTY_FUNCTION__)); | |||
762 | lex(); | |||
763 | if (consumeIfPresent(MIToken::lparen)) { | |||
764 | while (Token.isNot(MIToken::rparen) && !Token.isErrorOrEOF()) | |||
765 | lex(); | |||
766 | consumeIfPresent(MIToken::rparen); | |||
767 | } | |||
768 | consumeIfPresent(MIToken::colon); | |||
769 | ||||
770 | // Parse the liveins and successors. | |||
771 | // N.B: Multiple lists of successors and liveins are allowed and they're | |||
772 | // merged into one. | |||
773 | // Example: | |||
774 | // liveins: %edi | |||
775 | // liveins: %esi | |||
776 | // | |||
777 | // is equivalent to | |||
778 | // liveins: %edi, %esi | |||
779 | bool ExplicitSuccessors = false; | |||
780 | while (true) { | |||
781 | if (Token.is(MIToken::kw_successors)) { | |||
782 | if (parseBasicBlockSuccessors(MBB)) | |||
783 | return true; | |||
784 | ExplicitSuccessors = true; | |||
785 | } else if (Token.is(MIToken::kw_liveins)) { | |||
786 | if (parseBasicBlockLiveins(MBB)) | |||
787 | return true; | |||
788 | } else if (consumeIfPresent(MIToken::Newline)) { | |||
789 | continue; | |||
790 | } else | |||
791 | break; | |||
792 | if (!Token.isNewlineOrEOF()) | |||
793 | return error("expected line break at the end of a list"); | |||
794 | lex(); | |||
795 | } | |||
796 | ||||
797 | // Parse the instructions. | |||
798 | bool IsInBundle = false; | |||
799 | MachineInstr *PrevMI = nullptr; | |||
800 | while (!Token.is(MIToken::MachineBasicBlockLabel) && | |||
801 | !Token.is(MIToken::Eof)) { | |||
802 | if (consumeIfPresent(MIToken::Newline)) | |||
803 | continue; | |||
804 | if (consumeIfPresent(MIToken::rbrace)) { | |||
805 | // The first parsing pass should verify that all closing '}' have an | |||
806 | // opening '{'. | |||
807 | assert(IsInBundle)((IsInBundle) ? static_cast<void> (0) : __assert_fail ( "IsInBundle", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 807, __PRETTY_FUNCTION__)); | |||
808 | IsInBundle = false; | |||
809 | continue; | |||
810 | } | |||
811 | MachineInstr *MI = nullptr; | |||
812 | if (parse(MI)) | |||
813 | return true; | |||
814 | MBB.insert(MBB.end(), MI); | |||
815 | if (IsInBundle) { | |||
816 | PrevMI->setFlag(MachineInstr::BundledSucc); | |||
817 | MI->setFlag(MachineInstr::BundledPred); | |||
818 | } | |||
819 | PrevMI = MI; | |||
820 | if (Token.is(MIToken::lbrace)) { | |||
821 | if (IsInBundle) | |||
822 | return error("nested instruction bundles are not allowed"); | |||
823 | lex(); | |||
824 | // This instruction is the start of the bundle. | |||
825 | MI->setFlag(MachineInstr::BundledSucc); | |||
826 | IsInBundle = true; | |||
827 | if (!Token.is(MIToken::Newline)) | |||
828 | // The next instruction can be on the same line. | |||
829 | continue; | |||
830 | } | |||
831 | assert(Token.isNewlineOrEOF() && "MI is not fully parsed")((Token.isNewlineOrEOF() && "MI is not fully parsed") ? static_cast<void> (0) : __assert_fail ("Token.isNewlineOrEOF() && \"MI is not fully parsed\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 831, __PRETTY_FUNCTION__)); | |||
832 | lex(); | |||
833 | } | |||
834 | ||||
835 | // Construct successor list by searching for basic block machine operands. | |||
836 | if (!ExplicitSuccessors) { | |||
837 | SmallVector<MachineBasicBlock*,4> Successors; | |||
838 | bool IsFallthrough; | |||
839 | guessSuccessors(MBB, Successors, IsFallthrough); | |||
840 | for (MachineBasicBlock *Succ : Successors) | |||
841 | MBB.addSuccessor(Succ); | |||
842 | ||||
843 | if (IsFallthrough) { | |||
844 | AddFalthroughFrom = &MBB; | |||
845 | } else { | |||
846 | MBB.normalizeSuccProbs(); | |||
847 | } | |||
848 | } | |||
849 | ||||
850 | return false; | |||
851 | } | |||
852 | ||||
853 | bool MIParser::parseBasicBlocks() { | |||
854 | lex(); | |||
855 | // Skip until the first machine basic block. | |||
856 | while (Token.is(MIToken::Newline)) | |||
857 | lex(); | |||
858 | if (Token.isErrorOrEOF()) | |||
859 | return Token.isError(); | |||
860 | // The first parsing pass should have verified that this token is a MBB label | |||
861 | // in the 'parseBasicBlockDefinitions' method. | |||
862 | assert(Token.is(MIToken::MachineBasicBlockLabel))((Token.is(MIToken::MachineBasicBlockLabel)) ? static_cast< void> (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 862, __PRETTY_FUNCTION__)); | |||
863 | MachineBasicBlock *AddFalthroughFrom = nullptr; | |||
864 | do { | |||
865 | MachineBasicBlock *MBB = nullptr; | |||
866 | if (parseMBBReference(MBB)) | |||
867 | return true; | |||
868 | if (AddFalthroughFrom) { | |||
869 | if (!AddFalthroughFrom->isSuccessor(MBB)) | |||
870 | AddFalthroughFrom->addSuccessor(MBB); | |||
871 | AddFalthroughFrom->normalizeSuccProbs(); | |||
872 | AddFalthroughFrom = nullptr; | |||
873 | } | |||
874 | if (parseBasicBlock(*MBB, AddFalthroughFrom)) | |||
875 | return true; | |||
876 | // The method 'parseBasicBlock' should parse the whole block until the next | |||
877 | // block or the end of file. | |||
878 | assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof))((Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken ::Eof)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 878, __PRETTY_FUNCTION__)); | |||
879 | } while (Token.isNot(MIToken::Eof)); | |||
880 | return false; | |||
881 | } | |||
882 | ||||
883 | bool MIParser::parse(MachineInstr *&MI) { | |||
884 | // Parse any register operands before '=' | |||
885 | MachineOperand MO = MachineOperand::CreateImm(0); | |||
886 | SmallVector<ParsedMachineOperand, 8> Operands; | |||
887 | while (Token.isRegister() || Token.isRegisterFlag()) { | |||
888 | auto Loc = Token.location(); | |||
889 | Optional<unsigned> TiedDefIdx; | |||
890 | if (parseRegisterOperand(MO, TiedDefIdx, /*IsDef=*/true)) | |||
891 | return true; | |||
892 | Operands.push_back( | |||
893 | ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); | |||
894 | if (Token.isNot(MIToken::comma)) | |||
895 | break; | |||
896 | lex(); | |||
897 | } | |||
898 | if (!Operands.empty() && expectAndConsume(MIToken::equal)) | |||
899 | return true; | |||
900 | ||||
901 | unsigned OpCode, Flags = 0; | |||
902 | if (Token.isError() || parseInstruction(OpCode, Flags)) | |||
903 | return true; | |||
904 | ||||
905 | // Parse the remaining machine operands. | |||
906 | while (!Token.isNewlineOrEOF() && Token.isNot(MIToken::kw_pre_instr_symbol) && | |||
907 | Token.isNot(MIToken::kw_post_instr_symbol) && | |||
908 | Token.isNot(MIToken::kw_debug_location) && | |||
909 | Token.isNot(MIToken::coloncolon) && Token.isNot(MIToken::lbrace)) { | |||
910 | auto Loc = Token.location(); | |||
911 | Optional<unsigned> TiedDefIdx; | |||
912 | if (parseMachineOperandAndTargetFlags(MO, TiedDefIdx)) | |||
913 | return true; | |||
914 | if (OpCode == TargetOpcode::DBG_VALUE && MO.isReg()) | |||
915 | MO.setIsDebug(); | |||
916 | Operands.push_back( | |||
917 | ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); | |||
918 | if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) || | |||
919 | Token.is(MIToken::lbrace)) | |||
920 | break; | |||
921 | if (Token.isNot(MIToken::comma)) | |||
922 | return error("expected ',' before the next machine operand"); | |||
923 | lex(); | |||
924 | } | |||
925 | ||||
926 | MCSymbol *PreInstrSymbol = nullptr; | |||
927 | if (Token.is(MIToken::kw_pre_instr_symbol)) | |||
928 | if (parsePreOrPostInstrSymbol(PreInstrSymbol)) | |||
929 | return true; | |||
930 | MCSymbol *PostInstrSymbol = nullptr; | |||
931 | if (Token.is(MIToken::kw_post_instr_symbol)) | |||
932 | if (parsePreOrPostInstrSymbol(PostInstrSymbol)) | |||
933 | return true; | |||
934 | ||||
935 | DebugLoc DebugLocation; | |||
936 | if (Token.is(MIToken::kw_debug_location)) { | |||
937 | lex(); | |||
938 | MDNode *Node = nullptr; | |||
939 | if (Token.is(MIToken::exclaim)) { | |||
940 | if (parseMDNode(Node)) | |||
941 | return true; | |||
942 | } else if (Token.is(MIToken::md_dilocation)) { | |||
943 | if (parseDILocation(Node)) | |||
944 | return true; | |||
945 | } else | |||
946 | return error("expected a metadata node after 'debug-location'"); | |||
947 | if (!isa<DILocation>(Node)) | |||
948 | return error("referenced metadata is not a DILocation"); | |||
949 | DebugLocation = DebugLoc(Node); | |||
950 | } | |||
951 | ||||
952 | // Parse the machine memory operands. | |||
953 | SmallVector<MachineMemOperand *, 2> MemOperands; | |||
954 | if (Token.is(MIToken::coloncolon)) { | |||
955 | lex(); | |||
956 | while (!Token.isNewlineOrEOF()) { | |||
957 | MachineMemOperand *MemOp = nullptr; | |||
958 | if (parseMachineMemoryOperand(MemOp)) | |||
959 | return true; | |||
960 | MemOperands.push_back(MemOp); | |||
961 | if (Token.isNewlineOrEOF()) | |||
962 | break; | |||
963 | if (Token.isNot(MIToken::comma)) | |||
964 | return error("expected ',' before the next machine memory operand"); | |||
965 | lex(); | |||
966 | } | |||
967 | } | |||
968 | ||||
969 | const auto &MCID = MF.getSubtarget().getInstrInfo()->get(OpCode); | |||
970 | if (!MCID.isVariadic()) { | |||
971 | // FIXME: Move the implicit operand verification to the machine verifier. | |||
972 | if (verifyImplicitOperands(Operands, MCID)) | |||
973 | return true; | |||
974 | } | |||
975 | ||||
976 | // TODO: Check for extraneous machine operands. | |||
977 | MI = MF.CreateMachineInstr(MCID, DebugLocation, /*NoImplicit=*/true); | |||
978 | MI->setFlags(Flags); | |||
979 | for (const auto &Operand : Operands) | |||
980 | MI->addOperand(MF, Operand.Operand); | |||
981 | if (assignRegisterTies(*MI, Operands)) | |||
982 | return true; | |||
983 | if (PreInstrSymbol) | |||
984 | MI->setPreInstrSymbol(MF, PreInstrSymbol); | |||
985 | if (PostInstrSymbol) | |||
986 | MI->setPostInstrSymbol(MF, PostInstrSymbol); | |||
987 | if (!MemOperands.empty()) | |||
988 | MI->setMemRefs(MF, MemOperands); | |||
989 | return false; | |||
990 | } | |||
991 | ||||
992 | bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) { | |||
993 | lex(); | |||
994 | if (Token.isNot(MIToken::MachineBasicBlock)) | |||
995 | return error("expected a machine basic block reference"); | |||
996 | if (parseMBBReference(MBB)) | |||
997 | return true; | |||
998 | lex(); | |||
999 | if (Token.isNot(MIToken::Eof)) | |||
1000 | return error( | |||
1001 | "expected end of string after the machine basic block reference"); | |||
1002 | return false; | |||
1003 | } | |||
1004 | ||||
1005 | bool MIParser::parseStandaloneNamedRegister(unsigned &Reg) { | |||
1006 | lex(); | |||
1007 | if (Token.isNot(MIToken::NamedRegister)) | |||
1008 | return error("expected a named register"); | |||
1009 | if (parseNamedRegister(Reg)) | |||
1010 | return true; | |||
1011 | lex(); | |||
1012 | if (Token.isNot(MIToken::Eof)) | |||
1013 | return error("expected end of string after the register reference"); | |||
1014 | return false; | |||
1015 | } | |||
1016 | ||||
1017 | bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) { | |||
1018 | lex(); | |||
1019 | if (Token.isNot(MIToken::VirtualRegister)) | |||
1020 | return error("expected a virtual register"); | |||
1021 | if (parseVirtualRegister(Info)) | |||
1022 | return true; | |||
1023 | lex(); | |||
1024 | if (Token.isNot(MIToken::Eof)) | |||
1025 | return error("expected end of string after the register reference"); | |||
1026 | return false; | |||
1027 | } | |||
1028 | ||||
1029 | bool MIParser::parseStandaloneRegister(unsigned &Reg) { | |||
1030 | lex(); | |||
1031 | if (Token.isNot(MIToken::NamedRegister) && | |||
1032 | Token.isNot(MIToken::VirtualRegister)) | |||
1033 | return error("expected either a named or virtual register"); | |||
1034 | ||||
1035 | VRegInfo *Info; | |||
1036 | if (parseRegister(Reg, Info)) | |||
1037 | return true; | |||
1038 | ||||
1039 | lex(); | |||
1040 | if (Token.isNot(MIToken::Eof)) | |||
1041 | return error("expected end of string after the register reference"); | |||
1042 | return false; | |||
1043 | } | |||
1044 | ||||
1045 | bool MIParser::parseStandaloneStackObject(int &FI) { | |||
1046 | lex(); | |||
1047 | if (Token.isNot(MIToken::StackObject)) | |||
1048 | return error("expected a stack object"); | |||
1049 | if (parseStackFrameIndex(FI)) | |||
1050 | return true; | |||
1051 | if (Token.isNot(MIToken::Eof)) | |||
1052 | return error("expected end of string after the stack object reference"); | |||
1053 | return false; | |||
1054 | } | |||
1055 | ||||
1056 | bool MIParser::parseStandaloneMDNode(MDNode *&Node) { | |||
1057 | lex(); | |||
1058 | if (Token.is(MIToken::exclaim)) { | |||
1059 | if (parseMDNode(Node)) | |||
1060 | return true; | |||
1061 | } else if (Token.is(MIToken::md_diexpr)) { | |||
1062 | if (parseDIExpression(Node)) | |||
1063 | return true; | |||
1064 | } else if (Token.is(MIToken::md_dilocation)) { | |||
1065 | if (parseDILocation(Node)) | |||
1066 | return true; | |||
1067 | } else | |||
1068 | return error("expected a metadata node"); | |||
1069 | if (Token.isNot(MIToken::Eof)) | |||
1070 | return error("expected end of string after the metadata node"); | |||
1071 | return false; | |||
1072 | } | |||
1073 | ||||
1074 | static const char *printImplicitRegisterFlag(const MachineOperand &MO) { | |||
1075 | assert(MO.isImplicit())((MO.isImplicit()) ? static_cast<void> (0) : __assert_fail ("MO.isImplicit()", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1075, __PRETTY_FUNCTION__)); | |||
1076 | return MO.isDef() ? "implicit-def" : "implicit"; | |||
1077 | } | |||
1078 | ||||
1079 | static std::string getRegisterName(const TargetRegisterInfo *TRI, | |||
1080 | unsigned Reg) { | |||
1081 | assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg")((TargetRegisterInfo::isPhysicalRegister(Reg) && "expected phys reg" ) ? static_cast<void> (0) : __assert_fail ("TargetRegisterInfo::isPhysicalRegister(Reg) && \"expected phys reg\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1081, __PRETTY_FUNCTION__)); | |||
1082 | return StringRef(TRI->getName(Reg)).lower(); | |||
1083 | } | |||
1084 | ||||
1085 | /// Return true if the parsed machine operands contain a given machine operand. | |||
1086 | static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, | |||
1087 | ArrayRef<ParsedMachineOperand> Operands) { | |||
1088 | for (const auto &I : Operands) { | |||
1089 | if (ImplicitOperand.isIdenticalTo(I.Operand)) | |||
1090 | return true; | |||
1091 | } | |||
1092 | return false; | |||
1093 | } | |||
1094 | ||||
1095 | bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, | |||
1096 | const MCInstrDesc &MCID) { | |||
1097 | if (MCID.isCall()) | |||
1098 | // We can't verify call instructions as they can contain arbitrary implicit | |||
1099 | // register and register mask operands. | |||
1100 | return false; | |||
1101 | ||||
1102 | // Gather all the expected implicit operands. | |||
1103 | SmallVector<MachineOperand, 4> ImplicitOperands; | |||
1104 | if (MCID.ImplicitDefs) | |||
1105 | for (const MCPhysReg *ImpDefs = MCID.getImplicitDefs(); *ImpDefs; ++ImpDefs) | |||
1106 | ImplicitOperands.push_back( | |||
1107 | MachineOperand::CreateReg(*ImpDefs, true, true)); | |||
1108 | if (MCID.ImplicitUses) | |||
1109 | for (const MCPhysReg *ImpUses = MCID.getImplicitUses(); *ImpUses; ++ImpUses) | |||
1110 | ImplicitOperands.push_back( | |||
1111 | MachineOperand::CreateReg(*ImpUses, false, true)); | |||
1112 | ||||
1113 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); | |||
1114 | assert(TRI && "Expected target register info")((TRI && "Expected target register info") ? static_cast <void> (0) : __assert_fail ("TRI && \"Expected target register info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1114, __PRETTY_FUNCTION__)); | |||
1115 | for (const auto &I : ImplicitOperands) { | |||
1116 | if (isImplicitOperandIn(I, Operands)) | |||
1117 | continue; | |||
1118 | return error(Operands.empty() ? Token.location() : Operands.back().End, | |||
1119 | Twine("missing implicit register operand '") + | |||
1120 | printImplicitRegisterFlag(I) + " $" + | |||
1121 | getRegisterName(TRI, I.getReg()) + "'"); | |||
1122 | } | |||
1123 | return false; | |||
1124 | } | |||
1125 | ||||
1126 | bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) { | |||
1127 | // Allow frame and fast math flags for OPCODE | |||
1128 | while (Token.is(MIToken::kw_frame_setup) || | |||
1129 | Token.is(MIToken::kw_frame_destroy) || | |||
1130 | Token.is(MIToken::kw_nnan) || | |||
1131 | Token.is(MIToken::kw_ninf) || | |||
1132 | Token.is(MIToken::kw_nsz) || | |||
1133 | Token.is(MIToken::kw_arcp) || | |||
1134 | Token.is(MIToken::kw_contract) || | |||
1135 | Token.is(MIToken::kw_afn) || | |||
1136 | Token.is(MIToken::kw_reassoc) || | |||
1137 | Token.is(MIToken::kw_nuw) || | |||
1138 | Token.is(MIToken::kw_nsw) || | |||
1139 | Token.is(MIToken::kw_exact)) { | |||
1140 | // Mine frame and fast math flags | |||
1141 | if (Token.is(MIToken::kw_frame_setup)) | |||
1142 | Flags |= MachineInstr::FrameSetup; | |||
1143 | if (Token.is(MIToken::kw_frame_destroy)) | |||
1144 | Flags |= MachineInstr::FrameDestroy; | |||
1145 | if (Token.is(MIToken::kw_nnan)) | |||
1146 | Flags |= MachineInstr::FmNoNans; | |||
1147 | if (Token.is(MIToken::kw_ninf)) | |||
1148 | Flags |= MachineInstr::FmNoInfs; | |||
1149 | if (Token.is(MIToken::kw_nsz)) | |||
1150 | Flags |= MachineInstr::FmNsz; | |||
1151 | if (Token.is(MIToken::kw_arcp)) | |||
1152 | Flags |= MachineInstr::FmArcp; | |||
1153 | if (Token.is(MIToken::kw_contract)) | |||
1154 | Flags |= MachineInstr::FmContract; | |||
1155 | if (Token.is(MIToken::kw_afn)) | |||
1156 | Flags |= MachineInstr::FmAfn; | |||
1157 | if (Token.is(MIToken::kw_reassoc)) | |||
1158 | Flags |= MachineInstr::FmReassoc; | |||
1159 | if (Token.is(MIToken::kw_nuw)) | |||
1160 | Flags |= MachineInstr::NoUWrap; | |||
1161 | if (Token.is(MIToken::kw_nsw)) | |||
1162 | Flags |= MachineInstr::NoSWrap; | |||
1163 | if (Token.is(MIToken::kw_exact)) | |||
1164 | Flags |= MachineInstr::IsExact; | |||
1165 | ||||
1166 | lex(); | |||
1167 | } | |||
1168 | if (Token.isNot(MIToken::Identifier)) | |||
1169 | return error("expected a machine instruction"); | |||
1170 | StringRef InstrName = Token.stringValue(); | |||
1171 | if (PFS.Target.parseInstrName(InstrName, OpCode)) | |||
1172 | return error(Twine("unknown machine instruction name '") + InstrName + "'"); | |||
1173 | lex(); | |||
1174 | return false; | |||
1175 | } | |||
1176 | ||||
1177 | bool MIParser::parseNamedRegister(unsigned &Reg) { | |||
1178 | assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token")((Token.is(MIToken::NamedRegister) && "Needs NamedRegister token" ) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::NamedRegister) && \"Needs NamedRegister token\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1178, __PRETTY_FUNCTION__)); | |||
1179 | StringRef Name = Token.stringValue(); | |||
1180 | if (PFS.Target.getRegisterByName(Name, Reg)) | |||
1181 | return error(Twine("unknown register name '") + Name + "'"); | |||
1182 | return false; | |||
1183 | } | |||
1184 | ||||
1185 | bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) { | |||
1186 | assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token")((Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token" ) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::NamedVirtualRegister) && \"Expected NamedVReg token\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1186, __PRETTY_FUNCTION__)); | |||
1187 | StringRef Name = Token.stringValue(); | |||
1188 | // TODO: Check that the VReg name is not the same as a physical register name. | |||
1189 | // If it is, then print a warning (when warnings are implemented). | |||
1190 | Info = &PFS.getVRegInfoNamed(Name); | |||
1191 | return false; | |||
1192 | } | |||
1193 | ||||
1194 | bool MIParser::parseVirtualRegister(VRegInfo *&Info) { | |||
1195 | if (Token.is(MIToken::NamedVirtualRegister)) | |||
1196 | return parseNamedVirtualRegister(Info); | |||
1197 | assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token")((Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token" ) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::VirtualRegister) && \"Needs VirtualRegister token\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1197, __PRETTY_FUNCTION__)); | |||
1198 | unsigned ID; | |||
1199 | if (getUnsigned(ID)) | |||
1200 | return true; | |||
1201 | Info = &PFS.getVRegInfo(ID); | |||
1202 | return false; | |||
1203 | } | |||
1204 | ||||
1205 | bool MIParser::parseRegister(unsigned &Reg, VRegInfo *&Info) { | |||
1206 | switch (Token.kind()) { | |||
1207 | case MIToken::underscore: | |||
1208 | Reg = 0; | |||
1209 | return false; | |||
1210 | case MIToken::NamedRegister: | |||
1211 | return parseNamedRegister(Reg); | |||
1212 | case MIToken::NamedVirtualRegister: | |||
1213 | case MIToken::VirtualRegister: | |||
1214 | if (parseVirtualRegister(Info)) | |||
1215 | return true; | |||
1216 | Reg = Info->VReg; | |||
1217 | return false; | |||
1218 | // TODO: Parse other register kinds. | |||
1219 | default: | |||
1220 | llvm_unreachable("The current token should be a register")::llvm::llvm_unreachable_internal("The current token should be a register" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1220); | |||
1221 | } | |||
1222 | } | |||
1223 | ||||
1224 | bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) { | |||
1225 | if (Token.isNot(MIToken::Identifier) && Token.isNot(MIToken::underscore)) | |||
1226 | return error("expected '_', register class, or register bank name"); | |||
1227 | StringRef::iterator Loc = Token.location(); | |||
1228 | StringRef Name = Token.stringValue(); | |||
1229 | ||||
1230 | // Was it a register class? | |||
1231 | const TargetRegisterClass *RC = PFS.Target.getRegClass(Name); | |||
1232 | if (RC) { | |||
1233 | lex(); | |||
1234 | ||||
1235 | switch (RegInfo.Kind) { | |||
1236 | case VRegInfo::UNKNOWN: | |||
1237 | case VRegInfo::NORMAL: | |||
1238 | RegInfo.Kind = VRegInfo::NORMAL; | |||
1239 | if (RegInfo.Explicit && RegInfo.D.RC != RC) { | |||
1240 | const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); | |||
1241 | return error(Loc, Twine("conflicting register classes, previously: ") + | |||
1242 | Twine(TRI.getRegClassName(RegInfo.D.RC))); | |||
1243 | } | |||
1244 | RegInfo.D.RC = RC; | |||
1245 | RegInfo.Explicit = true; | |||
1246 | return false; | |||
1247 | ||||
1248 | case VRegInfo::GENERIC: | |||
1249 | case VRegInfo::REGBANK: | |||
1250 | return error(Loc, "register class specification on generic register"); | |||
1251 | } | |||
1252 | llvm_unreachable("Unexpected register kind")::llvm::llvm_unreachable_internal("Unexpected register kind", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1252); | |||
1253 | } | |||
1254 | ||||
1255 | // Should be a register bank or a generic register. | |||
1256 | const RegisterBank *RegBank = nullptr; | |||
1257 | if (Name != "_") { | |||
1258 | RegBank = PFS.Target.getRegBank(Name); | |||
1259 | if (!RegBank) | |||
1260 | return error(Loc, "expected '_', register class, or register bank name"); | |||
1261 | } | |||
1262 | ||||
1263 | lex(); | |||
1264 | ||||
1265 | switch (RegInfo.Kind) { | |||
1266 | case VRegInfo::UNKNOWN: | |||
1267 | case VRegInfo::GENERIC: | |||
1268 | case VRegInfo::REGBANK: | |||
1269 | RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC; | |||
1270 | if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank) | |||
1271 | return error(Loc, "conflicting generic register banks"); | |||
1272 | RegInfo.D.RegBank = RegBank; | |||
1273 | RegInfo.Explicit = true; | |||
1274 | return false; | |||
1275 | ||||
1276 | case VRegInfo::NORMAL: | |||
1277 | return error(Loc, "register bank specification on normal register"); | |||
1278 | } | |||
1279 | llvm_unreachable("Unexpected register kind")::llvm::llvm_unreachable_internal("Unexpected register kind", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1279); | |||
1280 | } | |||
1281 | ||||
1282 | bool MIParser::parseRegisterFlag(unsigned &Flags) { | |||
1283 | const unsigned OldFlags = Flags; | |||
1284 | switch (Token.kind()) { | |||
1285 | case MIToken::kw_implicit: | |||
1286 | Flags |= RegState::Implicit; | |||
1287 | break; | |||
1288 | case MIToken::kw_implicit_define: | |||
1289 | Flags |= RegState::ImplicitDefine; | |||
1290 | break; | |||
1291 | case MIToken::kw_def: | |||
1292 | Flags |= RegState::Define; | |||
1293 | break; | |||
1294 | case MIToken::kw_dead: | |||
1295 | Flags |= RegState::Dead; | |||
1296 | break; | |||
1297 | case MIToken::kw_killed: | |||
1298 | Flags |= RegState::Kill; | |||
1299 | break; | |||
1300 | case MIToken::kw_undef: | |||
1301 | Flags |= RegState::Undef; | |||
1302 | break; | |||
1303 | case MIToken::kw_internal: | |||
1304 | Flags |= RegState::InternalRead; | |||
1305 | break; | |||
1306 | case MIToken::kw_early_clobber: | |||
1307 | Flags |= RegState::EarlyClobber; | |||
1308 | break; | |||
1309 | case MIToken::kw_debug_use: | |||
1310 | Flags |= RegState::Debug; | |||
1311 | break; | |||
1312 | case MIToken::kw_renamable: | |||
1313 | Flags |= RegState::Renamable; | |||
1314 | break; | |||
1315 | default: | |||
1316 | llvm_unreachable("The current token should be a register flag")::llvm::llvm_unreachable_internal("The current token should be a register flag" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1316); | |||
1317 | } | |||
1318 | if (OldFlags == Flags) | |||
1319 | // We know that the same flag is specified more than once when the flags | |||
1320 | // weren't modified. | |||
1321 | return error("duplicate '" + Token.stringValue() + "' register flag"); | |||
1322 | lex(); | |||
1323 | return false; | |||
1324 | } | |||
1325 | ||||
1326 | bool MIParser::parseSubRegisterIndex(unsigned &SubReg) { | |||
1327 | assert(Token.is(MIToken::dot))((Token.is(MIToken::dot)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::dot)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1327, __PRETTY_FUNCTION__)); | |||
1328 | lex(); | |||
1329 | if (Token.isNot(MIToken::Identifier)) | |||
1330 | return error("expected a subregister index after '.'"); | |||
1331 | auto Name = Token.stringValue(); | |||
1332 | SubReg = PFS.Target.getSubRegIndex(Name); | |||
1333 | if (!SubReg) | |||
1334 | return error(Twine("use of unknown subregister index '") + Name + "'"); | |||
1335 | lex(); | |||
1336 | return false; | |||
1337 | } | |||
1338 | ||||
1339 | bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) { | |||
1340 | if (!consumeIfPresent(MIToken::kw_tied_def)) | |||
1341 | return true; | |||
1342 | if (Token.isNot(MIToken::IntegerLiteral)) | |||
1343 | return error("expected an integer literal after 'tied-def'"); | |||
1344 | if (getUnsigned(TiedDefIdx)) | |||
1345 | return true; | |||
1346 | lex(); | |||
1347 | if (expectAndConsume(MIToken::rparen)) | |||
1348 | return true; | |||
1349 | return false; | |||
1350 | } | |||
1351 | ||||
1352 | bool MIParser::assignRegisterTies(MachineInstr &MI, | |||
1353 | ArrayRef<ParsedMachineOperand> Operands) { | |||
1354 | SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs; | |||
1355 | for (unsigned I = 0, E = Operands.size(); I != E; ++I) { | |||
1356 | if (!Operands[I].TiedDefIdx) | |||
1357 | continue; | |||
1358 | // The parser ensures that this operand is a register use, so we just have | |||
1359 | // to check the tied-def operand. | |||
1360 | unsigned DefIdx = Operands[I].TiedDefIdx.getValue(); | |||
1361 | if (DefIdx >= E) | |||
1362 | return error(Operands[I].Begin, | |||
1363 | Twine("use of invalid tied-def operand index '" + | |||
1364 | Twine(DefIdx) + "'; instruction has only ") + | |||
1365 | Twine(E) + " operands"); | |||
1366 | const auto &DefOperand = Operands[DefIdx].Operand; | |||
1367 | if (!DefOperand.isReg() || !DefOperand.isDef()) | |||
1368 | // FIXME: add note with the def operand. | |||
1369 | return error(Operands[I].Begin, | |||
1370 | Twine("use of invalid tied-def operand index '") + | |||
1371 | Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) + | |||
1372 | " isn't a defined register"); | |||
1373 | // Check that the tied-def operand wasn't tied elsewhere. | |||
1374 | for (const auto &TiedPair : TiedRegisterPairs) { | |||
1375 | if (TiedPair.first == DefIdx) | |||
1376 | return error(Operands[I].Begin, | |||
1377 | Twine("the tied-def operand #") + Twine(DefIdx) + | |||
1378 | " is already tied with another register operand"); | |||
1379 | } | |||
1380 | TiedRegisterPairs.push_back(std::make_pair(DefIdx, I)); | |||
1381 | } | |||
1382 | // FIXME: Verify that for non INLINEASM instructions, the def and use tied | |||
1383 | // indices must be less than tied max. | |||
1384 | for (const auto &TiedPair : TiedRegisterPairs) | |||
1385 | MI.tieOperands(TiedPair.first, TiedPair.second); | |||
1386 | return false; | |||
1387 | } | |||
1388 | ||||
1389 | bool MIParser::parseRegisterOperand(MachineOperand &Dest, | |||
1390 | Optional<unsigned> &TiedDefIdx, | |||
1391 | bool IsDef) { | |||
1392 | unsigned Flags = IsDef ? RegState::Define : 0; | |||
1393 | while (Token.isRegisterFlag()) { | |||
1394 | if (parseRegisterFlag(Flags)) | |||
1395 | return true; | |||
1396 | } | |||
1397 | if (!Token.isRegister()) | |||
1398 | return error("expected a register after register flags"); | |||
1399 | unsigned Reg; | |||
1400 | VRegInfo *RegInfo; | |||
1401 | if (parseRegister(Reg, RegInfo)) | |||
1402 | return true; | |||
1403 | lex(); | |||
1404 | unsigned SubReg = 0; | |||
1405 | if (Token.is(MIToken::dot)) { | |||
1406 | if (parseSubRegisterIndex(SubReg)) | |||
1407 | return true; | |||
1408 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | |||
1409 | return error("subregister index expects a virtual register"); | |||
1410 | } | |||
1411 | if (Token.is(MIToken::colon)) { | |||
1412 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | |||
1413 | return error("register class specification expects a virtual register"); | |||
1414 | lex(); | |||
1415 | if (parseRegisterClassOrBank(*RegInfo)) | |||
1416 | return true; | |||
1417 | } | |||
1418 | MachineRegisterInfo &MRI = MF.getRegInfo(); | |||
1419 | if ((Flags & RegState::Define) == 0) { | |||
1420 | if (consumeIfPresent(MIToken::lparen)) { | |||
1421 | unsigned Idx; | |||
1422 | if (!parseRegisterTiedDefIndex(Idx)) | |||
1423 | TiedDefIdx = Idx; | |||
1424 | else { | |||
1425 | // Try a redundant low-level type. | |||
1426 | LLT Ty; | |||
1427 | if (parseLowLevelType(Token.location(), Ty)) | |||
1428 | return error("expected tied-def or low-level type after '('"); | |||
1429 | ||||
1430 | if (expectAndConsume(MIToken::rparen)) | |||
1431 | return true; | |||
1432 | ||||
1433 | if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty) | |||
1434 | return error("inconsistent type for generic virtual register"); | |||
1435 | ||||
1436 | MRI.setType(Reg, Ty); | |||
1437 | } | |||
1438 | } | |||
1439 | } else if (consumeIfPresent(MIToken::lparen)) { | |||
1440 | // Virtual registers may have a tpe with GlobalISel. | |||
1441 | if (!TargetRegisterInfo::isVirtualRegister(Reg)) | |||
1442 | return error("unexpected type on physical register"); | |||
1443 | ||||
1444 | LLT Ty; | |||
1445 | if (parseLowLevelType(Token.location(), Ty)) | |||
1446 | return true; | |||
1447 | ||||
1448 | if (expectAndConsume(MIToken::rparen)) | |||
1449 | return true; | |||
1450 | ||||
1451 | if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty) | |||
1452 | return error("inconsistent type for generic virtual register"); | |||
1453 | ||||
1454 | MRI.setType(Reg, Ty); | |||
1455 | } else if (TargetRegisterInfo::isVirtualRegister(Reg)) { | |||
1456 | // Generic virtual registers must have a type. | |||
1457 | // If we end up here this means the type hasn't been specified and | |||
1458 | // this is bad! | |||
1459 | if (RegInfo->Kind == VRegInfo::GENERIC || | |||
1460 | RegInfo->Kind == VRegInfo::REGBANK) | |||
1461 | return error("generic virtual registers must have a type"); | |||
1462 | } | |||
1463 | Dest = MachineOperand::CreateReg( | |||
1464 | Reg, Flags & RegState::Define, Flags & RegState::Implicit, | |||
1465 | Flags & RegState::Kill, Flags & RegState::Dead, Flags & RegState::Undef, | |||
1466 | Flags & RegState::EarlyClobber, SubReg, Flags & RegState::Debug, | |||
1467 | Flags & RegState::InternalRead, Flags & RegState::Renamable); | |||
1468 | ||||
1469 | return false; | |||
1470 | } | |||
1471 | ||||
1472 | bool MIParser::parseImmediateOperand(MachineOperand &Dest) { | |||
1473 | assert(Token.is(MIToken::IntegerLiteral))((Token.is(MIToken::IntegerLiteral)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::IntegerLiteral)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1473, __PRETTY_FUNCTION__)); | |||
1474 | const APSInt &Int = Token.integerValue(); | |||
1475 | if (Int.getMinSignedBits() > 64) | |||
1476 | return error("integer literal is too large to be an immediate operand"); | |||
1477 | Dest = MachineOperand::CreateImm(Int.getExtValue()); | |||
1478 | lex(); | |||
1479 | return false; | |||
1480 | } | |||
1481 | ||||
1482 | bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue, | |||
1483 | const Constant *&C) { | |||
1484 | auto Source = StringValue.str(); // The source has to be null terminated. | |||
1485 | SMDiagnostic Err; | |||
1486 | C = parseConstantValue(Source, Err, *MF.getFunction().getParent(), | |||
1487 | &PFS.IRSlots); | |||
1488 | if (!C) | |||
1489 | return error(Loc + Err.getColumnNo(), Err.getMessage()); | |||
1490 | return false; | |||
1491 | } | |||
1492 | ||||
1493 | bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) { | |||
1494 | if (parseIRConstant(Loc, StringRef(Loc, Token.range().end() - Loc), C)) | |||
1495 | return true; | |||
1496 | lex(); | |||
1497 | return false; | |||
1498 | } | |||
1499 | ||||
1500 | // See LLT implemntation for bit size limits. | |||
1501 | static bool verifyScalarSize(uint64_t Size) { | |||
1502 | return Size != 0 && isUInt<16>(Size); | |||
1503 | } | |||
1504 | ||||
1505 | static bool verifyVectorElementCount(uint64_t NumElts) { | |||
1506 | return NumElts != 0 && isUInt<16>(NumElts); | |||
1507 | } | |||
1508 | ||||
1509 | static bool verifyAddrSpace(uint64_t AddrSpace) { | |||
1510 | return isUInt<24>(AddrSpace); | |||
1511 | } | |||
1512 | ||||
1513 | bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) { | |||
1514 | if (Token.range().front() == 's' || Token.range().front() == 'p') { | |||
1515 | StringRef SizeStr = Token.range().drop_front(); | |||
1516 | if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit)) | |||
1517 | return error("expected integers after 's'/'p' type character"); | |||
1518 | } | |||
1519 | ||||
1520 | if (Token.range().front() == 's') { | |||
1521 | auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue(); | |||
1522 | if (!verifyScalarSize(ScalarSize)) | |||
1523 | return error("invalid size for scalar type"); | |||
1524 | ||||
1525 | Ty = LLT::scalar(ScalarSize); | |||
1526 | lex(); | |||
1527 | return false; | |||
1528 | } else if (Token.range().front() == 'p') { | |||
1529 | const DataLayout &DL = MF.getDataLayout(); | |||
1530 | uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue(); | |||
1531 | if (!verifyAddrSpace(AS)) | |||
1532 | return error("invalid address space number"); | |||
1533 | ||||
1534 | Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS)); | |||
1535 | lex(); | |||
1536 | return false; | |||
1537 | } | |||
1538 | ||||
1539 | // Now we're looking for a vector. | |||
1540 | if (Token.isNot(MIToken::less)) | |||
1541 | return error(Loc, | |||
1542 | "expected sN, pA, <M x sN>, or <M x pA> for GlobalISel type"); | |||
1543 | lex(); | |||
1544 | ||||
1545 | if (Token.isNot(MIToken::IntegerLiteral)) | |||
1546 | return error(Loc, "expected <M x sN> or <M x pA> for vector type"); | |||
1547 | uint64_t NumElements = Token.integerValue().getZExtValue(); | |||
1548 | if (!verifyVectorElementCount(NumElements)) | |||
1549 | return error("invalid number of vector elements"); | |||
1550 | ||||
1551 | lex(); | |||
1552 | ||||
1553 | if (Token.isNot(MIToken::Identifier) || Token.stringValue() != "x") | |||
1554 | return error(Loc, "expected <M x sN> or <M x pA> for vector type"); | |||
1555 | lex(); | |||
1556 | ||||
1557 | if (Token.range().front() != 's' && Token.range().front() != 'p') | |||
1558 | return error(Loc, "expected <M x sN> or <M x pA> for vector type"); | |||
1559 | StringRef SizeStr = Token.range().drop_front(); | |||
1560 | if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit)) | |||
1561 | return error("expected integers after 's'/'p' type character"); | |||
1562 | ||||
1563 | if (Token.range().front() == 's') { | |||
1564 | auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue(); | |||
1565 | if (!verifyScalarSize(ScalarSize)) | |||
1566 | return error("invalid size for scalar type"); | |||
1567 | Ty = LLT::scalar(ScalarSize); | |||
1568 | } else if (Token.range().front() == 'p') { | |||
1569 | const DataLayout &DL = MF.getDataLayout(); | |||
1570 | uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue(); | |||
1571 | if (!verifyAddrSpace(AS)) | |||
1572 | return error("invalid address space number"); | |||
1573 | ||||
1574 | Ty = LLT::pointer(AS, DL.getPointerSizeInBits(AS)); | |||
1575 | } else | |||
1576 | return error(Loc, "expected <M x sN> or <M x pA> for vector type"); | |||
1577 | lex(); | |||
1578 | ||||
1579 | if (Token.isNot(MIToken::greater)) | |||
1580 | return error(Loc, "expected <M x sN> or <M x pA> for vector type"); | |||
1581 | lex(); | |||
1582 | ||||
1583 | Ty = LLT::vector(NumElements, Ty); | |||
1584 | return false; | |||
1585 | } | |||
1586 | ||||
1587 | bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) { | |||
1588 | assert(Token.is(MIToken::Identifier))((Token.is(MIToken::Identifier)) ? static_cast<void> (0 ) : __assert_fail ("Token.is(MIToken::Identifier)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1588, __PRETTY_FUNCTION__)); | |||
1589 | StringRef TypeStr = Token.range(); | |||
1590 | if (TypeStr.front() != 'i' && TypeStr.front() != 's' && | |||
1591 | TypeStr.front() != 'p') | |||
1592 | return error( | |||
1593 | "a typed immediate operand should start with one of 'i', 's', or 'p'"); | |||
1594 | StringRef SizeStr = Token.range().drop_front(); | |||
1595 | if (SizeStr.size() == 0 || !llvm::all_of(SizeStr, isdigit)) | |||
1596 | return error("expected integers after 'i'/'s'/'p' type character"); | |||
1597 | ||||
1598 | auto Loc = Token.location(); | |||
1599 | lex(); | |||
1600 | if (Token.isNot(MIToken::IntegerLiteral)) { | |||
1601 | if (Token.isNot(MIToken::Identifier) || | |||
1602 | !(Token.range() == "true" || Token.range() == "false")) | |||
1603 | return error("expected an integer literal"); | |||
1604 | } | |||
1605 | const Constant *C = nullptr; | |||
1606 | if (parseIRConstant(Loc, C)) | |||
1607 | return true; | |||
1608 | Dest = MachineOperand::CreateCImm(cast<ConstantInt>(C)); | |||
1609 | return false; | |||
1610 | } | |||
1611 | ||||
1612 | bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) { | |||
1613 | auto Loc = Token.location(); | |||
1614 | lex(); | |||
1615 | if (Token.isNot(MIToken::FloatingPointLiteral) && | |||
1616 | Token.isNot(MIToken::HexLiteral)) | |||
1617 | return error("expected a floating point literal"); | |||
1618 | const Constant *C = nullptr; | |||
1619 | if (parseIRConstant(Loc, C)) | |||
1620 | return true; | |||
1621 | Dest = MachineOperand::CreateFPImm(cast<ConstantFP>(C)); | |||
1622 | return false; | |||
1623 | } | |||
1624 | ||||
1625 | bool MIParser::getUnsigned(unsigned &Result) { | |||
1626 | if (Token.hasIntegerValue()) { | |||
1627 | const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1; | |||
1628 | uint64_t Val64 = Token.integerValue().getLimitedValue(Limit); | |||
1629 | if (Val64 == Limit) | |||
1630 | return error("expected 32-bit integer (too large)"); | |||
1631 | Result = Val64; | |||
1632 | return false; | |||
1633 | } | |||
1634 | if (Token.is(MIToken::HexLiteral)) { | |||
1635 | APInt A; | |||
1636 | if (getHexUint(A)) | |||
1637 | return true; | |||
1638 | if (A.getBitWidth() > 32) | |||
1639 | return error("expected 32-bit integer (too large)"); | |||
1640 | Result = A.getZExtValue(); | |||
1641 | return false; | |||
1642 | } | |||
1643 | return true; | |||
1644 | } | |||
1645 | ||||
1646 | bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) { | |||
1647 | assert(Token.is(MIToken::MachineBasicBlock) ||((Token.is(MIToken::MachineBasicBlock) || Token.is(MIToken::MachineBasicBlockLabel )) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlock) || Token.is(MIToken::MachineBasicBlockLabel)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1648, __PRETTY_FUNCTION__)) | |||
1648 | Token.is(MIToken::MachineBasicBlockLabel))((Token.is(MIToken::MachineBasicBlock) || Token.is(MIToken::MachineBasicBlockLabel )) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::MachineBasicBlock) || Token.is(MIToken::MachineBasicBlockLabel)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1648, __PRETTY_FUNCTION__)); | |||
1649 | unsigned Number; | |||
1650 | if (getUnsigned(Number)) | |||
1651 | return true; | |||
1652 | auto MBBInfo = PFS.MBBSlots.find(Number); | |||
1653 | if (MBBInfo == PFS.MBBSlots.end()) | |||
1654 | return error(Twine("use of undefined machine basic block #") + | |||
1655 | Twine(Number)); | |||
1656 | MBB = MBBInfo->second; | |||
1657 | // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once | |||
1658 | // we drop the <irname> from the bb.<id>.<irname> format. | |||
1659 | if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName()) | |||
1660 | return error(Twine("the name of machine basic block #") + Twine(Number) + | |||
1661 | " isn't '" + Token.stringValue() + "'"); | |||
1662 | return false; | |||
1663 | } | |||
1664 | ||||
1665 | bool MIParser::parseMBBOperand(MachineOperand &Dest) { | |||
1666 | MachineBasicBlock *MBB; | |||
1667 | if (parseMBBReference(MBB)) | |||
1668 | return true; | |||
1669 | Dest = MachineOperand::CreateMBB(MBB); | |||
1670 | lex(); | |||
1671 | return false; | |||
1672 | } | |||
1673 | ||||
1674 | bool MIParser::parseStackFrameIndex(int &FI) { | |||
1675 | assert(Token.is(MIToken::StackObject))((Token.is(MIToken::StackObject)) ? static_cast<void> ( 0) : __assert_fail ("Token.is(MIToken::StackObject)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1675, __PRETTY_FUNCTION__)); | |||
1676 | unsigned ID; | |||
1677 | if (getUnsigned(ID)) | |||
1678 | return true; | |||
1679 | auto ObjectInfo = PFS.StackObjectSlots.find(ID); | |||
1680 | if (ObjectInfo == PFS.StackObjectSlots.end()) | |||
1681 | return error(Twine("use of undefined stack object '%stack.") + Twine(ID) + | |||
1682 | "'"); | |||
1683 | StringRef Name; | |||
1684 | if (const auto *Alloca = | |||
1685 | MF.getFrameInfo().getObjectAllocation(ObjectInfo->second)) | |||
1686 | Name = Alloca->getName(); | |||
1687 | if (!Token.stringValue().empty() && Token.stringValue() != Name) | |||
1688 | return error(Twine("the name of the stack object '%stack.") + Twine(ID) + | |||
1689 | "' isn't '" + Token.stringValue() + "'"); | |||
1690 | lex(); | |||
1691 | FI = ObjectInfo->second; | |||
1692 | return false; | |||
1693 | } | |||
1694 | ||||
1695 | bool MIParser::parseStackObjectOperand(MachineOperand &Dest) { | |||
1696 | int FI; | |||
1697 | if (parseStackFrameIndex(FI)) | |||
1698 | return true; | |||
1699 | Dest = MachineOperand::CreateFI(FI); | |||
1700 | return false; | |||
1701 | } | |||
1702 | ||||
1703 | bool MIParser::parseFixedStackFrameIndex(int &FI) { | |||
1704 | assert(Token.is(MIToken::FixedStackObject))((Token.is(MIToken::FixedStackObject)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::FixedStackObject)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1704, __PRETTY_FUNCTION__)); | |||
1705 | unsigned ID; | |||
1706 | if (getUnsigned(ID)) | |||
1707 | return true; | |||
1708 | auto ObjectInfo = PFS.FixedStackObjectSlots.find(ID); | |||
1709 | if (ObjectInfo == PFS.FixedStackObjectSlots.end()) | |||
1710 | return error(Twine("use of undefined fixed stack object '%fixed-stack.") + | |||
1711 | Twine(ID) + "'"); | |||
1712 | lex(); | |||
1713 | FI = ObjectInfo->second; | |||
1714 | return false; | |||
1715 | } | |||
1716 | ||||
1717 | bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) { | |||
1718 | int FI; | |||
1719 | if (parseFixedStackFrameIndex(FI)) | |||
1720 | return true; | |||
1721 | Dest = MachineOperand::CreateFI(FI); | |||
1722 | return false; | |||
1723 | } | |||
1724 | ||||
1725 | bool MIParser::parseGlobalValue(GlobalValue *&GV) { | |||
1726 | switch (Token.kind()) { | |||
1727 | case MIToken::NamedGlobalValue: { | |||
1728 | const Module *M = MF.getFunction().getParent(); | |||
1729 | GV = M->getNamedValue(Token.stringValue()); | |||
1730 | if (!GV) | |||
1731 | return error(Twine("use of undefined global value '") + Token.range() + | |||
1732 | "'"); | |||
1733 | break; | |||
1734 | } | |||
1735 | case MIToken::GlobalValue: { | |||
1736 | unsigned GVIdx; | |||
1737 | if (getUnsigned(GVIdx)) | |||
1738 | return true; | |||
1739 | if (GVIdx >= PFS.IRSlots.GlobalValues.size()) | |||
1740 | return error(Twine("use of undefined global value '@") + Twine(GVIdx) + | |||
1741 | "'"); | |||
1742 | GV = PFS.IRSlots.GlobalValues[GVIdx]; | |||
1743 | break; | |||
1744 | } | |||
1745 | default: | |||
1746 | llvm_unreachable("The current token should be a global value")::llvm::llvm_unreachable_internal("The current token should be a global value" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1746); | |||
1747 | } | |||
1748 | return false; | |||
1749 | } | |||
1750 | ||||
1751 | bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) { | |||
1752 | GlobalValue *GV = nullptr; | |||
1753 | if (parseGlobalValue(GV)) | |||
1754 | return true; | |||
1755 | lex(); | |||
1756 | Dest = MachineOperand::CreateGA(GV, /*Offset=*/0); | |||
1757 | if (parseOperandsOffset(Dest)) | |||
1758 | return true; | |||
1759 | return false; | |||
1760 | } | |||
1761 | ||||
1762 | bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) { | |||
1763 | assert(Token.is(MIToken::ConstantPoolItem))((Token.is(MIToken::ConstantPoolItem)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::ConstantPoolItem)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1763, __PRETTY_FUNCTION__)); | |||
1764 | unsigned ID; | |||
1765 | if (getUnsigned(ID)) | |||
1766 | return true; | |||
1767 | auto ConstantInfo = PFS.ConstantPoolSlots.find(ID); | |||
1768 | if (ConstantInfo == PFS.ConstantPoolSlots.end()) | |||
1769 | return error("use of undefined constant '%const." + Twine(ID) + "'"); | |||
1770 | lex(); | |||
1771 | Dest = MachineOperand::CreateCPI(ID, /*Offset=*/0); | |||
1772 | if (parseOperandsOffset(Dest)) | |||
1773 | return true; | |||
1774 | return false; | |||
1775 | } | |||
1776 | ||||
1777 | bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) { | |||
1778 | assert(Token.is(MIToken::JumpTableIndex))((Token.is(MIToken::JumpTableIndex)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::JumpTableIndex)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1778, __PRETTY_FUNCTION__)); | |||
1779 | unsigned ID; | |||
1780 | if (getUnsigned(ID)) | |||
1781 | return true; | |||
1782 | auto JumpTableEntryInfo = PFS.JumpTableSlots.find(ID); | |||
1783 | if (JumpTableEntryInfo == PFS.JumpTableSlots.end()) | |||
1784 | return error("use of undefined jump table '%jump-table." + Twine(ID) + "'"); | |||
1785 | lex(); | |||
1786 | Dest = MachineOperand::CreateJTI(JumpTableEntryInfo->second); | |||
1787 | return false; | |||
1788 | } | |||
1789 | ||||
1790 | bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) { | |||
1791 | assert(Token.is(MIToken::ExternalSymbol))((Token.is(MIToken::ExternalSymbol)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::ExternalSymbol)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1791, __PRETTY_FUNCTION__)); | |||
1792 | const char *Symbol = MF.createExternalSymbolName(Token.stringValue()); | |||
1793 | lex(); | |||
1794 | Dest = MachineOperand::CreateES(Symbol); | |||
1795 | if (parseOperandsOffset(Dest)) | |||
1796 | return true; | |||
1797 | return false; | |||
1798 | } | |||
1799 | ||||
1800 | bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) { | |||
1801 | assert(Token.is(MIToken::MCSymbol))((Token.is(MIToken::MCSymbol)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::MCSymbol)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1801, __PRETTY_FUNCTION__)); | |||
1802 | MCSymbol *Symbol = getOrCreateMCSymbol(Token.stringValue()); | |||
1803 | lex(); | |||
1804 | Dest = MachineOperand::CreateMCSymbol(Symbol); | |||
1805 | if (parseOperandsOffset(Dest)) | |||
1806 | return true; | |||
1807 | return false; | |||
1808 | } | |||
1809 | ||||
1810 | bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) { | |||
1811 | assert(Token.is(MIToken::SubRegisterIndex))((Token.is(MIToken::SubRegisterIndex)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::SubRegisterIndex)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1811, __PRETTY_FUNCTION__)); | |||
1812 | StringRef Name = Token.stringValue(); | |||
1813 | unsigned SubRegIndex = PFS.Target.getSubRegIndex(Token.stringValue()); | |||
1814 | if (SubRegIndex == 0) | |||
1815 | return error(Twine("unknown subregister index '") + Name + "'"); | |||
1816 | lex(); | |||
1817 | Dest = MachineOperand::CreateImm(SubRegIndex); | |||
1818 | return false; | |||
1819 | } | |||
1820 | ||||
1821 | bool MIParser::parseMDNode(MDNode *&Node) { | |||
1822 | assert(Token.is(MIToken::exclaim))((Token.is(MIToken::exclaim)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::exclaim)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1822, __PRETTY_FUNCTION__)); | |||
1823 | ||||
1824 | auto Loc = Token.location(); | |||
1825 | lex(); | |||
1826 | if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) | |||
1827 | return error("expected metadata id after '!'"); | |||
1828 | unsigned ID; | |||
1829 | if (getUnsigned(ID)) | |||
1830 | return true; | |||
1831 | auto NodeInfo = PFS.IRSlots.MetadataNodes.find(ID); | |||
1832 | if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) | |||
1833 | return error(Loc, "use of undefined metadata '!" + Twine(ID) + "'"); | |||
1834 | lex(); | |||
1835 | Node = NodeInfo->second.get(); | |||
1836 | return false; | |||
1837 | } | |||
1838 | ||||
1839 | bool MIParser::parseDIExpression(MDNode *&Expr) { | |||
1840 | assert(Token.is(MIToken::md_diexpr))((Token.is(MIToken::md_diexpr)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::md_diexpr)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1840, __PRETTY_FUNCTION__)); | |||
1841 | lex(); | |||
1842 | ||||
1843 | // FIXME: Share this parsing with the IL parser. | |||
1844 | SmallVector<uint64_t, 8> Elements; | |||
1845 | ||||
1846 | if (expectAndConsume(MIToken::lparen)) | |||
1847 | return true; | |||
1848 | ||||
1849 | if (Token.isNot(MIToken::rparen)) { | |||
1850 | do { | |||
1851 | if (Token.is(MIToken::Identifier)) { | |||
1852 | if (unsigned Op = dwarf::getOperationEncoding(Token.stringValue())) { | |||
1853 | lex(); | |||
1854 | Elements.push_back(Op); | |||
1855 | continue; | |||
1856 | } | |||
1857 | if (unsigned Enc = dwarf::getAttributeEncoding(Token.stringValue())) { | |||
1858 | lex(); | |||
1859 | Elements.push_back(Enc); | |||
1860 | continue; | |||
1861 | } | |||
1862 | return error(Twine("invalid DWARF op '") + Token.stringValue() + "'"); | |||
1863 | } | |||
1864 | ||||
1865 | if (Token.isNot(MIToken::IntegerLiteral) || | |||
1866 | Token.integerValue().isSigned()) | |||
1867 | return error("expected unsigned integer"); | |||
1868 | ||||
1869 | auto &U = Token.integerValue(); | |||
1870 | if (U.ugt(UINT64_MAX(18446744073709551615UL))) | |||
1871 | return error("element too large, limit is " + Twine(UINT64_MAX(18446744073709551615UL))); | |||
1872 | Elements.push_back(U.getZExtValue()); | |||
1873 | lex(); | |||
1874 | ||||
1875 | } while (consumeIfPresent(MIToken::comma)); | |||
1876 | } | |||
1877 | ||||
1878 | if (expectAndConsume(MIToken::rparen)) | |||
1879 | return true; | |||
1880 | ||||
1881 | Expr = DIExpression::get(MF.getFunction().getContext(), Elements); | |||
1882 | return false; | |||
1883 | } | |||
1884 | ||||
1885 | bool MIParser::parseDILocation(MDNode *&Loc) { | |||
1886 | assert(Token.is(MIToken::md_dilocation))((Token.is(MIToken::md_dilocation)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::md_dilocation)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 1886, __PRETTY_FUNCTION__)); | |||
1887 | lex(); | |||
1888 | ||||
1889 | bool HaveLine = false; | |||
1890 | unsigned Line = 0; | |||
1891 | unsigned Column = 0; | |||
1892 | MDNode *Scope = nullptr; | |||
1893 | MDNode *InlinedAt = nullptr; | |||
1894 | bool ImplicitCode = false; | |||
1895 | ||||
1896 | if (expectAndConsume(MIToken::lparen)) | |||
1897 | return true; | |||
1898 | ||||
1899 | if (Token.isNot(MIToken::rparen)) { | |||
1900 | do { | |||
1901 | if (Token.is(MIToken::Identifier)) { | |||
1902 | if (Token.stringValue() == "line") { | |||
1903 | lex(); | |||
1904 | if (expectAndConsume(MIToken::colon)) | |||
1905 | return true; | |||
1906 | if (Token.isNot(MIToken::IntegerLiteral) || | |||
1907 | Token.integerValue().isSigned()) | |||
1908 | return error("expected unsigned integer"); | |||
1909 | Line = Token.integerValue().getZExtValue(); | |||
1910 | HaveLine = true; | |||
1911 | lex(); | |||
1912 | continue; | |||
1913 | } | |||
1914 | if (Token.stringValue() == "column") { | |||
1915 | lex(); | |||
1916 | if (expectAndConsume(MIToken::colon)) | |||
1917 | return true; | |||
1918 | if (Token.isNot(MIToken::IntegerLiteral) || | |||
1919 | Token.integerValue().isSigned()) | |||
1920 | return error("expected unsigned integer"); | |||
1921 | Column = Token.integerValue().getZExtValue(); | |||
1922 | lex(); | |||
1923 | continue; | |||
1924 | } | |||
1925 | if (Token.stringValue() == "scope") { | |||
1926 | lex(); | |||
1927 | if (expectAndConsume(MIToken::colon)) | |||
1928 | return true; | |||
1929 | if (parseMDNode(Scope)) | |||
1930 | return error("expected metadata node"); | |||
1931 | if (!isa<DIScope>(Scope)) | |||
1932 | return error("expected DIScope node"); | |||
1933 | continue; | |||
1934 | } | |||
1935 | if (Token.stringValue() == "inlinedAt") { | |||
1936 | lex(); | |||
1937 | if (expectAndConsume(MIToken::colon)) | |||
1938 | return true; | |||
1939 | if (Token.is(MIToken::exclaim)) { | |||
1940 | if (parseMDNode(InlinedAt)) | |||
1941 | return true; | |||
1942 | } else if (Token.is(MIToken::md_dilocation)) { | |||
1943 | if (parseDILocation(InlinedAt)) | |||
1944 | return true; | |||
1945 | } else | |||
1946 | return error("expected metadata node"); | |||
1947 | if (!isa<DILocation>(InlinedAt)) | |||
1948 | return error("expected DILocation node"); | |||
1949 | continue; | |||
1950 | } | |||
1951 | if (Token.stringValue() == "isImplicitCode") { | |||
1952 | lex(); | |||
1953 | if (expectAndConsume(MIToken::colon)) | |||
1954 | return true; | |||
1955 | if (!Token.is(MIToken::Identifier)) | |||
1956 | return error("expected true/false"); | |||
1957 | // As far as I can see, we don't have any existing need for parsing | |||
1958 | // true/false in MIR yet. Do it ad-hoc until there's something else | |||
1959 | // that needs it. | |||
1960 | if (Token.stringValue() == "true") | |||
1961 | ImplicitCode = true; | |||
1962 | else if (Token.stringValue() == "false") | |||
1963 | ImplicitCode = false; | |||
1964 | else | |||
1965 | return error("expected true/false"); | |||
1966 | lex(); | |||
1967 | continue; | |||
1968 | } | |||
1969 | } | |||
1970 | return error(Twine("invalid DILocation argument '") + | |||
1971 | Token.stringValue() + "'"); | |||
1972 | } while (consumeIfPresent(MIToken::comma)); | |||
1973 | } | |||
1974 | ||||
1975 | if (expectAndConsume(MIToken::rparen)) | |||
1976 | return true; | |||
1977 | ||||
1978 | if (!HaveLine) | |||
1979 | return error("DILocation requires line number"); | |||
1980 | if (!Scope) | |||
1981 | return error("DILocation requires a scope"); | |||
1982 | ||||
1983 | Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope, | |||
1984 | InlinedAt, ImplicitCode); | |||
1985 | return false; | |||
1986 | } | |||
1987 | ||||
1988 | bool MIParser::parseMetadataOperand(MachineOperand &Dest) { | |||
1989 | MDNode *Node = nullptr; | |||
1990 | if (Token.is(MIToken::exclaim)) { | |||
1991 | if (parseMDNode(Node)) | |||
1992 | return true; | |||
1993 | } else if (Token.is(MIToken::md_diexpr)) { | |||
1994 | if (parseDIExpression(Node)) | |||
1995 | return true; | |||
1996 | } | |||
1997 | Dest = MachineOperand::CreateMetadata(Node); | |||
1998 | return false; | |||
1999 | } | |||
2000 | ||||
2001 | bool MIParser::parseCFIOffset(int &Offset) { | |||
2002 | if (Token.isNot(MIToken::IntegerLiteral)) | |||
2003 | return error("expected a cfi offset"); | |||
2004 | if (Token.integerValue().getMinSignedBits() > 32) | |||
2005 | return error("expected a 32 bit integer (the cfi offset is too large)"); | |||
2006 | Offset = (int)Token.integerValue().getExtValue(); | |||
2007 | lex(); | |||
2008 | return false; | |||
2009 | } | |||
2010 | ||||
2011 | bool MIParser::parseCFIRegister(unsigned &Reg) { | |||
2012 | if (Token.isNot(MIToken::NamedRegister)) | |||
2013 | return error("expected a cfi register"); | |||
2014 | unsigned LLVMReg; | |||
2015 | if (parseNamedRegister(LLVMReg)) | |||
2016 | return true; | |||
2017 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); | |||
2018 | assert(TRI && "Expected target register info")((TRI && "Expected target register info") ? static_cast <void> (0) : __assert_fail ("TRI && \"Expected target register info\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2018, __PRETTY_FUNCTION__)); | |||
2019 | int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true); | |||
2020 | if (DwarfReg < 0) | |||
2021 | return error("invalid DWARF register"); | |||
2022 | Reg = (unsigned)DwarfReg; | |||
2023 | lex(); | |||
2024 | return false; | |||
2025 | } | |||
2026 | ||||
2027 | bool MIParser::parseCFIEscapeValues(std::string &Values) { | |||
2028 | do { | |||
2029 | if (Token.isNot(MIToken::HexLiteral)) | |||
2030 | return error("expected a hexadecimal literal"); | |||
2031 | unsigned Value; | |||
2032 | if (getUnsigned(Value)) | |||
2033 | return true; | |||
2034 | if (Value > UINT8_MAX(255)) | |||
2035 | return error("expected a 8-bit integer (too large)"); | |||
2036 | Values.push_back(static_cast<uint8_t>(Value)); | |||
2037 | lex(); | |||
2038 | } while (consumeIfPresent(MIToken::comma)); | |||
2039 | return false; | |||
2040 | } | |||
2041 | ||||
2042 | bool MIParser::parseCFIOperand(MachineOperand &Dest) { | |||
2043 | auto Kind = Token.kind(); | |||
2044 | lex(); | |||
2045 | int Offset; | |||
2046 | unsigned Reg; | |||
2047 | unsigned CFIIndex; | |||
2048 | switch (Kind) { | |||
2049 | case MIToken::kw_cfi_same_value: | |||
2050 | if (parseCFIRegister(Reg)) | |||
2051 | return true; | |||
2052 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg)); | |||
2053 | break; | |||
2054 | case MIToken::kw_cfi_offset: | |||
2055 | if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || | |||
2056 | parseCFIOffset(Offset)) | |||
2057 | return true; | |||
2058 | CFIIndex = | |||
2059 | MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset)); | |||
2060 | break; | |||
2061 | case MIToken::kw_cfi_rel_offset: | |||
2062 | if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || | |||
2063 | parseCFIOffset(Offset)) | |||
2064 | return true; | |||
2065 | CFIIndex = MF.addFrameInst( | |||
2066 | MCCFIInstruction::createRelOffset(nullptr, Reg, Offset)); | |||
2067 | break; | |||
2068 | case MIToken::kw_cfi_def_cfa_register: | |||
2069 | if (parseCFIRegister(Reg)) | |||
2070 | return true; | |||
2071 | CFIIndex = | |||
2072 | MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg)); | |||
2073 | break; | |||
2074 | case MIToken::kw_cfi_def_cfa_offset: | |||
2075 | if (parseCFIOffset(Offset)) | |||
2076 | return true; | |||
2077 | // NB: MCCFIInstruction::createDefCfaOffset negates the offset. | |||
2078 | CFIIndex = MF.addFrameInst( | |||
2079 | MCCFIInstruction::createDefCfaOffset(nullptr, -Offset)); | |||
2080 | break; | |||
2081 | case MIToken::kw_cfi_adjust_cfa_offset: | |||
2082 | if (parseCFIOffset(Offset)) | |||
2083 | return true; | |||
2084 | CFIIndex = MF.addFrameInst( | |||
2085 | MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset)); | |||
2086 | break; | |||
2087 | case MIToken::kw_cfi_def_cfa: | |||
2088 | if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || | |||
2089 | parseCFIOffset(Offset)) | |||
2090 | return true; | |||
2091 | // NB: MCCFIInstruction::createDefCfa negates the offset. | |||
2092 | CFIIndex = | |||
2093 | MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset)); | |||
2094 | break; | |||
2095 | case MIToken::kw_cfi_remember_state: | |||
2096 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr)); | |||
2097 | break; | |||
2098 | case MIToken::kw_cfi_restore: | |||
2099 | if (parseCFIRegister(Reg)) | |||
2100 | return true; | |||
2101 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg)); | |||
2102 | break; | |||
2103 | case MIToken::kw_cfi_restore_state: | |||
2104 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr)); | |||
2105 | break; | |||
2106 | case MIToken::kw_cfi_undefined: | |||
2107 | if (parseCFIRegister(Reg)) | |||
2108 | return true; | |||
2109 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg)); | |||
2110 | break; | |||
2111 | case MIToken::kw_cfi_register: { | |||
2112 | unsigned Reg2; | |||
2113 | if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) || | |||
2114 | parseCFIRegister(Reg2)) | |||
2115 | return true; | |||
2116 | ||||
2117 | CFIIndex = | |||
2118 | MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2)); | |||
2119 | break; | |||
2120 | } | |||
2121 | case MIToken::kw_cfi_window_save: | |||
2122 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr)); | |||
2123 | break; | |||
2124 | case MIToken::kw_cfi_aarch64_negate_ra_sign_state: | |||
2125 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr)); | |||
2126 | break; | |||
2127 | case MIToken::kw_cfi_escape: { | |||
2128 | std::string Values; | |||
2129 | if (parseCFIEscapeValues(Values)) | |||
2130 | return true; | |||
2131 | CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values)); | |||
2132 | break; | |||
2133 | } | |||
2134 | default: | |||
2135 | // TODO: Parse the other CFI operands. | |||
2136 | llvm_unreachable("The current token should be a cfi operand")::llvm::llvm_unreachable_internal("The current token should be a cfi operand" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2136); | |||
2137 | } | |||
2138 | Dest = MachineOperand::CreateCFIIndex(CFIIndex); | |||
2139 | return false; | |||
2140 | } | |||
2141 | ||||
2142 | bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) { | |||
2143 | switch (Token.kind()) { | |||
2144 | case MIToken::NamedIRBlock: { | |||
2145 | BB = dyn_cast_or_null<BasicBlock>( | |||
2146 | F.getValueSymbolTable()->lookup(Token.stringValue())); | |||
2147 | if (!BB) | |||
2148 | return error(Twine("use of undefined IR block '") + Token.range() + "'"); | |||
2149 | break; | |||
2150 | } | |||
2151 | case MIToken::IRBlock: { | |||
2152 | unsigned SlotNumber = 0; | |||
2153 | if (getUnsigned(SlotNumber)) | |||
2154 | return true; | |||
2155 | BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F)); | |||
2156 | if (!BB) | |||
2157 | return error(Twine("use of undefined IR block '%ir-block.") + | |||
2158 | Twine(SlotNumber) + "'"); | |||
2159 | break; | |||
2160 | } | |||
2161 | default: | |||
2162 | llvm_unreachable("The current token should be an IR block reference")::llvm::llvm_unreachable_internal("The current token should be an IR block reference" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2162); | |||
2163 | } | |||
2164 | return false; | |||
2165 | } | |||
2166 | ||||
2167 | bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) { | |||
2168 | assert(Token.is(MIToken::kw_blockaddress))((Token.is(MIToken::kw_blockaddress)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::kw_blockaddress)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2168, __PRETTY_FUNCTION__)); | |||
2169 | lex(); | |||
2170 | if (expectAndConsume(MIToken::lparen)) | |||
2171 | return true; | |||
2172 | if (Token.isNot(MIToken::GlobalValue) && | |||
2173 | Token.isNot(MIToken::NamedGlobalValue)) | |||
2174 | return error("expected a global value"); | |||
2175 | GlobalValue *GV = nullptr; | |||
2176 | if (parseGlobalValue(GV)) | |||
2177 | return true; | |||
2178 | auto *F = dyn_cast<Function>(GV); | |||
2179 | if (!F) | |||
2180 | return error("expected an IR function reference"); | |||
2181 | lex(); | |||
2182 | if (expectAndConsume(MIToken::comma)) | |||
2183 | return true; | |||
2184 | BasicBlock *BB = nullptr; | |||
2185 | if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock)) | |||
2186 | return error("expected an IR block reference"); | |||
2187 | if (parseIRBlock(BB, *F)) | |||
2188 | return true; | |||
2189 | lex(); | |||
2190 | if (expectAndConsume(MIToken::rparen)) | |||
2191 | return true; | |||
2192 | Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0); | |||
2193 | if (parseOperandsOffset(Dest)) | |||
2194 | return true; | |||
2195 | return false; | |||
2196 | } | |||
2197 | ||||
2198 | bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) { | |||
2199 | assert(Token.is(MIToken::kw_intrinsic))((Token.is(MIToken::kw_intrinsic)) ? static_cast<void> ( 0) : __assert_fail ("Token.is(MIToken::kw_intrinsic)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2199, __PRETTY_FUNCTION__)); | |||
2200 | lex(); | |||
2201 | if (expectAndConsume(MIToken::lparen)) | |||
2202 | return error("expected syntax intrinsic(@llvm.whatever)"); | |||
2203 | ||||
2204 | if (Token.isNot(MIToken::NamedGlobalValue)) | |||
2205 | return error("expected syntax intrinsic(@llvm.whatever)"); | |||
2206 | ||||
2207 | std::string Name = Token.stringValue(); | |||
2208 | lex(); | |||
2209 | ||||
2210 | if (expectAndConsume(MIToken::rparen)) | |||
2211 | return error("expected ')' to terminate intrinsic name"); | |||
2212 | ||||
2213 | // Find out what intrinsic we're dealing with, first try the global namespace | |||
2214 | // and then the target's private intrinsics if that fails. | |||
2215 | const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo(); | |||
2216 | Intrinsic::ID ID = Function::lookupIntrinsicID(Name); | |||
2217 | if (ID == Intrinsic::not_intrinsic && TII) | |||
2218 | ID = static_cast<Intrinsic::ID>(TII->lookupName(Name)); | |||
2219 | ||||
2220 | if (ID == Intrinsic::not_intrinsic) | |||
2221 | return error("unknown intrinsic name"); | |||
2222 | Dest = MachineOperand::CreateIntrinsicID(ID); | |||
2223 | ||||
2224 | return false; | |||
2225 | } | |||
2226 | ||||
2227 | bool MIParser::parsePredicateOperand(MachineOperand &Dest) { | |||
2228 | assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred))((Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred )) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred)" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2228, __PRETTY_FUNCTION__)); | |||
2229 | bool IsFloat = Token.is(MIToken::kw_floatpred); | |||
2230 | lex(); | |||
2231 | ||||
2232 | if (expectAndConsume(MIToken::lparen)) | |||
2233 | return error("expected syntax intpred(whatever) or floatpred(whatever"); | |||
2234 | ||||
2235 | if (Token.isNot(MIToken::Identifier)) | |||
2236 | return error("whatever"); | |||
2237 | ||||
2238 | CmpInst::Predicate Pred; | |||
2239 | if (IsFloat) { | |||
2240 | Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue()) | |||
2241 | .Case("false", CmpInst::FCMP_FALSE) | |||
2242 | .Case("oeq", CmpInst::FCMP_OEQ) | |||
2243 | .Case("ogt", CmpInst::FCMP_OGT) | |||
2244 | .Case("oge", CmpInst::FCMP_OGE) | |||
2245 | .Case("olt", CmpInst::FCMP_OLT) | |||
2246 | .Case("ole", CmpInst::FCMP_OLE) | |||
2247 | .Case("one", CmpInst::FCMP_ONE) | |||
2248 | .Case("ord", CmpInst::FCMP_ORD) | |||
2249 | .Case("uno", CmpInst::FCMP_UNO) | |||
2250 | .Case("ueq", CmpInst::FCMP_UEQ) | |||
2251 | .Case("ugt", CmpInst::FCMP_UGT) | |||
2252 | .Case("uge", CmpInst::FCMP_UGE) | |||
2253 | .Case("ult", CmpInst::FCMP_ULT) | |||
2254 | .Case("ule", CmpInst::FCMP_ULE) | |||
2255 | .Case("une", CmpInst::FCMP_UNE) | |||
2256 | .Case("true", CmpInst::FCMP_TRUE) | |||
2257 | .Default(CmpInst::BAD_FCMP_PREDICATE); | |||
2258 | if (!CmpInst::isFPPredicate(Pred)) | |||
2259 | return error("invalid floating-point predicate"); | |||
2260 | } else { | |||
2261 | Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue()) | |||
2262 | .Case("eq", CmpInst::ICMP_EQ) | |||
2263 | .Case("ne", CmpInst::ICMP_NE) | |||
2264 | .Case("sgt", CmpInst::ICMP_SGT) | |||
2265 | .Case("sge", CmpInst::ICMP_SGE) | |||
2266 | .Case("slt", CmpInst::ICMP_SLT) | |||
2267 | .Case("sle", CmpInst::ICMP_SLE) | |||
2268 | .Case("ugt", CmpInst::ICMP_UGT) | |||
2269 | .Case("uge", CmpInst::ICMP_UGE) | |||
2270 | .Case("ult", CmpInst::ICMP_ULT) | |||
2271 | .Case("ule", CmpInst::ICMP_ULE) | |||
2272 | .Default(CmpInst::BAD_ICMP_PREDICATE); | |||
2273 | if (!CmpInst::isIntPredicate(Pred)) | |||
2274 | return error("invalid integer predicate"); | |||
2275 | } | |||
2276 | ||||
2277 | lex(); | |||
2278 | Dest = MachineOperand::CreatePredicate(Pred); | |||
2279 | if (expectAndConsume(MIToken::rparen)) | |||
2280 | return error("predicate should be terminated by ')'."); | |||
2281 | ||||
2282 | return false; | |||
2283 | } | |||
2284 | ||||
2285 | bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) { | |||
2286 | assert(Token.is(MIToken::kw_target_index))((Token.is(MIToken::kw_target_index)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::kw_target_index)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2286, __PRETTY_FUNCTION__)); | |||
2287 | lex(); | |||
2288 | if (expectAndConsume(MIToken::lparen)) | |||
2289 | return true; | |||
2290 | if (Token.isNot(MIToken::Identifier)) | |||
2291 | return error("expected the name of the target index"); | |||
2292 | int Index = 0; | |||
2293 | if (PFS.Target.getTargetIndex(Token.stringValue(), Index)) | |||
2294 | return error("use of undefined target index '" + Token.stringValue() + "'"); | |||
2295 | lex(); | |||
2296 | if (expectAndConsume(MIToken::rparen)) | |||
2297 | return true; | |||
2298 | Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0); | |||
2299 | if (parseOperandsOffset(Dest)) | |||
2300 | return true; | |||
2301 | return false; | |||
2302 | } | |||
2303 | ||||
2304 | bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) { | |||
2305 | assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask")((Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask" ) ? static_cast<void> (0) : __assert_fail ("Token.stringValue() == \"CustomRegMask\" && \"Expected a custom RegMask\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2305, __PRETTY_FUNCTION__)); | |||
2306 | lex(); | |||
2307 | if (expectAndConsume(MIToken::lparen)) | |||
2308 | return true; | |||
2309 | ||||
2310 | uint32_t *Mask = MF.allocateRegMask(); | |||
2311 | while (true) { | |||
2312 | if (Token.isNot(MIToken::NamedRegister)) | |||
2313 | return error("expected a named register"); | |||
2314 | unsigned Reg; | |||
2315 | if (parseNamedRegister(Reg)) | |||
2316 | return true; | |||
2317 | lex(); | |||
2318 | Mask[Reg / 32] |= 1U << (Reg % 32); | |||
2319 | // TODO: Report an error if the same register is used more than once. | |||
2320 | if (Token.isNot(MIToken::comma)) | |||
2321 | break; | |||
2322 | lex(); | |||
2323 | } | |||
2324 | ||||
2325 | if (expectAndConsume(MIToken::rparen)) | |||
2326 | return true; | |||
2327 | Dest = MachineOperand::CreateRegMask(Mask); | |||
2328 | return false; | |||
2329 | } | |||
2330 | ||||
2331 | bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) { | |||
2332 | assert(Token.is(MIToken::kw_liveout))((Token.is(MIToken::kw_liveout)) ? static_cast<void> (0 ) : __assert_fail ("Token.is(MIToken::kw_liveout)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2332, __PRETTY_FUNCTION__)); | |||
2333 | uint32_t *Mask = MF.allocateRegMask(); | |||
2334 | lex(); | |||
2335 | if (expectAndConsume(MIToken::lparen)) | |||
2336 | return true; | |||
2337 | while (true) { | |||
2338 | if (Token.isNot(MIToken::NamedRegister)) | |||
2339 | return error("expected a named register"); | |||
2340 | unsigned Reg; | |||
2341 | if (parseNamedRegister(Reg)) | |||
2342 | return true; | |||
2343 | lex(); | |||
2344 | Mask[Reg / 32] |= 1U << (Reg % 32); | |||
2345 | // TODO: Report an error if the same register is used more than once. | |||
2346 | if (Token.isNot(MIToken::comma)) | |||
2347 | break; | |||
2348 | lex(); | |||
2349 | } | |||
2350 | if (expectAndConsume(MIToken::rparen)) | |||
2351 | return true; | |||
2352 | Dest = MachineOperand::CreateRegLiveOut(Mask); | |||
2353 | return false; | |||
2354 | } | |||
2355 | ||||
2356 | bool MIParser::parseMachineOperand(MachineOperand &Dest, | |||
2357 | Optional<unsigned> &TiedDefIdx) { | |||
2358 | switch (Token.kind()) { | |||
2359 | case MIToken::kw_implicit: | |||
2360 | case MIToken::kw_implicit_define: | |||
2361 | case MIToken::kw_def: | |||
2362 | case MIToken::kw_dead: | |||
2363 | case MIToken::kw_killed: | |||
2364 | case MIToken::kw_undef: | |||
2365 | case MIToken::kw_internal: | |||
2366 | case MIToken::kw_early_clobber: | |||
2367 | case MIToken::kw_debug_use: | |||
2368 | case MIToken::kw_renamable: | |||
2369 | case MIToken::underscore: | |||
2370 | case MIToken::NamedRegister: | |||
2371 | case MIToken::VirtualRegister: | |||
2372 | case MIToken::NamedVirtualRegister: | |||
2373 | return parseRegisterOperand(Dest, TiedDefIdx); | |||
2374 | case MIToken::IntegerLiteral: | |||
2375 | return parseImmediateOperand(Dest); | |||
2376 | case MIToken::kw_half: | |||
2377 | case MIToken::kw_float: | |||
2378 | case MIToken::kw_double: | |||
2379 | case MIToken::kw_x86_fp80: | |||
2380 | case MIToken::kw_fp128: | |||
2381 | case MIToken::kw_ppc_fp128: | |||
2382 | return parseFPImmediateOperand(Dest); | |||
2383 | case MIToken::MachineBasicBlock: | |||
2384 | return parseMBBOperand(Dest); | |||
2385 | case MIToken::StackObject: | |||
2386 | return parseStackObjectOperand(Dest); | |||
2387 | case MIToken::FixedStackObject: | |||
2388 | return parseFixedStackObjectOperand(Dest); | |||
2389 | case MIToken::GlobalValue: | |||
2390 | case MIToken::NamedGlobalValue: | |||
2391 | return parseGlobalAddressOperand(Dest); | |||
2392 | case MIToken::ConstantPoolItem: | |||
2393 | return parseConstantPoolIndexOperand(Dest); | |||
2394 | case MIToken::JumpTableIndex: | |||
2395 | return parseJumpTableIndexOperand(Dest); | |||
2396 | case MIToken::ExternalSymbol: | |||
2397 | return parseExternalSymbolOperand(Dest); | |||
2398 | case MIToken::MCSymbol: | |||
2399 | return parseMCSymbolOperand(Dest); | |||
2400 | case MIToken::SubRegisterIndex: | |||
2401 | return parseSubRegisterIndexOperand(Dest); | |||
2402 | case MIToken::md_diexpr: | |||
2403 | case MIToken::exclaim: | |||
2404 | return parseMetadataOperand(Dest); | |||
2405 | case MIToken::kw_cfi_same_value: | |||
2406 | case MIToken::kw_cfi_offset: | |||
2407 | case MIToken::kw_cfi_rel_offset: | |||
2408 | case MIToken::kw_cfi_def_cfa_register: | |||
2409 | case MIToken::kw_cfi_def_cfa_offset: | |||
2410 | case MIToken::kw_cfi_adjust_cfa_offset: | |||
2411 | case MIToken::kw_cfi_escape: | |||
2412 | case MIToken::kw_cfi_def_cfa: | |||
2413 | case MIToken::kw_cfi_register: | |||
2414 | case MIToken::kw_cfi_remember_state: | |||
2415 | case MIToken::kw_cfi_restore: | |||
2416 | case MIToken::kw_cfi_restore_state: | |||
2417 | case MIToken::kw_cfi_undefined: | |||
2418 | case MIToken::kw_cfi_window_save: | |||
2419 | case MIToken::kw_cfi_aarch64_negate_ra_sign_state: | |||
2420 | return parseCFIOperand(Dest); | |||
2421 | case MIToken::kw_blockaddress: | |||
2422 | return parseBlockAddressOperand(Dest); | |||
2423 | case MIToken::kw_intrinsic: | |||
2424 | return parseIntrinsicOperand(Dest); | |||
2425 | case MIToken::kw_target_index: | |||
2426 | return parseTargetIndexOperand(Dest); | |||
2427 | case MIToken::kw_liveout: | |||
2428 | return parseLiveoutRegisterMaskOperand(Dest); | |||
2429 | case MIToken::kw_floatpred: | |||
2430 | case MIToken::kw_intpred: | |||
2431 | return parsePredicateOperand(Dest); | |||
2432 | case MIToken::Error: | |||
2433 | return true; | |||
2434 | case MIToken::Identifier: | |||
2435 | if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) { | |||
2436 | Dest = MachineOperand::CreateRegMask(RegMask); | |||
2437 | lex(); | |||
2438 | break; | |||
2439 | } else if (Token.stringValue() == "CustomRegMask") { | |||
2440 | return parseCustomRegisterMaskOperand(Dest); | |||
2441 | } else | |||
2442 | return parseTypedImmediateOperand(Dest); | |||
2443 | default: | |||
2444 | // FIXME: Parse the MCSymbol machine operand. | |||
2445 | return error("expected a machine operand"); | |||
2446 | } | |||
2447 | return false; | |||
2448 | } | |||
2449 | ||||
2450 | bool MIParser::parseMachineOperandAndTargetFlags( | |||
2451 | MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) { | |||
2452 | unsigned TF = 0; | |||
2453 | bool HasTargetFlags = false; | |||
2454 | if (Token.is(MIToken::kw_target_flags)) { | |||
2455 | HasTargetFlags = true; | |||
2456 | lex(); | |||
2457 | if (expectAndConsume(MIToken::lparen)) | |||
2458 | return true; | |||
2459 | if (Token.isNot(MIToken::Identifier)) | |||
2460 | return error("expected the name of the target flag"); | |||
2461 | if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) { | |||
2462 | if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF)) | |||
2463 | return error("use of undefined target flag '" + Token.stringValue() + | |||
2464 | "'"); | |||
2465 | } | |||
2466 | lex(); | |||
2467 | while (Token.is(MIToken::comma)) { | |||
2468 | lex(); | |||
2469 | if (Token.isNot(MIToken::Identifier)) | |||
2470 | return error("expected the name of the target flag"); | |||
2471 | unsigned BitFlag = 0; | |||
2472 | if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag)) | |||
2473 | return error("use of undefined target flag '" + Token.stringValue() + | |||
2474 | "'"); | |||
2475 | // TODO: Report an error when using a duplicate bit target flag. | |||
2476 | TF |= BitFlag; | |||
2477 | lex(); | |||
2478 | } | |||
2479 | if (expectAndConsume(MIToken::rparen)) | |||
2480 | return true; | |||
2481 | } | |||
2482 | auto Loc = Token.location(); | |||
2483 | if (parseMachineOperand(Dest, TiedDefIdx)) | |||
2484 | return true; | |||
2485 | if (!HasTargetFlags) | |||
2486 | return false; | |||
2487 | if (Dest.isReg()) | |||
2488 | return error(Loc, "register operands can't have target flags"); | |||
2489 | Dest.setTargetFlags(TF); | |||
2490 | return false; | |||
2491 | } | |||
2492 | ||||
2493 | bool MIParser::parseOffset(int64_t &Offset) { | |||
2494 | if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus)) | |||
2495 | return false; | |||
2496 | StringRef Sign = Token.range(); | |||
2497 | bool IsNegative = Token.is(MIToken::minus); | |||
2498 | lex(); | |||
2499 | if (Token.isNot(MIToken::IntegerLiteral)) | |||
2500 | return error("expected an integer literal after '" + Sign + "'"); | |||
2501 | if (Token.integerValue().getMinSignedBits() > 64) | |||
2502 | return error("expected 64-bit integer (too large)"); | |||
2503 | Offset = Token.integerValue().getExtValue(); | |||
2504 | if (IsNegative) | |||
2505 | Offset = -Offset; | |||
2506 | lex(); | |||
2507 | return false; | |||
2508 | } | |||
2509 | ||||
2510 | bool MIParser::parseAlignment(unsigned &Alignment) { | |||
2511 | assert(Token.is(MIToken::kw_align))((Token.is(MIToken::kw_align)) ? static_cast<void> (0) : __assert_fail ("Token.is(MIToken::kw_align)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2511, __PRETTY_FUNCTION__)); | |||
2512 | lex(); | |||
2513 | if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) | |||
2514 | return error("expected an integer literal after 'align'"); | |||
2515 | if (getUnsigned(Alignment)) | |||
2516 | return true; | |||
2517 | lex(); | |||
2518 | ||||
2519 | if (!isPowerOf2_32(Alignment)) | |||
2520 | return error("expected a power-of-2 literal after 'align'"); | |||
2521 | ||||
2522 | return false; | |||
2523 | } | |||
2524 | ||||
2525 | bool MIParser::parseAddrspace(unsigned &Addrspace) { | |||
2526 | assert(Token.is(MIToken::kw_addrspace))((Token.is(MIToken::kw_addrspace)) ? static_cast<void> ( 0) : __assert_fail ("Token.is(MIToken::kw_addrspace)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2526, __PRETTY_FUNCTION__)); | |||
2527 | lex(); | |||
2528 | if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned()) | |||
2529 | return error("expected an integer literal after 'addrspace'"); | |||
2530 | if (getUnsigned(Addrspace)) | |||
2531 | return true; | |||
2532 | lex(); | |||
2533 | return false; | |||
2534 | } | |||
2535 | ||||
2536 | bool MIParser::parseOperandsOffset(MachineOperand &Op) { | |||
2537 | int64_t Offset = 0; | |||
2538 | if (parseOffset(Offset)) | |||
2539 | return true; | |||
2540 | Op.setOffset(Offset); | |||
2541 | return false; | |||
2542 | } | |||
2543 | ||||
2544 | bool MIParser::parseIRValue(const Value *&V) { | |||
2545 | switch (Token.kind()) { | |||
2546 | case MIToken::NamedIRValue: { | |||
2547 | V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue()); | |||
2548 | break; | |||
2549 | } | |||
2550 | case MIToken::IRValue: { | |||
2551 | unsigned SlotNumber = 0; | |||
2552 | if (getUnsigned(SlotNumber)) | |||
2553 | return true; | |||
2554 | V = getIRValue(SlotNumber); | |||
2555 | break; | |||
2556 | } | |||
2557 | case MIToken::NamedGlobalValue: | |||
2558 | case MIToken::GlobalValue: { | |||
2559 | GlobalValue *GV = nullptr; | |||
2560 | if (parseGlobalValue(GV)) | |||
2561 | return true; | |||
2562 | V = GV; | |||
2563 | break; | |||
2564 | } | |||
2565 | case MIToken::QuotedIRValue: { | |||
2566 | const Constant *C = nullptr; | |||
2567 | if (parseIRConstant(Token.location(), Token.stringValue(), C)) | |||
2568 | return true; | |||
2569 | V = C; | |||
2570 | break; | |||
2571 | } | |||
2572 | default: | |||
2573 | llvm_unreachable("The current token should be an IR block reference")::llvm::llvm_unreachable_internal("The current token should be an IR block reference" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2573); | |||
2574 | } | |||
2575 | if (!V) | |||
2576 | return error(Twine("use of undefined IR value '") + Token.range() + "'"); | |||
2577 | return false; | |||
2578 | } | |||
2579 | ||||
2580 | bool MIParser::getUint64(uint64_t &Result) { | |||
2581 | if (Token.hasIntegerValue()) { | |||
2582 | if (Token.integerValue().getActiveBits() > 64) | |||
2583 | return error("expected 64-bit integer (too large)"); | |||
2584 | Result = Token.integerValue().getZExtValue(); | |||
2585 | return false; | |||
2586 | } | |||
2587 | if (Token.is(MIToken::HexLiteral)) { | |||
2588 | APInt A; | |||
2589 | if (getHexUint(A)) | |||
2590 | return true; | |||
2591 | if (A.getBitWidth() > 64) | |||
2592 | return error("expected 64-bit integer (too large)"); | |||
2593 | Result = A.getZExtValue(); | |||
2594 | return false; | |||
2595 | } | |||
2596 | return true; | |||
2597 | } | |||
2598 | ||||
2599 | bool MIParser::getHexUint(APInt &Result) { | |||
2600 | assert(Token.is(MIToken::HexLiteral))((Token.is(MIToken::HexLiteral)) ? static_cast<void> (0 ) : __assert_fail ("Token.is(MIToken::HexLiteral)", "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2600, __PRETTY_FUNCTION__)); | |||
2601 | StringRef S = Token.range(); | |||
2602 | assert(S[0] == '0' && tolower(S[1]) == 'x')((S[0] == '0' && tolower(S[1]) == 'x') ? static_cast< void> (0) : __assert_fail ("S[0] == '0' && tolower(S[1]) == 'x'" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2602, __PRETTY_FUNCTION__)); | |||
2603 | // This could be a floating point literal with a special prefix. | |||
2604 | if (!isxdigit(S[2])) | |||
2605 | return true; | |||
2606 | StringRef V = S.substr(2); | |||
2607 | APInt A(V.size()*4, V, 16); | |||
2608 | ||||
2609 | // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make | |||
2610 | // sure it isn't the case before constructing result. | |||
2611 | unsigned NumBits = (A == 0) ? 32 : A.getActiveBits(); | |||
2612 | Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords())); | |||
2613 | return false; | |||
2614 | } | |||
2615 | ||||
2616 | bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) { | |||
2617 | const auto OldFlags = Flags; | |||
2618 | switch (Token.kind()) { | |||
2619 | case MIToken::kw_volatile: | |||
2620 | Flags |= MachineMemOperand::MOVolatile; | |||
2621 | break; | |||
2622 | case MIToken::kw_non_temporal: | |||
2623 | Flags |= MachineMemOperand::MONonTemporal; | |||
2624 | break; | |||
2625 | case MIToken::kw_dereferenceable: | |||
2626 | Flags |= MachineMemOperand::MODereferenceable; | |||
2627 | break; | |||
2628 | case MIToken::kw_invariant: | |||
2629 | Flags |= MachineMemOperand::MOInvariant; | |||
2630 | break; | |||
2631 | case MIToken::StringConstant: { | |||
2632 | MachineMemOperand::Flags TF; | |||
2633 | if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF)) | |||
2634 | return error("use of undefined target MMO flag '" + Token.stringValue() + | |||
2635 | "'"); | |||
2636 | Flags |= TF; | |||
2637 | break; | |||
2638 | } | |||
2639 | default: | |||
2640 | llvm_unreachable("The current token should be a memory operand flag")::llvm::llvm_unreachable_internal("The current token should be a memory operand flag" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2640); | |||
2641 | } | |||
2642 | if (OldFlags == Flags) | |||
2643 | // We know that the same flag is specified more than once when the flags | |||
2644 | // weren't modified. | |||
2645 | return error("duplicate '" + Token.stringValue() + "' memory operand flag"); | |||
2646 | lex(); | |||
2647 | return false; | |||
2648 | } | |||
2649 | ||||
2650 | bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) { | |||
2651 | switch (Token.kind()) { | |||
2652 | case MIToken::kw_stack: | |||
2653 | PSV = MF.getPSVManager().getStack(); | |||
2654 | break; | |||
2655 | case MIToken::kw_got: | |||
2656 | PSV = MF.getPSVManager().getGOT(); | |||
2657 | break; | |||
2658 | case MIToken::kw_jump_table: | |||
2659 | PSV = MF.getPSVManager().getJumpTable(); | |||
2660 | break; | |||
2661 | case MIToken::kw_constant_pool: | |||
2662 | PSV = MF.getPSVManager().getConstantPool(); | |||
2663 | break; | |||
2664 | case MIToken::FixedStackObject: { | |||
2665 | int FI; | |||
2666 | if (parseFixedStackFrameIndex(FI)) | |||
2667 | return true; | |||
2668 | PSV = MF.getPSVManager().getFixedStack(FI); | |||
2669 | // The token was already consumed, so use return here instead of break. | |||
2670 | return false; | |||
2671 | } | |||
2672 | case MIToken::StackObject: { | |||
2673 | int FI; | |||
2674 | if (parseStackFrameIndex(FI)) | |||
2675 | return true; | |||
2676 | PSV = MF.getPSVManager().getFixedStack(FI); | |||
2677 | // The token was already consumed, so use return here instead of break. | |||
2678 | return false; | |||
2679 | } | |||
2680 | case MIToken::kw_call_entry: | |||
2681 | lex(); | |||
2682 | switch (Token.kind()) { | |||
2683 | case MIToken::GlobalValue: | |||
2684 | case MIToken::NamedGlobalValue: { | |||
2685 | GlobalValue *GV = nullptr; | |||
2686 | if (parseGlobalValue(GV)) | |||
2687 | return true; | |||
2688 | PSV = MF.getPSVManager().getGlobalValueCallEntry(GV); | |||
2689 | break; | |||
2690 | } | |||
2691 | case MIToken::ExternalSymbol: | |||
2692 | PSV = MF.getPSVManager().getExternalSymbolCallEntry( | |||
2693 | MF.createExternalSymbolName(Token.stringValue())); | |||
2694 | break; | |||
2695 | default: | |||
2696 | return error( | |||
2697 | "expected a global value or an external symbol after 'call-entry'"); | |||
2698 | } | |||
2699 | break; | |||
2700 | default: | |||
2701 | llvm_unreachable("The current token should be pseudo source value")::llvm::llvm_unreachable_internal("The current token should be pseudo source value" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2701); | |||
2702 | } | |||
2703 | lex(); | |||
2704 | return false; | |||
2705 | } | |||
2706 | ||||
2707 | bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) { | |||
2708 | if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) || | |||
2709 | Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) || | |||
2710 | Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) || | |||
2711 | Token.is(MIToken::kw_call_entry)) { | |||
2712 | const PseudoSourceValue *PSV = nullptr; | |||
2713 | if (parseMemoryPseudoSourceValue(PSV)) | |||
2714 | return true; | |||
2715 | int64_t Offset = 0; | |||
2716 | if (parseOffset(Offset)) | |||
2717 | return true; | |||
2718 | Dest = MachinePointerInfo(PSV, Offset); | |||
2719 | return false; | |||
2720 | } | |||
2721 | if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) && | |||
2722 | Token.isNot(MIToken::GlobalValue) && | |||
2723 | Token.isNot(MIToken::NamedGlobalValue) && | |||
2724 | Token.isNot(MIToken::QuotedIRValue)) | |||
2725 | return error("expected an IR value reference"); | |||
2726 | const Value *V = nullptr; | |||
2727 | if (parseIRValue(V)) | |||
2728 | return true; | |||
2729 | if (!V->getType()->isPointerTy()) | |||
2730 | return error("expected a pointer IR value"); | |||
2731 | lex(); | |||
2732 | int64_t Offset = 0; | |||
2733 | if (parseOffset(Offset)) | |||
2734 | return true; | |||
2735 | Dest = MachinePointerInfo(V, Offset); | |||
2736 | return false; | |||
2737 | } | |||
2738 | ||||
2739 | bool MIParser::parseOptionalScope(LLVMContext &Context, | |||
2740 | SyncScope::ID &SSID) { | |||
2741 | SSID = SyncScope::System; | |||
2742 | if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") { | |||
2743 | lex(); | |||
2744 | if (expectAndConsume(MIToken::lparen)) | |||
2745 | return error("expected '(' in syncscope"); | |||
2746 | ||||
2747 | std::string SSN; | |||
2748 | if (parseStringConstant(SSN)) | |||
2749 | return true; | |||
2750 | ||||
2751 | SSID = Context.getOrInsertSyncScopeID(SSN); | |||
2752 | if (expectAndConsume(MIToken::rparen)) | |||
2753 | return error("expected ')' in syncscope"); | |||
2754 | } | |||
2755 | ||||
2756 | return false; | |||
2757 | } | |||
2758 | ||||
2759 | bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) { | |||
2760 | Order = AtomicOrdering::NotAtomic; | |||
2761 | if (Token.isNot(MIToken::Identifier)) | |||
2762 | return false; | |||
2763 | ||||
2764 | Order = StringSwitch<AtomicOrdering>(Token.stringValue()) | |||
2765 | .Case("unordered", AtomicOrdering::Unordered) | |||
2766 | .Case("monotonic", AtomicOrdering::Monotonic) | |||
2767 | .Case("acquire", AtomicOrdering::Acquire) | |||
2768 | .Case("release", AtomicOrdering::Release) | |||
2769 | .Case("acq_rel", AtomicOrdering::AcquireRelease) | |||
2770 | .Case("seq_cst", AtomicOrdering::SequentiallyConsistent) | |||
2771 | .Default(AtomicOrdering::NotAtomic); | |||
2772 | ||||
2773 | if (Order != AtomicOrdering::NotAtomic) { | |||
2774 | lex(); | |||
2775 | return false; | |||
2776 | } | |||
2777 | ||||
2778 | return error("expected an atomic scope, ordering or a size specification"); | |||
2779 | } | |||
2780 | ||||
2781 | bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) { | |||
2782 | if (expectAndConsume(MIToken::lparen)) | |||
2783 | return true; | |||
2784 | MachineMemOperand::Flags Flags = MachineMemOperand::MONone; | |||
2785 | while (Token.isMemoryOperandFlag()) { | |||
2786 | if (parseMemoryOperandFlag(Flags)) | |||
2787 | return true; | |||
2788 | } | |||
2789 | if (Token.isNot(MIToken::Identifier) || | |||
2790 | (Token.stringValue() != "load" && Token.stringValue() != "store")) | |||
2791 | return error("expected 'load' or 'store' memory operation"); | |||
2792 | if (Token.stringValue() == "load") | |||
2793 | Flags |= MachineMemOperand::MOLoad; | |||
2794 | else | |||
2795 | Flags |= MachineMemOperand::MOStore; | |||
2796 | lex(); | |||
2797 | ||||
2798 | // Optional 'store' for operands that both load and store. | |||
2799 | if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") { | |||
2800 | Flags |= MachineMemOperand::MOStore; | |||
2801 | lex(); | |||
2802 | } | |||
2803 | ||||
2804 | // Optional synchronization scope. | |||
2805 | SyncScope::ID SSID; | |||
2806 | if (parseOptionalScope(MF.getFunction().getContext(), SSID)) | |||
2807 | return true; | |||
2808 | ||||
2809 | // Up to two atomic orderings (cmpxchg provides guarantees on failure). | |||
2810 | AtomicOrdering Order, FailureOrder; | |||
2811 | if (parseOptionalAtomicOrdering(Order)) | |||
2812 | return true; | |||
2813 | ||||
2814 | if (parseOptionalAtomicOrdering(FailureOrder)) | |||
2815 | return true; | |||
2816 | ||||
2817 | if (Token.isNot(MIToken::IntegerLiteral) && | |||
2818 | Token.isNot(MIToken::kw_unknown_size)) | |||
2819 | return error("expected the size integer literal or 'unknown-size' after " | |||
2820 | "memory operation"); | |||
2821 | uint64_t Size; | |||
2822 | if (Token.is(MIToken::IntegerLiteral)) { | |||
2823 | if (getUint64(Size)) | |||
2824 | return true; | |||
2825 | } else if (Token.is(MIToken::kw_unknown_size)) { | |||
2826 | Size = MemoryLocation::UnknownSize; | |||
2827 | } | |||
2828 | lex(); | |||
2829 | ||||
2830 | MachinePointerInfo Ptr = MachinePointerInfo(); | |||
2831 | if (Token.is(MIToken::Identifier)) { | |||
2832 | const char *Word = | |||
2833 | ((Flags & MachineMemOperand::MOLoad) && | |||
2834 | (Flags & MachineMemOperand::MOStore)) | |||
2835 | ? "on" | |||
2836 | : Flags & MachineMemOperand::MOLoad ? "from" : "into"; | |||
2837 | if (Token.stringValue() != Word) | |||
2838 | return error(Twine("expected '") + Word + "'"); | |||
2839 | lex(); | |||
2840 | ||||
2841 | if (parseMachinePointerInfo(Ptr)) | |||
2842 | return true; | |||
2843 | } | |||
2844 | unsigned BaseAlignment = (Size != MemoryLocation::UnknownSize ? Size : 1); | |||
| ||||
2845 | AAMDNodes AAInfo; | |||
2846 | MDNode *Range = nullptr; | |||
2847 | while (consumeIfPresent(MIToken::comma)) { | |||
2848 | switch (Token.kind()) { | |||
2849 | case MIToken::kw_align: | |||
2850 | if (parseAlignment(BaseAlignment)) | |||
2851 | return true; | |||
2852 | break; | |||
2853 | case MIToken::kw_addrspace: | |||
2854 | if (parseAddrspace(Ptr.AddrSpace)) | |||
2855 | return true; | |||
2856 | break; | |||
2857 | case MIToken::md_tbaa: | |||
2858 | lex(); | |||
2859 | if (parseMDNode(AAInfo.TBAA)) | |||
2860 | return true; | |||
2861 | break; | |||
2862 | case MIToken::md_alias_scope: | |||
2863 | lex(); | |||
2864 | if (parseMDNode(AAInfo.Scope)) | |||
2865 | return true; | |||
2866 | break; | |||
2867 | case MIToken::md_noalias: | |||
2868 | lex(); | |||
2869 | if (parseMDNode(AAInfo.NoAlias)) | |||
2870 | return true; | |||
2871 | break; | |||
2872 | case MIToken::md_range: | |||
2873 | lex(); | |||
2874 | if (parseMDNode(Range)) | |||
2875 | return true; | |||
2876 | break; | |||
2877 | // TODO: Report an error on duplicate metadata nodes. | |||
2878 | default: | |||
2879 | return error("expected 'align' or '!tbaa' or '!alias.scope' or " | |||
2880 | "'!noalias' or '!range'"); | |||
2881 | } | |||
2882 | } | |||
2883 | if (expectAndConsume(MIToken::rparen)) | |||
2884 | return true; | |||
2885 | Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range, | |||
2886 | SSID, Order, FailureOrder); | |||
2887 | return false; | |||
2888 | } | |||
2889 | ||||
2890 | bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) { | |||
2891 | assert((Token.is(MIToken::kw_pre_instr_symbol) ||(((Token.is(MIToken::kw_pre_instr_symbol) || Token.is(MIToken ::kw_post_instr_symbol)) && "Invalid token for a pre- post-instruction symbol!" ) ? static_cast<void> (0) : __assert_fail ("(Token.is(MIToken::kw_pre_instr_symbol) || Token.is(MIToken::kw_post_instr_symbol)) && \"Invalid token for a pre- post-instruction symbol!\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2893, __PRETTY_FUNCTION__)) | |||
2892 | Token.is(MIToken::kw_post_instr_symbol)) &&(((Token.is(MIToken::kw_pre_instr_symbol) || Token.is(MIToken ::kw_post_instr_symbol)) && "Invalid token for a pre- post-instruction symbol!" ) ? static_cast<void> (0) : __assert_fail ("(Token.is(MIToken::kw_pre_instr_symbol) || Token.is(MIToken::kw_post_instr_symbol)) && \"Invalid token for a pre- post-instruction symbol!\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2893, __PRETTY_FUNCTION__)) | |||
2893 | "Invalid token for a pre- post-instruction symbol!")(((Token.is(MIToken::kw_pre_instr_symbol) || Token.is(MIToken ::kw_post_instr_symbol)) && "Invalid token for a pre- post-instruction symbol!" ) ? static_cast<void> (0) : __assert_fail ("(Token.is(MIToken::kw_pre_instr_symbol) || Token.is(MIToken::kw_post_instr_symbol)) && \"Invalid token for a pre- post-instruction symbol!\"" , "/build/llvm-toolchain-snapshot-9~svn360825/lib/CodeGen/MIRParser/MIParser.cpp" , 2893, __PRETTY_FUNCTION__)); | |||
2894 | lex(); | |||
2895 | if (Token.isNot(MIToken::MCSymbol)) | |||
2896 | return error("expected a symbol after 'pre-instr-symbol'"); | |||
2897 | Symbol = getOrCreateMCSymbol(Token.stringValue()); | |||
2898 | lex(); | |||
2899 | if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) || | |||
2900 | Token.is(MIToken::lbrace)) | |||
2901 | return false; | |||
2902 | if (Token.isNot(MIToken::comma)) | |||
2903 | return error("expected ',' before the next machine operand"); | |||
2904 | lex(); | |||
2905 | return false; | |||
2906 | } | |||
2907 | ||||
2908 | static void initSlots2BasicBlocks( | |||
2909 | const Function &F, | |||
2910 | DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { | |||
2911 | ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); | |||
2912 | MST.incorporateFunction(F); | |||
2913 | for (auto &BB : F) { | |||
2914 | if (BB.hasName()) | |||
2915 | continue; | |||
2916 | int Slot = MST.getLocalSlot(&BB); | |||
2917 | if (Slot == -1) | |||
2918 | continue; | |||
2919 | Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB)); | |||
2920 | } | |||
2921 | } | |||
2922 | ||||
2923 | static const BasicBlock *getIRBlockFromSlot( | |||
2924 | unsigned Slot, | |||
2925 | const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { | |||
2926 | auto BlockInfo = Slots2BasicBlocks.find(Slot); | |||
2927 | if (BlockInfo == Slots2BasicBlocks.end()) | |||
2928 | return nullptr; | |||
2929 | return BlockInfo->second; | |||
2930 | } | |||
2931 | ||||
2932 | const BasicBlock *MIParser::getIRBlock(unsigned Slot) { | |||
2933 | if (Slots2BasicBlocks.empty()) | |||
2934 | initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks); | |||
2935 | return getIRBlockFromSlot(Slot, Slots2BasicBlocks); | |||
2936 | } | |||
2937 | ||||
2938 | const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) { | |||
2939 | if (&F == &MF.getFunction()) | |||
2940 | return getIRBlock(Slot); | |||
2941 | DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks; | |||
2942 | initSlots2BasicBlocks(F, CustomSlots2BasicBlocks); | |||
2943 | return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks); | |||
2944 | } | |||
2945 | ||||
2946 | static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, | |||
2947 | DenseMap<unsigned, const Value *> &Slots2Values) { | |||
2948 | int Slot = MST.getLocalSlot(V); | |||
2949 | if (Slot == -1) | |||
2950 | return; | |||
2951 | Slots2Values.insert(std::make_pair(unsigned(Slot), V)); | |||
2952 | } | |||
2953 | ||||
2954 | /// Creates the mapping from slot numbers to function's unnamed IR values. | |||
2955 | static void initSlots2Values(const Function &F, | |||
2956 | DenseMap<unsigned, const Value *> &Slots2Values) { | |||
2957 | ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); | |||
2958 | MST.incorporateFunction(F); | |||
2959 | for (const auto &Arg : F.args()) | |||
2960 | mapValueToSlot(&Arg, MST, Slots2Values); | |||
2961 | for (const auto &BB : F) { | |||
2962 | mapValueToSlot(&BB, MST, Slots2Values); | |||
2963 | for (const auto &I : BB) | |||
2964 | mapValueToSlot(&I, MST, Slots2Values); | |||
2965 | } | |||
2966 | } | |||
2967 | ||||
2968 | const Value *MIParser::getIRValue(unsigned Slot) { | |||
2969 | if (Slots2Values.empty()) | |||
2970 | initSlots2Values(MF.getFunction(), Slots2Values); | |||
2971 | auto ValueInfo = Slots2Values.find(Slot); | |||
2972 | if (ValueInfo == Slots2Values.end()) | |||
2973 | return nullptr; | |||
2974 | return ValueInfo->second; | |||
2975 | } | |||
2976 | ||||
2977 | MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) { | |||
2978 | // FIXME: Currently we can't recognize temporary or local symbols and call all | |||
2979 | // of the appropriate forms to create them. However, this handles basic cases | |||
2980 | // well as most of the special aspects are recognized by a prefix on their | |||
2981 | // name, and the input names should already be unique. For test cases, keeping | |||
2982 | // the symbol name out of the symbol table isn't terribly important. | |||
2983 | return MF.getContext().getOrCreateSymbol(Name); | |||
2984 | } | |||
2985 | ||||
2986 | bool MIParser::parseStringConstant(std::string &Result) { | |||
2987 | if (Token.isNot(MIToken::StringConstant)) | |||
2988 | return error("expected string constant"); | |||
2989 | Result = Token.stringValue(); | |||
2990 | lex(); | |||
2991 | return false; | |||
2992 | } | |||
2993 | ||||
2994 | bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, | |||
2995 | StringRef Src, | |||
2996 | SMDiagnostic &Error) { | |||
2997 | return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots); | |||
2998 | } | |||
2999 | ||||
3000 | bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS, | |||
3001 | StringRef Src, SMDiagnostic &Error) { | |||
3002 | return MIParser(PFS, Error, Src).parseBasicBlocks(); | |||
| ||||
3003 | } | |||
3004 | ||||
3005 | bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS, | |||
3006 | MachineBasicBlock *&MBB, StringRef Src, | |||
3007 | SMDiagnostic &Error) { | |||
3008 | return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB); | |||
3009 | } | |||
3010 | ||||
3011 | bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS, | |||
3012 | unsigned &Reg, StringRef Src, | |||
3013 | SMDiagnostic &Error) { | |||
3014 | return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg); | |||
3015 | } | |||
3016 | ||||
3017 | bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS, | |||
3018 | unsigned &Reg, StringRef Src, | |||
3019 | SMDiagnostic &Error) { | |||
3020 | return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg); | |||
3021 | } | |||
3022 | ||||
3023 | bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, | |||
3024 | VRegInfo *&Info, StringRef Src, | |||
3025 | SMDiagnostic &Error) { | |||
3026 | return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info); | |||
3027 | } | |||
3028 | ||||
3029 | bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS, | |||
3030 | int &FI, StringRef Src, | |||
3031 | SMDiagnostic &Error) { | |||
3032 | return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI); | |||
3033 | } | |||
3034 | ||||
3035 | bool llvm::parseMDNode(PerFunctionMIParsingState &PFS, | |||
3036 | MDNode *&Node, StringRef Src, SMDiagnostic &Error) { | |||
3037 | return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node); | |||
3038 | } |