Bug Summary

File:lib/CodeGen/MIRParser/MIParser.cpp
Warning:line 1660, column 63
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MIParser.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-9~svn358520/build-llvm/lib/CodeGen/MIRParser -I /build/llvm-toolchain-snapshot-9~svn358520/lib/CodeGen/MIRParser -I /build/llvm-toolchain-snapshot-9~svn358520/build-llvm/include -I /build/llvm-toolchain-snapshot-9~svn358520/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/include/clang/9.0.0/include/ -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-9~svn358520/build-llvm/lib/CodeGen/MIRParser -fdebug-prefix-map=/build/llvm-toolchain-snapshot-9~svn358520=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -o /tmp/scan-build-2019-04-17-050842-1547-1 -x c++ /build/llvm-toolchain-snapshot-9~svn358520/lib/CodeGen/MIRParser/MIParser.cpp -faddrsig
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
83using namespace llvm;
84
85void 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
105void 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~svn358520/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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 119, __PRETTY_FUNCTION__))
;
120 }
121}
122
123bool 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
133void 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~svn358520/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
142bool 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
152void 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~svn358520/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~svn358520/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
165const 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
173void 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(StringRef(TRI->getSubRegIndexName(I)).lower(), I));
180}
181
182unsigned 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
190void 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~svn358520/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
200bool 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
209void 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~svn358520/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
221bool 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
231void 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~svn358520/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
243bool 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
253void 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~svn358520/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
264bool 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
274void 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
286void 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
303const TargetRegisterClass *
304PerTargetMIParsingState::getRegClass(StringRef Name) {
305 auto RegClassInfo = Names2RegClasses.find(Name);
306 if (RegClassInfo == Names2RegClasses.end())
307 return nullptr;
308 return RegClassInfo->getValue();
309}
310
311const 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
318PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF,
319 SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T)
320 : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) {
321}
322
323VRegInfo &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
334VRegInfo &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~svn358520/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
346namespace {
347
348/// A wrapper struct around the 'MachineOperand' struct that includes a source
349/// range and other attributes.
350struct 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~svn358520/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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 361, __PRETTY_FUNCTION__))
;
362 }
363};
364
365class 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
376public:
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
474private:
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
522MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error,
523 StringRef Source)
524 : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS)
525{}
526
527void 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
533bool MIParser::error(const Twine &Msg) { return error(Token.location(), Msg); }
534
535bool 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~svn358520/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
552static 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
569bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) {
570 if (Token.isNot(TokenKind))
571 return error(Twine("expected ") + toString(TokenKind));
572 lex();
573 return false;
574}
575
576bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) {
577 if (Token.isNot(TokenKind))
578 return false;
579 lex();
580 return true;
581}
582
583bool 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~svn358520/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
650bool 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
694bool 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~svn358520/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
727bool 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 728, __PRETTY_FUNCTION__))
;
13
'?' condition is true
729 lex();
730 if (expectAndConsume(MIToken::colon))
14
Taking false branch
731 return true;
732 if (Token.isNewlineOrEOF()) // Allow an empty list of successors.
15
Taking false branch
733 return false;
734 do {
735 if (Token.isNot(MIToken::MachineBasicBlock))
16
Taking false branch
736 return error("expected a machine basic block reference");
737 MachineBasicBlock *SuccMBB = nullptr;
738 if (parseMBBReference(SuccMBB))
17
Calling 'MIParser::parseMBBReference'
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
758bool 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 761, __PRETTY_FUNCTION__))
;
8
'?' condition is true
762 lex();
763 if (consumeIfPresent(MIToken::lparen)) {
9
Taking false branch
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) {
10
Loop condition is true. Entering loop body
781 if (Token.is(MIToken::kw_successors)) {
11
Taking true branch
782 if (parseBasicBlockSuccessors(MBB))
12
Calling 'MIParser::parseBasicBlockSuccessors'
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~svn358520/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~svn358520/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
853bool MIParser::parseBasicBlocks() {
854 lex();
855 // Skip until the first machine basic block.
856 while (Token.is(MIToken::Newline))
2
Loop condition is false. Execution continues on line 858
857 lex();
858 if (Token.isErrorOrEOF())
3
Taking false branch
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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 862, __PRETTY_FUNCTION__))
;
4
'?' condition is true
863 MachineBasicBlock *AddFalthroughFrom = nullptr;
864 do {
865 MachineBasicBlock *MBB = nullptr;
866 if (parseMBBReference(MBB))
5
Taking false branch
867 return true;
868 if (AddFalthroughFrom) {
6
Taking false branch
869 if (!AddFalthroughFrom->isSuccessor(MBB))
870 AddFalthroughFrom->addSuccessor(MBB);
871 AddFalthroughFrom->normalizeSuccProbs();
872 AddFalthroughFrom = nullptr;
873 }
874 if (parseBasicBlock(*MBB, AddFalthroughFrom))
7
Calling 'MIParser::parseBasicBlock'
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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 878, __PRETTY_FUNCTION__))
;
879 } while (Token.isNot(MIToken::Eof));
880 return false;
881}
882
883bool 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
992bool 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
1005bool 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
1017bool 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
1029bool 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
1045bool 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
1056bool 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
1074static 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 1075, __PRETTY_FUNCTION__))
;
1076 return MO.isDef() ? "implicit-def" : "implicit";
1077}
1078
1079static 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~svn358520/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.
1086static 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
1095bool 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~svn358520/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
1126bool 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
1177bool 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~svn358520/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
1185bool 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~svn358520/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
1194bool 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~svn358520/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
1205bool 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 1220)
;
1221 }
1222}
1223
1224bool 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~svn358520/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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 1279)
;
1280}
1281
1282bool 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~svn358520/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
1326bool 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~svn358520/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
1339bool 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
1352bool 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
1389bool 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
1472bool 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~svn358520/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
1482bool 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
1493bool 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.
1501static bool verifyScalarSize(uint64_t Size) {
1502 return Size != 0 && isUInt<16>(Size);
1503}
1504
1505static bool verifyVectorElementCount(uint64_t NumElts) {
1506 return NumElts != 0 && isUInt<16>(NumElts);
1507}
1508
1509static bool verifyAddrSpace(uint64_t AddrSpace) {
1510 return isUInt<24>(AddrSpace);
1511}
1512
1513bool 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
1587bool 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~svn358520/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
1612bool 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
1625bool MIParser::getUnsigned(unsigned &Result) {
1626 if (Token.hasIntegerValue()) {
20
Assuming the condition is true
21
Taking true branch
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)
22
Assuming 'Val64' is equal to 'Limit'
23
Taking true branch
1630 return error("expected 32-bit integer (too large)");
24
Returning without writing to 'Result'
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
1646bool 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~svn358520/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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 1648, __PRETTY_FUNCTION__))
;
1649 unsigned Number;
18
'Number' declared without an initial value
1650 if (getUnsigned(Number))
19
Calling 'MIParser::getUnsigned'
25
Returning from 'MIParser::getUnsigned'
26
Assuming the condition is false
27
Taking false branch
1651 return true;
1652 auto MBBInfo = PFS.MBBSlots.find(Number);
1653 if (MBBInfo == PFS.MBBSlots.end())
28
Assuming the condition is false
29
Taking false branch
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())
30
Assuming the condition is true
31
Taking true branch
1660 return error(Twine("the name of machine basic block #") + Twine(Number) +
32
1st function call argument is an uninitialized value
1661 " isn't '" + Token.stringValue() + "'");
1662 return false;
1663}
1664
1665bool 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
1674bool 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~svn358520/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
1695bool MIParser::parseStackObjectOperand(MachineOperand &Dest) {
1696 int FI;
1697 if (parseStackFrameIndex(FI))
1698 return true;
1699 Dest = MachineOperand::CreateFI(FI);
1700 return false;
1701}
1702
1703bool 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~svn358520/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
1717bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) {
1718 int FI;
1719 if (parseFixedStackFrameIndex(FI))
1720 return true;
1721 Dest = MachineOperand::CreateFI(FI);
1722 return false;
1723}
1724
1725bool 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 1746)
;
1747 }
1748 return false;
1749}
1750
1751bool 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
1762bool 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~svn358520/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
1777bool 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~svn358520/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
1790bool 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~svn358520/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
1800bool 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~svn358520/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
1810bool 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~svn358520/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
1821bool 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~svn358520/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
1839bool 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~svn358520/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 return error(Twine("invalid DWARF op '") + Token.stringValue() + "'");
1858 }
1859
1860 if (Token.isNot(MIToken::IntegerLiteral) ||
1861 Token.integerValue().isSigned())
1862 return error("expected unsigned integer");
1863
1864 auto &U = Token.integerValue();
1865 if (U.ugt(UINT64_MAX(18446744073709551615UL)))
1866 return error("element too large, limit is " + Twine(UINT64_MAX(18446744073709551615UL)));
1867 Elements.push_back(U.getZExtValue());
1868 lex();
1869
1870 } while (consumeIfPresent(MIToken::comma));
1871 }
1872
1873 if (expectAndConsume(MIToken::rparen))
1874 return true;
1875
1876 Expr = DIExpression::get(MF.getFunction().getContext(), Elements);
1877 return false;
1878}
1879
1880bool MIParser::parseDILocation(MDNode *&Loc) {
1881 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 1881, __PRETTY_FUNCTION__))
;
1882 lex();
1883
1884 bool HaveLine = false;
1885 unsigned Line = 0;
1886 unsigned Column = 0;
1887 MDNode *Scope = nullptr;
1888 MDNode *InlinedAt = nullptr;
1889 bool ImplicitCode = false;
1890
1891 if (expectAndConsume(MIToken::lparen))
1892 return true;
1893
1894 if (Token.isNot(MIToken::rparen)) {
1895 do {
1896 if (Token.is(MIToken::Identifier)) {
1897 if (Token.stringValue() == "line") {
1898 lex();
1899 if (expectAndConsume(MIToken::colon))
1900 return true;
1901 if (Token.isNot(MIToken::IntegerLiteral) ||
1902 Token.integerValue().isSigned())
1903 return error("expected unsigned integer");
1904 Line = Token.integerValue().getZExtValue();
1905 HaveLine = true;
1906 lex();
1907 continue;
1908 }
1909 if (Token.stringValue() == "column") {
1910 lex();
1911 if (expectAndConsume(MIToken::colon))
1912 return true;
1913 if (Token.isNot(MIToken::IntegerLiteral) ||
1914 Token.integerValue().isSigned())
1915 return error("expected unsigned integer");
1916 Column = Token.integerValue().getZExtValue();
1917 lex();
1918 continue;
1919 }
1920 if (Token.stringValue() == "scope") {
1921 lex();
1922 if (expectAndConsume(MIToken::colon))
1923 return true;
1924 if (parseMDNode(Scope))
1925 return error("expected metadata node");
1926 if (!isa<DIScope>(Scope))
1927 return error("expected DIScope node");
1928 continue;
1929 }
1930 if (Token.stringValue() == "inlinedAt") {
1931 lex();
1932 if (expectAndConsume(MIToken::colon))
1933 return true;
1934 if (Token.is(MIToken::exclaim)) {
1935 if (parseMDNode(InlinedAt))
1936 return true;
1937 } else if (Token.is(MIToken::md_dilocation)) {
1938 if (parseDILocation(InlinedAt))
1939 return true;
1940 } else
1941 return error("expected metadata node");
1942 if (!isa<DILocation>(InlinedAt))
1943 return error("expected DILocation node");
1944 continue;
1945 }
1946 if (Token.stringValue() == "isImplicitCode") {
1947 lex();
1948 if (expectAndConsume(MIToken::colon))
1949 return true;
1950 if (!Token.is(MIToken::Identifier))
1951 return error("expected true/false");
1952 // As far as I can see, we don't have any existing need for parsing
1953 // true/false in MIR yet. Do it ad-hoc until there's something else
1954 // that needs it.
1955 if (Token.stringValue() == "true")
1956 ImplicitCode = true;
1957 else if (Token.stringValue() == "false")
1958 ImplicitCode = false;
1959 else
1960 return error("expected true/false");
1961 lex();
1962 continue;
1963 }
1964 }
1965 return error(Twine("invalid DILocation argument '") +
1966 Token.stringValue() + "'");
1967 } while (consumeIfPresent(MIToken::comma));
1968 }
1969
1970 if (expectAndConsume(MIToken::rparen))
1971 return true;
1972
1973 if (!HaveLine)
1974 return error("DILocation requires line number");
1975 if (!Scope)
1976 return error("DILocation requires a scope");
1977
1978 Loc = DILocation::get(MF.getFunction().getContext(), Line, Column, Scope,
1979 InlinedAt, ImplicitCode);
1980 return false;
1981}
1982
1983bool MIParser::parseMetadataOperand(MachineOperand &Dest) {
1984 MDNode *Node = nullptr;
1985 if (Token.is(MIToken::exclaim)) {
1986 if (parseMDNode(Node))
1987 return true;
1988 } else if (Token.is(MIToken::md_diexpr)) {
1989 if (parseDIExpression(Node))
1990 return true;
1991 }
1992 Dest = MachineOperand::CreateMetadata(Node);
1993 return false;
1994}
1995
1996bool MIParser::parseCFIOffset(int &Offset) {
1997 if (Token.isNot(MIToken::IntegerLiteral))
1998 return error("expected a cfi offset");
1999 if (Token.integerValue().getMinSignedBits() > 32)
2000 return error("expected a 32 bit integer (the cfi offset is too large)");
2001 Offset = (int)Token.integerValue().getExtValue();
2002 lex();
2003 return false;
2004}
2005
2006bool MIParser::parseCFIRegister(unsigned &Reg) {
2007 if (Token.isNot(MIToken::NamedRegister))
2008 return error("expected a cfi register");
2009 unsigned LLVMReg;
2010 if (parseNamedRegister(LLVMReg))
2011 return true;
2012 const auto *TRI = MF.getSubtarget().getRegisterInfo();
2013 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2013, __PRETTY_FUNCTION__))
;
2014 int DwarfReg = TRI->getDwarfRegNum(LLVMReg, true);
2015 if (DwarfReg < 0)
2016 return error("invalid DWARF register");
2017 Reg = (unsigned)DwarfReg;
2018 lex();
2019 return false;
2020}
2021
2022bool MIParser::parseCFIEscapeValues(std::string &Values) {
2023 do {
2024 if (Token.isNot(MIToken::HexLiteral))
2025 return error("expected a hexadecimal literal");
2026 unsigned Value;
2027 if (getUnsigned(Value))
2028 return true;
2029 if (Value > UINT8_MAX(255))
2030 return error("expected a 8-bit integer (too large)");
2031 Values.push_back(static_cast<uint8_t>(Value));
2032 lex();
2033 } while (consumeIfPresent(MIToken::comma));
2034 return false;
2035}
2036
2037bool MIParser::parseCFIOperand(MachineOperand &Dest) {
2038 auto Kind = Token.kind();
2039 lex();
2040 int Offset;
2041 unsigned Reg;
2042 unsigned CFIIndex;
2043 switch (Kind) {
2044 case MIToken::kw_cfi_same_value:
2045 if (parseCFIRegister(Reg))
2046 return true;
2047 CFIIndex = MF.addFrameInst(MCCFIInstruction::createSameValue(nullptr, Reg));
2048 break;
2049 case MIToken::kw_cfi_offset:
2050 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2051 parseCFIOffset(Offset))
2052 return true;
2053 CFIIndex =
2054 MF.addFrameInst(MCCFIInstruction::createOffset(nullptr, Reg, Offset));
2055 break;
2056 case MIToken::kw_cfi_rel_offset:
2057 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2058 parseCFIOffset(Offset))
2059 return true;
2060 CFIIndex = MF.addFrameInst(
2061 MCCFIInstruction::createRelOffset(nullptr, Reg, Offset));
2062 break;
2063 case MIToken::kw_cfi_def_cfa_register:
2064 if (parseCFIRegister(Reg))
2065 return true;
2066 CFIIndex =
2067 MF.addFrameInst(MCCFIInstruction::createDefCfaRegister(nullptr, Reg));
2068 break;
2069 case MIToken::kw_cfi_def_cfa_offset:
2070 if (parseCFIOffset(Offset))
2071 return true;
2072 // NB: MCCFIInstruction::createDefCfaOffset negates the offset.
2073 CFIIndex = MF.addFrameInst(
2074 MCCFIInstruction::createDefCfaOffset(nullptr, -Offset));
2075 break;
2076 case MIToken::kw_cfi_adjust_cfa_offset:
2077 if (parseCFIOffset(Offset))
2078 return true;
2079 CFIIndex = MF.addFrameInst(
2080 MCCFIInstruction::createAdjustCfaOffset(nullptr, Offset));
2081 break;
2082 case MIToken::kw_cfi_def_cfa:
2083 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2084 parseCFIOffset(Offset))
2085 return true;
2086 // NB: MCCFIInstruction::createDefCfa negates the offset.
2087 CFIIndex =
2088 MF.addFrameInst(MCCFIInstruction::createDefCfa(nullptr, Reg, -Offset));
2089 break;
2090 case MIToken::kw_cfi_remember_state:
2091 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
2092 break;
2093 case MIToken::kw_cfi_restore:
2094 if (parseCFIRegister(Reg))
2095 return true;
2096 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestore(nullptr, Reg));
2097 break;
2098 case MIToken::kw_cfi_restore_state:
2099 CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
2100 break;
2101 case MIToken::kw_cfi_undefined:
2102 if (parseCFIRegister(Reg))
2103 return true;
2104 CFIIndex = MF.addFrameInst(MCCFIInstruction::createUndefined(nullptr, Reg));
2105 break;
2106 case MIToken::kw_cfi_register: {
2107 unsigned Reg2;
2108 if (parseCFIRegister(Reg) || expectAndConsume(MIToken::comma) ||
2109 parseCFIRegister(Reg2))
2110 return true;
2111
2112 CFIIndex =
2113 MF.addFrameInst(MCCFIInstruction::createRegister(nullptr, Reg, Reg2));
2114 break;
2115 }
2116 case MIToken::kw_cfi_window_save:
2117 CFIIndex = MF.addFrameInst(MCCFIInstruction::createWindowSave(nullptr));
2118 break;
2119 case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
2120 CFIIndex = MF.addFrameInst(MCCFIInstruction::createNegateRAState(nullptr));
2121 break;
2122 case MIToken::kw_cfi_escape: {
2123 std::string Values;
2124 if (parseCFIEscapeValues(Values))
2125 return true;
2126 CFIIndex = MF.addFrameInst(MCCFIInstruction::createEscape(nullptr, Values));
2127 break;
2128 }
2129 default:
2130 // TODO: Parse the other CFI operands.
2131 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2131)
;
2132 }
2133 Dest = MachineOperand::CreateCFIIndex(CFIIndex);
2134 return false;
2135}
2136
2137bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) {
2138 switch (Token.kind()) {
2139 case MIToken::NamedIRBlock: {
2140 BB = dyn_cast_or_null<BasicBlock>(
2141 F.getValueSymbolTable()->lookup(Token.stringValue()));
2142 if (!BB)
2143 return error(Twine("use of undefined IR block '") + Token.range() + "'");
2144 break;
2145 }
2146 case MIToken::IRBlock: {
2147 unsigned SlotNumber = 0;
2148 if (getUnsigned(SlotNumber))
2149 return true;
2150 BB = const_cast<BasicBlock *>(getIRBlock(SlotNumber, F));
2151 if (!BB)
2152 return error(Twine("use of undefined IR block '%ir-block.") +
2153 Twine(SlotNumber) + "'");
2154 break;
2155 }
2156 default:
2157 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2157)
;
2158 }
2159 return false;
2160}
2161
2162bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) {
2163 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2163, __PRETTY_FUNCTION__))
;
2164 lex();
2165 if (expectAndConsume(MIToken::lparen))
2166 return true;
2167 if (Token.isNot(MIToken::GlobalValue) &&
2168 Token.isNot(MIToken::NamedGlobalValue))
2169 return error("expected a global value");
2170 GlobalValue *GV = nullptr;
2171 if (parseGlobalValue(GV))
2172 return true;
2173 auto *F = dyn_cast<Function>(GV);
2174 if (!F)
2175 return error("expected an IR function reference");
2176 lex();
2177 if (expectAndConsume(MIToken::comma))
2178 return true;
2179 BasicBlock *BB = nullptr;
2180 if (Token.isNot(MIToken::IRBlock) && Token.isNot(MIToken::NamedIRBlock))
2181 return error("expected an IR block reference");
2182 if (parseIRBlock(BB, *F))
2183 return true;
2184 lex();
2185 if (expectAndConsume(MIToken::rparen))
2186 return true;
2187 Dest = MachineOperand::CreateBA(BlockAddress::get(F, BB), /*Offset=*/0);
2188 if (parseOperandsOffset(Dest))
2189 return true;
2190 return false;
2191}
2192
2193bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) {
2194 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2194, __PRETTY_FUNCTION__))
;
2195 lex();
2196 if (expectAndConsume(MIToken::lparen))
2197 return error("expected syntax intrinsic(@llvm.whatever)");
2198
2199 if (Token.isNot(MIToken::NamedGlobalValue))
2200 return error("expected syntax intrinsic(@llvm.whatever)");
2201
2202 std::string Name = Token.stringValue();
2203 lex();
2204
2205 if (expectAndConsume(MIToken::rparen))
2206 return error("expected ')' to terminate intrinsic name");
2207
2208 // Find out what intrinsic we're dealing with, first try the global namespace
2209 // and then the target's private intrinsics if that fails.
2210 const TargetIntrinsicInfo *TII = MF.getTarget().getIntrinsicInfo();
2211 Intrinsic::ID ID = Function::lookupIntrinsicID(Name);
2212 if (ID == Intrinsic::not_intrinsic && TII)
2213 ID = static_cast<Intrinsic::ID>(TII->lookupName(Name));
2214
2215 if (ID == Intrinsic::not_intrinsic)
2216 return error("unknown intrinsic name");
2217 Dest = MachineOperand::CreateIntrinsicID(ID);
2218
2219 return false;
2220}
2221
2222bool MIParser::parsePredicateOperand(MachineOperand &Dest) {
2223 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2223, __PRETTY_FUNCTION__))
;
2224 bool IsFloat = Token.is(MIToken::kw_floatpred);
2225 lex();
2226
2227 if (expectAndConsume(MIToken::lparen))
2228 return error("expected syntax intpred(whatever) or floatpred(whatever");
2229
2230 if (Token.isNot(MIToken::Identifier))
2231 return error("whatever");
2232
2233 CmpInst::Predicate Pred;
2234 if (IsFloat) {
2235 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2236 .Case("false", CmpInst::FCMP_FALSE)
2237 .Case("oeq", CmpInst::FCMP_OEQ)
2238 .Case("ogt", CmpInst::FCMP_OGT)
2239 .Case("oge", CmpInst::FCMP_OGE)
2240 .Case("olt", CmpInst::FCMP_OLT)
2241 .Case("ole", CmpInst::FCMP_OLE)
2242 .Case("one", CmpInst::FCMP_ONE)
2243 .Case("ord", CmpInst::FCMP_ORD)
2244 .Case("uno", CmpInst::FCMP_UNO)
2245 .Case("ueq", CmpInst::FCMP_UEQ)
2246 .Case("ugt", CmpInst::FCMP_UGT)
2247 .Case("uge", CmpInst::FCMP_UGE)
2248 .Case("ult", CmpInst::FCMP_ULT)
2249 .Case("ule", CmpInst::FCMP_ULE)
2250 .Case("une", CmpInst::FCMP_UNE)
2251 .Case("true", CmpInst::FCMP_TRUE)
2252 .Default(CmpInst::BAD_FCMP_PREDICATE);
2253 if (!CmpInst::isFPPredicate(Pred))
2254 return error("invalid floating-point predicate");
2255 } else {
2256 Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue())
2257 .Case("eq", CmpInst::ICMP_EQ)
2258 .Case("ne", CmpInst::ICMP_NE)
2259 .Case("sgt", CmpInst::ICMP_SGT)
2260 .Case("sge", CmpInst::ICMP_SGE)
2261 .Case("slt", CmpInst::ICMP_SLT)
2262 .Case("sle", CmpInst::ICMP_SLE)
2263 .Case("ugt", CmpInst::ICMP_UGT)
2264 .Case("uge", CmpInst::ICMP_UGE)
2265 .Case("ult", CmpInst::ICMP_ULT)
2266 .Case("ule", CmpInst::ICMP_ULE)
2267 .Default(CmpInst::BAD_ICMP_PREDICATE);
2268 if (!CmpInst::isIntPredicate(Pred))
2269 return error("invalid integer predicate");
2270 }
2271
2272 lex();
2273 Dest = MachineOperand::CreatePredicate(Pred);
2274 if (expectAndConsume(MIToken::rparen))
2275 return error("predicate should be terminated by ')'.");
2276
2277 return false;
2278}
2279
2280bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) {
2281 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2281, __PRETTY_FUNCTION__))
;
2282 lex();
2283 if (expectAndConsume(MIToken::lparen))
2284 return true;
2285 if (Token.isNot(MIToken::Identifier))
2286 return error("expected the name of the target index");
2287 int Index = 0;
2288 if (PFS.Target.getTargetIndex(Token.stringValue(), Index))
2289 return error("use of undefined target index '" + Token.stringValue() + "'");
2290 lex();
2291 if (expectAndConsume(MIToken::rparen))
2292 return true;
2293 Dest = MachineOperand::CreateTargetIndex(unsigned(Index), /*Offset=*/0);
2294 if (parseOperandsOffset(Dest))
2295 return true;
2296 return false;
2297}
2298
2299bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) {
2300 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2300, __PRETTY_FUNCTION__))
;
2301 lex();
2302 if (expectAndConsume(MIToken::lparen))
2303 return true;
2304
2305 uint32_t *Mask = MF.allocateRegMask();
2306 while (true) {
2307 if (Token.isNot(MIToken::NamedRegister))
2308 return error("expected a named register");
2309 unsigned Reg;
2310 if (parseNamedRegister(Reg))
2311 return true;
2312 lex();
2313 Mask[Reg / 32] |= 1U << (Reg % 32);
2314 // TODO: Report an error if the same register is used more than once.
2315 if (Token.isNot(MIToken::comma))
2316 break;
2317 lex();
2318 }
2319
2320 if (expectAndConsume(MIToken::rparen))
2321 return true;
2322 Dest = MachineOperand::CreateRegMask(Mask);
2323 return false;
2324}
2325
2326bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) {
2327 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2327, __PRETTY_FUNCTION__))
;
2328 uint32_t *Mask = MF.allocateRegMask();
2329 lex();
2330 if (expectAndConsume(MIToken::lparen))
2331 return true;
2332 while (true) {
2333 if (Token.isNot(MIToken::NamedRegister))
2334 return error("expected a named register");
2335 unsigned Reg;
2336 if (parseNamedRegister(Reg))
2337 return true;
2338 lex();
2339 Mask[Reg / 32] |= 1U << (Reg % 32);
2340 // TODO: Report an error if the same register is used more than once.
2341 if (Token.isNot(MIToken::comma))
2342 break;
2343 lex();
2344 }
2345 if (expectAndConsume(MIToken::rparen))
2346 return true;
2347 Dest = MachineOperand::CreateRegLiveOut(Mask);
2348 return false;
2349}
2350
2351bool MIParser::parseMachineOperand(MachineOperand &Dest,
2352 Optional<unsigned> &TiedDefIdx) {
2353 switch (Token.kind()) {
2354 case MIToken::kw_implicit:
2355 case MIToken::kw_implicit_define:
2356 case MIToken::kw_def:
2357 case MIToken::kw_dead:
2358 case MIToken::kw_killed:
2359 case MIToken::kw_undef:
2360 case MIToken::kw_internal:
2361 case MIToken::kw_early_clobber:
2362 case MIToken::kw_debug_use:
2363 case MIToken::kw_renamable:
2364 case MIToken::underscore:
2365 case MIToken::NamedRegister:
2366 case MIToken::VirtualRegister:
2367 case MIToken::NamedVirtualRegister:
2368 return parseRegisterOperand(Dest, TiedDefIdx);
2369 case MIToken::IntegerLiteral:
2370 return parseImmediateOperand(Dest);
2371 case MIToken::kw_half:
2372 case MIToken::kw_float:
2373 case MIToken::kw_double:
2374 case MIToken::kw_x86_fp80:
2375 case MIToken::kw_fp128:
2376 case MIToken::kw_ppc_fp128:
2377 return parseFPImmediateOperand(Dest);
2378 case MIToken::MachineBasicBlock:
2379 return parseMBBOperand(Dest);
2380 case MIToken::StackObject:
2381 return parseStackObjectOperand(Dest);
2382 case MIToken::FixedStackObject:
2383 return parseFixedStackObjectOperand(Dest);
2384 case MIToken::GlobalValue:
2385 case MIToken::NamedGlobalValue:
2386 return parseGlobalAddressOperand(Dest);
2387 case MIToken::ConstantPoolItem:
2388 return parseConstantPoolIndexOperand(Dest);
2389 case MIToken::JumpTableIndex:
2390 return parseJumpTableIndexOperand(Dest);
2391 case MIToken::ExternalSymbol:
2392 return parseExternalSymbolOperand(Dest);
2393 case MIToken::MCSymbol:
2394 return parseMCSymbolOperand(Dest);
2395 case MIToken::SubRegisterIndex:
2396 return parseSubRegisterIndexOperand(Dest);
2397 case MIToken::md_diexpr:
2398 case MIToken::exclaim:
2399 return parseMetadataOperand(Dest);
2400 case MIToken::kw_cfi_same_value:
2401 case MIToken::kw_cfi_offset:
2402 case MIToken::kw_cfi_rel_offset:
2403 case MIToken::kw_cfi_def_cfa_register:
2404 case MIToken::kw_cfi_def_cfa_offset:
2405 case MIToken::kw_cfi_adjust_cfa_offset:
2406 case MIToken::kw_cfi_escape:
2407 case MIToken::kw_cfi_def_cfa:
2408 case MIToken::kw_cfi_register:
2409 case MIToken::kw_cfi_remember_state:
2410 case MIToken::kw_cfi_restore:
2411 case MIToken::kw_cfi_restore_state:
2412 case MIToken::kw_cfi_undefined:
2413 case MIToken::kw_cfi_window_save:
2414 case MIToken::kw_cfi_aarch64_negate_ra_sign_state:
2415 return parseCFIOperand(Dest);
2416 case MIToken::kw_blockaddress:
2417 return parseBlockAddressOperand(Dest);
2418 case MIToken::kw_intrinsic:
2419 return parseIntrinsicOperand(Dest);
2420 case MIToken::kw_target_index:
2421 return parseTargetIndexOperand(Dest);
2422 case MIToken::kw_liveout:
2423 return parseLiveoutRegisterMaskOperand(Dest);
2424 case MIToken::kw_floatpred:
2425 case MIToken::kw_intpred:
2426 return parsePredicateOperand(Dest);
2427 case MIToken::Error:
2428 return true;
2429 case MIToken::Identifier:
2430 if (const auto *RegMask = PFS.Target.getRegMask(Token.stringValue())) {
2431 Dest = MachineOperand::CreateRegMask(RegMask);
2432 lex();
2433 break;
2434 } else if (Token.stringValue() == "CustomRegMask") {
2435 return parseCustomRegisterMaskOperand(Dest);
2436 } else
2437 return parseTypedImmediateOperand(Dest);
2438 default:
2439 // FIXME: Parse the MCSymbol machine operand.
2440 return error("expected a machine operand");
2441 }
2442 return false;
2443}
2444
2445bool MIParser::parseMachineOperandAndTargetFlags(
2446 MachineOperand &Dest, Optional<unsigned> &TiedDefIdx) {
2447 unsigned TF = 0;
2448 bool HasTargetFlags = false;
2449 if (Token.is(MIToken::kw_target_flags)) {
2450 HasTargetFlags = true;
2451 lex();
2452 if (expectAndConsume(MIToken::lparen))
2453 return true;
2454 if (Token.isNot(MIToken::Identifier))
2455 return error("expected the name of the target flag");
2456 if (PFS.Target.getDirectTargetFlag(Token.stringValue(), TF)) {
2457 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), TF))
2458 return error("use of undefined target flag '" + Token.stringValue() +
2459 "'");
2460 }
2461 lex();
2462 while (Token.is(MIToken::comma)) {
2463 lex();
2464 if (Token.isNot(MIToken::Identifier))
2465 return error("expected the name of the target flag");
2466 unsigned BitFlag = 0;
2467 if (PFS.Target.getBitmaskTargetFlag(Token.stringValue(), BitFlag))
2468 return error("use of undefined target flag '" + Token.stringValue() +
2469 "'");
2470 // TODO: Report an error when using a duplicate bit target flag.
2471 TF |= BitFlag;
2472 lex();
2473 }
2474 if (expectAndConsume(MIToken::rparen))
2475 return true;
2476 }
2477 auto Loc = Token.location();
2478 if (parseMachineOperand(Dest, TiedDefIdx))
2479 return true;
2480 if (!HasTargetFlags)
2481 return false;
2482 if (Dest.isReg())
2483 return error(Loc, "register operands can't have target flags");
2484 Dest.setTargetFlags(TF);
2485 return false;
2486}
2487
2488bool MIParser::parseOffset(int64_t &Offset) {
2489 if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus))
2490 return false;
2491 StringRef Sign = Token.range();
2492 bool IsNegative = Token.is(MIToken::minus);
2493 lex();
2494 if (Token.isNot(MIToken::IntegerLiteral))
2495 return error("expected an integer literal after '" + Sign + "'");
2496 if (Token.integerValue().getMinSignedBits() > 64)
2497 return error("expected 64-bit integer (too large)");
2498 Offset = Token.integerValue().getExtValue();
2499 if (IsNegative)
2500 Offset = -Offset;
2501 lex();
2502 return false;
2503}
2504
2505bool MIParser::parseAlignment(unsigned &Alignment) {
2506 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2506, __PRETTY_FUNCTION__))
;
2507 lex();
2508 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2509 return error("expected an integer literal after 'align'");
2510 if (getUnsigned(Alignment))
2511 return true;
2512 lex();
2513
2514 if (!isPowerOf2_32(Alignment))
2515 return error("expected a power-of-2 literal after 'align'");
2516
2517 return false;
2518}
2519
2520bool MIParser::parseAddrspace(unsigned &Addrspace) {
2521 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2521, __PRETTY_FUNCTION__))
;
2522 lex();
2523 if (Token.isNot(MIToken::IntegerLiteral) || Token.integerValue().isSigned())
2524 return error("expected an integer literal after 'addrspace'");
2525 if (getUnsigned(Addrspace))
2526 return true;
2527 lex();
2528 return false;
2529}
2530
2531bool MIParser::parseOperandsOffset(MachineOperand &Op) {
2532 int64_t Offset = 0;
2533 if (parseOffset(Offset))
2534 return true;
2535 Op.setOffset(Offset);
2536 return false;
2537}
2538
2539bool MIParser::parseIRValue(const Value *&V) {
2540 switch (Token.kind()) {
2541 case MIToken::NamedIRValue: {
2542 V = MF.getFunction().getValueSymbolTable()->lookup(Token.stringValue());
2543 break;
2544 }
2545 case MIToken::IRValue: {
2546 unsigned SlotNumber = 0;
2547 if (getUnsigned(SlotNumber))
2548 return true;
2549 V = getIRValue(SlotNumber);
2550 break;
2551 }
2552 case MIToken::NamedGlobalValue:
2553 case MIToken::GlobalValue: {
2554 GlobalValue *GV = nullptr;
2555 if (parseGlobalValue(GV))
2556 return true;
2557 V = GV;
2558 break;
2559 }
2560 case MIToken::QuotedIRValue: {
2561 const Constant *C = nullptr;
2562 if (parseIRConstant(Token.location(), Token.stringValue(), C))
2563 return true;
2564 V = C;
2565 break;
2566 }
2567 default:
2568 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2568)
;
2569 }
2570 if (!V)
2571 return error(Twine("use of undefined IR value '") + Token.range() + "'");
2572 return false;
2573}
2574
2575bool MIParser::getUint64(uint64_t &Result) {
2576 if (Token.hasIntegerValue()) {
2577 if (Token.integerValue().getActiveBits() > 64)
2578 return error("expected 64-bit integer (too large)");
2579 Result = Token.integerValue().getZExtValue();
2580 return false;
2581 }
2582 if (Token.is(MIToken::HexLiteral)) {
2583 APInt A;
2584 if (getHexUint(A))
2585 return true;
2586 if (A.getBitWidth() > 64)
2587 return error("expected 64-bit integer (too large)");
2588 Result = A.getZExtValue();
2589 return false;
2590 }
2591 return true;
2592}
2593
2594bool MIParser::getHexUint(APInt &Result) {
2595 assert(Token.is(MIToken::HexLiteral))((Token.is(MIToken::HexLiteral)) ? static_cast<void> (0
) : __assert_fail ("Token.is(MIToken::HexLiteral)", "/build/llvm-toolchain-snapshot-9~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2595, __PRETTY_FUNCTION__))
;
2596 StringRef S = Token.range();
2597 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2597, __PRETTY_FUNCTION__))
;
2598 // This could be a floating point literal with a special prefix.
2599 if (!isxdigit(S[2]))
2600 return true;
2601 StringRef V = S.substr(2);
2602 APInt A(V.size()*4, V, 16);
2603
2604 // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make
2605 // sure it isn't the case before constructing result.
2606 unsigned NumBits = (A == 0) ? 32 : A.getActiveBits();
2607 Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords()));
2608 return false;
2609}
2610
2611bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) {
2612 const auto OldFlags = Flags;
2613 switch (Token.kind()) {
2614 case MIToken::kw_volatile:
2615 Flags |= MachineMemOperand::MOVolatile;
2616 break;
2617 case MIToken::kw_non_temporal:
2618 Flags |= MachineMemOperand::MONonTemporal;
2619 break;
2620 case MIToken::kw_dereferenceable:
2621 Flags |= MachineMemOperand::MODereferenceable;
2622 break;
2623 case MIToken::kw_invariant:
2624 Flags |= MachineMemOperand::MOInvariant;
2625 break;
2626 case MIToken::StringConstant: {
2627 MachineMemOperand::Flags TF;
2628 if (PFS.Target.getMMOTargetFlag(Token.stringValue(), TF))
2629 return error("use of undefined target MMO flag '" + Token.stringValue() +
2630 "'");
2631 Flags |= TF;
2632 break;
2633 }
2634 default:
2635 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2635)
;
2636 }
2637 if (OldFlags == Flags)
2638 // We know that the same flag is specified more than once when the flags
2639 // weren't modified.
2640 return error("duplicate '" + Token.stringValue() + "' memory operand flag");
2641 lex();
2642 return false;
2643}
2644
2645bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) {
2646 switch (Token.kind()) {
2647 case MIToken::kw_stack:
2648 PSV = MF.getPSVManager().getStack();
2649 break;
2650 case MIToken::kw_got:
2651 PSV = MF.getPSVManager().getGOT();
2652 break;
2653 case MIToken::kw_jump_table:
2654 PSV = MF.getPSVManager().getJumpTable();
2655 break;
2656 case MIToken::kw_constant_pool:
2657 PSV = MF.getPSVManager().getConstantPool();
2658 break;
2659 case MIToken::FixedStackObject: {
2660 int FI;
2661 if (parseFixedStackFrameIndex(FI))
2662 return true;
2663 PSV = MF.getPSVManager().getFixedStack(FI);
2664 // The token was already consumed, so use return here instead of break.
2665 return false;
2666 }
2667 case MIToken::StackObject: {
2668 int FI;
2669 if (parseStackFrameIndex(FI))
2670 return true;
2671 PSV = MF.getPSVManager().getFixedStack(FI);
2672 // The token was already consumed, so use return here instead of break.
2673 return false;
2674 }
2675 case MIToken::kw_call_entry:
2676 lex();
2677 switch (Token.kind()) {
2678 case MIToken::GlobalValue:
2679 case MIToken::NamedGlobalValue: {
2680 GlobalValue *GV = nullptr;
2681 if (parseGlobalValue(GV))
2682 return true;
2683 PSV = MF.getPSVManager().getGlobalValueCallEntry(GV);
2684 break;
2685 }
2686 case MIToken::ExternalSymbol:
2687 PSV = MF.getPSVManager().getExternalSymbolCallEntry(
2688 MF.createExternalSymbolName(Token.stringValue()));
2689 break;
2690 default:
2691 return error(
2692 "expected a global value or an external symbol after 'call-entry'");
2693 }
2694 break;
2695 default:
2696 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2696)
;
2697 }
2698 lex();
2699 return false;
2700}
2701
2702bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) {
2703 if (Token.is(MIToken::kw_constant_pool) || Token.is(MIToken::kw_stack) ||
2704 Token.is(MIToken::kw_got) || Token.is(MIToken::kw_jump_table) ||
2705 Token.is(MIToken::FixedStackObject) || Token.is(MIToken::StackObject) ||
2706 Token.is(MIToken::kw_call_entry)) {
2707 const PseudoSourceValue *PSV = nullptr;
2708 if (parseMemoryPseudoSourceValue(PSV))
2709 return true;
2710 int64_t Offset = 0;
2711 if (parseOffset(Offset))
2712 return true;
2713 Dest = MachinePointerInfo(PSV, Offset);
2714 return false;
2715 }
2716 if (Token.isNot(MIToken::NamedIRValue) && Token.isNot(MIToken::IRValue) &&
2717 Token.isNot(MIToken::GlobalValue) &&
2718 Token.isNot(MIToken::NamedGlobalValue) &&
2719 Token.isNot(MIToken::QuotedIRValue))
2720 return error("expected an IR value reference");
2721 const Value *V = nullptr;
2722 if (parseIRValue(V))
2723 return true;
2724 if (!V->getType()->isPointerTy())
2725 return error("expected a pointer IR value");
2726 lex();
2727 int64_t Offset = 0;
2728 if (parseOffset(Offset))
2729 return true;
2730 Dest = MachinePointerInfo(V, Offset);
2731 return false;
2732}
2733
2734bool MIParser::parseOptionalScope(LLVMContext &Context,
2735 SyncScope::ID &SSID) {
2736 SSID = SyncScope::System;
2737 if (Token.is(MIToken::Identifier) && Token.stringValue() == "syncscope") {
2738 lex();
2739 if (expectAndConsume(MIToken::lparen))
2740 return error("expected '(' in syncscope");
2741
2742 std::string SSN;
2743 if (parseStringConstant(SSN))
2744 return true;
2745
2746 SSID = Context.getOrInsertSyncScopeID(SSN);
2747 if (expectAndConsume(MIToken::rparen))
2748 return error("expected ')' in syncscope");
2749 }
2750
2751 return false;
2752}
2753
2754bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) {
2755 Order = AtomicOrdering::NotAtomic;
2756 if (Token.isNot(MIToken::Identifier))
2757 return false;
2758
2759 Order = StringSwitch<AtomicOrdering>(Token.stringValue())
2760 .Case("unordered", AtomicOrdering::Unordered)
2761 .Case("monotonic", AtomicOrdering::Monotonic)
2762 .Case("acquire", AtomicOrdering::Acquire)
2763 .Case("release", AtomicOrdering::Release)
2764 .Case("acq_rel", AtomicOrdering::AcquireRelease)
2765 .Case("seq_cst", AtomicOrdering::SequentiallyConsistent)
2766 .Default(AtomicOrdering::NotAtomic);
2767
2768 if (Order != AtomicOrdering::NotAtomic) {
2769 lex();
2770 return false;
2771 }
2772
2773 return error("expected an atomic scope, ordering or a size specification");
2774}
2775
2776bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) {
2777 if (expectAndConsume(MIToken::lparen))
2778 return true;
2779 MachineMemOperand::Flags Flags = MachineMemOperand::MONone;
2780 while (Token.isMemoryOperandFlag()) {
2781 if (parseMemoryOperandFlag(Flags))
2782 return true;
2783 }
2784 if (Token.isNot(MIToken::Identifier) ||
2785 (Token.stringValue() != "load" && Token.stringValue() != "store"))
2786 return error("expected 'load' or 'store' memory operation");
2787 if (Token.stringValue() == "load")
2788 Flags |= MachineMemOperand::MOLoad;
2789 else
2790 Flags |= MachineMemOperand::MOStore;
2791 lex();
2792
2793 // Optional 'store' for operands that both load and store.
2794 if (Token.is(MIToken::Identifier) && Token.stringValue() == "store") {
2795 Flags |= MachineMemOperand::MOStore;
2796 lex();
2797 }
2798
2799 // Optional synchronization scope.
2800 SyncScope::ID SSID;
2801 if (parseOptionalScope(MF.getFunction().getContext(), SSID))
2802 return true;
2803
2804 // Up to two atomic orderings (cmpxchg provides guarantees on failure).
2805 AtomicOrdering Order, FailureOrder;
2806 if (parseOptionalAtomicOrdering(Order))
2807 return true;
2808
2809 if (parseOptionalAtomicOrdering(FailureOrder))
2810 return true;
2811
2812 if (Token.isNot(MIToken::IntegerLiteral) &&
2813 Token.isNot(MIToken::kw_unknown_size))
2814 return error("expected the size integer literal or 'unknown-size' after "
2815 "memory operation");
2816 uint64_t Size;
2817 if (Token.is(MIToken::IntegerLiteral)) {
2818 if (getUint64(Size))
2819 return true;
2820 } else if (Token.is(MIToken::kw_unknown_size)) {
2821 Size = MemoryLocation::UnknownSize;
2822 }
2823 lex();
2824
2825 MachinePointerInfo Ptr = MachinePointerInfo();
2826 if (Token.is(MIToken::Identifier)) {
2827 const char *Word =
2828 ((Flags & MachineMemOperand::MOLoad) &&
2829 (Flags & MachineMemOperand::MOStore))
2830 ? "on"
2831 : Flags & MachineMemOperand::MOLoad ? "from" : "into";
2832 if (Token.stringValue() != Word)
2833 return error(Twine("expected '") + Word + "'");
2834 lex();
2835
2836 if (parseMachinePointerInfo(Ptr))
2837 return true;
2838 }
2839 unsigned BaseAlignment = (Size != MemoryLocation::UnknownSize ? Size : 1);
2840 AAMDNodes AAInfo;
2841 MDNode *Range = nullptr;
2842 while (consumeIfPresent(MIToken::comma)) {
2843 switch (Token.kind()) {
2844 case MIToken::kw_align:
2845 if (parseAlignment(BaseAlignment))
2846 return true;
2847 break;
2848 case MIToken::kw_addrspace:
2849 if (parseAddrspace(Ptr.AddrSpace))
2850 return true;
2851 break;
2852 case MIToken::md_tbaa:
2853 lex();
2854 if (parseMDNode(AAInfo.TBAA))
2855 return true;
2856 break;
2857 case MIToken::md_alias_scope:
2858 lex();
2859 if (parseMDNode(AAInfo.Scope))
2860 return true;
2861 break;
2862 case MIToken::md_noalias:
2863 lex();
2864 if (parseMDNode(AAInfo.NoAlias))
2865 return true;
2866 break;
2867 case MIToken::md_range:
2868 lex();
2869 if (parseMDNode(Range))
2870 return true;
2871 break;
2872 // TODO: Report an error on duplicate metadata nodes.
2873 default:
2874 return error("expected 'align' or '!tbaa' or '!alias.scope' or "
2875 "'!noalias' or '!range'");
2876 }
2877 }
2878 if (expectAndConsume(MIToken::rparen))
2879 return true;
2880 Dest = MF.getMachineMemOperand(Ptr, Flags, Size, BaseAlignment, AAInfo, Range,
2881 SSID, Order, FailureOrder);
2882 return false;
2883}
2884
2885bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) {
2886 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2888, __PRETTY_FUNCTION__))
2887 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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2888, __PRETTY_FUNCTION__))
2888 "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~svn358520/lib/CodeGen/MIRParser/MIParser.cpp"
, 2888, __PRETTY_FUNCTION__))
;
2889 lex();
2890 if (Token.isNot(MIToken::MCSymbol))
2891 return error("expected a symbol after 'pre-instr-symbol'");
2892 Symbol = getOrCreateMCSymbol(Token.stringValue());
2893 lex();
2894 if (Token.isNewlineOrEOF() || Token.is(MIToken::coloncolon) ||
2895 Token.is(MIToken::lbrace))
2896 return false;
2897 if (Token.isNot(MIToken::comma))
2898 return error("expected ',' before the next machine operand");
2899 lex();
2900 return false;
2901}
2902
2903static void initSlots2BasicBlocks(
2904 const Function &F,
2905 DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2906 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2907 MST.incorporateFunction(F);
2908 for (auto &BB : F) {
2909 if (BB.hasName())
2910 continue;
2911 int Slot = MST.getLocalSlot(&BB);
2912 if (Slot == -1)
2913 continue;
2914 Slots2BasicBlocks.insert(std::make_pair(unsigned(Slot), &BB));
2915 }
2916}
2917
2918static const BasicBlock *getIRBlockFromSlot(
2919 unsigned Slot,
2920 const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) {
2921 auto BlockInfo = Slots2BasicBlocks.find(Slot);
2922 if (BlockInfo == Slots2BasicBlocks.end())
2923 return nullptr;
2924 return BlockInfo->second;
2925}
2926
2927const BasicBlock *MIParser::getIRBlock(unsigned Slot) {
2928 if (Slots2BasicBlocks.empty())
2929 initSlots2BasicBlocks(MF.getFunction(), Slots2BasicBlocks);
2930 return getIRBlockFromSlot(Slot, Slots2BasicBlocks);
2931}
2932
2933const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) {
2934 if (&F == &MF.getFunction())
2935 return getIRBlock(Slot);
2936 DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks;
2937 initSlots2BasicBlocks(F, CustomSlots2BasicBlocks);
2938 return getIRBlockFromSlot(Slot, CustomSlots2BasicBlocks);
2939}
2940
2941static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST,
2942 DenseMap<unsigned, const Value *> &Slots2Values) {
2943 int Slot = MST.getLocalSlot(V);
2944 if (Slot == -1)
2945 return;
2946 Slots2Values.insert(std::make_pair(unsigned(Slot), V));
2947}
2948
2949/// Creates the mapping from slot numbers to function's unnamed IR values.
2950static void initSlots2Values(const Function &F,
2951 DenseMap<unsigned, const Value *> &Slots2Values) {
2952 ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false);
2953 MST.incorporateFunction(F);
2954 for (const auto &Arg : F.args())
2955 mapValueToSlot(&Arg, MST, Slots2Values);
2956 for (const auto &BB : F) {
2957 mapValueToSlot(&BB, MST, Slots2Values);
2958 for (const auto &I : BB)
2959 mapValueToSlot(&I, MST, Slots2Values);
2960 }
2961}
2962
2963const Value *MIParser::getIRValue(unsigned Slot) {
2964 if (Slots2Values.empty())
2965 initSlots2Values(MF.getFunction(), Slots2Values);
2966 auto ValueInfo = Slots2Values.find(Slot);
2967 if (ValueInfo == Slots2Values.end())
2968 return nullptr;
2969 return ValueInfo->second;
2970}
2971
2972MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) {
2973 // FIXME: Currently we can't recognize temporary or local symbols and call all
2974 // of the appropriate forms to create them. However, this handles basic cases
2975 // well as most of the special aspects are recognized by a prefix on their
2976 // name, and the input names should already be unique. For test cases, keeping
2977 // the symbol name out of the symbol table isn't terribly important.
2978 return MF.getContext().getOrCreateSymbol(Name);
2979}
2980
2981bool MIParser::parseStringConstant(std::string &Result) {
2982 if (Token.isNot(MIToken::StringConstant))
2983 return error("expected string constant");
2984 Result = Token.stringValue();
2985 lex();
2986 return false;
2987}
2988
2989bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS,
2990 StringRef Src,
2991 SMDiagnostic &Error) {
2992 return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(PFS.MBBSlots);
2993}
2994
2995bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS,
2996 StringRef Src, SMDiagnostic &Error) {
2997 return MIParser(PFS, Error, Src).parseBasicBlocks();
1
Calling 'MIParser::parseBasicBlocks'
2998}
2999
3000bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS,
3001 MachineBasicBlock *&MBB, StringRef Src,
3002 SMDiagnostic &Error) {
3003 return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB);
3004}
3005
3006bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS,
3007 unsigned &Reg, StringRef Src,
3008 SMDiagnostic &Error) {
3009 return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg);
3010}
3011
3012bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS,
3013 unsigned &Reg, StringRef Src,
3014 SMDiagnostic &Error) {
3015 return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg);
3016}
3017
3018bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS,
3019 VRegInfo *&Info, StringRef Src,
3020 SMDiagnostic &Error) {
3021 return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info);
3022}
3023
3024bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS,
3025 int &FI, StringRef Src,
3026 SMDiagnostic &Error) {
3027 return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI);
3028}
3029
3030bool llvm::parseMDNode(PerFunctionMIParsingState &PFS,
3031 MDNode *&Node, StringRef Src, SMDiagnostic &Error) {
3032 return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node);
3033}