| 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/SmallVector.h" |
| 20 | #include "llvm/ADT/StringMap.h" |
| 21 | #include "llvm/ADT/StringRef.h" |
| 22 | #include "llvm/ADT/StringSwitch.h" |
| 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/AsmParser/Parser.h" |
| 25 | #include "llvm/AsmParser/SlotMapping.h" |
| 26 | #include "llvm/CodeGen/MIRFormatter.h" |
| 27 | #include "llvm/CodeGen/MIRPrinter.h" |
| 28 | #include "llvm/CodeGen/MachineBasicBlock.h" |
| 29 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 30 | #include "llvm/CodeGen/MachineFunction.h" |
| 31 | #include "llvm/CodeGen/MachineInstr.h" |
| 32 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 33 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 34 | #include "llvm/CodeGen/MachineOperand.h" |
| 35 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 36 | #include "llvm/CodeGen/PseudoSourceValueManager.h" |
| 37 | #include "llvm/CodeGen/RegisterBank.h" |
| 38 | #include "llvm/CodeGen/RegisterBankInfo.h" |
| 39 | #include "llvm/CodeGen/TargetInstrInfo.h" |
| 40 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
| 41 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 42 | #include "llvm/CodeGenTypes/LowLevelType.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/Support/AtomicOrdering.h" |
| 63 | #include "llvm/Support/BranchProbability.h" |
| 64 | #include "llvm/Support/Casting.h" |
| 65 | #include "llvm/Support/ErrorHandling.h" |
| 66 | #include "llvm/Support/MemoryBuffer.h" |
| 67 | #include "llvm/Support/SMLoc.h" |
| 68 | #include "llvm/Support/SourceMgr.h" |
| 69 | #include "llvm/Target/TargetMachine.h" |
| 70 | #include <cassert> |
| 71 | #include <cctype> |
| 72 | #include <cstddef> |
| 73 | #include <cstdint> |
| 74 | #include <limits> |
| 75 | #include <string> |
| 76 | #include <utility> |
| 77 | |
| 78 | using namespace llvm; |
| 79 | |
| 80 | void PerTargetMIParsingState::setTarget( |
| 81 | const TargetSubtargetInfo &NewSubtarget) { |
| 82 | |
| 83 | // If the subtarget changed, over conservatively assume everything is invalid. |
| 84 | if (&Subtarget == &NewSubtarget) |
| 85 | return; |
| 86 | |
| 87 | Names2InstrOpCodes.clear(); |
| 88 | Names2Regs.clear(); |
| 89 | Names2RegMasks.clear(); |
| 90 | Names2SubRegIndices.clear(); |
| 91 | Names2TargetIndices.clear(); |
| 92 | Names2DirectTargetFlags.clear(); |
| 93 | Names2BitmaskTargetFlags.clear(); |
| 94 | Names2MMOTargetFlags.clear(); |
| 95 | |
| 96 | initNames2RegClasses(); |
| 97 | initNames2RegBanks(); |
| 98 | } |
| 99 | |
| 100 | void PerTargetMIParsingState::initNames2Regs() { |
| 101 | if (!Names2Regs.empty()) |
| 102 | return; |
| 103 | |
| 104 | // The '%noreg' register is the register 0. |
| 105 | Names2Regs.insert(KV: std::make_pair(x: "noreg" , y: 0)); |
| 106 | const auto *TRI = Subtarget.getRegisterInfo(); |
| 107 | assert(TRI && "Expected target register info" ); |
| 108 | |
| 109 | for (unsigned I = 0, E = TRI->getNumRegs(); I < E; ++I) { |
| 110 | bool WasInserted = |
| 111 | Names2Regs.insert(KV: std::make_pair(x: StringRef(TRI->getName(RegNo: I)).lower(), y&: I)) |
| 112 | .second; |
| 113 | (void)WasInserted; |
| 114 | assert(WasInserted && "Expected registers to be unique case-insensitively" ); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | bool PerTargetMIParsingState::getRegisterByName(StringRef RegName, |
| 119 | Register &Reg) { |
| 120 | initNames2Regs(); |
| 121 | auto RegInfo = Names2Regs.find(Key: RegName); |
| 122 | if (RegInfo == Names2Regs.end()) |
| 123 | return true; |
| 124 | Reg = RegInfo->getValue(); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | bool PerTargetMIParsingState::getVRegFlagValue(StringRef FlagName, |
| 129 | uint8_t &FlagValue) const { |
| 130 | const auto *TRI = Subtarget.getRegisterInfo(); |
| 131 | std::optional<uint8_t> FV = TRI->getVRegFlagValue(Name: FlagName); |
| 132 | if (!FV) |
| 133 | return true; |
| 134 | FlagValue = *FV; |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | void PerTargetMIParsingState::initNames2InstrOpCodes() { |
| 139 | if (!Names2InstrOpCodes.empty()) |
| 140 | return; |
| 141 | const auto *TII = Subtarget.getInstrInfo(); |
| 142 | assert(TII && "Expected target instruction info" ); |
| 143 | for (unsigned I = 0, E = TII->getNumOpcodes(); I < E; ++I) |
| 144 | Names2InstrOpCodes.insert(KV: std::make_pair(x: StringRef(TII->getName(Opcode: I)), y&: I)); |
| 145 | } |
| 146 | |
| 147 | bool PerTargetMIParsingState::parseInstrName(StringRef InstrName, |
| 148 | unsigned &OpCode) { |
| 149 | initNames2InstrOpCodes(); |
| 150 | auto InstrInfo = Names2InstrOpCodes.find(Key: InstrName); |
| 151 | if (InstrInfo == Names2InstrOpCodes.end()) |
| 152 | return true; |
| 153 | OpCode = InstrInfo->getValue(); |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | void PerTargetMIParsingState::initNames2RegMasks() { |
| 158 | if (!Names2RegMasks.empty()) |
| 159 | return; |
| 160 | const auto *TRI = Subtarget.getRegisterInfo(); |
| 161 | assert(TRI && "Expected target register info" ); |
| 162 | ArrayRef<const uint32_t *> RegMasks = TRI->getRegMasks(); |
| 163 | ArrayRef<const char *> RegMaskNames = TRI->getRegMaskNames(); |
| 164 | assert(RegMasks.size() == RegMaskNames.size()); |
| 165 | for (size_t I = 0, E = RegMasks.size(); I < E; ++I) |
| 166 | Names2RegMasks.insert( |
| 167 | KV: std::make_pair(x: StringRef(RegMaskNames[I]).lower(), y: RegMasks[I])); |
| 168 | } |
| 169 | |
| 170 | const uint32_t *PerTargetMIParsingState::getRegMask(StringRef Identifier) { |
| 171 | initNames2RegMasks(); |
| 172 | auto RegMaskInfo = Names2RegMasks.find(Key: Identifier); |
| 173 | if (RegMaskInfo == Names2RegMasks.end()) |
| 174 | return nullptr; |
| 175 | return RegMaskInfo->getValue(); |
| 176 | } |
| 177 | |
| 178 | void PerTargetMIParsingState::initNames2SubRegIndices() { |
| 179 | if (!Names2SubRegIndices.empty()) |
| 180 | return; |
| 181 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| 182 | for (unsigned I = 1, E = TRI->getNumSubRegIndices(); I < E; ++I) |
| 183 | Names2SubRegIndices.insert( |
| 184 | KV: std::make_pair(x: TRI->getSubRegIndexName(SubIdx: I), y&: I)); |
| 185 | } |
| 186 | |
| 187 | unsigned PerTargetMIParsingState::getSubRegIndex(StringRef Name) { |
| 188 | initNames2SubRegIndices(); |
| 189 | auto SubRegInfo = Names2SubRegIndices.find(Key: Name); |
| 190 | if (SubRegInfo == Names2SubRegIndices.end()) |
| 191 | return 0; |
| 192 | return SubRegInfo->getValue(); |
| 193 | } |
| 194 | |
| 195 | void PerTargetMIParsingState::initNames2TargetIndices() { |
| 196 | if (!Names2TargetIndices.empty()) |
| 197 | return; |
| 198 | const auto *TII = Subtarget.getInstrInfo(); |
| 199 | assert(TII && "Expected target instruction info" ); |
| 200 | auto Indices = TII->getSerializableTargetIndices(); |
| 201 | for (const auto &I : Indices) |
| 202 | Names2TargetIndices.insert(KV: std::make_pair(x: StringRef(I.second), y: I.first)); |
| 203 | } |
| 204 | |
| 205 | bool PerTargetMIParsingState::getTargetIndex(StringRef Name, int &Index) { |
| 206 | initNames2TargetIndices(); |
| 207 | auto IndexInfo = Names2TargetIndices.find(Key: Name); |
| 208 | if (IndexInfo == Names2TargetIndices.end()) |
| 209 | return true; |
| 210 | Index = IndexInfo->second; |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | void PerTargetMIParsingState::initNames2DirectTargetFlags() { |
| 215 | if (!Names2DirectTargetFlags.empty()) |
| 216 | return; |
| 217 | |
| 218 | const auto *TII = Subtarget.getInstrInfo(); |
| 219 | assert(TII && "Expected target instruction info" ); |
| 220 | auto Flags = TII->getSerializableDirectMachineOperandTargetFlags(); |
| 221 | for (const auto &I : Flags) |
| 222 | Names2DirectTargetFlags.insert( |
| 223 | KV: std::make_pair(x: StringRef(I.second), y: I.first)); |
| 224 | } |
| 225 | |
| 226 | bool PerTargetMIParsingState::getDirectTargetFlag(StringRef Name, |
| 227 | unsigned &Flag) { |
| 228 | initNames2DirectTargetFlags(); |
| 229 | auto FlagInfo = Names2DirectTargetFlags.find(Key: Name); |
| 230 | if (FlagInfo == Names2DirectTargetFlags.end()) |
| 231 | return true; |
| 232 | Flag = FlagInfo->second; |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | void PerTargetMIParsingState::initNames2BitmaskTargetFlags() { |
| 237 | if (!Names2BitmaskTargetFlags.empty()) |
| 238 | return; |
| 239 | |
| 240 | const auto *TII = Subtarget.getInstrInfo(); |
| 241 | assert(TII && "Expected target instruction info" ); |
| 242 | auto Flags = TII->getSerializableBitmaskMachineOperandTargetFlags(); |
| 243 | for (const auto &I : Flags) |
| 244 | Names2BitmaskTargetFlags.insert( |
| 245 | KV: std::make_pair(x: StringRef(I.second), y: I.first)); |
| 246 | } |
| 247 | |
| 248 | bool PerTargetMIParsingState::getBitmaskTargetFlag(StringRef Name, |
| 249 | unsigned &Flag) { |
| 250 | initNames2BitmaskTargetFlags(); |
| 251 | auto FlagInfo = Names2BitmaskTargetFlags.find(Key: Name); |
| 252 | if (FlagInfo == Names2BitmaskTargetFlags.end()) |
| 253 | return true; |
| 254 | Flag = FlagInfo->second; |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | void PerTargetMIParsingState::initNames2MMOTargetFlags() { |
| 259 | if (!Names2MMOTargetFlags.empty()) |
| 260 | return; |
| 261 | |
| 262 | const auto *TII = Subtarget.getInstrInfo(); |
| 263 | assert(TII && "Expected target instruction info" ); |
| 264 | auto Flags = TII->getSerializableMachineMemOperandTargetFlags(); |
| 265 | for (const auto &I : Flags) |
| 266 | Names2MMOTargetFlags.insert(KV: std::make_pair(x: StringRef(I.second), y: I.first)); |
| 267 | } |
| 268 | |
| 269 | bool PerTargetMIParsingState::getMMOTargetFlag(StringRef Name, |
| 270 | MachineMemOperand::Flags &Flag) { |
| 271 | initNames2MMOTargetFlags(); |
| 272 | auto FlagInfo = Names2MMOTargetFlags.find(Key: Name); |
| 273 | if (FlagInfo == Names2MMOTargetFlags.end()) |
| 274 | return true; |
| 275 | Flag = FlagInfo->second; |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | void PerTargetMIParsingState::initNames2RegClasses() { |
| 280 | if (!Names2RegClasses.empty()) |
| 281 | return; |
| 282 | |
| 283 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); |
| 284 | for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) { |
| 285 | const auto *RC = TRI->getRegClass(i: I); |
| 286 | Names2RegClasses.insert( |
| 287 | KV: std::make_pair(x: StringRef(TRI->getRegClassName(Class: RC)).lower(), y&: RC)); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | void PerTargetMIParsingState::initNames2RegBanks() { |
| 292 | if (!Names2RegBanks.empty()) |
| 293 | return; |
| 294 | |
| 295 | const RegisterBankInfo *RBI = Subtarget.getRegBankInfo(); |
| 296 | // If the target does not support GlobalISel, we may not have a |
| 297 | // register bank info. |
| 298 | if (!RBI) |
| 299 | return; |
| 300 | |
| 301 | for (unsigned I = 0, E = RBI->getNumRegBanks(); I < E; ++I) { |
| 302 | const auto &RegBank = RBI->getRegBank(ID: I); |
| 303 | Names2RegBanks.insert( |
| 304 | KV: std::make_pair(x: StringRef(RegBank.getName()).lower(), y: &RegBank)); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | const TargetRegisterClass * |
| 309 | PerTargetMIParsingState::getRegClass(StringRef Name) { |
| 310 | auto RegClassInfo = Names2RegClasses.find(Key: Name); |
| 311 | if (RegClassInfo == Names2RegClasses.end()) |
| 312 | return nullptr; |
| 313 | return RegClassInfo->getValue(); |
| 314 | } |
| 315 | |
| 316 | const RegisterBank *PerTargetMIParsingState::getRegBank(StringRef Name) { |
| 317 | auto RegBankInfo = Names2RegBanks.find(Key: Name); |
| 318 | if (RegBankInfo == Names2RegBanks.end()) |
| 319 | return nullptr; |
| 320 | return RegBankInfo->getValue(); |
| 321 | } |
| 322 | |
| 323 | PerFunctionMIParsingState::PerFunctionMIParsingState(MachineFunction &MF, |
| 324 | SourceMgr &SM, const SlotMapping &IRSlots, PerTargetMIParsingState &T) |
| 325 | : MF(MF), SM(&SM), IRSlots(IRSlots), Target(T) { |
| 326 | } |
| 327 | |
| 328 | VRegInfo &PerFunctionMIParsingState::getVRegInfo(Register Num) { |
| 329 | auto I = VRegInfos.try_emplace(Key: Num); |
| 330 | if (I.second) { |
| 331 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 332 | VRegInfo *Info = new (Allocator) VRegInfo; |
| 333 | Info->VReg = MRI.createIncompleteVirtualRegister(); |
| 334 | I.first->second = Info; |
| 335 | } |
| 336 | return *I.first->second; |
| 337 | } |
| 338 | |
| 339 | VRegInfo &PerFunctionMIParsingState::getVRegInfoNamed(StringRef RegName) { |
| 340 | assert(RegName != "" && "Expected named reg." ); |
| 341 | |
| 342 | auto I = VRegInfosNamed.try_emplace(Key: RegName.str()); |
| 343 | if (I.second) { |
| 344 | VRegInfo *Info = new (Allocator) VRegInfo; |
| 345 | Info->VReg = MF.getRegInfo().createIncompleteVirtualRegister(Name: RegName); |
| 346 | I.first->second = Info; |
| 347 | } |
| 348 | return *I.first->second; |
| 349 | } |
| 350 | |
| 351 | static void mapValueToSlot(const Value *V, ModuleSlotTracker &MST, |
| 352 | DenseMap<unsigned, const Value *> &Slots2Values) { |
| 353 | int Slot = MST.getLocalSlot(V); |
| 354 | if (Slot == -1) |
| 355 | return; |
| 356 | Slots2Values.insert(KV: std::make_pair(x: unsigned(Slot), y&: V)); |
| 357 | } |
| 358 | |
| 359 | /// Creates the mapping from slot numbers to function's unnamed IR values. |
| 360 | static void initSlots2Values(const Function &F, |
| 361 | DenseMap<unsigned, const Value *> &Slots2Values) { |
| 362 | ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); |
| 363 | MST.incorporateFunction(F); |
| 364 | for (const auto &Arg : F.args()) |
| 365 | mapValueToSlot(V: &Arg, MST, Slots2Values); |
| 366 | for (const auto &BB : F) { |
| 367 | mapValueToSlot(V: &BB, MST, Slots2Values); |
| 368 | for (const auto &I : BB) |
| 369 | mapValueToSlot(V: &I, MST, Slots2Values); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | const Value* PerFunctionMIParsingState::getIRValue(unsigned Slot) { |
| 374 | if (Slots2Values.empty()) |
| 375 | initSlots2Values(F: MF.getFunction(), Slots2Values); |
| 376 | return Slots2Values.lookup(Val: Slot); |
| 377 | } |
| 378 | |
| 379 | namespace { |
| 380 | |
| 381 | /// A wrapper struct around the 'MachineOperand' struct that includes a source |
| 382 | /// range and other attributes. |
| 383 | struct ParsedMachineOperand { |
| 384 | MachineOperand Operand; |
| 385 | StringRef::iterator Begin; |
| 386 | StringRef::iterator End; |
| 387 | std::optional<unsigned> TiedDefIdx; |
| 388 | |
| 389 | ParsedMachineOperand(const MachineOperand &Operand, StringRef::iterator Begin, |
| 390 | StringRef::iterator End, |
| 391 | std::optional<unsigned> &TiedDefIdx) |
| 392 | : Operand(Operand), Begin(Begin), End(End), TiedDefIdx(TiedDefIdx) { |
| 393 | if (TiedDefIdx) |
| 394 | assert(Operand.isReg() && Operand.isUse() && |
| 395 | "Only used register operands can be tied" ); |
| 396 | } |
| 397 | }; |
| 398 | |
| 399 | class MIParser { |
| 400 | MachineFunction &MF; |
| 401 | SMDiagnostic &Error; |
| 402 | StringRef Source, CurrentSource; |
| 403 | SMRange SourceRange; |
| 404 | MIToken Token; |
| 405 | PerFunctionMIParsingState &PFS; |
| 406 | /// Maps from slot numbers to function's unnamed basic blocks. |
| 407 | DenseMap<unsigned, const BasicBlock *> Slots2BasicBlocks; |
| 408 | |
| 409 | public: |
| 410 | MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error, |
| 411 | StringRef Source); |
| 412 | MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error, |
| 413 | StringRef Source, SMRange SourceRange); |
| 414 | |
| 415 | /// \p SkipChar gives the number of characters to skip before looking |
| 416 | /// for the next token. |
| 417 | void lex(unsigned SkipChar = 0); |
| 418 | |
| 419 | /// Report an error at the current location with the given message. |
| 420 | /// |
| 421 | /// This function always return true. |
| 422 | bool error(const Twine &Msg); |
| 423 | |
| 424 | /// Report an error at the given location with the given message. |
| 425 | /// |
| 426 | /// This function always return true. |
| 427 | bool error(StringRef::iterator Loc, const Twine &Msg); |
| 428 | |
| 429 | bool |
| 430 | parseBasicBlockDefinitions(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); |
| 431 | bool parseBasicBlocks(); |
| 432 | bool parse(MachineInstr *&MI); |
| 433 | bool parseStandaloneMBB(MachineBasicBlock *&MBB); |
| 434 | bool parseStandaloneNamedRegister(Register &Reg); |
| 435 | bool parseStandaloneVirtualRegister(VRegInfo *&Info); |
| 436 | bool parseStandaloneRegister(Register &Reg); |
| 437 | bool parseStandaloneStackObject(int &FI); |
| 438 | bool parseStandaloneMDNode(MDNode *&Node); |
| 439 | bool parseMachineMetadata(); |
| 440 | bool parseMDTuple(MDNode *&MD, bool IsDistinct); |
| 441 | bool parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts); |
| 442 | bool parseMetadata(Metadata *&MD); |
| 443 | |
| 444 | bool |
| 445 | parseBasicBlockDefinition(DenseMap<unsigned, MachineBasicBlock *> &MBBSlots); |
| 446 | bool parseBasicBlock(MachineBasicBlock &MBB, |
| 447 | MachineBasicBlock *&AddFalthroughFrom); |
| 448 | bool parseBasicBlockLiveins(MachineBasicBlock &MBB); |
| 449 | bool parseBasicBlockSuccessors(MachineBasicBlock &MBB); |
| 450 | |
| 451 | bool parseNamedRegister(Register &Reg); |
| 452 | bool parseVirtualRegister(VRegInfo *&Info); |
| 453 | bool parseNamedVirtualRegister(VRegInfo *&Info); |
| 454 | bool parseRegister(Register &Reg, VRegInfo *&VRegInfo); |
| 455 | bool parseRegisterFlag(RegState &Flags); |
| 456 | bool parseRegisterClassOrBank(VRegInfo &RegInfo); |
| 457 | bool parseSubRegisterIndex(unsigned &SubReg); |
| 458 | bool parseRegisterTiedDefIndex(unsigned &TiedDefIdx); |
| 459 | bool parseRegisterOperand(MachineOperand &Dest, |
| 460 | std::optional<unsigned> &TiedDefIdx, |
| 461 | bool IsDef = false); |
| 462 | bool parseImmediateOperand(MachineOperand &Dest); |
| 463 | bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue, |
| 464 | const Constant *&C); |
| 465 | bool parseIRConstant(StringRef::iterator Loc, const Constant *&C); |
| 466 | bool parseLowLevelType(StringRef::iterator Loc, LLT &Ty); |
| 467 | bool parseTypedImmediateOperand(MachineOperand &Dest); |
| 468 | bool parseFPImmediateOperand(MachineOperand &Dest); |
| 469 | bool parseMBBReference(MachineBasicBlock *&MBB); |
| 470 | bool parseMBBOperand(MachineOperand &Dest); |
| 471 | bool parseStackFrameIndex(int &FI); |
| 472 | bool parseStackObjectOperand(MachineOperand &Dest); |
| 473 | bool parseFixedStackFrameIndex(int &FI); |
| 474 | bool parseFixedStackObjectOperand(MachineOperand &Dest); |
| 475 | bool parseGlobalValue(GlobalValue *&GV); |
| 476 | bool parseGlobalAddressOperand(MachineOperand &Dest); |
| 477 | bool parseConstantPoolIndexOperand(MachineOperand &Dest); |
| 478 | bool parseSubRegisterIndexOperand(MachineOperand &Dest); |
| 479 | bool parseJumpTableIndexOperand(MachineOperand &Dest); |
| 480 | bool parseExternalSymbolOperand(MachineOperand &Dest); |
| 481 | bool parseMCSymbolOperand(MachineOperand &Dest); |
| 482 | [[nodiscard]] bool parseMDNode(MDNode *&Node); |
| 483 | bool parseDIExpression(MDNode *&Expr); |
| 484 | bool parseDILocation(MDNode *&Expr); |
| 485 | bool parseMetadataOperand(MachineOperand &Dest); |
| 486 | bool parseCFIOffset(int &Offset); |
| 487 | bool parseCFIRegister(unsigned &Reg); |
| 488 | bool parseCFIAddressSpace(unsigned &AddressSpace); |
| 489 | bool parseCFIEscapeValues(std::string& Values); |
| 490 | bool parseCFIOperand(MachineOperand &Dest); |
| 491 | bool parseIRBlock(BasicBlock *&BB, const Function &F); |
| 492 | bool parseBlockAddressOperand(MachineOperand &Dest); |
| 493 | bool parseIntrinsicOperand(MachineOperand &Dest); |
| 494 | bool parsePredicateOperand(MachineOperand &Dest); |
| 495 | bool parseShuffleMaskOperand(MachineOperand &Dest); |
| 496 | bool parseTargetIndexOperand(MachineOperand &Dest); |
| 497 | bool parseDbgInstrRefOperand(MachineOperand &Dest); |
| 498 | bool parseCustomRegisterMaskOperand(MachineOperand &Dest); |
| 499 | bool parseLaneMaskOperand(MachineOperand &Dest); |
| 500 | bool parseLiveoutRegisterMaskOperand(MachineOperand &Dest); |
| 501 | bool parseMachineOperand(const unsigned OpCode, const unsigned OpIdx, |
| 502 | MachineOperand &Dest, |
| 503 | std::optional<unsigned> &TiedDefIdx); |
| 504 | bool parseMachineOperandAndTargetFlags(const unsigned OpCode, |
| 505 | const unsigned OpIdx, |
| 506 | MachineOperand &Dest, |
| 507 | std::optional<unsigned> &TiedDefIdx); |
| 508 | bool parseOffset(int64_t &Offset); |
| 509 | bool parseIRBlockAddressTaken(BasicBlock *&BB); |
| 510 | bool parseAlignment(uint64_t &Alignment); |
| 511 | bool parseAddrspace(unsigned &Addrspace); |
| 512 | bool parseSectionID(std::optional<MBBSectionID> &SID); |
| 513 | bool parseBBID(std::optional<UniqueBBID> &BBID); |
| 514 | bool parseCallFrameSize(unsigned &CallFrameSize); |
| 515 | bool parseOperandsOffset(MachineOperand &Op); |
| 516 | bool parseIRValue(const Value *&V); |
| 517 | bool parseMemoryOperandFlag(MachineMemOperand::Flags &Flags); |
| 518 | bool parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV); |
| 519 | bool parseMachinePointerInfo(MachinePointerInfo &Dest); |
| 520 | bool parseOptionalScope(LLVMContext &Context, SyncScope::ID &SSID); |
| 521 | bool parseOptionalAtomicOrdering(AtomicOrdering &Order); |
| 522 | bool parseMachineMemoryOperand(MachineMemOperand *&Dest); |
| 523 | bool parsePreOrPostInstrSymbol(MCSymbol *&Symbol); |
| 524 | bool parseHeapAllocMarker(MDNode *&Node); |
| 525 | bool parsePCSections(MDNode *&Node); |
| 526 | |
| 527 | bool parseTargetImmMnemonic(const unsigned OpCode, const unsigned OpIdx, |
| 528 | MachineOperand &Dest, const MIRFormatter &MF); |
| 529 | |
| 530 | private: |
| 531 | /// Convert the integer literal in the current token into an unsigned integer. |
| 532 | /// |
| 533 | /// Return true if an error occurred. |
| 534 | bool getUnsigned(unsigned &Result); |
| 535 | |
| 536 | /// Convert the integer literal in the current token into an uint64. |
| 537 | /// |
| 538 | /// Return true if an error occurred. |
| 539 | bool getUint64(uint64_t &Result); |
| 540 | |
| 541 | /// Convert the hexadecimal literal in the current token into an unsigned |
| 542 | /// APInt with a minimum bitwidth required to represent the value. |
| 543 | /// |
| 544 | /// Return true if the literal does not represent an integer value. |
| 545 | bool getHexUint(APInt &Result); |
| 546 | |
| 547 | /// If the current token is of the given kind, consume it and return false. |
| 548 | /// Otherwise report an error and return true. |
| 549 | bool expectAndConsume(MIToken::TokenKind TokenKind); |
| 550 | |
| 551 | /// If the current token is of the given kind, consume it and return true. |
| 552 | /// Otherwise return false. |
| 553 | bool consumeIfPresent(MIToken::TokenKind TokenKind); |
| 554 | |
| 555 | bool parseInstruction(unsigned &OpCode, unsigned &Flags); |
| 556 | |
| 557 | bool assignRegisterTies(MachineInstr &MI, |
| 558 | ArrayRef<ParsedMachineOperand> Operands); |
| 559 | |
| 560 | bool verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, |
| 561 | const MCInstrDesc &MCID); |
| 562 | |
| 563 | const BasicBlock *getIRBlock(unsigned Slot); |
| 564 | const BasicBlock *getIRBlock(unsigned Slot, const Function &F); |
| 565 | |
| 566 | /// Get or create an MCSymbol for a given name. |
| 567 | MCSymbol *getOrCreateMCSymbol(StringRef Name); |
| 568 | |
| 569 | /// parseStringConstant |
| 570 | /// ::= StringConstant |
| 571 | bool parseStringConstant(std::string &Result); |
| 572 | |
| 573 | /// Map the location in the MI string to the corresponding location specified |
| 574 | /// in `SourceRange`. |
| 575 | SMLoc mapSMLoc(StringRef::iterator Loc); |
| 576 | }; |
| 577 | |
| 578 | } // end anonymous namespace |
| 579 | |
| 580 | MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error, |
| 581 | StringRef Source) |
| 582 | : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), PFS(PFS) |
| 583 | {} |
| 584 | |
| 585 | MIParser::MIParser(PerFunctionMIParsingState &PFS, SMDiagnostic &Error, |
| 586 | StringRef Source, SMRange SourceRange) |
| 587 | : MF(PFS.MF), Error(Error), Source(Source), CurrentSource(Source), |
| 588 | SourceRange(SourceRange), PFS(PFS) {} |
| 589 | |
| 590 | void MIParser::lex(unsigned SkipChar) { |
| 591 | CurrentSource = lexMIToken( |
| 592 | Source: CurrentSource.substr(Start: SkipChar), Token, |
| 593 | ErrorCallback: [this](StringRef::iterator Loc, const Twine &Msg) { error(Loc, Msg); }); |
| 594 | } |
| 595 | |
| 596 | bool MIParser::error(const Twine &Msg) { return error(Loc: Token.location(), Msg); } |
| 597 | |
| 598 | bool MIParser::error(StringRef::iterator Loc, const Twine &Msg) { |
| 599 | const SourceMgr &SM = *PFS.SM; |
| 600 | assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); |
| 601 | const MemoryBuffer &Buffer = *SM.getMemoryBuffer(i: SM.getMainFileID()); |
| 602 | if (Loc >= Buffer.getBufferStart() && Loc <= Buffer.getBufferEnd()) { |
| 603 | // Create an ordinary diagnostic when the source manager's buffer is the |
| 604 | // source string. |
| 605 | Error = SM.GetMessage(Loc: SMLoc::getFromPointer(Ptr: Loc), Kind: SourceMgr::DK_Error, Msg); |
| 606 | return true; |
| 607 | } |
| 608 | // Create a diagnostic for a YAML string literal. |
| 609 | Error = SMDiagnostic(SM, SMLoc(), Buffer.getBufferIdentifier(), 1, |
| 610 | Loc - Source.data(), SourceMgr::DK_Error, Msg.str(), |
| 611 | Source, {}, {}); |
| 612 | return true; |
| 613 | } |
| 614 | |
| 615 | SMLoc MIParser::mapSMLoc(StringRef::iterator Loc) { |
| 616 | assert(SourceRange.isValid() && "Invalid source range" ); |
| 617 | assert(Loc >= Source.data() && Loc <= (Source.data() + Source.size())); |
| 618 | return SMLoc::getFromPointer(Ptr: SourceRange.Start.getPointer() + |
| 619 | (Loc - Source.data())); |
| 620 | } |
| 621 | |
| 622 | typedef function_ref<bool(StringRef::iterator Loc, const Twine &)> |
| 623 | ErrorCallbackType; |
| 624 | |
| 625 | static const char *toString(MIToken::TokenKind TokenKind) { |
| 626 | switch (TokenKind) { |
| 627 | case MIToken::comma: |
| 628 | return "','" ; |
| 629 | case MIToken::equal: |
| 630 | return "'='" ; |
| 631 | case MIToken::colon: |
| 632 | return "':'" ; |
| 633 | case MIToken::lparen: |
| 634 | return "'('" ; |
| 635 | case MIToken::rparen: |
| 636 | return "')'" ; |
| 637 | default: |
| 638 | return "<unknown token>" ; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | bool MIParser::expectAndConsume(MIToken::TokenKind TokenKind) { |
| 643 | if (Token.isNot(K: TokenKind)) |
| 644 | return error(Msg: Twine("expected " ) + toString(TokenKind)); |
| 645 | lex(); |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | bool MIParser::consumeIfPresent(MIToken::TokenKind TokenKind) { |
| 650 | if (Token.isNot(K: TokenKind)) |
| 651 | return false; |
| 652 | lex(); |
| 653 | return true; |
| 654 | } |
| 655 | |
| 656 | // Parse Machine Basic Block Section ID. |
| 657 | bool MIParser::parseSectionID(std::optional<MBBSectionID> &SID) { |
| 658 | assert(Token.is(MIToken::kw_bbsections)); |
| 659 | lex(); |
| 660 | if (Token.is(K: MIToken::IntegerLiteral)) { |
| 661 | unsigned Value = 0; |
| 662 | if (getUnsigned(Result&: Value)) |
| 663 | return error(Msg: "Unknown Section ID" ); |
| 664 | SID = MBBSectionID{Value}; |
| 665 | } else { |
| 666 | const StringRef &S = Token.stringValue(); |
| 667 | if (S == "Exception" ) |
| 668 | SID = MBBSectionID::ExceptionSectionID; |
| 669 | else if (S == "Cold" ) |
| 670 | SID = MBBSectionID::ColdSectionID; |
| 671 | else |
| 672 | return error(Msg: "Unknown Section ID" ); |
| 673 | } |
| 674 | lex(); |
| 675 | return false; |
| 676 | } |
| 677 | |
| 678 | // Parse Machine Basic Block ID. |
| 679 | bool MIParser::parseBBID(std::optional<UniqueBBID> &BBID) { |
| 680 | assert(Token.is(MIToken::kw_bb_id)); |
| 681 | lex(); |
| 682 | unsigned BaseID = 0; |
| 683 | unsigned CloneID = 0; |
| 684 | if (getUnsigned(Result&: BaseID)) |
| 685 | return error(Msg: "Unknown BB ID" ); |
| 686 | lex(); |
| 687 | if (Token.is(K: MIToken::IntegerLiteral)) { |
| 688 | if (getUnsigned(Result&: CloneID)) |
| 689 | return error(Msg: "Unknown Clone ID" ); |
| 690 | lex(); |
| 691 | } |
| 692 | BBID = {.BaseID: BaseID, .CloneID: CloneID}; |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | // Parse basic block call frame size. |
| 697 | bool MIParser::parseCallFrameSize(unsigned &CallFrameSize) { |
| 698 | assert(Token.is(MIToken::kw_call_frame_size)); |
| 699 | lex(); |
| 700 | unsigned Value = 0; |
| 701 | if (getUnsigned(Result&: Value)) |
| 702 | return error(Msg: "Unknown call frame size" ); |
| 703 | CallFrameSize = Value; |
| 704 | lex(); |
| 705 | return false; |
| 706 | } |
| 707 | |
| 708 | bool MIParser::parseBasicBlockDefinition( |
| 709 | DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { |
| 710 | assert(Token.is(MIToken::MachineBasicBlockLabel)); |
| 711 | unsigned ID = 0; |
| 712 | if (getUnsigned(Result&: ID)) |
| 713 | return true; |
| 714 | auto Loc = Token.location(); |
| 715 | auto Name = Token.stringValue(); |
| 716 | lex(); |
| 717 | bool MachineBlockAddressTaken = false; |
| 718 | BasicBlock *AddressTakenIRBlock = nullptr; |
| 719 | bool IsLandingPad = false; |
| 720 | bool IsInlineAsmBrIndirectTarget = false; |
| 721 | bool IsEHFuncletEntry = false; |
| 722 | bool IsEHScopeEntry = false; |
| 723 | std::optional<MBBSectionID> SectionID; |
| 724 | uint64_t Alignment = 0; |
| 725 | std::optional<UniqueBBID> BBID; |
| 726 | unsigned CallFrameSize = 0; |
| 727 | BasicBlock *BB = nullptr; |
| 728 | if (consumeIfPresent(TokenKind: MIToken::lparen)) { |
| 729 | do { |
| 730 | // TODO: Report an error when multiple same attributes are specified. |
| 731 | switch (Token.kind()) { |
| 732 | case MIToken::kw_machine_block_address_taken: |
| 733 | MachineBlockAddressTaken = true; |
| 734 | lex(); |
| 735 | break; |
| 736 | case MIToken::kw_ir_block_address_taken: |
| 737 | if (parseIRBlockAddressTaken(BB&: AddressTakenIRBlock)) |
| 738 | return true; |
| 739 | break; |
| 740 | case MIToken::kw_landing_pad: |
| 741 | IsLandingPad = true; |
| 742 | lex(); |
| 743 | break; |
| 744 | case MIToken::kw_inlineasm_br_indirect_target: |
| 745 | IsInlineAsmBrIndirectTarget = true; |
| 746 | lex(); |
| 747 | break; |
| 748 | case MIToken::kw_ehfunclet_entry: |
| 749 | IsEHFuncletEntry = true; |
| 750 | lex(); |
| 751 | break; |
| 752 | case MIToken::kw_ehscope_entry: |
| 753 | IsEHScopeEntry = true; |
| 754 | lex(); |
| 755 | break; |
| 756 | case MIToken::kw_align: |
| 757 | if (parseAlignment(Alignment)) |
| 758 | return true; |
| 759 | break; |
| 760 | case MIToken::IRBlock: |
| 761 | case MIToken::NamedIRBlock: |
| 762 | // TODO: Report an error when both name and ir block are specified. |
| 763 | if (parseIRBlock(BB, F: MF.getFunction())) |
| 764 | return true; |
| 765 | lex(); |
| 766 | break; |
| 767 | case MIToken::kw_bbsections: |
| 768 | if (parseSectionID(SID&: SectionID)) |
| 769 | return true; |
| 770 | break; |
| 771 | case MIToken::kw_bb_id: |
| 772 | if (parseBBID(BBID)) |
| 773 | return true; |
| 774 | break; |
| 775 | case MIToken::kw_call_frame_size: |
| 776 | if (parseCallFrameSize(CallFrameSize)) |
| 777 | return true; |
| 778 | break; |
| 779 | default: |
| 780 | break; |
| 781 | } |
| 782 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 783 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 784 | return true; |
| 785 | } |
| 786 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 787 | return true; |
| 788 | |
| 789 | if (!Name.empty()) { |
| 790 | BB = dyn_cast_or_null<BasicBlock>( |
| 791 | Val: MF.getFunction().getValueSymbolTable()->lookup(Name)); |
| 792 | if (!BB) |
| 793 | return error(Loc, Msg: Twine("basic block '" ) + Name + |
| 794 | "' is not defined in the function '" + |
| 795 | MF.getName() + "'" ); |
| 796 | } |
| 797 | auto *MBB = MF.CreateMachineBasicBlock(BB, BBID); |
| 798 | MF.insert(MBBI: MF.end(), MBB); |
| 799 | bool WasInserted = MBBSlots.insert(KV: std::make_pair(x&: ID, y&: MBB)).second; |
| 800 | if (!WasInserted) |
| 801 | return error(Loc, Msg: Twine("redefinition of machine basic block with id #" ) + |
| 802 | Twine(ID)); |
| 803 | if (Alignment) |
| 804 | MBB->setAlignment(Align(Alignment)); |
| 805 | if (MachineBlockAddressTaken) |
| 806 | MBB->setMachineBlockAddressTaken(); |
| 807 | if (AddressTakenIRBlock) |
| 808 | MBB->setAddressTakenIRBlock(AddressTakenIRBlock); |
| 809 | MBB->setIsEHPad(IsLandingPad); |
| 810 | MBB->setIsInlineAsmBrIndirectTarget(IsInlineAsmBrIndirectTarget); |
| 811 | MBB->setIsEHFuncletEntry(IsEHFuncletEntry); |
| 812 | MBB->setIsEHScopeEntry(IsEHScopeEntry); |
| 813 | if (SectionID) { |
| 814 | MBB->setSectionID(*SectionID); |
| 815 | MF.setBBSectionsType(BasicBlockSection::List); |
| 816 | } |
| 817 | MBB->setCallFrameSize(CallFrameSize); |
| 818 | return false; |
| 819 | } |
| 820 | |
| 821 | bool MIParser::parseBasicBlockDefinitions( |
| 822 | DenseMap<unsigned, MachineBasicBlock *> &MBBSlots) { |
| 823 | lex(); |
| 824 | // Skip until the first machine basic block. |
| 825 | while (Token.is(K: MIToken::Newline)) |
| 826 | lex(); |
| 827 | if (Token.isErrorOrEOF()) |
| 828 | return Token.isError(); |
| 829 | if (Token.isNot(K: MIToken::MachineBasicBlockLabel)) |
| 830 | return error(Msg: "expected a basic block definition before instructions" ); |
| 831 | unsigned BraceDepth = 0; |
| 832 | do { |
| 833 | if (parseBasicBlockDefinition(MBBSlots)) |
| 834 | return true; |
| 835 | bool IsAfterNewline = false; |
| 836 | // Skip until the next machine basic block. |
| 837 | while (true) { |
| 838 | if ((Token.is(K: MIToken::MachineBasicBlockLabel) && IsAfterNewline) || |
| 839 | Token.isErrorOrEOF()) |
| 840 | break; |
| 841 | else if (Token.is(K: MIToken::MachineBasicBlockLabel)) |
| 842 | return error(Msg: "basic block definition should be located at the start of " |
| 843 | "the line" ); |
| 844 | else if (consumeIfPresent(TokenKind: MIToken::Newline)) { |
| 845 | IsAfterNewline = true; |
| 846 | continue; |
| 847 | } |
| 848 | IsAfterNewline = false; |
| 849 | if (Token.is(K: MIToken::lbrace)) |
| 850 | ++BraceDepth; |
| 851 | if (Token.is(K: MIToken::rbrace)) { |
| 852 | if (!BraceDepth) |
| 853 | return error(Msg: "extraneous closing brace ('}')" ); |
| 854 | --BraceDepth; |
| 855 | } |
| 856 | lex(); |
| 857 | } |
| 858 | // Verify that we closed all of the '{' at the end of a file or a block. |
| 859 | if (!Token.isError() && BraceDepth) |
| 860 | return error(Msg: "expected '}'" ); // FIXME: Report a note that shows '{'. |
| 861 | } while (!Token.isErrorOrEOF()); |
| 862 | return Token.isError(); |
| 863 | } |
| 864 | |
| 865 | bool MIParser::parseBasicBlockLiveins(MachineBasicBlock &MBB) { |
| 866 | assert(Token.is(MIToken::kw_liveins)); |
| 867 | lex(); |
| 868 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 869 | return true; |
| 870 | if (Token.isNewlineOrEOF()) // Allow an empty list of liveins. |
| 871 | return false; |
| 872 | do { |
| 873 | if (Token.isNot(K: MIToken::NamedRegister)) |
| 874 | return error(Msg: "expected a named register" ); |
| 875 | Register Reg; |
| 876 | if (parseNamedRegister(Reg)) |
| 877 | return true; |
| 878 | lex(); |
| 879 | LaneBitmask Mask = LaneBitmask::getAll(); |
| 880 | if (consumeIfPresent(TokenKind: MIToken::colon)) { |
| 881 | // Parse lane mask. |
| 882 | if (Token.isNot(K: MIToken::IntegerLiteral) && |
| 883 | Token.isNot(K: MIToken::HexLiteral)) |
| 884 | return error(Msg: "expected a lane mask" ); |
| 885 | static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t), |
| 886 | "Use correct get-function for lane mask" ); |
| 887 | LaneBitmask::Type V; |
| 888 | if (getUint64(Result&: V)) |
| 889 | return error(Msg: "invalid lane mask value" ); |
| 890 | Mask = LaneBitmask(V); |
| 891 | lex(); |
| 892 | } |
| 893 | MBB.addLiveIn(PhysReg: Reg, LaneMask: Mask); |
| 894 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 895 | return false; |
| 896 | } |
| 897 | |
| 898 | bool MIParser::parseBasicBlockSuccessors(MachineBasicBlock &MBB) { |
| 899 | assert(Token.is(MIToken::kw_successors)); |
| 900 | lex(); |
| 901 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 902 | return true; |
| 903 | if (Token.isNewlineOrEOF()) // Allow an empty list of successors. |
| 904 | return false; |
| 905 | do { |
| 906 | if (Token.isNot(K: MIToken::MachineBasicBlock)) |
| 907 | return error(Msg: "expected a machine basic block reference" ); |
| 908 | MachineBasicBlock *SuccMBB = nullptr; |
| 909 | if (parseMBBReference(MBB&: SuccMBB)) |
| 910 | return true; |
| 911 | lex(); |
| 912 | unsigned Weight = 0; |
| 913 | if (consumeIfPresent(TokenKind: MIToken::lparen)) { |
| 914 | if (Token.isNot(K: MIToken::IntegerLiteral) && |
| 915 | Token.isNot(K: MIToken::HexLiteral)) |
| 916 | return error(Msg: "expected an integer literal after '('" ); |
| 917 | if (getUnsigned(Result&: Weight)) |
| 918 | return true; |
| 919 | lex(); |
| 920 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 921 | return true; |
| 922 | } |
| 923 | MBB.addSuccessor(Succ: SuccMBB, Prob: BranchProbability::getRaw(N: Weight)); |
| 924 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 925 | MBB.normalizeSuccProbs(); |
| 926 | return false; |
| 927 | } |
| 928 | |
| 929 | bool MIParser::parseBasicBlock(MachineBasicBlock &MBB, |
| 930 | MachineBasicBlock *&AddFalthroughFrom) { |
| 931 | // Skip the definition. |
| 932 | assert(Token.is(MIToken::MachineBasicBlockLabel)); |
| 933 | lex(); |
| 934 | if (consumeIfPresent(TokenKind: MIToken::lparen)) { |
| 935 | while (Token.isNot(K: MIToken::rparen) && !Token.isErrorOrEOF()) |
| 936 | lex(); |
| 937 | consumeIfPresent(TokenKind: MIToken::rparen); |
| 938 | } |
| 939 | consumeIfPresent(TokenKind: MIToken::colon); |
| 940 | |
| 941 | // Parse the liveins and successors. |
| 942 | // N.B: Multiple lists of successors and liveins are allowed and they're |
| 943 | // merged into one. |
| 944 | // Example: |
| 945 | // liveins: $edi |
| 946 | // liveins: $esi |
| 947 | // |
| 948 | // is equivalent to |
| 949 | // liveins: $edi, $esi |
| 950 | bool ExplicitSuccessors = false; |
| 951 | while (true) { |
| 952 | if (Token.is(K: MIToken::kw_successors)) { |
| 953 | if (parseBasicBlockSuccessors(MBB)) |
| 954 | return true; |
| 955 | ExplicitSuccessors = true; |
| 956 | } else if (Token.is(K: MIToken::kw_liveins)) { |
| 957 | if (parseBasicBlockLiveins(MBB)) |
| 958 | return true; |
| 959 | } else if (consumeIfPresent(TokenKind: MIToken::Newline)) { |
| 960 | continue; |
| 961 | } else |
| 962 | break; |
| 963 | if (!Token.isNewlineOrEOF()) |
| 964 | return error(Msg: "expected line break at the end of a list" ); |
| 965 | lex(); |
| 966 | } |
| 967 | |
| 968 | // Parse the instructions. |
| 969 | bool IsInBundle = false; |
| 970 | MachineInstr *PrevMI = nullptr; |
| 971 | while (!Token.is(K: MIToken::MachineBasicBlockLabel) && |
| 972 | !Token.is(K: MIToken::Eof)) { |
| 973 | if (consumeIfPresent(TokenKind: MIToken::Newline)) |
| 974 | continue; |
| 975 | if (consumeIfPresent(TokenKind: MIToken::rbrace)) { |
| 976 | // The first parsing pass should verify that all closing '}' have an |
| 977 | // opening '{'. |
| 978 | assert(IsInBundle); |
| 979 | IsInBundle = false; |
| 980 | continue; |
| 981 | } |
| 982 | MachineInstr *MI = nullptr; |
| 983 | if (parse(MI)) |
| 984 | return true; |
| 985 | MBB.insert(I: MBB.end(), MI); |
| 986 | if (IsInBundle) { |
| 987 | PrevMI->setFlag(MachineInstr::BundledSucc); |
| 988 | MI->setFlag(MachineInstr::BundledPred); |
| 989 | } |
| 990 | PrevMI = MI; |
| 991 | if (Token.is(K: MIToken::lbrace)) { |
| 992 | if (IsInBundle) |
| 993 | return error(Msg: "nested instruction bundles are not allowed" ); |
| 994 | lex(); |
| 995 | // This instruction is the start of the bundle. |
| 996 | MI->setFlag(MachineInstr::BundledSucc); |
| 997 | IsInBundle = true; |
| 998 | if (!Token.is(K: MIToken::Newline)) |
| 999 | // The next instruction can be on the same line. |
| 1000 | continue; |
| 1001 | } |
| 1002 | assert(Token.isNewlineOrEOF() && "MI is not fully parsed" ); |
| 1003 | lex(); |
| 1004 | } |
| 1005 | |
| 1006 | // Construct successor list by searching for basic block machine operands. |
| 1007 | if (!ExplicitSuccessors) { |
| 1008 | SmallVector<MachineBasicBlock*,4> Successors; |
| 1009 | bool IsFallthrough; |
| 1010 | guessSuccessors(MBB, Result&: Successors, IsFallthrough); |
| 1011 | for (MachineBasicBlock *Succ : Successors) |
| 1012 | MBB.addSuccessor(Succ); |
| 1013 | |
| 1014 | if (IsFallthrough) { |
| 1015 | AddFalthroughFrom = &MBB; |
| 1016 | } else { |
| 1017 | MBB.normalizeSuccProbs(); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
| 1024 | bool MIParser::parseBasicBlocks() { |
| 1025 | lex(); |
| 1026 | // Skip until the first machine basic block. |
| 1027 | while (Token.is(K: MIToken::Newline)) |
| 1028 | lex(); |
| 1029 | if (Token.isErrorOrEOF()) |
| 1030 | return Token.isError(); |
| 1031 | // The first parsing pass should have verified that this token is a MBB label |
| 1032 | // in the 'parseBasicBlockDefinitions' method. |
| 1033 | assert(Token.is(MIToken::MachineBasicBlockLabel)); |
| 1034 | MachineBasicBlock *AddFalthroughFrom = nullptr; |
| 1035 | do { |
| 1036 | MachineBasicBlock *MBB = nullptr; |
| 1037 | if (parseMBBReference(MBB)) |
| 1038 | return true; |
| 1039 | if (AddFalthroughFrom) { |
| 1040 | if (!AddFalthroughFrom->isSuccessor(MBB)) |
| 1041 | AddFalthroughFrom->addSuccessor(Succ: MBB); |
| 1042 | AddFalthroughFrom->normalizeSuccProbs(); |
| 1043 | AddFalthroughFrom = nullptr; |
| 1044 | } |
| 1045 | if (parseBasicBlock(MBB&: *MBB, AddFalthroughFrom)) |
| 1046 | return true; |
| 1047 | // The method 'parseBasicBlock' should parse the whole block until the next |
| 1048 | // block or the end of file. |
| 1049 | assert(Token.is(MIToken::MachineBasicBlockLabel) || Token.is(MIToken::Eof)); |
| 1050 | } while (Token.isNot(K: MIToken::Eof)); |
| 1051 | return false; |
| 1052 | } |
| 1053 | |
| 1054 | bool MIParser::parse(MachineInstr *&MI) { |
| 1055 | // Parse any register operands before '=' |
| 1056 | MachineOperand MO = MachineOperand::CreateImm(Val: 0); |
| 1057 | SmallVector<ParsedMachineOperand, 8> Operands; |
| 1058 | while (Token.isRegister() || Token.isRegisterFlag()) { |
| 1059 | auto Loc = Token.location(); |
| 1060 | std::optional<unsigned> TiedDefIdx; |
| 1061 | if (parseRegisterOperand(Dest&: MO, TiedDefIdx, /*IsDef=*/true)) |
| 1062 | return true; |
| 1063 | Operands.push_back( |
| 1064 | Elt: ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); |
| 1065 | if (Token.isNot(K: MIToken::comma)) |
| 1066 | break; |
| 1067 | lex(); |
| 1068 | } |
| 1069 | if (!Operands.empty() && expectAndConsume(TokenKind: MIToken::equal)) |
| 1070 | return true; |
| 1071 | |
| 1072 | unsigned OpCode, Flags = 0; |
| 1073 | if (Token.isError() || parseInstruction(OpCode, Flags)) |
| 1074 | return true; |
| 1075 | |
| 1076 | // Parse the remaining machine operands. |
| 1077 | while (!Token.isNewlineOrEOF() && Token.isNot(K: MIToken::kw_pre_instr_symbol) && |
| 1078 | Token.isNot(K: MIToken::kw_post_instr_symbol) && |
| 1079 | Token.isNot(K: MIToken::kw_heap_alloc_marker) && |
| 1080 | Token.isNot(K: MIToken::kw_pcsections) && |
| 1081 | Token.isNot(K: MIToken::kw_cfi_type) && |
| 1082 | Token.isNot(K: MIToken::kw_deactivation_symbol) && |
| 1083 | Token.isNot(K: MIToken::kw_debug_location) && |
| 1084 | Token.isNot(K: MIToken::kw_debug_instr_number) && |
| 1085 | Token.isNot(K: MIToken::coloncolon) && Token.isNot(K: MIToken::lbrace)) { |
| 1086 | auto Loc = Token.location(); |
| 1087 | std::optional<unsigned> TiedDefIdx; |
| 1088 | if (parseMachineOperandAndTargetFlags(OpCode, OpIdx: Operands.size(), Dest&: MO, TiedDefIdx)) |
| 1089 | return true; |
| 1090 | Operands.push_back( |
| 1091 | Elt: ParsedMachineOperand(MO, Loc, Token.location(), TiedDefIdx)); |
| 1092 | if (Token.isNewlineOrEOF() || Token.is(K: MIToken::coloncolon) || |
| 1093 | Token.is(K: MIToken::lbrace)) |
| 1094 | break; |
| 1095 | if (Token.isNot(K: MIToken::comma)) |
| 1096 | return error(Msg: "expected ',' before the next machine operand" ); |
| 1097 | lex(); |
| 1098 | } |
| 1099 | |
| 1100 | MCSymbol *PreInstrSymbol = nullptr; |
| 1101 | if (Token.is(K: MIToken::kw_pre_instr_symbol)) |
| 1102 | if (parsePreOrPostInstrSymbol(Symbol&: PreInstrSymbol)) |
| 1103 | return true; |
| 1104 | MCSymbol *PostInstrSymbol = nullptr; |
| 1105 | if (Token.is(K: MIToken::kw_post_instr_symbol)) |
| 1106 | if (parsePreOrPostInstrSymbol(Symbol&: PostInstrSymbol)) |
| 1107 | return true; |
| 1108 | MDNode *HeapAllocMarker = nullptr; |
| 1109 | if (Token.is(K: MIToken::kw_heap_alloc_marker)) |
| 1110 | if (parseHeapAllocMarker(Node&: HeapAllocMarker)) |
| 1111 | return true; |
| 1112 | MDNode *PCSections = nullptr; |
| 1113 | if (Token.is(K: MIToken::kw_pcsections)) |
| 1114 | if (parsePCSections(Node&: PCSections)) |
| 1115 | return true; |
| 1116 | |
| 1117 | unsigned CFIType = 0; |
| 1118 | if (Token.is(K: MIToken::kw_cfi_type)) { |
| 1119 | lex(); |
| 1120 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 1121 | return error(Msg: "expected an integer literal after 'cfi-type'" ); |
| 1122 | // getUnsigned is sufficient for 32-bit integers. |
| 1123 | if (getUnsigned(Result&: CFIType)) |
| 1124 | return true; |
| 1125 | lex(); |
| 1126 | // Lex past trailing comma if present. |
| 1127 | if (Token.is(K: MIToken::comma)) |
| 1128 | lex(); |
| 1129 | } |
| 1130 | |
| 1131 | GlobalValue *DS = nullptr; |
| 1132 | if (Token.is(K: MIToken::kw_deactivation_symbol)) { |
| 1133 | lex(); |
| 1134 | if (parseGlobalValue(GV&: DS)) |
| 1135 | return true; |
| 1136 | lex(); |
| 1137 | } |
| 1138 | |
| 1139 | unsigned InstrNum = 0; |
| 1140 | if (Token.is(K: MIToken::kw_debug_instr_number)) { |
| 1141 | lex(); |
| 1142 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 1143 | return error(Msg: "expected an integer literal after 'debug-instr-number'" ); |
| 1144 | if (getUnsigned(Result&: InstrNum)) |
| 1145 | return true; |
| 1146 | lex(); |
| 1147 | // Lex past trailing comma if present. |
| 1148 | if (Token.is(K: MIToken::comma)) |
| 1149 | lex(); |
| 1150 | } |
| 1151 | |
| 1152 | DebugLoc DebugLocation; |
| 1153 | if (Token.is(K: MIToken::kw_debug_location)) { |
| 1154 | lex(); |
| 1155 | MDNode *Node = nullptr; |
| 1156 | if (Token.is(K: MIToken::exclaim)) { |
| 1157 | if (parseMDNode(Node)) |
| 1158 | return true; |
| 1159 | } else if (Token.is(K: MIToken::md_dilocation)) { |
| 1160 | if (parseDILocation(Expr&: Node)) |
| 1161 | return true; |
| 1162 | } else |
| 1163 | return error(Msg: "expected a metadata node after 'debug-location'" ); |
| 1164 | if (!isa<DILocation>(Val: Node)) |
| 1165 | return error(Msg: "referenced metadata is not a DILocation" ); |
| 1166 | DebugLocation = DebugLoc(Node); |
| 1167 | } |
| 1168 | |
| 1169 | // Parse the machine memory operands. |
| 1170 | SmallVector<MachineMemOperand *, 2> MemOperands; |
| 1171 | if (Token.is(K: MIToken::coloncolon)) { |
| 1172 | lex(); |
| 1173 | while (!Token.isNewlineOrEOF()) { |
| 1174 | MachineMemOperand *MemOp = nullptr; |
| 1175 | if (parseMachineMemoryOperand(Dest&: MemOp)) |
| 1176 | return true; |
| 1177 | MemOperands.push_back(Elt: MemOp); |
| 1178 | if (Token.isNewlineOrEOF()) |
| 1179 | break; |
| 1180 | if (OpCode == TargetOpcode::BUNDLE && Token.is(K: MIToken::lbrace)) |
| 1181 | break; |
| 1182 | if (Token.isNot(K: MIToken::comma)) |
| 1183 | return error(Msg: "expected ',' before the next machine memory operand" ); |
| 1184 | lex(); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | const auto &MCID = MF.getSubtarget().getInstrInfo()->get(Opcode: OpCode); |
| 1189 | if (!MCID.isVariadic()) { |
| 1190 | // FIXME: Move the implicit operand verification to the machine verifier. |
| 1191 | if (verifyImplicitOperands(Operands, MCID)) |
| 1192 | return true; |
| 1193 | } |
| 1194 | |
| 1195 | MI = MF.CreateMachineInstr(MCID, DL: DebugLocation, /*NoImplicit=*/true); |
| 1196 | MI->setFlags(Flags); |
| 1197 | |
| 1198 | // Don't check the operands make sense, let the verifier catch any |
| 1199 | // improprieties. |
| 1200 | for (const auto &Operand : Operands) |
| 1201 | MI->addOperand(MF, Op: Operand.Operand); |
| 1202 | |
| 1203 | if (assignRegisterTies(MI&: *MI, Operands)) |
| 1204 | return true; |
| 1205 | if (PreInstrSymbol) |
| 1206 | MI->setPreInstrSymbol(MF, Symbol: PreInstrSymbol); |
| 1207 | if (PostInstrSymbol) |
| 1208 | MI->setPostInstrSymbol(MF, Symbol: PostInstrSymbol); |
| 1209 | if (HeapAllocMarker) |
| 1210 | MI->setHeapAllocMarker(MF, MD: HeapAllocMarker); |
| 1211 | if (PCSections) |
| 1212 | MI->setPCSections(MF, MD: PCSections); |
| 1213 | if (CFIType) |
| 1214 | MI->setCFIType(MF, Type: CFIType); |
| 1215 | if (DS) |
| 1216 | MI->setDeactivationSymbol(MF, DS); |
| 1217 | if (!MemOperands.empty()) |
| 1218 | MI->setMemRefs(MF, MemRefs: MemOperands); |
| 1219 | if (InstrNum) |
| 1220 | MI->setDebugInstrNum(InstrNum); |
| 1221 | return false; |
| 1222 | } |
| 1223 | |
| 1224 | bool MIParser::parseStandaloneMBB(MachineBasicBlock *&MBB) { |
| 1225 | lex(); |
| 1226 | if (Token.isNot(K: MIToken::MachineBasicBlock)) |
| 1227 | return error(Msg: "expected a machine basic block reference" ); |
| 1228 | if (parseMBBReference(MBB)) |
| 1229 | return true; |
| 1230 | lex(); |
| 1231 | if (Token.isNot(K: MIToken::Eof)) |
| 1232 | return error( |
| 1233 | Msg: "expected end of string after the machine basic block reference" ); |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | bool MIParser::parseStandaloneNamedRegister(Register &Reg) { |
| 1238 | lex(); |
| 1239 | if (Token.isNot(K: MIToken::NamedRegister)) |
| 1240 | return error(Msg: "expected a named register" ); |
| 1241 | if (parseNamedRegister(Reg)) |
| 1242 | return true; |
| 1243 | lex(); |
| 1244 | if (Token.isNot(K: MIToken::Eof)) |
| 1245 | return error(Msg: "expected end of string after the register reference" ); |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
| 1249 | bool MIParser::parseStandaloneVirtualRegister(VRegInfo *&Info) { |
| 1250 | lex(); |
| 1251 | if (Token.isNot(K: MIToken::VirtualRegister)) |
| 1252 | return error(Msg: "expected a virtual register" ); |
| 1253 | if (parseVirtualRegister(Info)) |
| 1254 | return true; |
| 1255 | lex(); |
| 1256 | if (Token.isNot(K: MIToken::Eof)) |
| 1257 | return error(Msg: "expected end of string after the register reference" ); |
| 1258 | return false; |
| 1259 | } |
| 1260 | |
| 1261 | bool MIParser::parseStandaloneRegister(Register &Reg) { |
| 1262 | lex(); |
| 1263 | if (Token.isNot(K: MIToken::NamedRegister) && |
| 1264 | Token.isNot(K: MIToken::VirtualRegister)) |
| 1265 | return error(Msg: "expected either a named or virtual register" ); |
| 1266 | |
| 1267 | VRegInfo *Info; |
| 1268 | if (parseRegister(Reg, VRegInfo&: Info)) |
| 1269 | return true; |
| 1270 | |
| 1271 | lex(); |
| 1272 | if (Token.isNot(K: MIToken::Eof)) |
| 1273 | return error(Msg: "expected end of string after the register reference" ); |
| 1274 | return false; |
| 1275 | } |
| 1276 | |
| 1277 | bool MIParser::parseStandaloneStackObject(int &FI) { |
| 1278 | lex(); |
| 1279 | if (Token.isNot(K: MIToken::StackObject)) |
| 1280 | return error(Msg: "expected a stack object" ); |
| 1281 | if (parseStackFrameIndex(FI)) |
| 1282 | return true; |
| 1283 | if (Token.isNot(K: MIToken::Eof)) |
| 1284 | return error(Msg: "expected end of string after the stack object reference" ); |
| 1285 | return false; |
| 1286 | } |
| 1287 | |
| 1288 | bool MIParser::parseStandaloneMDNode(MDNode *&Node) { |
| 1289 | lex(); |
| 1290 | if (Token.is(K: MIToken::exclaim)) { |
| 1291 | if (parseMDNode(Node)) |
| 1292 | return true; |
| 1293 | } else if (Token.is(K: MIToken::md_diexpr)) { |
| 1294 | if (parseDIExpression(Expr&: Node)) |
| 1295 | return true; |
| 1296 | } else if (Token.is(K: MIToken::md_dilocation)) { |
| 1297 | if (parseDILocation(Expr&: Node)) |
| 1298 | return true; |
| 1299 | } else |
| 1300 | return error(Msg: "expected a metadata node" ); |
| 1301 | if (Token.isNot(K: MIToken::Eof)) |
| 1302 | return error(Msg: "expected end of string after the metadata node" ); |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
| 1306 | bool MIParser::parseMachineMetadata() { |
| 1307 | lex(); |
| 1308 | if (Token.isNot(K: MIToken::exclaim)) |
| 1309 | return error(Msg: "expected a metadata node" ); |
| 1310 | |
| 1311 | lex(); |
| 1312 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isSigned()) |
| 1313 | return error(Msg: "expected metadata id after '!'" ); |
| 1314 | unsigned ID = 0; |
| 1315 | if (getUnsigned(Result&: ID)) |
| 1316 | return true; |
| 1317 | lex(); |
| 1318 | if (expectAndConsume(TokenKind: MIToken::equal)) |
| 1319 | return true; |
| 1320 | bool IsDistinct = Token.is(K: MIToken::kw_distinct); |
| 1321 | if (IsDistinct) |
| 1322 | lex(); |
| 1323 | if (Token.isNot(K: MIToken::exclaim)) |
| 1324 | return error(Msg: "expected a metadata node" ); |
| 1325 | lex(); |
| 1326 | |
| 1327 | MDNode *MD; |
| 1328 | if (parseMDTuple(MD, IsDistinct)) |
| 1329 | return true; |
| 1330 | |
| 1331 | auto FI = PFS.MachineForwardRefMDNodes.find(x: ID); |
| 1332 | if (FI != PFS.MachineForwardRefMDNodes.end()) { |
| 1333 | FI->second.first->replaceAllUsesWith(MD); |
| 1334 | PFS.MachineForwardRefMDNodes.erase(position: FI); |
| 1335 | |
| 1336 | assert(PFS.MachineMetadataNodes[ID] == MD && "Tracking VH didn't work" ); |
| 1337 | } else { |
| 1338 | auto [It, Inserted] = PFS.MachineMetadataNodes.try_emplace(k: ID); |
| 1339 | if (!Inserted) |
| 1340 | return error(Msg: "Metadata id is already used" ); |
| 1341 | It->second.reset(MD); |
| 1342 | } |
| 1343 | |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
| 1347 | bool MIParser::parseMDTuple(MDNode *&MD, bool IsDistinct) { |
| 1348 | SmallVector<Metadata *, 16> Elts; |
| 1349 | if (parseMDNodeVector(Elts)) |
| 1350 | return true; |
| 1351 | MD = (IsDistinct ? MDTuple::getDistinct |
| 1352 | : MDTuple::get)(MF.getFunction().getContext(), Elts); |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
| 1356 | bool MIParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) { |
| 1357 | if (Token.isNot(K: MIToken::lbrace)) |
| 1358 | return error(Msg: "expected '{' here" ); |
| 1359 | lex(); |
| 1360 | |
| 1361 | if (Token.is(K: MIToken::rbrace)) { |
| 1362 | lex(); |
| 1363 | return false; |
| 1364 | } |
| 1365 | |
| 1366 | do { |
| 1367 | Metadata *MD; |
| 1368 | if (parseMetadata(MD)) |
| 1369 | return true; |
| 1370 | |
| 1371 | Elts.push_back(Elt: MD); |
| 1372 | |
| 1373 | if (Token.isNot(K: MIToken::comma)) |
| 1374 | break; |
| 1375 | lex(); |
| 1376 | } while (true); |
| 1377 | |
| 1378 | if (Token.isNot(K: MIToken::rbrace)) |
| 1379 | return error(Msg: "expected end of metadata node" ); |
| 1380 | lex(); |
| 1381 | |
| 1382 | return false; |
| 1383 | } |
| 1384 | |
| 1385 | // ::= !42 |
| 1386 | // ::= !"string" |
| 1387 | bool MIParser::parseMetadata(Metadata *&MD) { |
| 1388 | if (Token.isNot(K: MIToken::exclaim)) |
| 1389 | return error(Msg: "expected '!' here" ); |
| 1390 | lex(); |
| 1391 | |
| 1392 | if (Token.is(K: MIToken::StringConstant)) { |
| 1393 | std::string Str; |
| 1394 | if (parseStringConstant(Result&: Str)) |
| 1395 | return true; |
| 1396 | MD = MDString::get(Context&: MF.getFunction().getContext(), Str); |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isSigned()) |
| 1401 | return error(Msg: "expected metadata id after '!'" ); |
| 1402 | |
| 1403 | SMLoc Loc = mapSMLoc(Loc: Token.location()); |
| 1404 | |
| 1405 | unsigned ID = 0; |
| 1406 | if (getUnsigned(Result&: ID)) |
| 1407 | return true; |
| 1408 | lex(); |
| 1409 | |
| 1410 | auto NodeInfo = PFS.IRSlots.MetadataNodes.find(x: ID); |
| 1411 | if (NodeInfo != PFS.IRSlots.MetadataNodes.end()) { |
| 1412 | MD = NodeInfo->second.get(); |
| 1413 | return false; |
| 1414 | } |
| 1415 | // Check machine metadata. |
| 1416 | NodeInfo = PFS.MachineMetadataNodes.find(x: ID); |
| 1417 | if (NodeInfo != PFS.MachineMetadataNodes.end()) { |
| 1418 | MD = NodeInfo->second.get(); |
| 1419 | return false; |
| 1420 | } |
| 1421 | // Forward reference. |
| 1422 | auto &FwdRef = PFS.MachineForwardRefMDNodes[ID]; |
| 1423 | FwdRef = std::make_pair( |
| 1424 | x: MDTuple::getTemporary(Context&: MF.getFunction().getContext(), MDs: {}), y&: Loc); |
| 1425 | PFS.MachineMetadataNodes[ID].reset(MD: FwdRef.first.get()); |
| 1426 | MD = FwdRef.first.get(); |
| 1427 | |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
| 1431 | static const char *printImplicitRegisterFlag(const MachineOperand &MO) { |
| 1432 | assert(MO.isImplicit()); |
| 1433 | return MO.isDef() ? "implicit-def" : "implicit" ; |
| 1434 | } |
| 1435 | |
| 1436 | static std::string getRegisterName(const TargetRegisterInfo *TRI, |
| 1437 | Register Reg) { |
| 1438 | assert(Reg.isPhysical() && "expected phys reg" ); |
| 1439 | return StringRef(TRI->getName(RegNo: Reg)).lower(); |
| 1440 | } |
| 1441 | |
| 1442 | /// Return true if the parsed machine operands contain a given machine operand. |
| 1443 | static bool isImplicitOperandIn(const MachineOperand &ImplicitOperand, |
| 1444 | ArrayRef<ParsedMachineOperand> Operands) { |
| 1445 | for (const auto &I : Operands) { |
| 1446 | if (ImplicitOperand.isIdenticalTo(Other: I.Operand)) |
| 1447 | return true; |
| 1448 | } |
| 1449 | return false; |
| 1450 | } |
| 1451 | |
| 1452 | bool MIParser::verifyImplicitOperands(ArrayRef<ParsedMachineOperand> Operands, |
| 1453 | const MCInstrDesc &MCID) { |
| 1454 | if (MCID.isCall()) |
| 1455 | // We can't verify call instructions as they can contain arbitrary implicit |
| 1456 | // register and register mask operands. |
| 1457 | return false; |
| 1458 | |
| 1459 | // Gather all the expected implicit operands. |
| 1460 | SmallVector<MachineOperand, 4> ImplicitOperands; |
| 1461 | for (MCPhysReg ImpDef : MCID.implicit_defs()) |
| 1462 | ImplicitOperands.push_back(Elt: MachineOperand::CreateReg(Reg: ImpDef, isDef: true, isImp: true)); |
| 1463 | for (MCPhysReg ImpUse : MCID.implicit_uses()) |
| 1464 | ImplicitOperands.push_back(Elt: MachineOperand::CreateReg(Reg: ImpUse, isDef: false, isImp: true)); |
| 1465 | |
| 1466 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); |
| 1467 | assert(TRI && "Expected target register info" ); |
| 1468 | for (const auto &I : ImplicitOperands) { |
| 1469 | if (isImplicitOperandIn(ImplicitOperand: I, Operands)) |
| 1470 | continue; |
| 1471 | return error(Loc: Operands.empty() ? Token.location() : Operands.back().End, |
| 1472 | Msg: Twine("missing implicit register operand '" ) + |
| 1473 | printImplicitRegisterFlag(MO: I) + " $" + |
| 1474 | getRegisterName(TRI, Reg: I.getReg()) + "'" ); |
| 1475 | } |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | bool MIParser::parseInstruction(unsigned &OpCode, unsigned &Flags) { |
| 1480 | // Allow frame and fast math flags for OPCODE |
| 1481 | // clang-format off |
| 1482 | while (Token.is(K: MIToken::kw_frame_setup) || |
| 1483 | Token.is(K: MIToken::kw_frame_destroy) || |
| 1484 | Token.is(K: MIToken::kw_nnan) || |
| 1485 | Token.is(K: MIToken::kw_ninf) || |
| 1486 | Token.is(K: MIToken::kw_nsz) || |
| 1487 | Token.is(K: MIToken::kw_arcp) || |
| 1488 | Token.is(K: MIToken::kw_contract) || |
| 1489 | Token.is(K: MIToken::kw_afn) || |
| 1490 | Token.is(K: MIToken::kw_reassoc) || |
| 1491 | Token.is(K: MIToken::kw_nuw) || |
| 1492 | Token.is(K: MIToken::kw_nsw) || |
| 1493 | Token.is(K: MIToken::kw_exact) || |
| 1494 | Token.is(K: MIToken::kw_nofpexcept) || |
| 1495 | Token.is(K: MIToken::kw_noconvergent) || |
| 1496 | Token.is(K: MIToken::kw_unpredictable) || |
| 1497 | Token.is(K: MIToken::kw_nneg) || |
| 1498 | Token.is(K: MIToken::kw_disjoint) || |
| 1499 | Token.is(K: MIToken::kw_nusw) || |
| 1500 | Token.is(K: MIToken::kw_samesign) || |
| 1501 | Token.is(K: MIToken::kw_inbounds)) { |
| 1502 | // clang-format on |
| 1503 | // Mine frame and fast math flags |
| 1504 | if (Token.is(K: MIToken::kw_frame_setup)) |
| 1505 | Flags |= MachineInstr::FrameSetup; |
| 1506 | if (Token.is(K: MIToken::kw_frame_destroy)) |
| 1507 | Flags |= MachineInstr::FrameDestroy; |
| 1508 | if (Token.is(K: MIToken::kw_nnan)) |
| 1509 | Flags |= MachineInstr::FmNoNans; |
| 1510 | if (Token.is(K: MIToken::kw_ninf)) |
| 1511 | Flags |= MachineInstr::FmNoInfs; |
| 1512 | if (Token.is(K: MIToken::kw_nsz)) |
| 1513 | Flags |= MachineInstr::FmNsz; |
| 1514 | if (Token.is(K: MIToken::kw_arcp)) |
| 1515 | Flags |= MachineInstr::FmArcp; |
| 1516 | if (Token.is(K: MIToken::kw_contract)) |
| 1517 | Flags |= MachineInstr::FmContract; |
| 1518 | if (Token.is(K: MIToken::kw_afn)) |
| 1519 | Flags |= MachineInstr::FmAfn; |
| 1520 | if (Token.is(K: MIToken::kw_reassoc)) |
| 1521 | Flags |= MachineInstr::FmReassoc; |
| 1522 | if (Token.is(K: MIToken::kw_nuw)) |
| 1523 | Flags |= MachineInstr::NoUWrap; |
| 1524 | if (Token.is(K: MIToken::kw_nsw)) |
| 1525 | Flags |= MachineInstr::NoSWrap; |
| 1526 | if (Token.is(K: MIToken::kw_exact)) |
| 1527 | Flags |= MachineInstr::IsExact; |
| 1528 | if (Token.is(K: MIToken::kw_nofpexcept)) |
| 1529 | Flags |= MachineInstr::NoFPExcept; |
| 1530 | if (Token.is(K: MIToken::kw_unpredictable)) |
| 1531 | Flags |= MachineInstr::Unpredictable; |
| 1532 | if (Token.is(K: MIToken::kw_noconvergent)) |
| 1533 | Flags |= MachineInstr::NoConvergent; |
| 1534 | if (Token.is(K: MIToken::kw_nneg)) |
| 1535 | Flags |= MachineInstr::NonNeg; |
| 1536 | if (Token.is(K: MIToken::kw_disjoint)) |
| 1537 | Flags |= MachineInstr::Disjoint; |
| 1538 | if (Token.is(K: MIToken::kw_nusw)) |
| 1539 | Flags |= MachineInstr::NoUSWrap; |
| 1540 | if (Token.is(K: MIToken::kw_samesign)) |
| 1541 | Flags |= MachineInstr::SameSign; |
| 1542 | if (Token.is(K: MIToken::kw_inbounds)) |
| 1543 | Flags |= MachineInstr::InBounds; |
| 1544 | |
| 1545 | lex(); |
| 1546 | } |
| 1547 | if (Token.isNot(K: MIToken::Identifier)) |
| 1548 | return error(Msg: "expected a machine instruction" ); |
| 1549 | StringRef InstrName = Token.stringValue(); |
| 1550 | if (PFS.Target.parseInstrName(InstrName, OpCode)) |
| 1551 | return error(Msg: Twine("unknown machine instruction name '" ) + InstrName + "'" ); |
| 1552 | lex(); |
| 1553 | return false; |
| 1554 | } |
| 1555 | |
| 1556 | bool MIParser::parseNamedRegister(Register &Reg) { |
| 1557 | assert(Token.is(MIToken::NamedRegister) && "Needs NamedRegister token" ); |
| 1558 | StringRef Name = Token.stringValue(); |
| 1559 | if (PFS.Target.getRegisterByName(RegName: Name, Reg)) |
| 1560 | return error(Msg: Twine("unknown register name '" ) + Name + "'" ); |
| 1561 | return false; |
| 1562 | } |
| 1563 | |
| 1564 | bool MIParser::parseNamedVirtualRegister(VRegInfo *&Info) { |
| 1565 | assert(Token.is(MIToken::NamedVirtualRegister) && "Expected NamedVReg token" ); |
| 1566 | StringRef Name = Token.stringValue(); |
| 1567 | // TODO: Check that the VReg name is not the same as a physical register name. |
| 1568 | // If it is, then print a warning (when warnings are implemented). |
| 1569 | Info = &PFS.getVRegInfoNamed(RegName: Name); |
| 1570 | return false; |
| 1571 | } |
| 1572 | |
| 1573 | bool MIParser::parseVirtualRegister(VRegInfo *&Info) { |
| 1574 | if (Token.is(K: MIToken::NamedVirtualRegister)) |
| 1575 | return parseNamedVirtualRegister(Info); |
| 1576 | assert(Token.is(MIToken::VirtualRegister) && "Needs VirtualRegister token" ); |
| 1577 | unsigned ID; |
| 1578 | if (getUnsigned(Result&: ID)) |
| 1579 | return true; |
| 1580 | Info = &PFS.getVRegInfo(Num: ID); |
| 1581 | return false; |
| 1582 | } |
| 1583 | |
| 1584 | bool MIParser::parseRegister(Register &Reg, VRegInfo *&Info) { |
| 1585 | switch (Token.kind()) { |
| 1586 | case MIToken::underscore: |
| 1587 | Reg = 0; |
| 1588 | return false; |
| 1589 | case MIToken::NamedRegister: |
| 1590 | return parseNamedRegister(Reg); |
| 1591 | case MIToken::NamedVirtualRegister: |
| 1592 | case MIToken::VirtualRegister: |
| 1593 | if (parseVirtualRegister(Info)) |
| 1594 | return true; |
| 1595 | Reg = Info->VReg; |
| 1596 | return false; |
| 1597 | // TODO: Parse other register kinds. |
| 1598 | default: |
| 1599 | llvm_unreachable("The current token should be a register" ); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | bool MIParser::parseRegisterClassOrBank(VRegInfo &RegInfo) { |
| 1604 | if (Token.isNot(K: MIToken::Identifier) && Token.isNot(K: MIToken::underscore)) |
| 1605 | return error(Msg: "expected '_', register class, or register bank name" ); |
| 1606 | StringRef::iterator Loc = Token.location(); |
| 1607 | StringRef Name = Token.stringValue(); |
| 1608 | |
| 1609 | // Was it a register class? |
| 1610 | const TargetRegisterClass *RC = PFS.Target.getRegClass(Name); |
| 1611 | if (RC) { |
| 1612 | lex(); |
| 1613 | |
| 1614 | switch (RegInfo.Kind) { |
| 1615 | case VRegInfo::UNKNOWN: |
| 1616 | case VRegInfo::NORMAL: |
| 1617 | RegInfo.Kind = VRegInfo::NORMAL; |
| 1618 | if (RegInfo.Explicit && RegInfo.D.RC != RC) { |
| 1619 | const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo(); |
| 1620 | return error(Loc, Msg: Twine("conflicting register classes, previously: " ) + |
| 1621 | Twine(TRI.getRegClassName(Class: RegInfo.D.RC))); |
| 1622 | } |
| 1623 | RegInfo.D.RC = RC; |
| 1624 | RegInfo.Explicit = true; |
| 1625 | return false; |
| 1626 | |
| 1627 | case VRegInfo::GENERIC: |
| 1628 | case VRegInfo::REGBANK: |
| 1629 | return error(Loc, Msg: "register class specification on generic register" ); |
| 1630 | } |
| 1631 | llvm_unreachable("Unexpected register kind" ); |
| 1632 | } |
| 1633 | |
| 1634 | // Should be a register bank or a generic register. |
| 1635 | const RegisterBank *RegBank = nullptr; |
| 1636 | if (Name != "_" ) { |
| 1637 | RegBank = PFS.Target.getRegBank(Name); |
| 1638 | if (!RegBank) |
| 1639 | return error(Loc, Msg: "expected '_', register class, or register bank name" ); |
| 1640 | } |
| 1641 | |
| 1642 | lex(); |
| 1643 | |
| 1644 | switch (RegInfo.Kind) { |
| 1645 | case VRegInfo::UNKNOWN: |
| 1646 | case VRegInfo::GENERIC: |
| 1647 | case VRegInfo::REGBANK: |
| 1648 | RegInfo.Kind = RegBank ? VRegInfo::REGBANK : VRegInfo::GENERIC; |
| 1649 | if (RegInfo.Explicit && RegInfo.D.RegBank != RegBank) |
| 1650 | return error(Loc, Msg: "conflicting generic register banks" ); |
| 1651 | RegInfo.D.RegBank = RegBank; |
| 1652 | RegInfo.Explicit = true; |
| 1653 | return false; |
| 1654 | |
| 1655 | case VRegInfo::NORMAL: |
| 1656 | return error(Loc, Msg: "register bank specification on normal register" ); |
| 1657 | } |
| 1658 | llvm_unreachable("Unexpected register kind" ); |
| 1659 | } |
| 1660 | |
| 1661 | bool MIParser::parseRegisterFlag(RegState &Flags) { |
| 1662 | const RegState OldFlags = Flags; |
| 1663 | switch (Token.kind()) { |
| 1664 | case MIToken::kw_implicit: |
| 1665 | Flags |= RegState::Implicit; |
| 1666 | break; |
| 1667 | case MIToken::kw_implicit_define: |
| 1668 | Flags |= RegState::ImplicitDefine; |
| 1669 | break; |
| 1670 | case MIToken::kw_def: |
| 1671 | Flags |= RegState::Define; |
| 1672 | break; |
| 1673 | case MIToken::kw_dead: |
| 1674 | Flags |= RegState::Dead; |
| 1675 | break; |
| 1676 | case MIToken::kw_killed: |
| 1677 | Flags |= RegState::Kill; |
| 1678 | break; |
| 1679 | case MIToken::kw_undef: |
| 1680 | Flags |= RegState::Undef; |
| 1681 | break; |
| 1682 | case MIToken::kw_internal: |
| 1683 | Flags |= RegState::InternalRead; |
| 1684 | break; |
| 1685 | case MIToken::kw_early_clobber: |
| 1686 | Flags |= RegState::EarlyClobber; |
| 1687 | break; |
| 1688 | case MIToken::kw_debug_use: |
| 1689 | Flags |= RegState::Debug; |
| 1690 | break; |
| 1691 | case MIToken::kw_renamable: |
| 1692 | Flags |= RegState::Renamable; |
| 1693 | break; |
| 1694 | default: |
| 1695 | llvm_unreachable("The current token should be a register flag" ); |
| 1696 | } |
| 1697 | if (OldFlags == Flags) |
| 1698 | // We know that the same flag is specified more than once when the flags |
| 1699 | // weren't modified. |
| 1700 | return error(Msg: "duplicate '" + Token.stringValue() + "' register flag" ); |
| 1701 | lex(); |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | bool MIParser::parseSubRegisterIndex(unsigned &SubReg) { |
| 1706 | assert(Token.is(MIToken::dot)); |
| 1707 | lex(); |
| 1708 | if (Token.isNot(K: MIToken::Identifier)) |
| 1709 | return error(Msg: "expected a subregister index after '.'" ); |
| 1710 | auto Name = Token.stringValue(); |
| 1711 | SubReg = PFS.Target.getSubRegIndex(Name); |
| 1712 | if (!SubReg) |
| 1713 | return error(Msg: Twine("use of unknown subregister index '" ) + Name + "'" ); |
| 1714 | lex(); |
| 1715 | return false; |
| 1716 | } |
| 1717 | |
| 1718 | bool MIParser::parseRegisterTiedDefIndex(unsigned &TiedDefIdx) { |
| 1719 | if (!consumeIfPresent(TokenKind: MIToken::kw_tied_def)) |
| 1720 | return true; |
| 1721 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 1722 | return error(Msg: "expected an integer literal after 'tied-def'" ); |
| 1723 | if (getUnsigned(Result&: TiedDefIdx)) |
| 1724 | return true; |
| 1725 | lex(); |
| 1726 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 1727 | return true; |
| 1728 | return false; |
| 1729 | } |
| 1730 | |
| 1731 | bool MIParser::assignRegisterTies(MachineInstr &MI, |
| 1732 | ArrayRef<ParsedMachineOperand> Operands) { |
| 1733 | SmallVector<std::pair<unsigned, unsigned>, 4> TiedRegisterPairs; |
| 1734 | for (unsigned I = 0, E = Operands.size(); I != E; ++I) { |
| 1735 | if (!Operands[I].TiedDefIdx) |
| 1736 | continue; |
| 1737 | // The parser ensures that this operand is a register use, so we just have |
| 1738 | // to check the tied-def operand. |
| 1739 | unsigned DefIdx = *Operands[I].TiedDefIdx; |
| 1740 | if (DefIdx >= E) |
| 1741 | return error(Loc: Operands[I].Begin, |
| 1742 | Msg: Twine("use of invalid tied-def operand index '" + |
| 1743 | Twine(DefIdx) + "'; instruction has only " ) + |
| 1744 | Twine(E) + " operands" ); |
| 1745 | const auto &DefOperand = Operands[DefIdx].Operand; |
| 1746 | if (!DefOperand.isReg() || !DefOperand.isDef()) |
| 1747 | // FIXME: add note with the def operand. |
| 1748 | return error(Loc: Operands[I].Begin, |
| 1749 | Msg: Twine("use of invalid tied-def operand index '" ) + |
| 1750 | Twine(DefIdx) + "'; the operand #" + Twine(DefIdx) + |
| 1751 | " isn't a defined register" ); |
| 1752 | // Check that the tied-def operand wasn't tied elsewhere. |
| 1753 | for (const auto &TiedPair : TiedRegisterPairs) { |
| 1754 | if (TiedPair.first == DefIdx) |
| 1755 | return error(Loc: Operands[I].Begin, |
| 1756 | Msg: Twine("the tied-def operand #" ) + Twine(DefIdx) + |
| 1757 | " is already tied with another register operand" ); |
| 1758 | } |
| 1759 | TiedRegisterPairs.push_back(Elt: std::make_pair(x&: DefIdx, y&: I)); |
| 1760 | } |
| 1761 | // FIXME: Verify that for non INLINEASM instructions, the def and use tied |
| 1762 | // indices must be less than tied max. |
| 1763 | for (const auto &TiedPair : TiedRegisterPairs) |
| 1764 | MI.tieOperands(DefIdx: TiedPair.first, UseIdx: TiedPair.second); |
| 1765 | return false; |
| 1766 | } |
| 1767 | |
| 1768 | bool MIParser::parseRegisterOperand(MachineOperand &Dest, |
| 1769 | std::optional<unsigned> &TiedDefIdx, |
| 1770 | bool IsDef) { |
| 1771 | RegState Flags = getDefRegState(B: IsDef); |
| 1772 | while (Token.isRegisterFlag()) { |
| 1773 | if (parseRegisterFlag(Flags)) |
| 1774 | return true; |
| 1775 | } |
| 1776 | if (!Token.isRegister()) |
| 1777 | return error(Msg: "expected a register after register flags" ); |
| 1778 | Register Reg; |
| 1779 | VRegInfo *RegInfo; |
| 1780 | if (parseRegister(Reg, Info&: RegInfo)) |
| 1781 | return true; |
| 1782 | lex(); |
| 1783 | unsigned SubReg = 0; |
| 1784 | if (Token.is(K: MIToken::dot)) { |
| 1785 | if (parseSubRegisterIndex(SubReg)) |
| 1786 | return true; |
| 1787 | if (!Reg.isVirtual()) |
| 1788 | return error(Msg: "subregister index expects a virtual register" ); |
| 1789 | } |
| 1790 | if (Token.is(K: MIToken::colon)) { |
| 1791 | if (!Reg.isVirtual()) |
| 1792 | return error(Msg: "register class specification expects a virtual register" ); |
| 1793 | lex(); |
| 1794 | if (parseRegisterClassOrBank(RegInfo&: *RegInfo)) |
| 1795 | return true; |
| 1796 | } |
| 1797 | MachineRegisterInfo &MRI = MF.getRegInfo(); |
| 1798 | if (!hasRegState(Value: Flags, Test: RegState::Define)) { |
| 1799 | if (consumeIfPresent(TokenKind: MIToken::lparen)) { |
| 1800 | unsigned Idx; |
| 1801 | if (!parseRegisterTiedDefIndex(TiedDefIdx&: Idx)) |
| 1802 | TiedDefIdx = Idx; |
| 1803 | else { |
| 1804 | // Try a redundant low-level type. |
| 1805 | LLT Ty; |
| 1806 | if (parseLowLevelType(Loc: Token.location(), Ty)) |
| 1807 | return error(Msg: "expected tied-def or low-level type after '('" ); |
| 1808 | |
| 1809 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 1810 | return true; |
| 1811 | |
| 1812 | if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty) |
| 1813 | return error(Msg: "inconsistent type for generic virtual register" ); |
| 1814 | |
| 1815 | MRI.setRegClassOrRegBank(Reg, RCOrRB: static_cast<RegisterBank *>(nullptr)); |
| 1816 | MRI.setType(VReg: Reg, Ty); |
| 1817 | MRI.noteNewVirtualRegister(Reg); |
| 1818 | } |
| 1819 | } |
| 1820 | } else if (consumeIfPresent(TokenKind: MIToken::lparen)) { |
| 1821 | // Virtual registers may have a tpe with GlobalISel. |
| 1822 | if (!Reg.isVirtual()) |
| 1823 | return error(Msg: "unexpected type on physical register" ); |
| 1824 | |
| 1825 | LLT Ty; |
| 1826 | if (parseLowLevelType(Loc: Token.location(), Ty)) |
| 1827 | return true; |
| 1828 | |
| 1829 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 1830 | return true; |
| 1831 | |
| 1832 | if (MRI.getType(Reg).isValid() && MRI.getType(Reg) != Ty) |
| 1833 | return error(Msg: "inconsistent type for generic virtual register" ); |
| 1834 | |
| 1835 | MRI.setRegClassOrRegBank(Reg, RCOrRB: static_cast<RegisterBank *>(nullptr)); |
| 1836 | MRI.setType(VReg: Reg, Ty); |
| 1837 | } else if (Reg.isVirtual()) { |
| 1838 | // Generic virtual registers must have a type. |
| 1839 | // If we end up here this means the type hasn't been specified and |
| 1840 | // this is bad! |
| 1841 | if (RegInfo->Kind == VRegInfo::GENERIC || |
| 1842 | RegInfo->Kind == VRegInfo::REGBANK) |
| 1843 | return error(Msg: "generic virtual registers must have a type" ); |
| 1844 | } |
| 1845 | |
| 1846 | if (hasRegState(Value: Flags, Test: RegState::Define)) { |
| 1847 | if (hasRegState(Value: Flags, Test: RegState::Kill)) |
| 1848 | return error(Msg: "cannot have a killed def operand" ); |
| 1849 | } else { |
| 1850 | if (hasRegState(Value: Flags, Test: RegState::Dead)) |
| 1851 | return error(Msg: "cannot have a dead use operand" ); |
| 1852 | } |
| 1853 | |
| 1854 | Dest = MachineOperand::CreateReg(Reg, isDef: hasRegState(Value: Flags, Test: RegState::Define), |
| 1855 | isImp: hasRegState(Value: Flags, Test: RegState::Implicit), |
| 1856 | isKill: hasRegState(Value: Flags, Test: RegState::Kill), |
| 1857 | isDead: hasRegState(Value: Flags, Test: RegState::Dead), |
| 1858 | isUndef: hasRegState(Value: Flags, Test: RegState::Undef), |
| 1859 | isEarlyClobber: hasRegState(Value: Flags, Test: RegState::EarlyClobber), |
| 1860 | SubReg, isDebug: hasRegState(Value: Flags, Test: RegState::Debug), |
| 1861 | isInternalRead: hasRegState(Value: Flags, Test: RegState::InternalRead), |
| 1862 | isRenamable: hasRegState(Value: Flags, Test: RegState::Renamable)); |
| 1863 | |
| 1864 | return false; |
| 1865 | } |
| 1866 | |
| 1867 | bool MIParser::parseImmediateOperand(MachineOperand &Dest) { |
| 1868 | assert(Token.is(MIToken::IntegerLiteral)); |
| 1869 | const APSInt &Int = Token.integerValue(); |
| 1870 | if (auto SImm = Int.trySExtValue(); Int.isSigned() && SImm.has_value()) |
| 1871 | Dest = MachineOperand::CreateImm(Val: *SImm); |
| 1872 | else if (auto UImm = Int.tryZExtValue(); !Int.isSigned() && UImm.has_value()) |
| 1873 | Dest = MachineOperand::CreateImm(Val: *UImm); |
| 1874 | else |
| 1875 | return error(Msg: "integer literal is too large to be an immediate operand" ); |
| 1876 | lex(); |
| 1877 | return false; |
| 1878 | } |
| 1879 | |
| 1880 | bool MIParser::parseTargetImmMnemonic(const unsigned OpCode, |
| 1881 | const unsigned OpIdx, |
| 1882 | MachineOperand &Dest, |
| 1883 | const MIRFormatter &MF) { |
| 1884 | assert(Token.is(MIToken::dot)); |
| 1885 | auto Loc = Token.location(); // record start position |
| 1886 | size_t Len = 1; // for "." |
| 1887 | lex(); |
| 1888 | |
| 1889 | // Handle the case that mnemonic starts with number. |
| 1890 | if (Token.is(K: MIToken::IntegerLiteral)) { |
| 1891 | Len += Token.range().size(); |
| 1892 | lex(); |
| 1893 | } |
| 1894 | |
| 1895 | StringRef Src; |
| 1896 | if (Token.is(K: MIToken::comma)) |
| 1897 | Src = StringRef(Loc, Len); |
| 1898 | else { |
| 1899 | assert(Token.is(MIToken::Identifier)); |
| 1900 | Src = StringRef(Loc, Len + Token.stringValue().size()); |
| 1901 | } |
| 1902 | int64_t Val; |
| 1903 | if (MF.parseImmMnemonic(OpCode, OpIdx, Src, Imm&: Val, |
| 1904 | ErrorCallback: [this](StringRef::iterator Loc, const Twine &Msg) |
| 1905 | -> bool { return error(Loc, Msg); })) |
| 1906 | return true; |
| 1907 | |
| 1908 | Dest = MachineOperand::CreateImm(Val); |
| 1909 | if (!Token.is(K: MIToken::comma)) |
| 1910 | lex(); |
| 1911 | return false; |
| 1912 | } |
| 1913 | |
| 1914 | static bool parseIRConstant(StringRef::iterator Loc, StringRef StringValue, |
| 1915 | PerFunctionMIParsingState &PFS, const Constant *&C, |
| 1916 | ErrorCallbackType ErrCB) { |
| 1917 | auto Source = StringValue.str(); // The source has to be null terminated. |
| 1918 | SMDiagnostic Err; |
| 1919 | C = parseConstantValue(Asm: Source, Err, M: *PFS.MF.getFunction().getParent(), |
| 1920 | Slots: &PFS.IRSlots); |
| 1921 | if (!C) |
| 1922 | return ErrCB(Loc + Err.getColumnNo(), Err.getMessage()); |
| 1923 | return false; |
| 1924 | } |
| 1925 | |
| 1926 | bool MIParser::parseIRConstant(StringRef::iterator Loc, StringRef StringValue, |
| 1927 | const Constant *&C) { |
| 1928 | return ::parseIRConstant( |
| 1929 | Loc, StringValue, PFS, C, |
| 1930 | ErrCB: [this](StringRef::iterator Loc, const Twine &Msg) -> bool { |
| 1931 | return error(Loc, Msg); |
| 1932 | }); |
| 1933 | } |
| 1934 | |
| 1935 | bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) { |
| 1936 | if (parseIRConstant(Loc, StringValue: StringRef(Loc, Token.range().end() - Loc), C)) |
| 1937 | return true; |
| 1938 | lex(); |
| 1939 | return false; |
| 1940 | } |
| 1941 | |
| 1942 | // See LLT implementation for bit size limits. |
| 1943 | static bool verifyScalarSize(uint64_t Size) { |
| 1944 | return Size != 0 && isUInt<16>(x: Size); |
| 1945 | } |
| 1946 | |
| 1947 | static bool verifyVectorElementCount(uint64_t NumElts) { |
| 1948 | return NumElts != 0 && isUInt<16>(x: NumElts); |
| 1949 | } |
| 1950 | |
| 1951 | static bool verifyAddrSpace(uint64_t AddrSpace) { |
| 1952 | return isUInt<24>(x: AddrSpace); |
| 1953 | } |
| 1954 | |
| 1955 | bool MIParser::parseLowLevelType(StringRef::iterator Loc, LLT &Ty) { |
| 1956 | if (Token.range().front() == 's' || Token.range().front() == 'p') { |
| 1957 | StringRef SizeStr = Token.range().drop_front(); |
| 1958 | if (SizeStr.size() == 0 || !llvm::all_of(Range&: SizeStr, P: isdigit)) |
| 1959 | return error(Msg: "expected integers after 's'/'p' type character" ); |
| 1960 | } |
| 1961 | |
| 1962 | if (Token.range().front() == 's') { |
| 1963 | auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue(); |
| 1964 | if (ScalarSize) { |
| 1965 | if (!verifyScalarSize(Size: ScalarSize)) |
| 1966 | return error(Msg: "invalid size for scalar type" ); |
| 1967 | Ty = LLT::scalar(SizeInBits: ScalarSize); |
| 1968 | } else { |
| 1969 | Ty = LLT::token(); |
| 1970 | } |
| 1971 | lex(); |
| 1972 | return false; |
| 1973 | } else if (Token.range().front() == 'p') { |
| 1974 | const DataLayout &DL = MF.getDataLayout(); |
| 1975 | uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue(); |
| 1976 | if (!verifyAddrSpace(AddrSpace: AS)) |
| 1977 | return error(Msg: "invalid address space number" ); |
| 1978 | |
| 1979 | Ty = LLT::pointer(AddressSpace: AS, SizeInBits: DL.getPointerSizeInBits(AS)); |
| 1980 | lex(); |
| 1981 | return false; |
| 1982 | } |
| 1983 | |
| 1984 | // Now we're looking for a vector. |
| 1985 | if (Token.isNot(K: MIToken::less)) |
| 1986 | return error(Loc, Msg: "expected sN, pA, <M x sN>, <M x pA>, <vscale x M x sN>, " |
| 1987 | "or <vscale x M x pA> for GlobalISel type" ); |
| 1988 | lex(); |
| 1989 | |
| 1990 | bool HasVScale = |
| 1991 | Token.is(K: MIToken::Identifier) && Token.stringValue() == "vscale" ; |
| 1992 | if (HasVScale) { |
| 1993 | lex(); |
| 1994 | if (Token.isNot(K: MIToken::Identifier) || Token.stringValue() != "x" ) |
| 1995 | return error(Msg: "expected <vscale x M x sN> or <vscale x M x pA>" ); |
| 1996 | lex(); |
| 1997 | } |
| 1998 | |
| 1999 | auto GetError = [this, &HasVScale, Loc]() { |
| 2000 | if (HasVScale) |
| 2001 | return error( |
| 2002 | Loc, Msg: "expected <vscale x M x sN> or <vscale M x pA> for vector type" ); |
| 2003 | return error(Loc, Msg: "expected <M x sN> or <M x pA> for vector type" ); |
| 2004 | }; |
| 2005 | |
| 2006 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 2007 | return GetError(); |
| 2008 | uint64_t NumElements = Token.integerValue().getZExtValue(); |
| 2009 | if (!verifyVectorElementCount(NumElts: NumElements)) |
| 2010 | return error(Msg: "invalid number of vector elements" ); |
| 2011 | |
| 2012 | lex(); |
| 2013 | |
| 2014 | if (Token.isNot(K: MIToken::Identifier) || Token.stringValue() != "x" ) |
| 2015 | return GetError(); |
| 2016 | lex(); |
| 2017 | |
| 2018 | if (Token.range().front() != 's' && Token.range().front() != 'p') |
| 2019 | return GetError(); |
| 2020 | |
| 2021 | StringRef SizeStr = Token.range().drop_front(); |
| 2022 | if (SizeStr.size() == 0 || !llvm::all_of(Range&: SizeStr, P: isdigit)) |
| 2023 | return error(Msg: "expected integers after 's'/'p' type character" ); |
| 2024 | |
| 2025 | if (Token.range().front() == 's') { |
| 2026 | auto ScalarSize = APSInt(Token.range().drop_front()).getZExtValue(); |
| 2027 | if (!verifyScalarSize(Size: ScalarSize)) |
| 2028 | return error(Msg: "invalid size for scalar element in vector" ); |
| 2029 | Ty = LLT::scalar(SizeInBits: ScalarSize); |
| 2030 | } else if (Token.range().front() == 'p') { |
| 2031 | const DataLayout &DL = MF.getDataLayout(); |
| 2032 | uint64_t AS = APSInt(Token.range().drop_front()).getZExtValue(); |
| 2033 | if (!verifyAddrSpace(AddrSpace: AS)) |
| 2034 | return error(Msg: "invalid address space number" ); |
| 2035 | |
| 2036 | Ty = LLT::pointer(AddressSpace: AS, SizeInBits: DL.getPointerSizeInBits(AS)); |
| 2037 | } else |
| 2038 | return GetError(); |
| 2039 | lex(); |
| 2040 | |
| 2041 | if (Token.isNot(K: MIToken::greater)) |
| 2042 | return GetError(); |
| 2043 | |
| 2044 | lex(); |
| 2045 | |
| 2046 | Ty = LLT::vector(EC: ElementCount::get(MinVal: NumElements, Scalable: HasVScale), ScalarTy: Ty); |
| 2047 | return false; |
| 2048 | } |
| 2049 | |
| 2050 | bool MIParser::parseTypedImmediateOperand(MachineOperand &Dest) { |
| 2051 | assert(Token.is(MIToken::Identifier)); |
| 2052 | StringRef TypeStr = Token.range(); |
| 2053 | if (TypeStr.front() != 'i' && TypeStr.front() != 's' && |
| 2054 | TypeStr.front() != 'p') |
| 2055 | return error( |
| 2056 | Msg: "a typed immediate operand should start with one of 'i', 's', or 'p'" ); |
| 2057 | StringRef SizeStr = Token.range().drop_front(); |
| 2058 | if (SizeStr.size() == 0 || !llvm::all_of(Range&: SizeStr, P: isdigit)) |
| 2059 | return error(Msg: "expected integers after 'i'/'s'/'p' type character" ); |
| 2060 | |
| 2061 | auto Loc = Token.location(); |
| 2062 | lex(); |
| 2063 | if (Token.isNot(K: MIToken::IntegerLiteral)) { |
| 2064 | if (Token.isNot(K: MIToken::Identifier) || |
| 2065 | !(Token.range() == "true" || Token.range() == "false" )) |
| 2066 | return error(Msg: "expected an integer literal" ); |
| 2067 | } |
| 2068 | const Constant *C = nullptr; |
| 2069 | if (parseIRConstant(Loc, C)) |
| 2070 | return true; |
| 2071 | Dest = MachineOperand::CreateCImm(CI: cast<ConstantInt>(Val: C)); |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| 2075 | bool MIParser::parseFPImmediateOperand(MachineOperand &Dest) { |
| 2076 | auto Loc = Token.location(); |
| 2077 | lex(); |
| 2078 | if (Token.isNot(K: MIToken::FloatingPointLiteral) && |
| 2079 | Token.isNot(K: MIToken::HexLiteral)) |
| 2080 | return error(Msg: "expected a floating point literal" ); |
| 2081 | const Constant *C = nullptr; |
| 2082 | if (parseIRConstant(Loc, C)) |
| 2083 | return true; |
| 2084 | Dest = MachineOperand::CreateFPImm(CFP: cast<ConstantFP>(Val: C)); |
| 2085 | return false; |
| 2086 | } |
| 2087 | |
| 2088 | static bool getHexUint(const MIToken &Token, APInt &Result) { |
| 2089 | assert(Token.is(MIToken::HexLiteral)); |
| 2090 | StringRef S = Token.range(); |
| 2091 | assert(S[0] == '0' && tolower(S[1]) == 'x'); |
| 2092 | // This could be a floating point literal with a special prefix. |
| 2093 | if (!isxdigit(S[2])) |
| 2094 | return true; |
| 2095 | StringRef V = S.substr(Start: 2); |
| 2096 | APInt A(V.size()*4, V, 16); |
| 2097 | |
| 2098 | // If A is 0, then A.getActiveBits() is 0. This isn't a valid bitwidth. Make |
| 2099 | // sure it isn't the case before constructing result. |
| 2100 | unsigned NumBits = (A == 0) ? 32 : A.getActiveBits(); |
| 2101 | Result = APInt(NumBits, ArrayRef<uint64_t>(A.getRawData(), A.getNumWords())); |
| 2102 | return false; |
| 2103 | } |
| 2104 | |
| 2105 | static bool getUnsigned(const MIToken &Token, unsigned &Result, |
| 2106 | ErrorCallbackType ErrCB) { |
| 2107 | if (Token.hasIntegerValue()) { |
| 2108 | const uint64_t Limit = uint64_t(std::numeric_limits<unsigned>::max()) + 1; |
| 2109 | uint64_t Val64 = Token.integerValue().getLimitedValue(Limit); |
| 2110 | if (Val64 == Limit) |
| 2111 | return ErrCB(Token.location(), "expected 32-bit integer (too large)" ); |
| 2112 | Result = Val64; |
| 2113 | return false; |
| 2114 | } |
| 2115 | if (Token.is(K: MIToken::HexLiteral)) { |
| 2116 | APInt A; |
| 2117 | if (getHexUint(Token, Result&: A)) |
| 2118 | return true; |
| 2119 | if (A.getBitWidth() > 32) |
| 2120 | return ErrCB(Token.location(), "expected 32-bit integer (too large)" ); |
| 2121 | Result = A.getZExtValue(); |
| 2122 | return false; |
| 2123 | } |
| 2124 | return true; |
| 2125 | } |
| 2126 | |
| 2127 | bool MIParser::getUnsigned(unsigned &Result) { |
| 2128 | return ::getUnsigned( |
| 2129 | Token, Result, ErrCB: [this](StringRef::iterator Loc, const Twine &Msg) -> bool { |
| 2130 | return error(Loc, Msg); |
| 2131 | }); |
| 2132 | } |
| 2133 | |
| 2134 | bool MIParser::parseMBBReference(MachineBasicBlock *&MBB) { |
| 2135 | assert(Token.is(MIToken::MachineBasicBlock) || |
| 2136 | Token.is(MIToken::MachineBasicBlockLabel)); |
| 2137 | unsigned Number; |
| 2138 | if (getUnsigned(Result&: Number)) |
| 2139 | return true; |
| 2140 | auto MBBInfo = PFS.MBBSlots.find(Val: Number); |
| 2141 | if (MBBInfo == PFS.MBBSlots.end()) |
| 2142 | return error(Msg: Twine("use of undefined machine basic block #" ) + |
| 2143 | Twine(Number)); |
| 2144 | MBB = MBBInfo->second; |
| 2145 | // TODO: Only parse the name if it's a MachineBasicBlockLabel. Deprecate once |
| 2146 | // we drop the <irname> from the bb.<id>.<irname> format. |
| 2147 | if (!Token.stringValue().empty() && Token.stringValue() != MBB->getName()) |
| 2148 | return error(Msg: Twine("the name of machine basic block #" ) + Twine(Number) + |
| 2149 | " isn't '" + Token.stringValue() + "'" ); |
| 2150 | return false; |
| 2151 | } |
| 2152 | |
| 2153 | bool MIParser::parseMBBOperand(MachineOperand &Dest) { |
| 2154 | MachineBasicBlock *MBB; |
| 2155 | if (parseMBBReference(MBB)) |
| 2156 | return true; |
| 2157 | Dest = MachineOperand::CreateMBB(MBB); |
| 2158 | lex(); |
| 2159 | return false; |
| 2160 | } |
| 2161 | |
| 2162 | bool MIParser::parseStackFrameIndex(int &FI) { |
| 2163 | assert(Token.is(MIToken::StackObject)); |
| 2164 | unsigned ID; |
| 2165 | if (getUnsigned(Result&: ID)) |
| 2166 | return true; |
| 2167 | auto ObjectInfo = PFS.StackObjectSlots.find(Val: ID); |
| 2168 | if (ObjectInfo == PFS.StackObjectSlots.end()) |
| 2169 | return error(Msg: Twine("use of undefined stack object '%stack." ) + Twine(ID) + |
| 2170 | "'" ); |
| 2171 | StringRef Name; |
| 2172 | if (const auto *Alloca = |
| 2173 | MF.getFrameInfo().getObjectAllocation(ObjectIdx: ObjectInfo->second)) |
| 2174 | Name = Alloca->getName(); |
| 2175 | if (!Token.stringValue().empty() && Token.stringValue() != Name) |
| 2176 | return error(Msg: Twine("the name of the stack object '%stack." ) + Twine(ID) + |
| 2177 | "' isn't '" + Token.stringValue() + "'" ); |
| 2178 | lex(); |
| 2179 | FI = ObjectInfo->second; |
| 2180 | return false; |
| 2181 | } |
| 2182 | |
| 2183 | bool MIParser::parseStackObjectOperand(MachineOperand &Dest) { |
| 2184 | int FI; |
| 2185 | if (parseStackFrameIndex(FI)) |
| 2186 | return true; |
| 2187 | Dest = MachineOperand::CreateFI(Idx: FI); |
| 2188 | return false; |
| 2189 | } |
| 2190 | |
| 2191 | bool MIParser::parseFixedStackFrameIndex(int &FI) { |
| 2192 | assert(Token.is(MIToken::FixedStackObject)); |
| 2193 | unsigned ID; |
| 2194 | if (getUnsigned(Result&: ID)) |
| 2195 | return true; |
| 2196 | auto ObjectInfo = PFS.FixedStackObjectSlots.find(Val: ID); |
| 2197 | if (ObjectInfo == PFS.FixedStackObjectSlots.end()) |
| 2198 | return error(Msg: Twine("use of undefined fixed stack object '%fixed-stack." ) + |
| 2199 | Twine(ID) + "'" ); |
| 2200 | lex(); |
| 2201 | FI = ObjectInfo->second; |
| 2202 | return false; |
| 2203 | } |
| 2204 | |
| 2205 | bool MIParser::parseFixedStackObjectOperand(MachineOperand &Dest) { |
| 2206 | int FI; |
| 2207 | if (parseFixedStackFrameIndex(FI)) |
| 2208 | return true; |
| 2209 | Dest = MachineOperand::CreateFI(Idx: FI); |
| 2210 | return false; |
| 2211 | } |
| 2212 | |
| 2213 | static bool parseGlobalValue(const MIToken &Token, |
| 2214 | PerFunctionMIParsingState &PFS, GlobalValue *&GV, |
| 2215 | ErrorCallbackType ErrCB) { |
| 2216 | switch (Token.kind()) { |
| 2217 | case MIToken::NamedGlobalValue: { |
| 2218 | const Module *M = PFS.MF.getFunction().getParent(); |
| 2219 | GV = M->getNamedValue(Name: Token.stringValue()); |
| 2220 | if (!GV) |
| 2221 | return ErrCB(Token.location(), Twine("use of undefined global value '" ) + |
| 2222 | Token.range() + "'" ); |
| 2223 | break; |
| 2224 | } |
| 2225 | case MIToken::GlobalValue: { |
| 2226 | unsigned GVIdx; |
| 2227 | if (getUnsigned(Token, Result&: GVIdx, ErrCB)) |
| 2228 | return true; |
| 2229 | GV = PFS.IRSlots.GlobalValues.get(ID: GVIdx); |
| 2230 | if (!GV) |
| 2231 | return ErrCB(Token.location(), Twine("use of undefined global value '@" ) + |
| 2232 | Twine(GVIdx) + "'" ); |
| 2233 | break; |
| 2234 | } |
| 2235 | default: |
| 2236 | llvm_unreachable("The current token should be a global value" ); |
| 2237 | } |
| 2238 | return false; |
| 2239 | } |
| 2240 | |
| 2241 | bool MIParser::parseGlobalValue(GlobalValue *&GV) { |
| 2242 | return ::parseGlobalValue( |
| 2243 | Token, PFS, GV, |
| 2244 | ErrCB: [this](StringRef::iterator Loc, const Twine &Msg) -> bool { |
| 2245 | return error(Loc, Msg); |
| 2246 | }); |
| 2247 | } |
| 2248 | |
| 2249 | bool MIParser::parseGlobalAddressOperand(MachineOperand &Dest) { |
| 2250 | GlobalValue *GV = nullptr; |
| 2251 | if (parseGlobalValue(GV)) |
| 2252 | return true; |
| 2253 | lex(); |
| 2254 | Dest = MachineOperand::CreateGA(GV, /*Offset=*/0); |
| 2255 | if (parseOperandsOffset(Op&: Dest)) |
| 2256 | return true; |
| 2257 | return false; |
| 2258 | } |
| 2259 | |
| 2260 | bool MIParser::parseConstantPoolIndexOperand(MachineOperand &Dest) { |
| 2261 | assert(Token.is(MIToken::ConstantPoolItem)); |
| 2262 | unsigned ID; |
| 2263 | if (getUnsigned(Result&: ID)) |
| 2264 | return true; |
| 2265 | auto ConstantInfo = PFS.ConstantPoolSlots.find(Val: ID); |
| 2266 | if (ConstantInfo == PFS.ConstantPoolSlots.end()) |
| 2267 | return error(Msg: "use of undefined constant '%const." + Twine(ID) + "'" ); |
| 2268 | lex(); |
| 2269 | Dest = MachineOperand::CreateCPI(Idx: ID, /*Offset=*/0); |
| 2270 | if (parseOperandsOffset(Op&: Dest)) |
| 2271 | return true; |
| 2272 | return false; |
| 2273 | } |
| 2274 | |
| 2275 | bool MIParser::parseJumpTableIndexOperand(MachineOperand &Dest) { |
| 2276 | assert(Token.is(MIToken::JumpTableIndex)); |
| 2277 | unsigned ID; |
| 2278 | if (getUnsigned(Result&: ID)) |
| 2279 | return true; |
| 2280 | auto JumpTableEntryInfo = PFS.JumpTableSlots.find(Val: ID); |
| 2281 | if (JumpTableEntryInfo == PFS.JumpTableSlots.end()) |
| 2282 | return error(Msg: "use of undefined jump table '%jump-table." + Twine(ID) + "'" ); |
| 2283 | lex(); |
| 2284 | Dest = MachineOperand::CreateJTI(Idx: JumpTableEntryInfo->second); |
| 2285 | return false; |
| 2286 | } |
| 2287 | |
| 2288 | bool MIParser::parseExternalSymbolOperand(MachineOperand &Dest) { |
| 2289 | assert(Token.is(MIToken::ExternalSymbol)); |
| 2290 | const char *Symbol = MF.createExternalSymbolName(Name: Token.stringValue()); |
| 2291 | lex(); |
| 2292 | Dest = MachineOperand::CreateES(SymName: Symbol); |
| 2293 | if (parseOperandsOffset(Op&: Dest)) |
| 2294 | return true; |
| 2295 | return false; |
| 2296 | } |
| 2297 | |
| 2298 | bool MIParser::parseMCSymbolOperand(MachineOperand &Dest) { |
| 2299 | assert(Token.is(MIToken::MCSymbol)); |
| 2300 | MCSymbol *Symbol = getOrCreateMCSymbol(Name: Token.stringValue()); |
| 2301 | lex(); |
| 2302 | Dest = MachineOperand::CreateMCSymbol(Sym: Symbol); |
| 2303 | if (parseOperandsOffset(Op&: Dest)) |
| 2304 | return true; |
| 2305 | return false; |
| 2306 | } |
| 2307 | |
| 2308 | bool MIParser::parseSubRegisterIndexOperand(MachineOperand &Dest) { |
| 2309 | assert(Token.is(MIToken::SubRegisterIndex)); |
| 2310 | StringRef Name = Token.stringValue(); |
| 2311 | unsigned SubRegIndex = PFS.Target.getSubRegIndex(Name: Token.stringValue()); |
| 2312 | if (SubRegIndex == 0) |
| 2313 | return error(Msg: Twine("unknown subregister index '" ) + Name + "'" ); |
| 2314 | lex(); |
| 2315 | Dest = MachineOperand::CreateImm(Val: SubRegIndex); |
| 2316 | return false; |
| 2317 | } |
| 2318 | |
| 2319 | bool MIParser::parseMDNode(MDNode *&Node) { |
| 2320 | assert(Token.is(MIToken::exclaim)); |
| 2321 | |
| 2322 | auto Loc = Token.location(); |
| 2323 | lex(); |
| 2324 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isSigned()) |
| 2325 | return error(Msg: "expected metadata id after '!'" ); |
| 2326 | unsigned ID; |
| 2327 | if (getUnsigned(Result&: ID)) |
| 2328 | return true; |
| 2329 | auto NodeInfo = PFS.IRSlots.MetadataNodes.find(x: ID); |
| 2330 | if (NodeInfo == PFS.IRSlots.MetadataNodes.end()) { |
| 2331 | NodeInfo = PFS.MachineMetadataNodes.find(x: ID); |
| 2332 | if (NodeInfo == PFS.MachineMetadataNodes.end()) |
| 2333 | return error(Loc, Msg: "use of undefined metadata '!" + Twine(ID) + "'" ); |
| 2334 | } |
| 2335 | lex(); |
| 2336 | Node = NodeInfo->second.get(); |
| 2337 | return false; |
| 2338 | } |
| 2339 | |
| 2340 | bool MIParser::parseDIExpression(MDNode *&Expr) { |
| 2341 | unsigned Read; |
| 2342 | Expr = llvm::parseDIExpressionBodyAtBeginning( |
| 2343 | Asm: CurrentSource, Read, Err&: Error, M: *PFS.MF.getFunction().getParent(), |
| 2344 | Slots: &PFS.IRSlots); |
| 2345 | CurrentSource = CurrentSource.substr(Start: Read); |
| 2346 | lex(); |
| 2347 | if (!Expr) |
| 2348 | return error(Msg: Error.getMessage()); |
| 2349 | return false; |
| 2350 | } |
| 2351 | |
| 2352 | bool MIParser::parseDILocation(MDNode *&Loc) { |
| 2353 | assert(Token.is(MIToken::md_dilocation)); |
| 2354 | lex(); |
| 2355 | |
| 2356 | bool HaveLine = false; |
| 2357 | unsigned Line = 0; |
| 2358 | unsigned Column = 0; |
| 2359 | MDNode *Scope = nullptr; |
| 2360 | MDNode *InlinedAt = nullptr; |
| 2361 | bool ImplicitCode = false; |
| 2362 | uint64_t AtomGroup = 0; |
| 2363 | uint64_t AtomRank = 0; |
| 2364 | |
| 2365 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2366 | return true; |
| 2367 | |
| 2368 | if (Token.isNot(K: MIToken::rparen)) { |
| 2369 | do { |
| 2370 | if (Token.is(K: MIToken::Identifier)) { |
| 2371 | if (Token.stringValue() == "line" ) { |
| 2372 | lex(); |
| 2373 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2374 | return true; |
| 2375 | if (Token.isNot(K: MIToken::IntegerLiteral) || |
| 2376 | Token.integerValue().isSigned()) |
| 2377 | return error(Msg: "expected unsigned integer" ); |
| 2378 | Line = Token.integerValue().getZExtValue(); |
| 2379 | HaveLine = true; |
| 2380 | lex(); |
| 2381 | continue; |
| 2382 | } |
| 2383 | if (Token.stringValue() == "column" ) { |
| 2384 | lex(); |
| 2385 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2386 | return true; |
| 2387 | if (Token.isNot(K: MIToken::IntegerLiteral) || |
| 2388 | Token.integerValue().isSigned()) |
| 2389 | return error(Msg: "expected unsigned integer" ); |
| 2390 | Column = Token.integerValue().getZExtValue(); |
| 2391 | lex(); |
| 2392 | continue; |
| 2393 | } |
| 2394 | if (Token.stringValue() == "scope" ) { |
| 2395 | lex(); |
| 2396 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2397 | return true; |
| 2398 | if (parseMDNode(Node&: Scope)) |
| 2399 | return error(Msg: "expected metadata node" ); |
| 2400 | if (!isa<DIScope>(Val: Scope)) |
| 2401 | return error(Msg: "expected DIScope node" ); |
| 2402 | continue; |
| 2403 | } |
| 2404 | if (Token.stringValue() == "inlinedAt" ) { |
| 2405 | lex(); |
| 2406 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2407 | return true; |
| 2408 | if (Token.is(K: MIToken::exclaim)) { |
| 2409 | if (parseMDNode(Node&: InlinedAt)) |
| 2410 | return true; |
| 2411 | } else if (Token.is(K: MIToken::md_dilocation)) { |
| 2412 | if (parseDILocation(Loc&: InlinedAt)) |
| 2413 | return true; |
| 2414 | } else |
| 2415 | return error(Msg: "expected metadata node" ); |
| 2416 | if (!isa<DILocation>(Val: InlinedAt)) |
| 2417 | return error(Msg: "expected DILocation node" ); |
| 2418 | continue; |
| 2419 | } |
| 2420 | if (Token.stringValue() == "isImplicitCode" ) { |
| 2421 | lex(); |
| 2422 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2423 | return true; |
| 2424 | if (!Token.is(K: MIToken::Identifier)) |
| 2425 | return error(Msg: "expected true/false" ); |
| 2426 | // As far as I can see, we don't have any existing need for parsing |
| 2427 | // true/false in MIR yet. Do it ad-hoc until there's something else |
| 2428 | // that needs it. |
| 2429 | if (Token.stringValue() == "true" ) |
| 2430 | ImplicitCode = true; |
| 2431 | else if (Token.stringValue() == "false" ) |
| 2432 | ImplicitCode = false; |
| 2433 | else |
| 2434 | return error(Msg: "expected true/false" ); |
| 2435 | lex(); |
| 2436 | continue; |
| 2437 | } |
| 2438 | if (Token.stringValue() == "atomGroup" ) { |
| 2439 | lex(); |
| 2440 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2441 | return true; |
| 2442 | if (Token.isNot(K: MIToken::IntegerLiteral) || |
| 2443 | Token.integerValue().isSigned()) |
| 2444 | return error(Msg: "expected unsigned integer" ); |
| 2445 | AtomGroup = Token.integerValue().getZExtValue(); |
| 2446 | lex(); |
| 2447 | continue; |
| 2448 | } |
| 2449 | if (Token.stringValue() == "atomRank" ) { |
| 2450 | lex(); |
| 2451 | if (expectAndConsume(TokenKind: MIToken::colon)) |
| 2452 | return true; |
| 2453 | if (Token.isNot(K: MIToken::IntegerLiteral) || |
| 2454 | Token.integerValue().isSigned()) |
| 2455 | return error(Msg: "expected unsigned integer" ); |
| 2456 | AtomRank = Token.integerValue().getZExtValue(); |
| 2457 | lex(); |
| 2458 | continue; |
| 2459 | } |
| 2460 | } |
| 2461 | return error(Msg: Twine("invalid DILocation argument '" ) + |
| 2462 | Token.stringValue() + "'" ); |
| 2463 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 2464 | } |
| 2465 | |
| 2466 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2467 | return true; |
| 2468 | |
| 2469 | if (!HaveLine) |
| 2470 | return error(Msg: "DILocation requires line number" ); |
| 2471 | if (!Scope) |
| 2472 | return error(Msg: "DILocation requires a scope" ); |
| 2473 | |
| 2474 | Loc = DILocation::get(Context&: MF.getFunction().getContext(), Line, Column, Scope, |
| 2475 | InlinedAt, ImplicitCode, AtomGroup, AtomRank); |
| 2476 | return false; |
| 2477 | } |
| 2478 | |
| 2479 | bool MIParser::parseMetadataOperand(MachineOperand &Dest) { |
| 2480 | MDNode *Node = nullptr; |
| 2481 | if (Token.is(K: MIToken::exclaim)) { |
| 2482 | if (parseMDNode(Node)) |
| 2483 | return true; |
| 2484 | } else if (Token.is(K: MIToken::md_diexpr)) { |
| 2485 | if (parseDIExpression(Expr&: Node)) |
| 2486 | return true; |
| 2487 | } |
| 2488 | Dest = MachineOperand::CreateMetadata(Meta: Node); |
| 2489 | return false; |
| 2490 | } |
| 2491 | |
| 2492 | bool MIParser::parseCFIOffset(int &Offset) { |
| 2493 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 2494 | return error(Msg: "expected a cfi offset" ); |
| 2495 | if (Token.integerValue().getSignificantBits() > 32) |
| 2496 | return error(Msg: "expected a 32 bit integer (the cfi offset is too large)" ); |
| 2497 | Offset = (int)Token.integerValue().getExtValue(); |
| 2498 | lex(); |
| 2499 | return false; |
| 2500 | } |
| 2501 | |
| 2502 | bool MIParser::parseCFIRegister(unsigned &Reg) { |
| 2503 | if (Token.isNot(K: MIToken::NamedRegister)) |
| 2504 | return error(Msg: "expected a cfi register" ); |
| 2505 | Register LLVMReg; |
| 2506 | if (parseNamedRegister(Reg&: LLVMReg)) |
| 2507 | return true; |
| 2508 | const auto *TRI = MF.getSubtarget().getRegisterInfo(); |
| 2509 | assert(TRI && "Expected target register info" ); |
| 2510 | int DwarfReg = TRI->getDwarfRegNum(Reg: LLVMReg, isEH: true); |
| 2511 | if (DwarfReg < 0) |
| 2512 | return error(Msg: "invalid DWARF register" ); |
| 2513 | Reg = (unsigned)DwarfReg; |
| 2514 | lex(); |
| 2515 | return false; |
| 2516 | } |
| 2517 | |
| 2518 | bool MIParser::parseCFIAddressSpace(unsigned &AddressSpace) { |
| 2519 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 2520 | return error(Msg: "expected a cfi address space literal" ); |
| 2521 | if (Token.integerValue().isSigned()) |
| 2522 | return error(Msg: "expected an unsigned integer (cfi address space)" ); |
| 2523 | AddressSpace = Token.integerValue().getZExtValue(); |
| 2524 | lex(); |
| 2525 | return false; |
| 2526 | } |
| 2527 | |
| 2528 | bool MIParser::parseCFIEscapeValues(std::string &Values) { |
| 2529 | do { |
| 2530 | if (Token.isNot(K: MIToken::HexLiteral)) |
| 2531 | return error(Msg: "expected a hexadecimal literal" ); |
| 2532 | unsigned Value; |
| 2533 | if (getUnsigned(Result&: Value)) |
| 2534 | return true; |
| 2535 | if (Value > UINT8_MAX) |
| 2536 | return error(Msg: "expected a 8-bit integer (too large)" ); |
| 2537 | Values.push_back(c: static_cast<uint8_t>(Value)); |
| 2538 | lex(); |
| 2539 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 2540 | return false; |
| 2541 | } |
| 2542 | |
| 2543 | bool MIParser::parseCFIOperand(MachineOperand &Dest) { |
| 2544 | auto Kind = Token.kind(); |
| 2545 | lex(); |
| 2546 | int Offset; |
| 2547 | unsigned Reg; |
| 2548 | unsigned AddressSpace; |
| 2549 | unsigned CFIIndex; |
| 2550 | switch (Kind) { |
| 2551 | case MIToken::kw_cfi_same_value: |
| 2552 | if (parseCFIRegister(Reg)) |
| 2553 | return true; |
| 2554 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createSameValue(L: nullptr, Register: Reg)); |
| 2555 | break; |
| 2556 | case MIToken::kw_cfi_offset: |
| 2557 | if (parseCFIRegister(Reg) || expectAndConsume(TokenKind: MIToken::comma) || |
| 2558 | parseCFIOffset(Offset)) |
| 2559 | return true; |
| 2560 | CFIIndex = |
| 2561 | MF.addFrameInst(Inst: MCCFIInstruction::createOffset(L: nullptr, Register: Reg, Offset)); |
| 2562 | break; |
| 2563 | case MIToken::kw_cfi_rel_offset: |
| 2564 | if (parseCFIRegister(Reg) || expectAndConsume(TokenKind: MIToken::comma) || |
| 2565 | parseCFIOffset(Offset)) |
| 2566 | return true; |
| 2567 | CFIIndex = MF.addFrameInst( |
| 2568 | Inst: MCCFIInstruction::createRelOffset(L: nullptr, Register: Reg, Offset)); |
| 2569 | break; |
| 2570 | case MIToken::kw_cfi_def_cfa_register: |
| 2571 | if (parseCFIRegister(Reg)) |
| 2572 | return true; |
| 2573 | CFIIndex = |
| 2574 | MF.addFrameInst(Inst: MCCFIInstruction::createDefCfaRegister(L: nullptr, Register: Reg)); |
| 2575 | break; |
| 2576 | case MIToken::kw_cfi_def_cfa_offset: |
| 2577 | if (parseCFIOffset(Offset)) |
| 2578 | return true; |
| 2579 | CFIIndex = |
| 2580 | MF.addFrameInst(Inst: MCCFIInstruction::cfiDefCfaOffset(L: nullptr, Offset)); |
| 2581 | break; |
| 2582 | case MIToken::kw_cfi_adjust_cfa_offset: |
| 2583 | if (parseCFIOffset(Offset)) |
| 2584 | return true; |
| 2585 | CFIIndex = MF.addFrameInst( |
| 2586 | Inst: MCCFIInstruction::createAdjustCfaOffset(L: nullptr, Adjustment: Offset)); |
| 2587 | break; |
| 2588 | case MIToken::kw_cfi_def_cfa: |
| 2589 | if (parseCFIRegister(Reg) || expectAndConsume(TokenKind: MIToken::comma) || |
| 2590 | parseCFIOffset(Offset)) |
| 2591 | return true; |
| 2592 | CFIIndex = |
| 2593 | MF.addFrameInst(Inst: MCCFIInstruction::cfiDefCfa(L: nullptr, Register: Reg, Offset)); |
| 2594 | break; |
| 2595 | case MIToken::kw_cfi_llvm_def_aspace_cfa: |
| 2596 | if (parseCFIRegister(Reg) || expectAndConsume(TokenKind: MIToken::comma) || |
| 2597 | parseCFIOffset(Offset) || expectAndConsume(TokenKind: MIToken::comma) || |
| 2598 | parseCFIAddressSpace(AddressSpace)) |
| 2599 | return true; |
| 2600 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createLLVMDefAspaceCfa( |
| 2601 | L: nullptr, Register: Reg, Offset, AddressSpace, Loc: SMLoc())); |
| 2602 | break; |
| 2603 | case MIToken::kw_cfi_remember_state: |
| 2604 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createRememberState(L: nullptr)); |
| 2605 | break; |
| 2606 | case MIToken::kw_cfi_restore: |
| 2607 | if (parseCFIRegister(Reg)) |
| 2608 | return true; |
| 2609 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createRestore(L: nullptr, Register: Reg)); |
| 2610 | break; |
| 2611 | case MIToken::kw_cfi_restore_state: |
| 2612 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createRestoreState(L: nullptr)); |
| 2613 | break; |
| 2614 | case MIToken::kw_cfi_undefined: |
| 2615 | if (parseCFIRegister(Reg)) |
| 2616 | return true; |
| 2617 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createUndefined(L: nullptr, Register: Reg)); |
| 2618 | break; |
| 2619 | case MIToken::kw_cfi_register: { |
| 2620 | unsigned Reg2; |
| 2621 | if (parseCFIRegister(Reg) || expectAndConsume(TokenKind: MIToken::comma) || |
| 2622 | parseCFIRegister(Reg&: Reg2)) |
| 2623 | return true; |
| 2624 | |
| 2625 | CFIIndex = |
| 2626 | MF.addFrameInst(Inst: MCCFIInstruction::createRegister(L: nullptr, Register1: Reg, Register2: Reg2)); |
| 2627 | break; |
| 2628 | } |
| 2629 | case MIToken::kw_cfi_window_save: |
| 2630 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createWindowSave(L: nullptr)); |
| 2631 | break; |
| 2632 | case MIToken::kw_cfi_aarch64_negate_ra_sign_state: |
| 2633 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createNegateRAState(L: nullptr)); |
| 2634 | break; |
| 2635 | case MIToken::kw_cfi_aarch64_negate_ra_sign_state_with_pc: |
| 2636 | CFIIndex = |
| 2637 | MF.addFrameInst(Inst: MCCFIInstruction::createNegateRAStateWithPC(L: nullptr)); |
| 2638 | break; |
| 2639 | case MIToken::kw_cfi_escape: { |
| 2640 | std::string Values; |
| 2641 | if (parseCFIEscapeValues(Values)) |
| 2642 | return true; |
| 2643 | CFIIndex = MF.addFrameInst(Inst: MCCFIInstruction::createEscape(L: nullptr, Vals: Values)); |
| 2644 | break; |
| 2645 | } |
| 2646 | default: |
| 2647 | // TODO: Parse the other CFI operands. |
| 2648 | llvm_unreachable("The current token should be a cfi operand" ); |
| 2649 | } |
| 2650 | Dest = MachineOperand::CreateCFIIndex(CFIIndex); |
| 2651 | return false; |
| 2652 | } |
| 2653 | |
| 2654 | bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) { |
| 2655 | switch (Token.kind()) { |
| 2656 | case MIToken::NamedIRBlock: { |
| 2657 | BB = dyn_cast_or_null<BasicBlock>( |
| 2658 | Val: F.getValueSymbolTable()->lookup(Name: Token.stringValue())); |
| 2659 | if (!BB) |
| 2660 | return error(Msg: Twine("use of undefined IR block '" ) + Token.range() + "'" ); |
| 2661 | break; |
| 2662 | } |
| 2663 | case MIToken::IRBlock: { |
| 2664 | unsigned SlotNumber = 0; |
| 2665 | if (getUnsigned(Result&: SlotNumber)) |
| 2666 | return true; |
| 2667 | BB = const_cast<BasicBlock *>(getIRBlock(Slot: SlotNumber, F)); |
| 2668 | if (!BB) |
| 2669 | return error(Msg: Twine("use of undefined IR block '%ir-block." ) + |
| 2670 | Twine(SlotNumber) + "'" ); |
| 2671 | break; |
| 2672 | } |
| 2673 | default: |
| 2674 | llvm_unreachable("The current token should be an IR block reference" ); |
| 2675 | } |
| 2676 | return false; |
| 2677 | } |
| 2678 | |
| 2679 | bool MIParser::parseBlockAddressOperand(MachineOperand &Dest) { |
| 2680 | assert(Token.is(MIToken::kw_blockaddress)); |
| 2681 | lex(); |
| 2682 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2683 | return true; |
| 2684 | if (Token.isNot(K: MIToken::GlobalValue) && |
| 2685 | Token.isNot(K: MIToken::NamedGlobalValue)) |
| 2686 | return error(Msg: "expected a global value" ); |
| 2687 | GlobalValue *GV = nullptr; |
| 2688 | if (parseGlobalValue(GV)) |
| 2689 | return true; |
| 2690 | auto *F = dyn_cast<Function>(Val: GV); |
| 2691 | if (!F) |
| 2692 | return error(Msg: "expected an IR function reference" ); |
| 2693 | lex(); |
| 2694 | if (expectAndConsume(TokenKind: MIToken::comma)) |
| 2695 | return true; |
| 2696 | BasicBlock *BB = nullptr; |
| 2697 | if (Token.isNot(K: MIToken::IRBlock) && Token.isNot(K: MIToken::NamedIRBlock)) |
| 2698 | return error(Msg: "expected an IR block reference" ); |
| 2699 | if (parseIRBlock(BB, F: *F)) |
| 2700 | return true; |
| 2701 | lex(); |
| 2702 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2703 | return true; |
| 2704 | Dest = MachineOperand::CreateBA(BA: BlockAddress::get(F, BB), /*Offset=*/0); |
| 2705 | if (parseOperandsOffset(Op&: Dest)) |
| 2706 | return true; |
| 2707 | return false; |
| 2708 | } |
| 2709 | |
| 2710 | bool MIParser::parseIntrinsicOperand(MachineOperand &Dest) { |
| 2711 | assert(Token.is(MIToken::kw_intrinsic)); |
| 2712 | lex(); |
| 2713 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2714 | return error(Msg: "expected syntax intrinsic(@llvm.whatever)" ); |
| 2715 | |
| 2716 | if (Token.isNot(K: MIToken::NamedGlobalValue)) |
| 2717 | return error(Msg: "expected syntax intrinsic(@llvm.whatever)" ); |
| 2718 | |
| 2719 | std::string Name = std::string(Token.stringValue()); |
| 2720 | lex(); |
| 2721 | |
| 2722 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2723 | return error(Msg: "expected ')' to terminate intrinsic name" ); |
| 2724 | |
| 2725 | // Find out what intrinsic we're dealing with. |
| 2726 | Intrinsic::ID ID = Intrinsic::lookupIntrinsicID(Name); |
| 2727 | if (ID == Intrinsic::not_intrinsic) |
| 2728 | return error(Msg: "unknown intrinsic name" ); |
| 2729 | Dest = MachineOperand::CreateIntrinsicID(ID); |
| 2730 | |
| 2731 | return false; |
| 2732 | } |
| 2733 | |
| 2734 | bool MIParser::parsePredicateOperand(MachineOperand &Dest) { |
| 2735 | assert(Token.is(MIToken::kw_intpred) || Token.is(MIToken::kw_floatpred)); |
| 2736 | bool IsFloat = Token.is(K: MIToken::kw_floatpred); |
| 2737 | lex(); |
| 2738 | |
| 2739 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2740 | return error(Msg: "expected syntax intpred(whatever) or floatpred(whatever" ); |
| 2741 | |
| 2742 | if (Token.isNot(K: MIToken::Identifier)) |
| 2743 | return error(Msg: "whatever" ); |
| 2744 | |
| 2745 | CmpInst::Predicate Pred; |
| 2746 | if (IsFloat) { |
| 2747 | Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue()) |
| 2748 | .Case(S: "false" , Value: CmpInst::FCMP_FALSE) |
| 2749 | .Case(S: "oeq" , Value: CmpInst::FCMP_OEQ) |
| 2750 | .Case(S: "ogt" , Value: CmpInst::FCMP_OGT) |
| 2751 | .Case(S: "oge" , Value: CmpInst::FCMP_OGE) |
| 2752 | .Case(S: "olt" , Value: CmpInst::FCMP_OLT) |
| 2753 | .Case(S: "ole" , Value: CmpInst::FCMP_OLE) |
| 2754 | .Case(S: "one" , Value: CmpInst::FCMP_ONE) |
| 2755 | .Case(S: "ord" , Value: CmpInst::FCMP_ORD) |
| 2756 | .Case(S: "uno" , Value: CmpInst::FCMP_UNO) |
| 2757 | .Case(S: "ueq" , Value: CmpInst::FCMP_UEQ) |
| 2758 | .Case(S: "ugt" , Value: CmpInst::FCMP_UGT) |
| 2759 | .Case(S: "uge" , Value: CmpInst::FCMP_UGE) |
| 2760 | .Case(S: "ult" , Value: CmpInst::FCMP_ULT) |
| 2761 | .Case(S: "ule" , Value: CmpInst::FCMP_ULE) |
| 2762 | .Case(S: "une" , Value: CmpInst::FCMP_UNE) |
| 2763 | .Case(S: "true" , Value: CmpInst::FCMP_TRUE) |
| 2764 | .Default(Value: CmpInst::BAD_FCMP_PREDICATE); |
| 2765 | if (!CmpInst::isFPPredicate(P: Pred)) |
| 2766 | return error(Msg: "invalid floating-point predicate" ); |
| 2767 | } else { |
| 2768 | Pred = StringSwitch<CmpInst::Predicate>(Token.stringValue()) |
| 2769 | .Case(S: "eq" , Value: CmpInst::ICMP_EQ) |
| 2770 | .Case(S: "ne" , Value: CmpInst::ICMP_NE) |
| 2771 | .Case(S: "sgt" , Value: CmpInst::ICMP_SGT) |
| 2772 | .Case(S: "sge" , Value: CmpInst::ICMP_SGE) |
| 2773 | .Case(S: "slt" , Value: CmpInst::ICMP_SLT) |
| 2774 | .Case(S: "sle" , Value: CmpInst::ICMP_SLE) |
| 2775 | .Case(S: "ugt" , Value: CmpInst::ICMP_UGT) |
| 2776 | .Case(S: "uge" , Value: CmpInst::ICMP_UGE) |
| 2777 | .Case(S: "ult" , Value: CmpInst::ICMP_ULT) |
| 2778 | .Case(S: "ule" , Value: CmpInst::ICMP_ULE) |
| 2779 | .Default(Value: CmpInst::BAD_ICMP_PREDICATE); |
| 2780 | if (!CmpInst::isIntPredicate(P: Pred)) |
| 2781 | return error(Msg: "invalid integer predicate" ); |
| 2782 | } |
| 2783 | |
| 2784 | lex(); |
| 2785 | Dest = MachineOperand::CreatePredicate(Pred); |
| 2786 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2787 | return error(Msg: "predicate should be terminated by ')'." ); |
| 2788 | |
| 2789 | return false; |
| 2790 | } |
| 2791 | |
| 2792 | bool MIParser::parseShuffleMaskOperand(MachineOperand &Dest) { |
| 2793 | assert(Token.is(MIToken::kw_shufflemask)); |
| 2794 | |
| 2795 | lex(); |
| 2796 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2797 | return error(Msg: "expected syntax shufflemask(<integer or undef>, ...)" ); |
| 2798 | |
| 2799 | SmallVector<int, 32> ShufMask; |
| 2800 | do { |
| 2801 | if (Token.is(K: MIToken::kw_undef)) { |
| 2802 | ShufMask.push_back(Elt: -1); |
| 2803 | } else if (Token.is(K: MIToken::IntegerLiteral)) { |
| 2804 | const APSInt &Int = Token.integerValue(); |
| 2805 | ShufMask.push_back(Elt: Int.getExtValue()); |
| 2806 | } else |
| 2807 | return error(Msg: "expected integer constant" ); |
| 2808 | |
| 2809 | lex(); |
| 2810 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 2811 | |
| 2812 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2813 | return error(Msg: "shufflemask should be terminated by ')'." ); |
| 2814 | |
| 2815 | if (ShufMask.size() < 2) |
| 2816 | return error(Msg: "shufflemask should have > 1 element" ); |
| 2817 | |
| 2818 | ArrayRef<int> MaskAlloc = MF.allocateShuffleMask(Mask: ShufMask); |
| 2819 | Dest = MachineOperand::CreateShuffleMask(Mask: MaskAlloc); |
| 2820 | return false; |
| 2821 | } |
| 2822 | |
| 2823 | bool MIParser::parseDbgInstrRefOperand(MachineOperand &Dest) { |
| 2824 | assert(Token.is(MIToken::kw_dbg_instr_ref)); |
| 2825 | |
| 2826 | lex(); |
| 2827 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2828 | return error(Msg: "expected syntax dbg-instr-ref(<unsigned>, <unsigned>)" ); |
| 2829 | |
| 2830 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isNegative()) |
| 2831 | return error(Msg: "expected unsigned integer for instruction index" ); |
| 2832 | uint64_t InstrIdx = Token.integerValue().getZExtValue(); |
| 2833 | assert(InstrIdx <= std::numeric_limits<unsigned>::max() && |
| 2834 | "Instruction reference's instruction index is too large" ); |
| 2835 | lex(); |
| 2836 | |
| 2837 | if (expectAndConsume(TokenKind: MIToken::comma)) |
| 2838 | return error(Msg: "expected syntax dbg-instr-ref(<unsigned>, <unsigned>)" ); |
| 2839 | |
| 2840 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isNegative()) |
| 2841 | return error(Msg: "expected unsigned integer for operand index" ); |
| 2842 | uint64_t OpIdx = Token.integerValue().getZExtValue(); |
| 2843 | assert(OpIdx <= std::numeric_limits<unsigned>::max() && |
| 2844 | "Instruction reference's operand index is too large" ); |
| 2845 | lex(); |
| 2846 | |
| 2847 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2848 | return error(Msg: "expected syntax dbg-instr-ref(<unsigned>, <unsigned>)" ); |
| 2849 | |
| 2850 | Dest = MachineOperand::CreateDbgInstrRef(InstrIdx, OpIdx); |
| 2851 | return false; |
| 2852 | } |
| 2853 | |
| 2854 | bool MIParser::parseTargetIndexOperand(MachineOperand &Dest) { |
| 2855 | assert(Token.is(MIToken::kw_target_index)); |
| 2856 | lex(); |
| 2857 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2858 | return true; |
| 2859 | if (Token.isNot(K: MIToken::Identifier)) |
| 2860 | return error(Msg: "expected the name of the target index" ); |
| 2861 | int Index = 0; |
| 2862 | if (PFS.Target.getTargetIndex(Name: Token.stringValue(), Index)) |
| 2863 | return error(Msg: "use of undefined target index '" + Token.stringValue() + "'" ); |
| 2864 | lex(); |
| 2865 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2866 | return true; |
| 2867 | Dest = MachineOperand::CreateTargetIndex(Idx: unsigned(Index), /*Offset=*/0); |
| 2868 | if (parseOperandsOffset(Op&: Dest)) |
| 2869 | return true; |
| 2870 | return false; |
| 2871 | } |
| 2872 | |
| 2873 | bool MIParser::parseCustomRegisterMaskOperand(MachineOperand &Dest) { |
| 2874 | assert(Token.stringValue() == "CustomRegMask" && "Expected a custom RegMask" ); |
| 2875 | lex(); |
| 2876 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2877 | return true; |
| 2878 | |
| 2879 | uint32_t *Mask = MF.allocateRegMask(); |
| 2880 | do { |
| 2881 | if (Token.isNot(K: MIToken::rparen)) { |
| 2882 | if (Token.isNot(K: MIToken::NamedRegister)) |
| 2883 | return error(Msg: "expected a named register" ); |
| 2884 | Register Reg; |
| 2885 | if (parseNamedRegister(Reg)) |
| 2886 | return true; |
| 2887 | lex(); |
| 2888 | Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32); |
| 2889 | } |
| 2890 | |
| 2891 | // TODO: Report an error if the same register is used more than once. |
| 2892 | } while (consumeIfPresent(TokenKind: MIToken::comma)); |
| 2893 | |
| 2894 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2895 | return true; |
| 2896 | Dest = MachineOperand::CreateRegMask(Mask); |
| 2897 | return false; |
| 2898 | } |
| 2899 | |
| 2900 | bool MIParser::parseLaneMaskOperand(MachineOperand &Dest) { |
| 2901 | assert(Token.is(MIToken::kw_lanemask)); |
| 2902 | |
| 2903 | lex(); |
| 2904 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2905 | return true; |
| 2906 | |
| 2907 | // Parse lanemask. |
| 2908 | if (Token.isNot(K: MIToken::IntegerLiteral) && Token.isNot(K: MIToken::HexLiteral)) |
| 2909 | return error(Msg: "expected a valid lane mask value" ); |
| 2910 | static_assert(sizeof(LaneBitmask::Type) == sizeof(uint64_t), |
| 2911 | "Use correct get-function for lane mask." ); |
| 2912 | LaneBitmask::Type V; |
| 2913 | if (getUint64(Result&: V)) |
| 2914 | return true; |
| 2915 | LaneBitmask LaneMask(V); |
| 2916 | lex(); |
| 2917 | |
| 2918 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2919 | return true; |
| 2920 | |
| 2921 | Dest = MachineOperand::CreateLaneMask(LaneMask); |
| 2922 | return false; |
| 2923 | } |
| 2924 | |
| 2925 | bool MIParser::parseLiveoutRegisterMaskOperand(MachineOperand &Dest) { |
| 2926 | assert(Token.is(MIToken::kw_liveout)); |
| 2927 | uint32_t *Mask = MF.allocateRegMask(); |
| 2928 | lex(); |
| 2929 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 2930 | return true; |
| 2931 | while (true) { |
| 2932 | if (Token.isNot(K: MIToken::NamedRegister)) |
| 2933 | return error(Msg: "expected a named register" ); |
| 2934 | Register Reg; |
| 2935 | if (parseNamedRegister(Reg)) |
| 2936 | return true; |
| 2937 | lex(); |
| 2938 | Mask[Reg.id() / 32] |= 1U << (Reg.id() % 32); |
| 2939 | // TODO: Report an error if the same register is used more than once. |
| 2940 | if (Token.isNot(K: MIToken::comma)) |
| 2941 | break; |
| 2942 | lex(); |
| 2943 | } |
| 2944 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 2945 | return true; |
| 2946 | Dest = MachineOperand::CreateRegLiveOut(Mask); |
| 2947 | return false; |
| 2948 | } |
| 2949 | |
| 2950 | bool MIParser::parseMachineOperand(const unsigned OpCode, const unsigned OpIdx, |
| 2951 | MachineOperand &Dest, |
| 2952 | std::optional<unsigned> &TiedDefIdx) { |
| 2953 | switch (Token.kind()) { |
| 2954 | case MIToken::kw_implicit: |
| 2955 | case MIToken::kw_implicit_define: |
| 2956 | case MIToken::kw_def: |
| 2957 | case MIToken::kw_dead: |
| 2958 | case MIToken::kw_killed: |
| 2959 | case MIToken::kw_undef: |
| 2960 | case MIToken::kw_internal: |
| 2961 | case MIToken::kw_early_clobber: |
| 2962 | case MIToken::kw_debug_use: |
| 2963 | case MIToken::kw_renamable: |
| 2964 | case MIToken::underscore: |
| 2965 | case MIToken::NamedRegister: |
| 2966 | case MIToken::VirtualRegister: |
| 2967 | case MIToken::NamedVirtualRegister: |
| 2968 | return parseRegisterOperand(Dest, TiedDefIdx); |
| 2969 | case MIToken::IntegerLiteral: |
| 2970 | return parseImmediateOperand(Dest); |
| 2971 | case MIToken::kw_half: |
| 2972 | case MIToken::kw_bfloat: |
| 2973 | case MIToken::kw_float: |
| 2974 | case MIToken::kw_double: |
| 2975 | case MIToken::kw_x86_fp80: |
| 2976 | case MIToken::kw_fp128: |
| 2977 | case MIToken::kw_ppc_fp128: |
| 2978 | return parseFPImmediateOperand(Dest); |
| 2979 | case MIToken::MachineBasicBlock: |
| 2980 | return parseMBBOperand(Dest); |
| 2981 | case MIToken::StackObject: |
| 2982 | return parseStackObjectOperand(Dest); |
| 2983 | case MIToken::FixedStackObject: |
| 2984 | return parseFixedStackObjectOperand(Dest); |
| 2985 | case MIToken::GlobalValue: |
| 2986 | case MIToken::NamedGlobalValue: |
| 2987 | return parseGlobalAddressOperand(Dest); |
| 2988 | case MIToken::ConstantPoolItem: |
| 2989 | return parseConstantPoolIndexOperand(Dest); |
| 2990 | case MIToken::JumpTableIndex: |
| 2991 | return parseJumpTableIndexOperand(Dest); |
| 2992 | case MIToken::ExternalSymbol: |
| 2993 | return parseExternalSymbolOperand(Dest); |
| 2994 | case MIToken::MCSymbol: |
| 2995 | return parseMCSymbolOperand(Dest); |
| 2996 | case MIToken::SubRegisterIndex: |
| 2997 | return parseSubRegisterIndexOperand(Dest); |
| 2998 | case MIToken::md_diexpr: |
| 2999 | case MIToken::exclaim: |
| 3000 | return parseMetadataOperand(Dest); |
| 3001 | case MIToken::kw_cfi_same_value: |
| 3002 | case MIToken::kw_cfi_offset: |
| 3003 | case MIToken::kw_cfi_rel_offset: |
| 3004 | case MIToken::kw_cfi_def_cfa_register: |
| 3005 | case MIToken::kw_cfi_def_cfa_offset: |
| 3006 | case MIToken::kw_cfi_adjust_cfa_offset: |
| 3007 | case MIToken::kw_cfi_escape: |
| 3008 | case MIToken::kw_cfi_def_cfa: |
| 3009 | case MIToken::kw_cfi_llvm_def_aspace_cfa: |
| 3010 | case MIToken::kw_cfi_register: |
| 3011 | case MIToken::kw_cfi_remember_state: |
| 3012 | case MIToken::kw_cfi_restore: |
| 3013 | case MIToken::kw_cfi_restore_state: |
| 3014 | case MIToken::kw_cfi_undefined: |
| 3015 | case MIToken::kw_cfi_window_save: |
| 3016 | case MIToken::kw_cfi_aarch64_negate_ra_sign_state: |
| 3017 | case MIToken::kw_cfi_aarch64_negate_ra_sign_state_with_pc: |
| 3018 | return parseCFIOperand(Dest); |
| 3019 | case MIToken::kw_blockaddress: |
| 3020 | return parseBlockAddressOperand(Dest); |
| 3021 | case MIToken::kw_intrinsic: |
| 3022 | return parseIntrinsicOperand(Dest); |
| 3023 | case MIToken::kw_target_index: |
| 3024 | return parseTargetIndexOperand(Dest); |
| 3025 | case MIToken::kw_lanemask: |
| 3026 | return parseLaneMaskOperand(Dest); |
| 3027 | case MIToken::kw_liveout: |
| 3028 | return parseLiveoutRegisterMaskOperand(Dest); |
| 3029 | case MIToken::kw_floatpred: |
| 3030 | case MIToken::kw_intpred: |
| 3031 | return parsePredicateOperand(Dest); |
| 3032 | case MIToken::kw_shufflemask: |
| 3033 | return parseShuffleMaskOperand(Dest); |
| 3034 | case MIToken::kw_dbg_instr_ref: |
| 3035 | return parseDbgInstrRefOperand(Dest); |
| 3036 | case MIToken::Error: |
| 3037 | return true; |
| 3038 | case MIToken::Identifier: |
| 3039 | if (const auto *RegMask = PFS.Target.getRegMask(Identifier: Token.stringValue())) { |
| 3040 | Dest = MachineOperand::CreateRegMask(Mask: RegMask); |
| 3041 | lex(); |
| 3042 | break; |
| 3043 | } else if (Token.stringValue() == "CustomRegMask" ) { |
| 3044 | return parseCustomRegisterMaskOperand(Dest); |
| 3045 | } else |
| 3046 | return parseTypedImmediateOperand(Dest); |
| 3047 | case MIToken::dot: { |
| 3048 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
| 3049 | if (const auto *Formatter = TII->getMIRFormatter()) { |
| 3050 | return parseTargetImmMnemonic(OpCode, OpIdx, Dest, MF: *Formatter); |
| 3051 | } |
| 3052 | [[fallthrough]]; |
| 3053 | } |
| 3054 | default: |
| 3055 | // FIXME: Parse the MCSymbol machine operand. |
| 3056 | return error(Msg: "expected a machine operand" ); |
| 3057 | } |
| 3058 | return false; |
| 3059 | } |
| 3060 | |
| 3061 | bool MIParser::parseMachineOperandAndTargetFlags( |
| 3062 | const unsigned OpCode, const unsigned OpIdx, MachineOperand &Dest, |
| 3063 | std::optional<unsigned> &TiedDefIdx) { |
| 3064 | unsigned TF = 0; |
| 3065 | bool HasTargetFlags = false; |
| 3066 | if (Token.is(K: MIToken::kw_target_flags)) { |
| 3067 | HasTargetFlags = true; |
| 3068 | lex(); |
| 3069 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 3070 | return true; |
| 3071 | if (Token.isNot(K: MIToken::Identifier)) |
| 3072 | return error(Msg: "expected the name of the target flag" ); |
| 3073 | if (PFS.Target.getDirectTargetFlag(Name: Token.stringValue(), Flag&: TF)) { |
| 3074 | if (PFS.Target.getBitmaskTargetFlag(Name: Token.stringValue(), Flag&: TF)) |
| 3075 | return error(Msg: "use of undefined target flag '" + Token.stringValue() + |
| 3076 | "'" ); |
| 3077 | } |
| 3078 | lex(); |
| 3079 | while (Token.is(K: MIToken::comma)) { |
| 3080 | lex(); |
| 3081 | if (Token.isNot(K: MIToken::Identifier)) |
| 3082 | return error(Msg: "expected the name of the target flag" ); |
| 3083 | unsigned BitFlag = 0; |
| 3084 | if (PFS.Target.getBitmaskTargetFlag(Name: Token.stringValue(), Flag&: BitFlag)) |
| 3085 | return error(Msg: "use of undefined target flag '" + Token.stringValue() + |
| 3086 | "'" ); |
| 3087 | // TODO: Report an error when using a duplicate bit target flag. |
| 3088 | TF |= BitFlag; |
| 3089 | lex(); |
| 3090 | } |
| 3091 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 3092 | return true; |
| 3093 | } |
| 3094 | auto Loc = Token.location(); |
| 3095 | if (parseMachineOperand(OpCode, OpIdx, Dest, TiedDefIdx)) |
| 3096 | return true; |
| 3097 | if (!HasTargetFlags) |
| 3098 | return false; |
| 3099 | if (Dest.isReg()) |
| 3100 | return error(Loc, Msg: "register operands can't have target flags" ); |
| 3101 | Dest.setTargetFlags(TF); |
| 3102 | return false; |
| 3103 | } |
| 3104 | |
| 3105 | bool MIParser::parseOffset(int64_t &Offset) { |
| 3106 | if (Token.isNot(K: MIToken::plus) && Token.isNot(K: MIToken::minus)) |
| 3107 | return false; |
| 3108 | StringRef Sign = Token.range(); |
| 3109 | bool IsNegative = Token.is(K: MIToken::minus); |
| 3110 | lex(); |
| 3111 | if (Token.isNot(K: MIToken::IntegerLiteral)) |
| 3112 | return error(Msg: "expected an integer literal after '" + Sign + "'" ); |
| 3113 | if (Token.integerValue().getSignificantBits() > 64) |
| 3114 | return error(Msg: "expected 64-bit integer (too large)" ); |
| 3115 | Offset = Token.integerValue().getExtValue(); |
| 3116 | if (IsNegative) |
| 3117 | Offset = -Offset; |
| 3118 | lex(); |
| 3119 | return false; |
| 3120 | } |
| 3121 | |
| 3122 | bool MIParser::parseIRBlockAddressTaken(BasicBlock *&BB) { |
| 3123 | assert(Token.is(MIToken::kw_ir_block_address_taken)); |
| 3124 | lex(); |
| 3125 | if (Token.isNot(K: MIToken::IRBlock) && Token.isNot(K: MIToken::NamedIRBlock)) |
| 3126 | return error(Msg: "expected basic block after 'ir_block_address_taken'" ); |
| 3127 | |
| 3128 | if (parseIRBlock(BB, F: MF.getFunction())) |
| 3129 | return true; |
| 3130 | |
| 3131 | lex(); |
| 3132 | return false; |
| 3133 | } |
| 3134 | |
| 3135 | bool MIParser::parseAlignment(uint64_t &Alignment) { |
| 3136 | assert(Token.is(MIToken::kw_align) || Token.is(MIToken::kw_basealign)); |
| 3137 | lex(); |
| 3138 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isSigned()) |
| 3139 | return error(Msg: "expected an integer literal after 'align'" ); |
| 3140 | if (getUint64(Result&: Alignment)) |
| 3141 | return true; |
| 3142 | lex(); |
| 3143 | |
| 3144 | if (!isPowerOf2_64(Value: Alignment)) |
| 3145 | return error(Msg: "expected a power-of-2 literal after 'align'" ); |
| 3146 | |
| 3147 | return false; |
| 3148 | } |
| 3149 | |
| 3150 | bool MIParser::parseAddrspace(unsigned &Addrspace) { |
| 3151 | assert(Token.is(MIToken::kw_addrspace)); |
| 3152 | lex(); |
| 3153 | if (Token.isNot(K: MIToken::IntegerLiteral) || Token.integerValue().isSigned()) |
| 3154 | return error(Msg: "expected an integer literal after 'addrspace'" ); |
| 3155 | if (getUnsigned(Result&: Addrspace)) |
| 3156 | return true; |
| 3157 | lex(); |
| 3158 | return false; |
| 3159 | } |
| 3160 | |
| 3161 | bool MIParser::parseOperandsOffset(MachineOperand &Op) { |
| 3162 | int64_t Offset = 0; |
| 3163 | if (parseOffset(Offset)) |
| 3164 | return true; |
| 3165 | Op.setOffset(Offset); |
| 3166 | return false; |
| 3167 | } |
| 3168 | |
| 3169 | static bool parseIRValue(const MIToken &Token, PerFunctionMIParsingState &PFS, |
| 3170 | const Value *&V, ErrorCallbackType ErrCB) { |
| 3171 | switch (Token.kind()) { |
| 3172 | case MIToken::NamedIRValue: { |
| 3173 | V = PFS.MF.getFunction().getValueSymbolTable()->lookup(Name: Token.stringValue()); |
| 3174 | break; |
| 3175 | } |
| 3176 | case MIToken::IRValue: { |
| 3177 | unsigned SlotNumber = 0; |
| 3178 | if (getUnsigned(Token, Result&: SlotNumber, ErrCB)) |
| 3179 | return true; |
| 3180 | V = PFS.getIRValue(Slot: SlotNumber); |
| 3181 | break; |
| 3182 | } |
| 3183 | case MIToken::NamedGlobalValue: |
| 3184 | case MIToken::GlobalValue: { |
| 3185 | GlobalValue *GV = nullptr; |
| 3186 | if (parseGlobalValue(Token, PFS, GV, ErrCB)) |
| 3187 | return true; |
| 3188 | V = GV; |
| 3189 | break; |
| 3190 | } |
| 3191 | case MIToken::QuotedIRValue: { |
| 3192 | const Constant *C = nullptr; |
| 3193 | if (parseIRConstant(Loc: Token.location(), StringValue: Token.stringValue(), PFS, C, ErrCB)) |
| 3194 | return true; |
| 3195 | V = C; |
| 3196 | break; |
| 3197 | } |
| 3198 | case MIToken::kw_unknown_address: |
| 3199 | V = nullptr; |
| 3200 | return false; |
| 3201 | default: |
| 3202 | llvm_unreachable("The current token should be an IR block reference" ); |
| 3203 | } |
| 3204 | if (!V) |
| 3205 | return ErrCB(Token.location(), Twine("use of undefined IR value '" ) + Token.range() + "'" ); |
| 3206 | return false; |
| 3207 | } |
| 3208 | |
| 3209 | bool MIParser::parseIRValue(const Value *&V) { |
| 3210 | return ::parseIRValue( |
| 3211 | Token, PFS, V, ErrCB: [this](StringRef::iterator Loc, const Twine &Msg) -> bool { |
| 3212 | return error(Loc, Msg); |
| 3213 | }); |
| 3214 | } |
| 3215 | |
| 3216 | bool MIParser::getUint64(uint64_t &Result) { |
| 3217 | if (Token.hasIntegerValue()) { |
| 3218 | if (Token.integerValue().getActiveBits() > 64) |
| 3219 | return error(Msg: "expected 64-bit integer (too large)" ); |
| 3220 | Result = Token.integerValue().getZExtValue(); |
| 3221 | return false; |
| 3222 | } |
| 3223 | if (Token.is(K: MIToken::HexLiteral)) { |
| 3224 | APInt A; |
| 3225 | if (getHexUint(Result&: A)) |
| 3226 | return true; |
| 3227 | if (A.getBitWidth() > 64) |
| 3228 | return error(Msg: "expected 64-bit integer (too large)" ); |
| 3229 | Result = A.getZExtValue(); |
| 3230 | return false; |
| 3231 | } |
| 3232 | return true; |
| 3233 | } |
| 3234 | |
| 3235 | bool MIParser::getHexUint(APInt &Result) { |
| 3236 | return ::getHexUint(Token, Result); |
| 3237 | } |
| 3238 | |
| 3239 | bool MIParser::parseMemoryOperandFlag(MachineMemOperand::Flags &Flags) { |
| 3240 | const auto OldFlags = Flags; |
| 3241 | switch (Token.kind()) { |
| 3242 | case MIToken::kw_volatile: |
| 3243 | Flags |= MachineMemOperand::MOVolatile; |
| 3244 | break; |
| 3245 | case MIToken::kw_non_temporal: |
| 3246 | Flags |= MachineMemOperand::MONonTemporal; |
| 3247 | break; |
| 3248 | case MIToken::kw_dereferenceable: |
| 3249 | Flags |= MachineMemOperand::MODereferenceable; |
| 3250 | break; |
| 3251 | case MIToken::kw_invariant: |
| 3252 | Flags |= MachineMemOperand::MOInvariant; |
| 3253 | break; |
| 3254 | case MIToken::StringConstant: { |
| 3255 | MachineMemOperand::Flags TF; |
| 3256 | if (PFS.Target.getMMOTargetFlag(Name: Token.stringValue(), Flag&: TF)) |
| 3257 | return error(Msg: "use of undefined target MMO flag '" + Token.stringValue() + |
| 3258 | "'" ); |
| 3259 | Flags |= TF; |
| 3260 | break; |
| 3261 | } |
| 3262 | default: |
| 3263 | llvm_unreachable("The current token should be a memory operand flag" ); |
| 3264 | } |
| 3265 | if (OldFlags == Flags) |
| 3266 | // We know that the same flag is specified more than once when the flags |
| 3267 | // weren't modified. |
| 3268 | return error(Msg: "duplicate '" + Token.stringValue() + "' memory operand flag" ); |
| 3269 | lex(); |
| 3270 | return false; |
| 3271 | } |
| 3272 | |
| 3273 | bool MIParser::parseMemoryPseudoSourceValue(const PseudoSourceValue *&PSV) { |
| 3274 | switch (Token.kind()) { |
| 3275 | case MIToken::kw_stack: |
| 3276 | PSV = MF.getPSVManager().getStack(); |
| 3277 | break; |
| 3278 | case MIToken::kw_got: |
| 3279 | PSV = MF.getPSVManager().getGOT(); |
| 3280 | break; |
| 3281 | case MIToken::kw_jump_table: |
| 3282 | PSV = MF.getPSVManager().getJumpTable(); |
| 3283 | break; |
| 3284 | case MIToken::kw_constant_pool: |
| 3285 | PSV = MF.getPSVManager().getConstantPool(); |
| 3286 | break; |
| 3287 | case MIToken::FixedStackObject: { |
| 3288 | int FI; |
| 3289 | if (parseFixedStackFrameIndex(FI)) |
| 3290 | return true; |
| 3291 | PSV = MF.getPSVManager().getFixedStack(FI); |
| 3292 | // The token was already consumed, so use return here instead of break. |
| 3293 | return false; |
| 3294 | } |
| 3295 | case MIToken::StackObject: { |
| 3296 | int FI; |
| 3297 | if (parseStackFrameIndex(FI)) |
| 3298 | return true; |
| 3299 | PSV = MF.getPSVManager().getFixedStack(FI); |
| 3300 | // The token was already consumed, so use return here instead of break. |
| 3301 | return false; |
| 3302 | } |
| 3303 | case MIToken::kw_call_entry: |
| 3304 | lex(); |
| 3305 | switch (Token.kind()) { |
| 3306 | case MIToken::GlobalValue: |
| 3307 | case MIToken::NamedGlobalValue: { |
| 3308 | GlobalValue *GV = nullptr; |
| 3309 | if (parseGlobalValue(GV)) |
| 3310 | return true; |
| 3311 | PSV = MF.getPSVManager().getGlobalValueCallEntry(GV); |
| 3312 | break; |
| 3313 | } |
| 3314 | case MIToken::ExternalSymbol: |
| 3315 | PSV = MF.getPSVManager().getExternalSymbolCallEntry( |
| 3316 | ES: MF.createExternalSymbolName(Name: Token.stringValue())); |
| 3317 | break; |
| 3318 | default: |
| 3319 | return error( |
| 3320 | Msg: "expected a global value or an external symbol after 'call-entry'" ); |
| 3321 | } |
| 3322 | break; |
| 3323 | case MIToken::kw_custom: { |
| 3324 | lex(); |
| 3325 | const auto *TII = MF.getSubtarget().getInstrInfo(); |
| 3326 | if (const auto *Formatter = TII->getMIRFormatter()) { |
| 3327 | if (Formatter->parseCustomPseudoSourceValue( |
| 3328 | Src: Token.stringValue(), MF, PFS, PSV, |
| 3329 | ErrorCallback: [this](StringRef::iterator Loc, const Twine &Msg) -> bool { |
| 3330 | return error(Loc, Msg); |
| 3331 | })) |
| 3332 | return true; |
| 3333 | } else |
| 3334 | return error(Msg: "unable to parse target custom pseudo source value" ); |
| 3335 | break; |
| 3336 | } |
| 3337 | default: |
| 3338 | llvm_unreachable("The current token should be pseudo source value" ); |
| 3339 | } |
| 3340 | lex(); |
| 3341 | return false; |
| 3342 | } |
| 3343 | |
| 3344 | bool MIParser::parseMachinePointerInfo(MachinePointerInfo &Dest) { |
| 3345 | if (Token.is(K: MIToken::kw_constant_pool) || Token.is(K: MIToken::kw_stack) || |
| 3346 | Token.is(K: MIToken::kw_got) || Token.is(K: MIToken::kw_jump_table) || |
| 3347 | Token.is(K: MIToken::FixedStackObject) || Token.is(K: MIToken::StackObject) || |
| 3348 | Token.is(K: MIToken::kw_call_entry) || Token.is(K: MIToken::kw_custom)) { |
| 3349 | const PseudoSourceValue *PSV = nullptr; |
| 3350 | if (parseMemoryPseudoSourceValue(PSV)) |
| 3351 | return true; |
| 3352 | int64_t Offset = 0; |
| 3353 | if (parseOffset(Offset)) |
| 3354 | return true; |
| 3355 | Dest = MachinePointerInfo(PSV, Offset); |
| 3356 | return false; |
| 3357 | } |
| 3358 | if (Token.isNot(K: MIToken::NamedIRValue) && Token.isNot(K: MIToken::IRValue) && |
| 3359 | Token.isNot(K: MIToken::GlobalValue) && |
| 3360 | Token.isNot(K: MIToken::NamedGlobalValue) && |
| 3361 | Token.isNot(K: MIToken::QuotedIRValue) && |
| 3362 | Token.isNot(K: MIToken::kw_unknown_address)) |
| 3363 | return error(Msg: "expected an IR value reference" ); |
| 3364 | const Value *V = nullptr; |
| 3365 | if (parseIRValue(V)) |
| 3366 | return true; |
| 3367 | if (V && !V->getType()->isPointerTy()) |
| 3368 | return error(Msg: "expected a pointer IR value" ); |
| 3369 | lex(); |
| 3370 | int64_t Offset = 0; |
| 3371 | if (parseOffset(Offset)) |
| 3372 | return true; |
| 3373 | Dest = MachinePointerInfo(V, Offset); |
| 3374 | return false; |
| 3375 | } |
| 3376 | |
| 3377 | bool MIParser::parseOptionalScope(LLVMContext &Context, |
| 3378 | SyncScope::ID &SSID) { |
| 3379 | SSID = SyncScope::System; |
| 3380 | if (Token.is(K: MIToken::Identifier) && Token.stringValue() == "syncscope" ) { |
| 3381 | lex(); |
| 3382 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 3383 | return error(Msg: "expected '(' in syncscope" ); |
| 3384 | |
| 3385 | std::string SSN; |
| 3386 | if (parseStringConstant(Result&: SSN)) |
| 3387 | return true; |
| 3388 | |
| 3389 | SSID = Context.getOrInsertSyncScopeID(SSN); |
| 3390 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 3391 | return error(Msg: "expected ')' in syncscope" ); |
| 3392 | } |
| 3393 | |
| 3394 | return false; |
| 3395 | } |
| 3396 | |
| 3397 | bool MIParser::parseOptionalAtomicOrdering(AtomicOrdering &Order) { |
| 3398 | Order = AtomicOrdering::NotAtomic; |
| 3399 | if (Token.isNot(K: MIToken::Identifier)) |
| 3400 | return false; |
| 3401 | |
| 3402 | Order = StringSwitch<AtomicOrdering>(Token.stringValue()) |
| 3403 | .Case(S: "unordered" , Value: AtomicOrdering::Unordered) |
| 3404 | .Case(S: "monotonic" , Value: AtomicOrdering::Monotonic) |
| 3405 | .Case(S: "acquire" , Value: AtomicOrdering::Acquire) |
| 3406 | .Case(S: "release" , Value: AtomicOrdering::Release) |
| 3407 | .Case(S: "acq_rel" , Value: AtomicOrdering::AcquireRelease) |
| 3408 | .Case(S: "seq_cst" , Value: AtomicOrdering::SequentiallyConsistent) |
| 3409 | .Default(Value: AtomicOrdering::NotAtomic); |
| 3410 | |
| 3411 | if (Order != AtomicOrdering::NotAtomic) { |
| 3412 | lex(); |
| 3413 | return false; |
| 3414 | } |
| 3415 | |
| 3416 | return error(Msg: "expected an atomic scope, ordering or a size specification" ); |
| 3417 | } |
| 3418 | |
| 3419 | bool MIParser::parseMachineMemoryOperand(MachineMemOperand *&Dest) { |
| 3420 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 3421 | return true; |
| 3422 | MachineMemOperand::Flags Flags = MachineMemOperand::MONone; |
| 3423 | while (Token.isMemoryOperandFlag()) { |
| 3424 | if (parseMemoryOperandFlag(Flags)) |
| 3425 | return true; |
| 3426 | } |
| 3427 | if (Token.isNot(K: MIToken::Identifier) || |
| 3428 | (Token.stringValue() != "load" && Token.stringValue() != "store" )) |
| 3429 | return error(Msg: "expected 'load' or 'store' memory operation" ); |
| 3430 | if (Token.stringValue() == "load" ) |
| 3431 | Flags |= MachineMemOperand::MOLoad; |
| 3432 | else |
| 3433 | Flags |= MachineMemOperand::MOStore; |
| 3434 | lex(); |
| 3435 | |
| 3436 | // Optional 'store' for operands that both load and store. |
| 3437 | if (Token.is(K: MIToken::Identifier) && Token.stringValue() == "store" ) { |
| 3438 | Flags |= MachineMemOperand::MOStore; |
| 3439 | lex(); |
| 3440 | } |
| 3441 | |
| 3442 | // Optional synchronization scope. |
| 3443 | SyncScope::ID SSID; |
| 3444 | if (parseOptionalScope(Context&: MF.getFunction().getContext(), SSID)) |
| 3445 | return true; |
| 3446 | |
| 3447 | // Up to two atomic orderings (cmpxchg provides guarantees on failure). |
| 3448 | AtomicOrdering Order, FailureOrder; |
| 3449 | if (parseOptionalAtomicOrdering(Order)) |
| 3450 | return true; |
| 3451 | |
| 3452 | if (parseOptionalAtomicOrdering(Order&: FailureOrder)) |
| 3453 | return true; |
| 3454 | |
| 3455 | if (Token.isNot(K: MIToken::IntegerLiteral) && |
| 3456 | Token.isNot(K: MIToken::kw_unknown_size) && |
| 3457 | Token.isNot(K: MIToken::lparen)) |
| 3458 | return error(Msg: "expected memory LLT, the size integer literal or 'unknown-size' after " |
| 3459 | "memory operation" ); |
| 3460 | |
| 3461 | LLT MemoryType; |
| 3462 | if (Token.is(K: MIToken::IntegerLiteral)) { |
| 3463 | uint64_t Size; |
| 3464 | if (getUint64(Result&: Size)) |
| 3465 | return true; |
| 3466 | |
| 3467 | // Convert from bytes to bits for storage. |
| 3468 | MemoryType = LLT::scalar(SizeInBits: 8 * Size); |
| 3469 | lex(); |
| 3470 | } else if (Token.is(K: MIToken::kw_unknown_size)) { |
| 3471 | lex(); |
| 3472 | } else { |
| 3473 | if (expectAndConsume(TokenKind: MIToken::lparen)) |
| 3474 | return true; |
| 3475 | if (parseLowLevelType(Loc: Token.location(), Ty&: MemoryType)) |
| 3476 | return true; |
| 3477 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 3478 | return true; |
| 3479 | } |
| 3480 | |
| 3481 | MachinePointerInfo Ptr = MachinePointerInfo(); |
| 3482 | if (Token.is(K: MIToken::Identifier)) { |
| 3483 | const char *Word = |
| 3484 | ((Flags & MachineMemOperand::MOLoad) && |
| 3485 | (Flags & MachineMemOperand::MOStore)) |
| 3486 | ? "on" |
| 3487 | : Flags & MachineMemOperand::MOLoad ? "from" : "into" ; |
| 3488 | if (Token.stringValue() != Word) |
| 3489 | return error(Msg: Twine("expected '" ) + Word + "'" ); |
| 3490 | lex(); |
| 3491 | |
| 3492 | if (parseMachinePointerInfo(Dest&: Ptr)) |
| 3493 | return true; |
| 3494 | } |
| 3495 | uint64_t BaseAlignment = |
| 3496 | MemoryType.isValid() |
| 3497 | ? PowerOf2Ceil(A: MemoryType.getSizeInBytes().getKnownMinValue()) |
| 3498 | : 1; |
| 3499 | AAMDNodes AAInfo; |
| 3500 | MDNode *Range = nullptr; |
| 3501 | while (consumeIfPresent(TokenKind: MIToken::comma)) { |
| 3502 | switch (Token.kind()) { |
| 3503 | case MIToken::kw_align: { |
| 3504 | // align is printed if it is different than size. |
| 3505 | uint64_t Alignment; |
| 3506 | if (parseAlignment(Alignment)) |
| 3507 | return true; |
| 3508 | if (Ptr.Offset & (Alignment - 1)) { |
| 3509 | // MachineMemOperand::getAlign never returns a value greater than the |
| 3510 | // alignment of offset, so this just guards against hand-written MIR |
| 3511 | // that specifies a large "align" value when it should probably use |
| 3512 | // "basealign" instead. |
| 3513 | return error(Msg: "specified alignment is more aligned than offset" ); |
| 3514 | } |
| 3515 | BaseAlignment = Alignment; |
| 3516 | break; |
| 3517 | } |
| 3518 | case MIToken::kw_basealign: |
| 3519 | // basealign is printed if it is different than align. |
| 3520 | if (parseAlignment(Alignment&: BaseAlignment)) |
| 3521 | return true; |
| 3522 | break; |
| 3523 | case MIToken::kw_addrspace: |
| 3524 | if (parseAddrspace(Addrspace&: Ptr.AddrSpace)) |
| 3525 | return true; |
| 3526 | break; |
| 3527 | case MIToken::md_tbaa: |
| 3528 | lex(); |
| 3529 | if (parseMDNode(Node&: AAInfo.TBAA)) |
| 3530 | return true; |
| 3531 | break; |
| 3532 | case MIToken::md_alias_scope: |
| 3533 | lex(); |
| 3534 | if (parseMDNode(Node&: AAInfo.Scope)) |
| 3535 | return true; |
| 3536 | break; |
| 3537 | case MIToken::md_noalias: |
| 3538 | lex(); |
| 3539 | if (parseMDNode(Node&: AAInfo.NoAlias)) |
| 3540 | return true; |
| 3541 | break; |
| 3542 | case MIToken::md_noalias_addrspace: |
| 3543 | lex(); |
| 3544 | if (parseMDNode(Node&: AAInfo.NoAliasAddrSpace)) |
| 3545 | return true; |
| 3546 | break; |
| 3547 | case MIToken::md_range: |
| 3548 | lex(); |
| 3549 | if (parseMDNode(Node&: Range)) |
| 3550 | return true; |
| 3551 | break; |
| 3552 | // TODO: Report an error on duplicate metadata nodes. |
| 3553 | default: |
| 3554 | return error(Msg: "expected 'align' or '!tbaa' or '!alias.scope' or " |
| 3555 | "'!noalias' or '!range' or '!noalias.addrspace'" ); |
| 3556 | } |
| 3557 | } |
| 3558 | if (expectAndConsume(TokenKind: MIToken::rparen)) |
| 3559 | return true; |
| 3560 | Dest = MF.getMachineMemOperand(PtrInfo: Ptr, f: Flags, MemTy: MemoryType, base_alignment: Align(BaseAlignment), |
| 3561 | AAInfo, Ranges: Range, SSID, Ordering: Order, FailureOrdering: FailureOrder); |
| 3562 | return false; |
| 3563 | } |
| 3564 | |
| 3565 | bool MIParser::parsePreOrPostInstrSymbol(MCSymbol *&Symbol) { |
| 3566 | assert((Token.is(MIToken::kw_pre_instr_symbol) || |
| 3567 | Token.is(MIToken::kw_post_instr_symbol)) && |
| 3568 | "Invalid token for a pre- post-instruction symbol!" ); |
| 3569 | lex(); |
| 3570 | if (Token.isNot(K: MIToken::MCSymbol)) |
| 3571 | return error(Msg: "expected a symbol after 'pre-instr-symbol'" ); |
| 3572 | Symbol = getOrCreateMCSymbol(Name: Token.stringValue()); |
| 3573 | lex(); |
| 3574 | if (Token.isNewlineOrEOF() || Token.is(K: MIToken::coloncolon) || |
| 3575 | Token.is(K: MIToken::lbrace)) |
| 3576 | return false; |
| 3577 | if (Token.isNot(K: MIToken::comma)) |
| 3578 | return error(Msg: "expected ',' before the next machine operand" ); |
| 3579 | lex(); |
| 3580 | return false; |
| 3581 | } |
| 3582 | |
| 3583 | bool MIParser::parseHeapAllocMarker(MDNode *&Node) { |
| 3584 | assert(Token.is(MIToken::kw_heap_alloc_marker) && |
| 3585 | "Invalid token for a heap alloc marker!" ); |
| 3586 | lex(); |
| 3587 | if (parseMDNode(Node)) |
| 3588 | return true; |
| 3589 | if (!Node) |
| 3590 | return error(Msg: "expected a MDNode after 'heap-alloc-marker'" ); |
| 3591 | if (Token.isNewlineOrEOF() || Token.is(K: MIToken::coloncolon) || |
| 3592 | Token.is(K: MIToken::lbrace)) |
| 3593 | return false; |
| 3594 | if (Token.isNot(K: MIToken::comma)) |
| 3595 | return error(Msg: "expected ',' before the next machine operand" ); |
| 3596 | lex(); |
| 3597 | return false; |
| 3598 | } |
| 3599 | |
| 3600 | bool MIParser::parsePCSections(MDNode *&Node) { |
| 3601 | assert(Token.is(MIToken::kw_pcsections) && |
| 3602 | "Invalid token for a PC sections!" ); |
| 3603 | lex(); |
| 3604 | if (parseMDNode(Node)) |
| 3605 | return true; |
| 3606 | if (!Node) |
| 3607 | return error(Msg: "expected a MDNode after 'pcsections'" ); |
| 3608 | if (Token.isNewlineOrEOF() || Token.is(K: MIToken::coloncolon) || |
| 3609 | Token.is(K: MIToken::lbrace)) |
| 3610 | return false; |
| 3611 | if (Token.isNot(K: MIToken::comma)) |
| 3612 | return error(Msg: "expected ',' before the next machine operand" ); |
| 3613 | lex(); |
| 3614 | return false; |
| 3615 | } |
| 3616 | |
| 3617 | static void initSlots2BasicBlocks( |
| 3618 | const Function &F, |
| 3619 | DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { |
| 3620 | ModuleSlotTracker MST(F.getParent(), /*ShouldInitializeAllMetadata=*/false); |
| 3621 | MST.incorporateFunction(F); |
| 3622 | for (const auto &BB : F) { |
| 3623 | if (BB.hasName()) |
| 3624 | continue; |
| 3625 | int Slot = MST.getLocalSlot(V: &BB); |
| 3626 | if (Slot == -1) |
| 3627 | continue; |
| 3628 | Slots2BasicBlocks.insert(KV: std::make_pair(x: unsigned(Slot), y: &BB)); |
| 3629 | } |
| 3630 | } |
| 3631 | |
| 3632 | static const BasicBlock *getIRBlockFromSlot( |
| 3633 | unsigned Slot, |
| 3634 | const DenseMap<unsigned, const BasicBlock *> &Slots2BasicBlocks) { |
| 3635 | return Slots2BasicBlocks.lookup(Val: Slot); |
| 3636 | } |
| 3637 | |
| 3638 | const BasicBlock *MIParser::getIRBlock(unsigned Slot) { |
| 3639 | if (Slots2BasicBlocks.empty()) |
| 3640 | initSlots2BasicBlocks(F: MF.getFunction(), Slots2BasicBlocks); |
| 3641 | return getIRBlockFromSlot(Slot, Slots2BasicBlocks); |
| 3642 | } |
| 3643 | |
| 3644 | const BasicBlock *MIParser::getIRBlock(unsigned Slot, const Function &F) { |
| 3645 | if (&F == &MF.getFunction()) |
| 3646 | return getIRBlock(Slot); |
| 3647 | DenseMap<unsigned, const BasicBlock *> CustomSlots2BasicBlocks; |
| 3648 | initSlots2BasicBlocks(F, Slots2BasicBlocks&: CustomSlots2BasicBlocks); |
| 3649 | return getIRBlockFromSlot(Slot, Slots2BasicBlocks: CustomSlots2BasicBlocks); |
| 3650 | } |
| 3651 | |
| 3652 | MCSymbol *MIParser::getOrCreateMCSymbol(StringRef Name) { |
| 3653 | // FIXME: Currently we can't recognize temporary or local symbols and call all |
| 3654 | // of the appropriate forms to create them. However, this handles basic cases |
| 3655 | // well as most of the special aspects are recognized by a prefix on their |
| 3656 | // name, and the input names should already be unique. For test cases, keeping |
| 3657 | // the symbol name out of the symbol table isn't terribly important. |
| 3658 | return MF.getContext().getOrCreateSymbol(Name); |
| 3659 | } |
| 3660 | |
| 3661 | bool MIParser::parseStringConstant(std::string &Result) { |
| 3662 | if (Token.isNot(K: MIToken::StringConstant)) |
| 3663 | return error(Msg: "expected string constant" ); |
| 3664 | Result = std::string(Token.stringValue()); |
| 3665 | lex(); |
| 3666 | return false; |
| 3667 | } |
| 3668 | |
| 3669 | bool llvm::parseMachineBasicBlockDefinitions(PerFunctionMIParsingState &PFS, |
| 3670 | StringRef Src, |
| 3671 | SMDiagnostic &Error) { |
| 3672 | return MIParser(PFS, Error, Src).parseBasicBlockDefinitions(MBBSlots&: PFS.MBBSlots); |
| 3673 | } |
| 3674 | |
| 3675 | bool llvm::parseMachineInstructions(PerFunctionMIParsingState &PFS, |
| 3676 | StringRef Src, SMDiagnostic &Error) { |
| 3677 | return MIParser(PFS, Error, Src).parseBasicBlocks(); |
| 3678 | } |
| 3679 | |
| 3680 | bool llvm::parseMBBReference(PerFunctionMIParsingState &PFS, |
| 3681 | MachineBasicBlock *&MBB, StringRef Src, |
| 3682 | SMDiagnostic &Error) { |
| 3683 | return MIParser(PFS, Error, Src).parseStandaloneMBB(MBB); |
| 3684 | } |
| 3685 | |
| 3686 | bool llvm::parseRegisterReference(PerFunctionMIParsingState &PFS, |
| 3687 | Register &Reg, StringRef Src, |
| 3688 | SMDiagnostic &Error) { |
| 3689 | return MIParser(PFS, Error, Src).parseStandaloneRegister(Reg); |
| 3690 | } |
| 3691 | |
| 3692 | bool llvm::parseNamedRegisterReference(PerFunctionMIParsingState &PFS, |
| 3693 | Register &Reg, StringRef Src, |
| 3694 | SMDiagnostic &Error) { |
| 3695 | return MIParser(PFS, Error, Src).parseStandaloneNamedRegister(Reg); |
| 3696 | } |
| 3697 | |
| 3698 | bool llvm::parseVirtualRegisterReference(PerFunctionMIParsingState &PFS, |
| 3699 | VRegInfo *&Info, StringRef Src, |
| 3700 | SMDiagnostic &Error) { |
| 3701 | return MIParser(PFS, Error, Src).parseStandaloneVirtualRegister(Info); |
| 3702 | } |
| 3703 | |
| 3704 | bool llvm::parseStackObjectReference(PerFunctionMIParsingState &PFS, |
| 3705 | int &FI, StringRef Src, |
| 3706 | SMDiagnostic &Error) { |
| 3707 | return MIParser(PFS, Error, Src).parseStandaloneStackObject(FI); |
| 3708 | } |
| 3709 | |
| 3710 | bool llvm::parseMDNode(PerFunctionMIParsingState &PFS, |
| 3711 | MDNode *&Node, StringRef Src, SMDiagnostic &Error) { |
| 3712 | return MIParser(PFS, Error, Src).parseStandaloneMDNode(Node); |
| 3713 | } |
| 3714 | |
| 3715 | bool llvm::parseMachineMetadata(PerFunctionMIParsingState &PFS, StringRef Src, |
| 3716 | SMRange SrcRange, SMDiagnostic &Error) { |
| 3717 | return MIParser(PFS, Error, Src, SrcRange).parseMachineMetadata(); |
| 3718 | } |
| 3719 | |
| 3720 | bool MIRFormatter::parseIRValue(StringRef Src, MachineFunction &MF, |
| 3721 | PerFunctionMIParsingState &PFS, const Value *&V, |
| 3722 | ErrorCallbackType ErrorCallback) { |
| 3723 | MIToken Token; |
| 3724 | Src = lexMIToken(Source: Src, Token, ErrorCallback: [&](StringRef::iterator Loc, const Twine &Msg) { |
| 3725 | ErrorCallback(Loc, Msg); |
| 3726 | }); |
| 3727 | V = nullptr; |
| 3728 | |
| 3729 | return ::parseIRValue(Token, PFS, V, ErrCB: ErrorCallback); |
| 3730 | } |
| 3731 | |