| 1 | //===-- LLParser.cpp - Parser Class ---------------------------------------===// |
| 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 defines the parser class for .ll files. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/AsmParser/LLParser.h" |
| 14 | #include "llvm/ADT/APSInt.h" |
| 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/ScopeExit.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/AsmParser/LLToken.h" |
| 20 | #include "llvm/AsmParser/SlotMapping.h" |
| 21 | #include "llvm/BinaryFormat/Dwarf.h" |
| 22 | #include "llvm/IR/Argument.h" |
| 23 | #include "llvm/IR/Attributes.h" |
| 24 | #include "llvm/IR/AutoUpgrade.h" |
| 25 | #include "llvm/IR/BasicBlock.h" |
| 26 | #include "llvm/IR/CallingConv.h" |
| 27 | #include "llvm/IR/Comdat.h" |
| 28 | #include "llvm/IR/ConstantRange.h" |
| 29 | #include "llvm/IR/ConstantRangeList.h" |
| 30 | #include "llvm/IR/Constants.h" |
| 31 | #include "llvm/IR/DebugInfoMetadata.h" |
| 32 | #include "llvm/IR/DerivedTypes.h" |
| 33 | #include "llvm/IR/Function.h" |
| 34 | #include "llvm/IR/GlobalIFunc.h" |
| 35 | #include "llvm/IR/GlobalObject.h" |
| 36 | #include "llvm/IR/InlineAsm.h" |
| 37 | #include "llvm/IR/InstIterator.h" |
| 38 | #include "llvm/IR/Instructions.h" |
| 39 | #include "llvm/IR/IntrinsicInst.h" |
| 40 | #include "llvm/IR/Intrinsics.h" |
| 41 | #include "llvm/IR/LLVMContext.h" |
| 42 | #include "llvm/IR/Metadata.h" |
| 43 | #include "llvm/IR/Module.h" |
| 44 | #include "llvm/IR/Operator.h" |
| 45 | #include "llvm/IR/Value.h" |
| 46 | #include "llvm/IR/ValueSymbolTable.h" |
| 47 | #include "llvm/Support/Casting.h" |
| 48 | #include "llvm/Support/Compiler.h" |
| 49 | #include "llvm/Support/ErrorHandling.h" |
| 50 | #include "llvm/Support/MathExtras.h" |
| 51 | #include "llvm/Support/ModRef.h" |
| 52 | #include "llvm/Support/SaveAndRestore.h" |
| 53 | #include "llvm/Support/raw_ostream.h" |
| 54 | #include <algorithm> |
| 55 | #include <cassert> |
| 56 | #include <cstring> |
| 57 | #include <optional> |
| 58 | #include <vector> |
| 59 | |
| 60 | using namespace llvm; |
| 61 | |
| 62 | static cl::opt<bool> AllowIncompleteIR( |
| 63 | "allow-incomplete-ir" , cl::init(Val: false), cl::Hidden, |
| 64 | cl::desc( |
| 65 | "Allow incomplete IR on a best effort basis (references to unknown " |
| 66 | "metadata will be dropped)" )); |
| 67 | |
| 68 | static std::string getTypeString(Type *T) { |
| 69 | std::string Result; |
| 70 | raw_string_ostream Tmp(Result); |
| 71 | Tmp << *T; |
| 72 | return Tmp.str(); |
| 73 | } |
| 74 | |
| 75 | /// Return whether skipped trivia contains a block comment that crosses the |
| 76 | /// boundary between two metadata definitions. |
| 77 | static bool (SMLoc BeginLoc, SMLoc EndLoc, |
| 78 | SMLoc BoundaryLoc) { |
| 79 | const char *Begin = BeginLoc.getPointer(); |
| 80 | const char *End = EndLoc.getPointer(); |
| 81 | const char *Boundary = BoundaryLoc.getPointer(); |
| 82 | const char * = nullptr; |
| 83 | bool = false; |
| 84 | |
| 85 | for (const char *Ptr = Begin; Ptr < End;) { |
| 86 | if (BlockCommentStart) { |
| 87 | if (Ptr + 1 < End && Ptr[0] == '*' && Ptr[1] == '/') { |
| 88 | Ptr += 2; |
| 89 | if (BlockCommentStart < Boundary && Ptr > Boundary) |
| 90 | return true; |
| 91 | BlockCommentStart = nullptr; |
| 92 | continue; |
| 93 | } |
| 94 | ++Ptr; |
| 95 | continue; |
| 96 | } |
| 97 | |
| 98 | if (InLineComment) { |
| 99 | if (*Ptr == '\n' || *Ptr == '\r') |
| 100 | InLineComment = false; |
| 101 | ++Ptr; |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if (*Ptr == ';') { |
| 106 | InLineComment = true; |
| 107 | ++Ptr; |
| 108 | continue; |
| 109 | } |
| 110 | if (Ptr + 1 < End && Ptr[0] == '/' && Ptr[1] == '*') { |
| 111 | BlockCommentStart = Ptr; |
| 112 | Ptr += 2; |
| 113 | continue; |
| 114 | } |
| 115 | ++Ptr; |
| 116 | } |
| 117 | |
| 118 | return BlockCommentStart && BlockCommentStart < Boundary && End > Boundary; |
| 119 | } |
| 120 | |
| 121 | /// Run: module ::= toplevelentity* |
| 122 | bool LLParser::Run(bool UpgradeDebugInfo, |
| 123 | DataLayoutCallbackTy DataLayoutCallback) { |
| 124 | // Prime the lexer. |
| 125 | Lex.Lex(); |
| 126 | |
| 127 | if (Context.shouldDiscardValueNames()) |
| 128 | return error( |
| 129 | L: Lex.getLoc(), |
| 130 | Msg: "Can't read textual IR with a Context that discards named Values" ); |
| 131 | |
| 132 | if (M) { |
| 133 | if (parseTargetDefinitions(DataLayoutCallback)) |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | return parseTopLevelEntities() || validateEndOfModule(UpgradeDebugInfo) || |
| 138 | validateEndOfIndex(); |
| 139 | } |
| 140 | |
| 141 | bool LLParser::parseStandaloneConstantValue(Constant *&C, |
| 142 | const SlotMapping *Slots) { |
| 143 | restoreParsingState(Slots); |
| 144 | Lex.Lex(); |
| 145 | |
| 146 | Type *Ty = nullptr; |
| 147 | if (parseType(Result&: Ty) || parseConstantValue(Ty, C)) |
| 148 | return true; |
| 149 | if (Lex.getKind() != lltok::Eof) |
| 150 | return error(L: Lex.getLoc(), Msg: "expected end of string" ); |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | bool LLParser::parseTypeAtBeginning(Type *&Ty, unsigned &Read, |
| 155 | const SlotMapping *Slots) { |
| 156 | restoreParsingState(Slots); |
| 157 | Lex.Lex(); |
| 158 | |
| 159 | Read = 0; |
| 160 | SMLoc Start = Lex.getLoc(); |
| 161 | Ty = nullptr; |
| 162 | if (parseType(Result&: Ty)) |
| 163 | return true; |
| 164 | SMLoc End = Lex.getLoc(); |
| 165 | Read = End.getPointer() - Start.getPointer(); |
| 166 | |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | bool LLParser::parseDIExpressionBodyAtBeginning(MDNode *&Result, unsigned &Read, |
| 171 | const SlotMapping *Slots) { |
| 172 | restoreParsingState(Slots); |
| 173 | Lex.Lex(); |
| 174 | |
| 175 | Read = 0; |
| 176 | SMLoc Start = Lex.getLoc(); |
| 177 | Result = nullptr; |
| 178 | bool Status = parseDIExpressionBody(Result, /*IsDistinct=*/false); |
| 179 | SMLoc End = Lex.getLoc(); |
| 180 | Read = End.getPointer() - Start.getPointer(); |
| 181 | |
| 182 | return Status; |
| 183 | } |
| 184 | |
| 185 | bool LLParser::parseMetadataDefinitions(SlotMapping &Slots, |
| 186 | ArrayRef<SMLoc> DefinitionEnds) { |
| 187 | restoreParsingState(Slots: &Slots); |
| 188 | Lex.Lex(); |
| 189 | |
| 190 | for (SMLoc End : DefinitionEnds) { |
| 191 | if (Lex.getLoc().getPointer() >= End.getPointer()) |
| 192 | return error(L: End, Msg: "expected end of metadata definition" ); |
| 193 | if (Lex.getKind() != lltok::exclaim) |
| 194 | return tokError(Msg: "expected a metadata definition" ); |
| 195 | if (parseStandaloneMetadata()) |
| 196 | return true; |
| 197 | if (Lex.getPrevTokEndLoc().getPointer() > End.getPointer() || |
| 198 | (Lex.getKind() != lltok::Eof && |
| 199 | Lex.getLoc().getPointer() < End.getPointer()) || |
| 200 | blockCommentCrossesBoundary(BeginLoc: Lex.getPrevTokEndLoc(), EndLoc: Lex.getLoc(), BoundaryLoc: End)) |
| 201 | return error(L: End, Msg: "expected end of metadata definition" ); |
| 202 | } |
| 203 | |
| 204 | if (Lex.getKind() != lltok::Eof) |
| 205 | return tokError(Msg: "expected end of metadata definitions" ); |
| 206 | |
| 207 | if (!ForwardRefMDNodes.empty()) |
| 208 | return error(L: ForwardRefMDNodes.begin()->second.second, |
| 209 | Msg: "use of undefined metadata '!" + |
| 210 | Twine(ForwardRefMDNodes.begin()->first) + "'" ); |
| 211 | |
| 212 | for (auto &[_, MD] : NumberedMetadata) |
| 213 | if (MD && !MD->isResolved()) |
| 214 | MD->resolveCycles(); |
| 215 | DISubprogram::cleanupRetainedNodes(NewDistinctSPs); |
| 216 | NewDistinctSPs.clear(); |
| 217 | |
| 218 | Slots.MetadataNodes = std::move(NumberedMetadata); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | void LLParser::restoreParsingState(const SlotMapping *Slots) { |
| 223 | if (!Slots) |
| 224 | return; |
| 225 | NumberedVals = Slots->GlobalValues; |
| 226 | NumberedMetadata = Slots->MetadataNodes; |
| 227 | for (const auto &I : Slots->NamedTypes) |
| 228 | NamedTypes.insert( |
| 229 | KV: std::make_pair(x: I.getKey(), y: std::make_pair(x: I.second, y: LocTy()))); |
| 230 | for (const auto &I : Slots->Types) |
| 231 | NumberedTypes.insert( |
| 232 | x: std::make_pair(x: I.first, y: std::make_pair(x: I.second, y: LocTy()))); |
| 233 | } |
| 234 | |
| 235 | static void dropIntrinsicWithUnknownMetadataArgument(IntrinsicInst *II) { |
| 236 | // White-list intrinsics that are safe to drop. |
| 237 | if (!isa<DbgInfoIntrinsic>(Val: II) && |
| 238 | II->getIntrinsicID() != Intrinsic::experimental_noalias_scope_decl) |
| 239 | return; |
| 240 | |
| 241 | SmallVector<MetadataAsValue *> MVs; |
| 242 | for (Value *V : II->args()) |
| 243 | if (auto *MV = dyn_cast<MetadataAsValue>(Val: V)) |
| 244 | if (auto *MD = dyn_cast<MDNode>(Val: MV->getMetadata())) |
| 245 | if (MD->isTemporary()) |
| 246 | MVs.push_back(Elt: MV); |
| 247 | |
| 248 | if (!MVs.empty()) { |
| 249 | assert(II->use_empty() && "Cannot have uses" ); |
| 250 | II->eraseFromParent(); |
| 251 | |
| 252 | // Also remove no longer used MetadataAsValue wrappers. |
| 253 | for (MetadataAsValue *MV : MVs) |
| 254 | if (MV->use_empty()) |
| 255 | delete MV; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void LLParser::dropUnknownMetadataReferences() { |
| 260 | auto Pred = [](unsigned MDKind, MDNode *Node) { return Node->isTemporary(); }; |
| 261 | for (Function &F : *M) { |
| 262 | F.eraseMetadataIf(Pred); |
| 263 | for (Instruction &I : make_early_inc_range(Range: instructions(F))) { |
| 264 | I.eraseMetadataIf(Pred); |
| 265 | |
| 266 | if (auto *II = dyn_cast<IntrinsicInst>(Val: &I)) |
| 267 | dropIntrinsicWithUnknownMetadataArgument(II); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | for (GlobalVariable &GV : M->globals()) |
| 272 | GV.eraseMetadataIf(Pred); |
| 273 | |
| 274 | llvm::erase_if(C&: PendingDbgRecords, |
| 275 | P: [](const auto &E) { return std::get<2>(E)->isTemporary(); }); |
| 276 | llvm::erase_if(C&: PendingDbgInsts, |
| 277 | P: [](const auto &E) { return std::get<2>(E)->isTemporary(); }); |
| 278 | |
| 279 | for (const auto &[ID, Info] : make_early_inc_range(Range&: ForwardRefMDNodes)) { |
| 280 | // Check whether there is only a single use left, which would be in our |
| 281 | // own NumberedMetadata. |
| 282 | if (Info.first->getNumTemporaryUses() == 1) { |
| 283 | NumberedMetadata.erase(x: ID); |
| 284 | ForwardRefMDNodes.erase(x: ID); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /// validateEndOfModule - Do final validity and basic correctness checks at the |
| 290 | /// end of the module. |
| 291 | bool LLParser::validateEndOfModule(bool UpgradeDebugInfo) { |
| 292 | if (!M) |
| 293 | return false; |
| 294 | |
| 295 | // We should have already returned an error if we observed both intrinsics and |
| 296 | // records in this IR. |
| 297 | assert(!(SeenNewDbgInfoFormat && SeenOldDbgInfoFormat) && |
| 298 | "Mixed debug intrinsics/records seen without a parsing error?" ); |
| 299 | |
| 300 | // Handle any function attribute group forward references. |
| 301 | for (const auto &RAG : ForwardRefAttrGroups) { |
| 302 | Value *V = RAG.first; |
| 303 | const std::vector<unsigned> &Attrs = RAG.second; |
| 304 | AttrBuilder B(Context); |
| 305 | |
| 306 | for (const auto &Attr : Attrs) { |
| 307 | auto R = NumberedAttrBuilders.find(x: Attr); |
| 308 | if (R != NumberedAttrBuilders.end()) |
| 309 | B.merge(B: R->second); |
| 310 | } |
| 311 | |
| 312 | if (Function *Fn = dyn_cast<Function>(Val: V)) { |
| 313 | AttributeList AS = Fn->getAttributes(); |
| 314 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
| 315 | AS = AS.removeFnAttributes(C&: Context); |
| 316 | |
| 317 | FnAttrs.merge(B); |
| 318 | |
| 319 | // If the alignment was parsed as an attribute, move to the alignment |
| 320 | // field. |
| 321 | if (MaybeAlign A = FnAttrs.getAlignment()) { |
| 322 | Fn->setAlignment(*A); |
| 323 | FnAttrs.removeAttribute(Val: Attribute::Alignment); |
| 324 | } |
| 325 | |
| 326 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
| 327 | Fn->setAttributes(AS); |
| 328 | } else if (CallInst *CI = dyn_cast<CallInst>(Val: V)) { |
| 329 | AttributeList AS = CI->getAttributes(); |
| 330 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
| 331 | AS = AS.removeFnAttributes(C&: Context); |
| 332 | FnAttrs.merge(B); |
| 333 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
| 334 | CI->setAttributes(AS); |
| 335 | } else if (InvokeInst *II = dyn_cast<InvokeInst>(Val: V)) { |
| 336 | AttributeList AS = II->getAttributes(); |
| 337 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
| 338 | AS = AS.removeFnAttributes(C&: Context); |
| 339 | FnAttrs.merge(B); |
| 340 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
| 341 | II->setAttributes(AS); |
| 342 | } else if (CallBrInst *CBI = dyn_cast<CallBrInst>(Val: V)) { |
| 343 | AttributeList AS = CBI->getAttributes(); |
| 344 | AttrBuilder FnAttrs(M->getContext(), AS.getFnAttrs()); |
| 345 | AS = AS.removeFnAttributes(C&: Context); |
| 346 | FnAttrs.merge(B); |
| 347 | AS = AS.addFnAttributes(C&: Context, B: FnAttrs); |
| 348 | CBI->setAttributes(AS); |
| 349 | } else if (auto *GV = dyn_cast<GlobalVariable>(Val: V)) { |
| 350 | AttrBuilder Attrs(M->getContext(), GV->getAttributes()); |
| 351 | Attrs.merge(B); |
| 352 | GV->setAttributes(AttributeSet::get(C&: Context,B: Attrs)); |
| 353 | } else { |
| 354 | llvm_unreachable("invalid object with forward attribute group reference" ); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // If there are entries in ForwardRefBlockAddresses at this point, the |
| 359 | // function was never defined. |
| 360 | if (!ForwardRefBlockAddresses.empty()) |
| 361 | return error(L: ForwardRefBlockAddresses.begin()->first.Loc, |
| 362 | Msg: "expected function name in blockaddress" ); |
| 363 | |
| 364 | auto ResolveForwardRefDSOLocalEquivalents = [&](const ValID &GVRef, |
| 365 | GlobalValue *FwdRef) { |
| 366 | GlobalValue *GV = nullptr; |
| 367 | if (GVRef.Kind == ValID::t_GlobalName) { |
| 368 | GV = M->getNamedValue(Name: GVRef.StrVal); |
| 369 | } else { |
| 370 | GV = NumberedVals.get(ID: GVRef.UIntVal); |
| 371 | } |
| 372 | |
| 373 | if (!GV) |
| 374 | return error(L: GVRef.Loc, Msg: "unknown function '" + GVRef.StrVal + |
| 375 | "' referenced by dso_local_equivalent" ); |
| 376 | |
| 377 | if (!GV->getValueType()->isFunctionTy()) |
| 378 | return error(L: GVRef.Loc, |
| 379 | Msg: "expected a function, alias to function, or ifunc " |
| 380 | "in dso_local_equivalent" ); |
| 381 | |
| 382 | auto *Equiv = DSOLocalEquivalent::get(GV); |
| 383 | FwdRef->replaceAllUsesWith(V: Equiv); |
| 384 | FwdRef->eraseFromParent(); |
| 385 | return false; |
| 386 | }; |
| 387 | |
| 388 | // If there are entries in ForwardRefDSOLocalEquivalentIDs/Names at this |
| 389 | // point, they are references after the function was defined. Resolve those |
| 390 | // now. |
| 391 | for (auto &Iter : ForwardRefDSOLocalEquivalentIDs) { |
| 392 | if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second)) |
| 393 | return true; |
| 394 | } |
| 395 | for (auto &Iter : ForwardRefDSOLocalEquivalentNames) { |
| 396 | if (ResolveForwardRefDSOLocalEquivalents(Iter.first, Iter.second)) |
| 397 | return true; |
| 398 | } |
| 399 | ForwardRefDSOLocalEquivalentIDs.clear(); |
| 400 | ForwardRefDSOLocalEquivalentNames.clear(); |
| 401 | |
| 402 | for (const auto &NT : NumberedTypes) |
| 403 | if (NT.second.second.isValid()) |
| 404 | return error(L: NT.second.second, |
| 405 | Msg: "use of undefined type '%" + Twine(NT.first) + "'" ); |
| 406 | |
| 407 | for (const auto &[Name, TypeInfo] : NamedTypes) |
| 408 | if (TypeInfo.second.isValid()) |
| 409 | return error(L: TypeInfo.second, |
| 410 | Msg: "use of undefined type named '" + Name + "'" ); |
| 411 | |
| 412 | if (!ForwardRefComdats.empty()) |
| 413 | return error(L: ForwardRefComdats.begin()->second, |
| 414 | Msg: "use of undefined comdat '$" + |
| 415 | ForwardRefComdats.begin()->first + "'" ); |
| 416 | |
| 417 | if (AllowIncompleteIR && !ForwardRefMDNodes.empty()) |
| 418 | dropUnknownMetadataReferences(); |
| 419 | |
| 420 | if (!ForwardRefMDNodes.empty()) |
| 421 | return error(L: ForwardRefMDNodes.begin()->second.second, |
| 422 | Msg: "use of undefined metadata '!" + |
| 423 | Twine(ForwardRefMDNodes.begin()->first) + "'" ); |
| 424 | |
| 425 | // Set debug locations. |
| 426 | for (auto [Loc, DR, MD] : PendingDbgRecords) { |
| 427 | if (auto *DI = dyn_cast<DILocation>(Val&: MD)) |
| 428 | DR->setDebugLoc(DebugLoc(DI)); |
| 429 | else |
| 430 | return error(L: Loc, Msg: "invalid debug location" ); |
| 431 | } |
| 432 | PendingDbgRecords.clear(); |
| 433 | for (auto [Loc, I, MD] : PendingDbgInsts) { |
| 434 | if (auto *DI = dyn_cast<DILocation>(Val&: MD)) |
| 435 | I->setDebugLoc(DebugLoc(DI)); |
| 436 | else |
| 437 | return error(L: Loc, Msg: "invalid !dbg metadata" ); |
| 438 | } |
| 439 | PendingDbgInsts.clear(); |
| 440 | |
| 441 | for (const auto &[Name, Info] : make_early_inc_range(Range&: ForwardRefVals)) { |
| 442 | if (StringRef(Name).starts_with(Prefix: "llvm." )) { |
| 443 | Intrinsic::ID IID = Intrinsic::lookupIntrinsicID(Name); |
| 444 | // Automatically create declarations for intrinsics. Intrinsics can only |
| 445 | // be called directly, so the call function type directly determines the |
| 446 | // declaration function type. |
| 447 | // |
| 448 | // Additionally, automatically add the required mangling suffix to the |
| 449 | // intrinsic name. This means that we may replace a single forward |
| 450 | // declaration with multiple functions here. |
| 451 | for (Use &U : make_early_inc_range(Range: Info.first->uses())) { |
| 452 | auto *CB = dyn_cast<CallBase>(Val: U.getUser()); |
| 453 | if (!CB || !CB->isCallee(U: &U)) |
| 454 | return error(L: Info.second, Msg: "intrinsic can only be used as callee" ); |
| 455 | |
| 456 | std::string ErrorMsg; |
| 457 | raw_string_ostream ErrorOS(ErrorMsg); |
| 458 | |
| 459 | SmallVector<Type *> OverloadTys; |
| 460 | if (IID != Intrinsic::not_intrinsic && |
| 461 | Intrinsic::isSignatureValid(ID: IID, FT: CB->getFunctionType(), OverloadTys, |
| 462 | OS&: ErrorOS)) { |
| 463 | U.set(Intrinsic::getOrInsertDeclaration(M, id: IID, OverloadTys)); |
| 464 | } else { |
| 465 | // Try to upgrade the intrinsic. |
| 466 | Function *TmpF = Function::Create(Ty: CB->getFunctionType(), |
| 467 | Linkage: Function::ExternalLinkage, N: Name, M); |
| 468 | Function *NewF = nullptr; |
| 469 | if (!UpgradeIntrinsicFunction(F: TmpF, NewFn&: NewF)) { |
| 470 | if (IID == Intrinsic::not_intrinsic) |
| 471 | return error(L: Info.second, Msg: "unknown intrinsic '" + Name + "'" ); |
| 472 | return error(L: Info.second, Msg: ErrorMsg); |
| 473 | } |
| 474 | |
| 475 | U.set(TmpF); |
| 476 | UpgradeIntrinsicCall(CB, NewFn: NewF); |
| 477 | if (TmpF->use_empty()) |
| 478 | TmpF->eraseFromParent(); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | Info.first->eraseFromParent(); |
| 483 | ForwardRefVals.erase(x: Name); |
| 484 | continue; |
| 485 | } |
| 486 | |
| 487 | // If incomplete IR is allowed, also add declarations for |
| 488 | // non-intrinsics. |
| 489 | if (!AllowIncompleteIR) |
| 490 | continue; |
| 491 | |
| 492 | auto GetCommonFunctionType = [](Value *V) -> FunctionType * { |
| 493 | FunctionType *FTy = nullptr; |
| 494 | for (Use &U : V->uses()) { |
| 495 | auto *CB = dyn_cast<CallBase>(Val: U.getUser()); |
| 496 | if (!CB || !CB->isCallee(U: &U) || (FTy && FTy != CB->getFunctionType())) |
| 497 | return nullptr; |
| 498 | FTy = CB->getFunctionType(); |
| 499 | } |
| 500 | return FTy; |
| 501 | }; |
| 502 | |
| 503 | // First check whether this global is only used in calls with the same |
| 504 | // type, in which case we'll insert a function. Otherwise, fall back to |
| 505 | // using a dummy i8 type. |
| 506 | Type *Ty = GetCommonFunctionType(Info.first); |
| 507 | if (!Ty) |
| 508 | Ty = Type::getInt8Ty(C&: Context); |
| 509 | |
| 510 | GlobalValue *GV; |
| 511 | if (auto *FTy = dyn_cast<FunctionType>(Val: Ty)) |
| 512 | GV = Function::Create(Ty: FTy, Linkage: GlobalValue::ExternalLinkage, N: Name, M); |
| 513 | else |
| 514 | GV = new GlobalVariable(*M, Ty, /*isConstant*/ false, |
| 515 | GlobalValue::ExternalLinkage, |
| 516 | /*Initializer*/ nullptr, Name); |
| 517 | Info.first->replaceAllUsesWith(V: GV); |
| 518 | Info.first->eraseFromParent(); |
| 519 | ForwardRefVals.erase(x: Name); |
| 520 | } |
| 521 | |
| 522 | if (!ForwardRefVals.empty()) |
| 523 | return error(L: ForwardRefVals.begin()->second.second, |
| 524 | Msg: "use of undefined value '@" + ForwardRefVals.begin()->first + |
| 525 | "'" ); |
| 526 | |
| 527 | if (!ForwardRefValIDs.empty()) |
| 528 | return error(L: ForwardRefValIDs.begin()->second.second, |
| 529 | Msg: "use of undefined value '@" + |
| 530 | Twine(ForwardRefValIDs.begin()->first) + "'" ); |
| 531 | |
| 532 | // Resolve metadata cycles. |
| 533 | for (auto &N : NumberedMetadata) { |
| 534 | if (N.second && !N.second->isResolved()) |
| 535 | N.second->resolveCycles(); |
| 536 | } |
| 537 | |
| 538 | DISubprogram::cleanupRetainedNodes(NewDistinctSPs); |
| 539 | NewDistinctSPs.clear(); |
| 540 | |
| 541 | // Look for intrinsic functions and CallInst that need to be upgraded. We use |
| 542 | // make_early_inc_range here because we may remove some functions. |
| 543 | for (Function &F : llvm::make_early_inc_range(Range&: *M)) |
| 544 | UpgradeCallsToIntrinsic(F: &F); |
| 545 | |
| 546 | if (UpgradeDebugInfo) |
| 547 | llvm::UpgradeDebugInfo(M&: *M); |
| 548 | |
| 549 | UpgradeModuleFlags(M&: *M); |
| 550 | UpgradeCFIFunctionsMetadata(M&: *M); |
| 551 | UpgradeNVVMAnnotations(M&: *M); |
| 552 | UpgradeSectionAttributes(M&: *M); |
| 553 | copyModuleAttrToFunctions(M&: *M); |
| 554 | |
| 555 | if (!Slots) |
| 556 | return false; |
| 557 | // Initialize the slot mapping. |
| 558 | // Because by this point we've parsed and validated everything, we can "steal" |
| 559 | // the mapping from LLParser as it doesn't need it anymore. |
| 560 | Slots->GlobalValues = std::move(NumberedVals); |
| 561 | Slots->MetadataNodes = std::move(NumberedMetadata); |
| 562 | for (const auto &I : NamedTypes) |
| 563 | Slots->NamedTypes.insert(KV: std::make_pair(x: I.getKey(), y: I.second.first)); |
| 564 | for (const auto &I : NumberedTypes) |
| 565 | Slots->Types.insert(x: std::make_pair(x: I.first, y: I.second.first)); |
| 566 | |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | /// Do final validity and basic correctness checks at the end of the index. |
| 571 | bool LLParser::validateEndOfIndex() { |
| 572 | if (!Index) |
| 573 | return false; |
| 574 | |
| 575 | if (!ForwardRefValueInfos.empty()) |
| 576 | return error(L: ForwardRefValueInfos.begin()->second.front().second, |
| 577 | Msg: "use of undefined summary '^" + |
| 578 | Twine(ForwardRefValueInfos.begin()->first) + "'" ); |
| 579 | |
| 580 | if (!ForwardRefAliasees.empty()) |
| 581 | return error(L: ForwardRefAliasees.begin()->second.front().second, |
| 582 | Msg: "use of undefined summary '^" + |
| 583 | Twine(ForwardRefAliasees.begin()->first) + "'" ); |
| 584 | |
| 585 | if (!ForwardRefTypeIds.empty()) |
| 586 | return error(L: ForwardRefTypeIds.begin()->second.front().second, |
| 587 | Msg: "use of undefined type id summary '^" + |
| 588 | Twine(ForwardRefTypeIds.begin()->first) + "'" ); |
| 589 | |
| 590 | return false; |
| 591 | } |
| 592 | |
| 593 | //===----------------------------------------------------------------------===// |
| 594 | // Top-Level Entities |
| 595 | //===----------------------------------------------------------------------===// |
| 596 | |
| 597 | bool LLParser::parseTargetDefinitions(DataLayoutCallbackTy DataLayoutCallback) { |
| 598 | // Delay parsing of the data layout string until the target triple is known. |
| 599 | // Then, pass both the the target triple and the tentative data layout string |
| 600 | // to DataLayoutCallback, allowing to override the DL string. |
| 601 | // This enables importing modules with invalid DL strings. |
| 602 | std::string TentativeDLStr = M->getDataLayoutStr(); |
| 603 | LocTy DLStrLoc; |
| 604 | |
| 605 | bool Done = false; |
| 606 | while (!Done) { |
| 607 | switch (Lex.getKind()) { |
| 608 | case lltok::kw_target: |
| 609 | if (parseTargetDefinition(TentativeDLStr, DLStrLoc)) |
| 610 | return true; |
| 611 | break; |
| 612 | case lltok::kw_source_filename: |
| 613 | if (parseSourceFileName()) |
| 614 | return true; |
| 615 | break; |
| 616 | default: |
| 617 | Done = true; |
| 618 | } |
| 619 | } |
| 620 | // Run the override callback to potentially change the data layout string, and |
| 621 | // parse the data layout string. |
| 622 | if (auto LayoutOverride = |
| 623 | DataLayoutCallback(M->getTargetTriple().str(), TentativeDLStr)) { |
| 624 | TentativeDLStr = *LayoutOverride; |
| 625 | DLStrLoc = {}; |
| 626 | } |
| 627 | Expected<DataLayout> MaybeDL = DataLayout::parse(LayoutString: TentativeDLStr); |
| 628 | if (!MaybeDL) |
| 629 | return error(L: DLStrLoc, Msg: toString(E: MaybeDL.takeError())); |
| 630 | M->setDataLayout(MaybeDL.get()); |
| 631 | return false; |
| 632 | } |
| 633 | |
| 634 | bool LLParser::parseTopLevelEntities() { |
| 635 | // If there is no Module, then parse just the summary index entries. |
| 636 | if (!M) { |
| 637 | while (true) { |
| 638 | switch (Lex.getKind()) { |
| 639 | case lltok::Eof: |
| 640 | return false; |
| 641 | case lltok::SummaryID: |
| 642 | if (parseSummaryEntry()) |
| 643 | return true; |
| 644 | break; |
| 645 | case lltok::kw_source_filename: |
| 646 | if (parseSourceFileName()) |
| 647 | return true; |
| 648 | break; |
| 649 | default: |
| 650 | // Skip everything else |
| 651 | Lex.Lex(); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | while (true) { |
| 656 | switch (Lex.getKind()) { |
| 657 | default: |
| 658 | return tokError(Msg: "expected top-level entity" ); |
| 659 | case lltok::Eof: return false; |
| 660 | case lltok::kw_declare: |
| 661 | if (parseDeclare()) |
| 662 | return true; |
| 663 | break; |
| 664 | case lltok::kw_define: |
| 665 | if (parseDefine()) |
| 666 | return true; |
| 667 | break; |
| 668 | case lltok::kw_module: |
| 669 | if (parseModuleAsm()) |
| 670 | return true; |
| 671 | break; |
| 672 | case lltok::LocalVarID: |
| 673 | if (parseUnnamedType()) |
| 674 | return true; |
| 675 | break; |
| 676 | case lltok::LocalVar: |
| 677 | if (parseNamedType()) |
| 678 | return true; |
| 679 | break; |
| 680 | case lltok::GlobalID: |
| 681 | if (parseUnnamedGlobal()) |
| 682 | return true; |
| 683 | break; |
| 684 | case lltok::GlobalVar: |
| 685 | if (parseNamedGlobal()) |
| 686 | return true; |
| 687 | break; |
| 688 | case lltok::ComdatVar: if (parseComdat()) return true; break; |
| 689 | case lltok::exclaim: |
| 690 | if (parseStandaloneMetadata()) |
| 691 | return true; |
| 692 | break; |
| 693 | case lltok::SummaryID: |
| 694 | if (parseSummaryEntry()) |
| 695 | return true; |
| 696 | break; |
| 697 | case lltok::MetadataVar: |
| 698 | if (parseNamedMetadata()) |
| 699 | return true; |
| 700 | break; |
| 701 | case lltok::kw_attributes: |
| 702 | if (parseUnnamedAttrGrp()) |
| 703 | return true; |
| 704 | break; |
| 705 | case lltok::kw_uselistorder: |
| 706 | if (parseUseListOrder()) |
| 707 | return true; |
| 708 | break; |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /// toplevelentity |
| 714 | /// ::= 'module' 'asm' STRINGCONSTANT |
| 715 | /// ::= 'module' 'asm' '(' 'property_name1:' STRINGCONSTANT ',' |
| 716 | /// 'property_name2:' STRINGCONSTANT ')' |
| 717 | /// STRINGCONSTANT |
| 718 | bool LLParser::parseModuleAsm() { |
| 719 | assert(Lex.getKind() == lltok::kw_module); |
| 720 | Lex.Lex(); |
| 721 | |
| 722 | std::string AsmStr; |
| 723 | if (parseToken(T: lltok::kw_asm, ErrMsg: "expected 'module asm'" )) |
| 724 | return true; |
| 725 | |
| 726 | Module::GlobalAsmProperties Props; |
| 727 | if (EatIfPresent(T: lltok::lparen)) { |
| 728 | while (true) { |
| 729 | std::string Key, Value; |
| 730 | SMLoc Loc = Lex.getLoc(); |
| 731 | if (Lex.getKind() != lltok::LabelStr) |
| 732 | return error(L: Loc, Msg: "expected property name followed by ':'" ); |
| 733 | |
| 734 | Key = Lex.getStrVal(); |
| 735 | Lex.Lex(); |
| 736 | |
| 737 | if (parseStringConstant(Result&: Value)) |
| 738 | return true; |
| 739 | |
| 740 | if (!Props.set(Name: Key, Value)) |
| 741 | return error(L: Loc, Msg: "unknown property name" ); |
| 742 | |
| 743 | if (EatIfPresent(T: lltok::rparen)) |
| 744 | break; |
| 745 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' or ')'" )) |
| 746 | return true; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | do { |
| 751 | std::string AsmStrPart; |
| 752 | if (parseStringConstant(Result&: AsmStrPart)) |
| 753 | return true; |
| 754 | AsmStr += AsmStrPart + "\n" ; |
| 755 | } while (Lex.getKind() == lltok::StringConstant); |
| 756 | |
| 757 | M->appendModuleInlineAsm(Fragment: {AsmStr, Props}); |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | /// toplevelentity |
| 762 | /// ::= 'target' 'triple' '=' STRINGCONSTANT |
| 763 | /// ::= 'target' 'datalayout' '=' STRINGCONSTANT |
| 764 | bool LLParser::parseTargetDefinition(std::string &TentativeDLStr, |
| 765 | LocTy &DLStrLoc) { |
| 766 | assert(Lex.getKind() == lltok::kw_target); |
| 767 | std::string Str; |
| 768 | switch (Lex.Lex()) { |
| 769 | default: |
| 770 | return tokError(Msg: "unknown target property" ); |
| 771 | case lltok::kw_triple: |
| 772 | Lex.Lex(); |
| 773 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after target triple" ) || |
| 774 | parseStringConstant(Result&: Str)) |
| 775 | return true; |
| 776 | M->setTargetTriple(Triple(std::move(Str))); |
| 777 | return false; |
| 778 | case lltok::kw_datalayout: |
| 779 | Lex.Lex(); |
| 780 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after target datalayout" )) |
| 781 | return true; |
| 782 | DLStrLoc = Lex.getLoc(); |
| 783 | if (parseStringConstant(Result&: TentativeDLStr)) |
| 784 | return true; |
| 785 | return false; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | /// toplevelentity |
| 790 | /// ::= 'source_filename' '=' STRINGCONSTANT |
| 791 | bool LLParser::parseSourceFileName() { |
| 792 | assert(Lex.getKind() == lltok::kw_source_filename); |
| 793 | Lex.Lex(); |
| 794 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after source_filename" ) || |
| 795 | parseStringConstant(Result&: SourceFileName)) |
| 796 | return true; |
| 797 | if (M) |
| 798 | M->setSourceFileName(SourceFileName); |
| 799 | return false; |
| 800 | } |
| 801 | |
| 802 | /// parseUnnamedType: |
| 803 | /// ::= LocalVarID '=' 'type' type |
| 804 | bool LLParser::parseUnnamedType() { |
| 805 | LocTy TypeLoc = Lex.getLoc(); |
| 806 | unsigned TypeID = Lex.getUIntVal(); |
| 807 | Lex.Lex(); // eat LocalVarID; |
| 808 | |
| 809 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name" ) || |
| 810 | parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' after '='" )) |
| 811 | return true; |
| 812 | |
| 813 | Type *Result = nullptr; |
| 814 | if (parseStructDefinition(TypeLoc, Name: "" , Entry&: NumberedTypes[TypeID], ResultTy&: Result)) |
| 815 | return true; |
| 816 | |
| 817 | if (!isa<StructType>(Val: Result)) { |
| 818 | std::pair<Type*, LocTy> &Entry = NumberedTypes[TypeID]; |
| 819 | if (Entry.first) |
| 820 | return error(L: TypeLoc, Msg: "non-struct types may not be recursive" ); |
| 821 | Entry.first = Result; |
| 822 | Entry.second = SMLoc(); |
| 823 | } |
| 824 | |
| 825 | return false; |
| 826 | } |
| 827 | |
| 828 | /// toplevelentity |
| 829 | /// ::= LocalVar '=' 'type' type |
| 830 | bool LLParser::parseNamedType() { |
| 831 | std::string Name = Lex.getStrVal(); |
| 832 | LocTy NameLoc = Lex.getLoc(); |
| 833 | Lex.Lex(); // eat LocalVar. |
| 834 | |
| 835 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name" ) || |
| 836 | parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' after name" )) |
| 837 | return true; |
| 838 | |
| 839 | Type *Result = nullptr; |
| 840 | if (parseStructDefinition(TypeLoc: NameLoc, Name, Entry&: NamedTypes[Name], ResultTy&: Result)) |
| 841 | return true; |
| 842 | |
| 843 | if (!isa<StructType>(Val: Result)) { |
| 844 | std::pair<Type*, LocTy> &Entry = NamedTypes[Name]; |
| 845 | if (Entry.first) |
| 846 | return error(L: NameLoc, Msg: "non-struct types may not be recursive" ); |
| 847 | Entry.first = Result; |
| 848 | Entry.second = SMLoc(); |
| 849 | } |
| 850 | |
| 851 | return false; |
| 852 | } |
| 853 | |
| 854 | /// toplevelentity |
| 855 | /// ::= 'declare' FunctionHeader |
| 856 | bool LLParser::parseDeclare() { |
| 857 | assert(Lex.getKind() == lltok::kw_declare); |
| 858 | Lex.Lex(); |
| 859 | |
| 860 | std::vector<std::pair<unsigned, MDNode *>> MDs; |
| 861 | while (Lex.getKind() == lltok::MetadataVar) { |
| 862 | unsigned MDK; |
| 863 | MDNode *N; |
| 864 | if (parseMetadataAttachment(Kind&: MDK, MD&: N)) |
| 865 | return true; |
| 866 | MDs.push_back(x: {MDK, N}); |
| 867 | } |
| 868 | |
| 869 | Function *F; |
| 870 | unsigned FunctionNumber = -1; |
| 871 | SmallVector<unsigned> UnnamedArgNums; |
| 872 | if (parseFunctionHeader(Fn&: F, IsDefine: false, FunctionNumber, UnnamedArgNums)) |
| 873 | return true; |
| 874 | for (auto &MD : MDs) |
| 875 | F->addMetadata(KindID: MD.first, MD&: *MD.second); |
| 876 | return false; |
| 877 | } |
| 878 | |
| 879 | /// toplevelentity |
| 880 | /// ::= 'define' FunctionHeader (!dbg !56)* '{' ... |
| 881 | bool LLParser::parseDefine() { |
| 882 | assert(Lex.getKind() == lltok::kw_define); |
| 883 | |
| 884 | FileLoc FunctionStart = getTokLineColumnPos(); |
| 885 | Lex.Lex(); |
| 886 | |
| 887 | Function *F; |
| 888 | unsigned FunctionNumber = -1; |
| 889 | SmallVector<unsigned> UnnamedArgNums; |
| 890 | bool RetValue = |
| 891 | parseFunctionHeader(Fn&: F, IsDefine: true, FunctionNumber, UnnamedArgNums) || |
| 892 | parseOptionalFunctionMetadata(F&: *F) || |
| 893 | parseFunctionBody(Fn&: *F, FunctionNumber, UnnamedArgNums); |
| 894 | if (ParserContext) |
| 895 | ParserContext->addFunctionLocation( |
| 896 | F, FileLocRange(FunctionStart, getPrevTokEndLineColumnPos())); |
| 897 | |
| 898 | return RetValue; |
| 899 | } |
| 900 | |
| 901 | /// parseGlobalType |
| 902 | /// ::= 'constant' |
| 903 | /// ::= 'global' |
| 904 | bool LLParser::parseGlobalType(bool &IsConstant) { |
| 905 | if (Lex.getKind() == lltok::kw_constant) |
| 906 | IsConstant = true; |
| 907 | else if (Lex.getKind() == lltok::kw_global) |
| 908 | IsConstant = false; |
| 909 | else { |
| 910 | IsConstant = false; |
| 911 | return tokError(Msg: "expected 'global' or 'constant'" ); |
| 912 | } |
| 913 | Lex.Lex(); |
| 914 | return false; |
| 915 | } |
| 916 | |
| 917 | bool LLParser::parseOptionalUnnamedAddr( |
| 918 | GlobalVariable::UnnamedAddr &UnnamedAddr) { |
| 919 | if (EatIfPresent(T: lltok::kw_unnamed_addr)) |
| 920 | UnnamedAddr = GlobalValue::UnnamedAddr::Global; |
| 921 | else if (EatIfPresent(T: lltok::kw_local_unnamed_addr)) |
| 922 | UnnamedAddr = GlobalValue::UnnamedAddr::Local; |
| 923 | else |
| 924 | UnnamedAddr = GlobalValue::UnnamedAddr::None; |
| 925 | return false; |
| 926 | } |
| 927 | |
| 928 | /// parseUnnamedGlobal: |
| 929 | /// OptionalVisibility (ALIAS | IFUNC) ... |
| 930 | /// OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility |
| 931 | /// OptionalDLLStorageClass |
| 932 | /// ... -> global variable |
| 933 | /// GlobalID '=' OptionalVisibility (ALIAS | IFUNC) ... |
| 934 | /// GlobalID '=' OptionalLinkage OptionalPreemptionSpecifier |
| 935 | /// OptionalVisibility |
| 936 | /// OptionalDLLStorageClass |
| 937 | /// ... -> global variable |
| 938 | bool LLParser::parseUnnamedGlobal() { |
| 939 | unsigned VarID; |
| 940 | std::string Name; |
| 941 | LocTy NameLoc = Lex.getLoc(); |
| 942 | |
| 943 | // Handle the GlobalID form. |
| 944 | if (Lex.getKind() == lltok::GlobalID) { |
| 945 | VarID = Lex.getUIntVal(); |
| 946 | if (checkValueID(L: NameLoc, Kind: "global" , Prefix: "@" , NextID: NumberedVals.getNext(), ID: VarID)) |
| 947 | return true; |
| 948 | |
| 949 | Lex.Lex(); // eat GlobalID; |
| 950 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after name" )) |
| 951 | return true; |
| 952 | } else { |
| 953 | VarID = NumberedVals.getNext(); |
| 954 | } |
| 955 | |
| 956 | bool HasLinkage; |
| 957 | unsigned Linkage, Visibility, DLLStorageClass; |
| 958 | bool DSOLocal; |
| 959 | GlobalVariable::ThreadLocalMode TLM; |
| 960 | GlobalVariable::UnnamedAddr UnnamedAddr; |
| 961 | if (parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass, |
| 962 | DSOLocal) || |
| 963 | parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr)) |
| 964 | return true; |
| 965 | |
| 966 | switch (Lex.getKind()) { |
| 967 | default: |
| 968 | return parseGlobal(Name, NameID: VarID, NameLoc, Linkage, HasLinkage, Visibility, |
| 969 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
| 970 | case lltok::kw_alias: |
| 971 | case lltok::kw_ifunc: |
| 972 | return parseAliasOrIFunc(Name, NameID: VarID, NameLoc, L: Linkage, Visibility, |
| 973 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /// parseNamedGlobal: |
| 978 | /// GlobalVar '=' OptionalVisibility (ALIAS | IFUNC) ... |
| 979 | /// GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier |
| 980 | /// OptionalVisibility OptionalDLLStorageClass |
| 981 | /// ... -> global variable |
| 982 | bool LLParser::parseNamedGlobal() { |
| 983 | assert(Lex.getKind() == lltok::GlobalVar); |
| 984 | LocTy NameLoc = Lex.getLoc(); |
| 985 | std::string Name = Lex.getStrVal(); |
| 986 | Lex.Lex(); |
| 987 | |
| 988 | bool HasLinkage; |
| 989 | unsigned Linkage, Visibility, DLLStorageClass; |
| 990 | bool DSOLocal; |
| 991 | GlobalVariable::ThreadLocalMode TLM; |
| 992 | GlobalVariable::UnnamedAddr UnnamedAddr; |
| 993 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' in global variable" ) || |
| 994 | parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass, |
| 995 | DSOLocal) || |
| 996 | parseOptionalThreadLocal(TLM) || parseOptionalUnnamedAddr(UnnamedAddr)) |
| 997 | return true; |
| 998 | |
| 999 | switch (Lex.getKind()) { |
| 1000 | default: |
| 1001 | return parseGlobal(Name, NameID: -1, NameLoc, Linkage, HasLinkage, Visibility, |
| 1002 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
| 1003 | case lltok::kw_alias: |
| 1004 | case lltok::kw_ifunc: |
| 1005 | return parseAliasOrIFunc(Name, NameID: -1, NameLoc, L: Linkage, Visibility, |
| 1006 | DLLStorageClass, DSOLocal, TLM, UnnamedAddr); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | bool LLParser::parseComdat() { |
| 1011 | assert(Lex.getKind() == lltok::ComdatVar); |
| 1012 | std::string Name = Lex.getStrVal(); |
| 1013 | LocTy NameLoc = Lex.getLoc(); |
| 1014 | Lex.Lex(); |
| 1015 | |
| 1016 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" )) |
| 1017 | return true; |
| 1018 | |
| 1019 | if (parseToken(T: lltok::kw_comdat, ErrMsg: "expected comdat keyword" )) |
| 1020 | return tokError(Msg: "expected comdat type" ); |
| 1021 | |
| 1022 | Comdat::SelectionKind SK; |
| 1023 | switch (Lex.getKind()) { |
| 1024 | default: |
| 1025 | return tokError(Msg: "unknown selection kind" ); |
| 1026 | case lltok::kw_any: |
| 1027 | SK = Comdat::Any; |
| 1028 | break; |
| 1029 | case lltok::kw_exactmatch: |
| 1030 | SK = Comdat::ExactMatch; |
| 1031 | break; |
| 1032 | case lltok::kw_largest: |
| 1033 | SK = Comdat::Largest; |
| 1034 | break; |
| 1035 | case lltok::kw_nodeduplicate: |
| 1036 | SK = Comdat::NoDeduplicate; |
| 1037 | break; |
| 1038 | case lltok::kw_samesize: |
| 1039 | SK = Comdat::SameSize; |
| 1040 | break; |
| 1041 | } |
| 1042 | Lex.Lex(); |
| 1043 | |
| 1044 | // See if the comdat was forward referenced, if so, use the comdat. |
| 1045 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
| 1046 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Key: Name); |
| 1047 | if (I != ComdatSymTab.end() && !ForwardRefComdats.erase(x: Name)) |
| 1048 | return error(L: NameLoc, Msg: "redefinition of comdat '$" + Name + "'" ); |
| 1049 | |
| 1050 | Comdat *C; |
| 1051 | if (I != ComdatSymTab.end()) |
| 1052 | C = &I->second; |
| 1053 | else |
| 1054 | C = M->getOrInsertComdat(Name); |
| 1055 | C->setSelectionKind(SK); |
| 1056 | |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | // MDString: |
| 1061 | // ::= '!' STRINGCONSTANT |
| 1062 | bool LLParser::parseMDString(MDString *&Result) { |
| 1063 | std::string Str; |
| 1064 | if (parseStringConstant(Result&: Str)) |
| 1065 | return true; |
| 1066 | Result = MDString::get(Context, Str); |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | // MDNode: |
| 1071 | // ::= '!' MDNodeNumber |
| 1072 | bool LLParser::parseMDNodeID(MDNode *&Result) { |
| 1073 | // !{ ..., !42, ... } |
| 1074 | LocTy IDLoc = Lex.getLoc(); |
| 1075 | unsigned MID = 0; |
| 1076 | if (parseUInt32(Val&: MID)) |
| 1077 | return true; |
| 1078 | |
| 1079 | // If not a forward reference, just return it now. |
| 1080 | auto [It, Inserted] = NumberedMetadata.try_emplace(k: MID); |
| 1081 | if (!Inserted) { |
| 1082 | Result = It->second; |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | // Otherwise, create MDNode forward reference. |
| 1087 | auto &FwdRef = ForwardRefMDNodes[MID]; |
| 1088 | FwdRef = std::make_pair(x: MDTuple::getTemporary(Context, MDs: {}), y&: IDLoc); |
| 1089 | |
| 1090 | Result = FwdRef.first.get(); |
| 1091 | It->second.reset(MD: Result); |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | /// parseNamedMetadata: |
| 1096 | /// !foo = !{ !1, !2 } |
| 1097 | bool LLParser::parseNamedMetadata() { |
| 1098 | assert(Lex.getKind() == lltok::MetadataVar); |
| 1099 | std::string Name = Lex.getStrVal(); |
| 1100 | Lex.Lex(); |
| 1101 | |
| 1102 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || |
| 1103 | parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here" ) || |
| 1104 | parseToken(T: lltok::lbrace, ErrMsg: "Expected '{' here" )) |
| 1105 | return true; |
| 1106 | |
| 1107 | NamedMDNode *NMD = M->getOrInsertNamedMetadata(Name); |
| 1108 | if (Lex.getKind() != lltok::rbrace) |
| 1109 | do { |
| 1110 | MDNode *N = nullptr; |
| 1111 | // parse DIExpressions inline as a special case. They are still MDNodes, |
| 1112 | // so they can still appear in named metadata. Remove this logic if they |
| 1113 | // become plain Metadata. |
| 1114 | if (Lex.getKind() == lltok::MetadataVar && |
| 1115 | Lex.getStrVal() == "DIExpression" ) { |
| 1116 | if (parseDIExpression(Result&: N, /*IsDistinct=*/false)) |
| 1117 | return true; |
| 1118 | // DIArgLists should only appear inline in a function, as they may |
| 1119 | // contain LocalAsMetadata arguments which require a function context. |
| 1120 | } else if (Lex.getKind() == lltok::MetadataVar && |
| 1121 | Lex.getStrVal() == "DIArgList" ) { |
| 1122 | return tokError(Msg: "found DIArgList outside of function" ); |
| 1123 | } else if (parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here" ) || |
| 1124 | parseMDNodeID(Result&: N)) { |
| 1125 | return true; |
| 1126 | } |
| 1127 | NMD->addOperand(M: N); |
| 1128 | } while (EatIfPresent(T: lltok::comma)); |
| 1129 | |
| 1130 | return parseToken(T: lltok::rbrace, ErrMsg: "expected end of metadata node" ); |
| 1131 | } |
| 1132 | |
| 1133 | /// parseStandaloneMetadata: |
| 1134 | /// !42 = !{...} |
| 1135 | bool LLParser::parseStandaloneMetadata() { |
| 1136 | assert(Lex.getKind() == lltok::exclaim); |
| 1137 | Lex.Lex(); |
| 1138 | unsigned MetadataID = 0; |
| 1139 | |
| 1140 | MDNode *Init; |
| 1141 | if (parseUInt32(Val&: MetadataID) || parseToken(T: lltok::equal, ErrMsg: "expected '=' here" )) |
| 1142 | return true; |
| 1143 | |
| 1144 | // Detect common error, from old metadata syntax. |
| 1145 | if (Lex.getKind() == lltok::Type) |
| 1146 | return tokError(Msg: "unexpected type in metadata definition" ); |
| 1147 | |
| 1148 | bool IsDistinct = EatIfPresent(T: lltok::kw_distinct); |
| 1149 | if (Lex.getKind() == lltok::MetadataVar) { |
| 1150 | if (parseSpecializedMDNode(N&: Init, IsDistinct)) |
| 1151 | return true; |
| 1152 | } else if (parseToken(T: lltok::exclaim, ErrMsg: "Expected '!' here" ) || |
| 1153 | parseMDTuple(MD&: Init, IsDistinct)) |
| 1154 | return true; |
| 1155 | |
| 1156 | // See if this was forward referenced, if so, handle it. |
| 1157 | auto FI = ForwardRefMDNodes.find(x: MetadataID); |
| 1158 | if (FI != ForwardRefMDNodes.end()) { |
| 1159 | auto *ToReplace = FI->second.first.get(); |
| 1160 | // DIAssignID has its own special forward-reference "replacement" for |
| 1161 | // attachments (the temporary attachments are never actually attached). |
| 1162 | if (isa<DIAssignID>(Val: Init)) { |
| 1163 | for (auto *Inst : TempDIAssignIDAttachments[ToReplace]) { |
| 1164 | assert(!Inst->getMetadata(LLVMContext::MD_DIAssignID) && |
| 1165 | "Inst unexpectedly already has DIAssignID attachment" ); |
| 1166 | Inst->setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: Init); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | ToReplace->replaceAllUsesWith(MD: Init); |
| 1171 | ForwardRefMDNodes.erase(position: FI); |
| 1172 | |
| 1173 | assert(NumberedMetadata[MetadataID] == Init && "Tracking VH didn't work" ); |
| 1174 | } else { |
| 1175 | auto [It, Inserted] = NumberedMetadata.try_emplace(k: MetadataID); |
| 1176 | if (!Inserted) |
| 1177 | return tokError(Msg: "Metadata id is already used" ); |
| 1178 | It->second.reset(MD: Init); |
| 1179 | } |
| 1180 | |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
| 1184 | // Skips a single module summary entry. |
| 1185 | bool LLParser::skipModuleSummaryEntry() { |
| 1186 | // Each module summary entry consists of a tag for the entry |
| 1187 | // type, followed by a colon, then the fields which may be surrounded by |
| 1188 | // nested sets of parentheses. The "tag:" looks like a Label. Once parsing |
| 1189 | // support is in place we will look for the tokens corresponding to the |
| 1190 | // expected tags. |
| 1191 | if (Lex.getKind() != lltok::kw_gv && Lex.getKind() != lltok::kw_module && |
| 1192 | Lex.getKind() != lltok::kw_typeid && |
| 1193 | Lex.getKind() != lltok::kw_typeidCompatibleVTable && |
| 1194 | Lex.getKind() != lltok::kw_flags && Lex.getKind() != lltok::kw_blockcount) |
| 1195 | return tokError(Msg: "Expected 'gv', 'module', 'typeid', " |
| 1196 | "'typeidCompatibleVTable', 'flags' or 'blockcount' at the " |
| 1197 | "start of summary entry" ); |
| 1198 | if (Lex.getKind() == lltok::kw_flags) |
| 1199 | return parseSummaryIndexFlags(); |
| 1200 | if (Lex.getKind() == lltok::kw_blockcount) |
| 1201 | return parseBlockCount(); |
| 1202 | Lex.Lex(); |
| 1203 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' at start of summary entry" ) || |
| 1204 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' at start of summary entry" )) |
| 1205 | return true; |
| 1206 | // Now walk through the parenthesized entry, until the number of open |
| 1207 | // parentheses goes back down to 0 (the first '(' was parsed above). |
| 1208 | unsigned NumOpenParen = 1; |
| 1209 | do { |
| 1210 | switch (Lex.getKind()) { |
| 1211 | case lltok::lparen: |
| 1212 | NumOpenParen++; |
| 1213 | break; |
| 1214 | case lltok::rparen: |
| 1215 | NumOpenParen--; |
| 1216 | break; |
| 1217 | case lltok::Eof: |
| 1218 | return tokError(Msg: "found end of file while parsing summary entry" ); |
| 1219 | default: |
| 1220 | // Skip everything in between parentheses. |
| 1221 | break; |
| 1222 | } |
| 1223 | Lex.Lex(); |
| 1224 | } while (NumOpenParen > 0); |
| 1225 | return false; |
| 1226 | } |
| 1227 | |
| 1228 | /// SummaryEntry |
| 1229 | /// ::= SummaryID '=' GVEntry | ModuleEntry | TypeIdEntry |
| 1230 | bool LLParser::parseSummaryEntry() { |
| 1231 | assert(Lex.getKind() == lltok::SummaryID); |
| 1232 | unsigned SummaryID = Lex.getUIntVal(); |
| 1233 | |
| 1234 | // For summary entries, colons should be treated as distinct tokens, |
| 1235 | // not an indication of the end of a label token. |
| 1236 | Lex.setIgnoreColonInIdentifiers(true); |
| 1237 | |
| 1238 | Lex.Lex(); |
| 1239 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" )) |
| 1240 | return true; |
| 1241 | |
| 1242 | // If we don't have an index object, skip the summary entry. |
| 1243 | if (!Index) |
| 1244 | return skipModuleSummaryEntry(); |
| 1245 | |
| 1246 | bool result = false; |
| 1247 | switch (Lex.getKind()) { |
| 1248 | case lltok::kw_gv: |
| 1249 | result = parseGVEntry(ID: SummaryID); |
| 1250 | break; |
| 1251 | case lltok::kw_module: |
| 1252 | result = parseModuleEntry(ID: SummaryID); |
| 1253 | break; |
| 1254 | case lltok::kw_typeid: |
| 1255 | result = parseTypeIdEntry(ID: SummaryID); |
| 1256 | break; |
| 1257 | case lltok::kw_typeidCompatibleVTable: |
| 1258 | result = parseTypeIdCompatibleVtableEntry(ID: SummaryID); |
| 1259 | break; |
| 1260 | case lltok::kw_flags: |
| 1261 | result = parseSummaryIndexFlags(); |
| 1262 | break; |
| 1263 | case lltok::kw_blockcount: |
| 1264 | result = parseBlockCount(); |
| 1265 | break; |
| 1266 | default: |
| 1267 | result = error(L: Lex.getLoc(), Msg: "unexpected summary kind" ); |
| 1268 | break; |
| 1269 | } |
| 1270 | Lex.setIgnoreColonInIdentifiers(false); |
| 1271 | return result; |
| 1272 | } |
| 1273 | |
| 1274 | static bool isValidVisibilityForLinkage(unsigned V, unsigned L) { |
| 1275 | return !GlobalValue::isLocalLinkage(Linkage: (GlobalValue::LinkageTypes)L) || |
| 1276 | (GlobalValue::VisibilityTypes)V == GlobalValue::DefaultVisibility; |
| 1277 | } |
| 1278 | static bool isValidDLLStorageClassForLinkage(unsigned S, unsigned L) { |
| 1279 | return !GlobalValue::isLocalLinkage(Linkage: (GlobalValue::LinkageTypes)L) || |
| 1280 | (GlobalValue::DLLStorageClassTypes)S == GlobalValue::DefaultStorageClass; |
| 1281 | } |
| 1282 | |
| 1283 | // If there was an explicit dso_local, update GV. In the absence of an explicit |
| 1284 | // dso_local we keep the default value. |
| 1285 | static void maybeSetDSOLocal(bool DSOLocal, GlobalValue &GV) { |
| 1286 | if (DSOLocal) |
| 1287 | GV.setDSOLocal(true); |
| 1288 | } |
| 1289 | |
| 1290 | /// parseAliasOrIFunc: |
| 1291 | /// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier |
| 1292 | /// OptionalVisibility OptionalDLLStorageClass |
| 1293 | /// OptionalThreadLocal OptionalUnnamedAddr |
| 1294 | /// 'alias|ifunc' AliaseeOrResolver SymbolAttrs* |
| 1295 | /// |
| 1296 | /// AliaseeOrResolver |
| 1297 | /// ::= TypeAndValue |
| 1298 | /// |
| 1299 | /// SymbolAttrs |
| 1300 | /// ::= ',' 'partition' StringConstant |
| 1301 | /// |
| 1302 | /// Everything through OptionalUnnamedAddr has already been parsed. |
| 1303 | /// |
| 1304 | bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID, |
| 1305 | LocTy NameLoc, unsigned L, unsigned Visibility, |
| 1306 | unsigned DLLStorageClass, bool DSOLocal, |
| 1307 | GlobalVariable::ThreadLocalMode TLM, |
| 1308 | GlobalVariable::UnnamedAddr UnnamedAddr) { |
| 1309 | bool IsAlias; |
| 1310 | if (Lex.getKind() == lltok::kw_alias) |
| 1311 | IsAlias = true; |
| 1312 | else if (Lex.getKind() == lltok::kw_ifunc) |
| 1313 | IsAlias = false; |
| 1314 | else |
| 1315 | llvm_unreachable("Not an alias or ifunc!" ); |
| 1316 | Lex.Lex(); |
| 1317 | |
| 1318 | GlobalValue::LinkageTypes Linkage = (GlobalValue::LinkageTypes) L; |
| 1319 | |
| 1320 | if(IsAlias && !GlobalAlias::isValidLinkage(L: Linkage)) |
| 1321 | return error(L: NameLoc, Msg: "invalid linkage type for alias" ); |
| 1322 | |
| 1323 | if (!isValidVisibilityForLinkage(V: Visibility, L)) |
| 1324 | return error(L: NameLoc, |
| 1325 | Msg: "symbol with local linkage must have default visibility" ); |
| 1326 | |
| 1327 | if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L)) |
| 1328 | return error(L: NameLoc, |
| 1329 | Msg: "symbol with local linkage cannot have a DLL storage class" ); |
| 1330 | |
| 1331 | Type *Ty; |
| 1332 | LocTy ExplicitTypeLoc = Lex.getLoc(); |
| 1333 | if (parseType(Result&: Ty) || |
| 1334 | parseToken(T: lltok::comma, ErrMsg: "expected comma after alias or ifunc's type" )) |
| 1335 | return true; |
| 1336 | |
| 1337 | Constant *Aliasee; |
| 1338 | LocTy AliaseeLoc = Lex.getLoc(); |
| 1339 | if (Lex.getKind() != lltok::kw_bitcast && |
| 1340 | Lex.getKind() != lltok::kw_getelementptr && |
| 1341 | Lex.getKind() != lltok::kw_addrspacecast && |
| 1342 | Lex.getKind() != lltok::kw_inttoptr) { |
| 1343 | if (parseGlobalTypeAndValue(V&: Aliasee)) |
| 1344 | return true; |
| 1345 | } else { |
| 1346 | // The bitcast dest type is not present, it is implied by the dest type. |
| 1347 | ValID ID; |
| 1348 | if (parseValID(ID, /*PFS=*/nullptr)) |
| 1349 | return true; |
| 1350 | if (ID.Kind != ValID::t_Constant) |
| 1351 | return error(L: AliaseeLoc, Msg: "invalid aliasee" ); |
| 1352 | Aliasee = ID.ConstantVal; |
| 1353 | } |
| 1354 | |
| 1355 | Type *AliaseeType = Aliasee->getType(); |
| 1356 | auto *PTy = dyn_cast<PointerType>(Val: AliaseeType); |
| 1357 | if (!PTy) |
| 1358 | return error(L: AliaseeLoc, Msg: "An alias or ifunc must have pointer type" ); |
| 1359 | unsigned AddrSpace = PTy->getAddressSpace(); |
| 1360 | |
| 1361 | GlobalValue *GVal = nullptr; |
| 1362 | |
| 1363 | // See if the alias was forward referenced, if so, prepare to replace the |
| 1364 | // forward reference. |
| 1365 | if (!Name.empty()) { |
| 1366 | auto I = ForwardRefVals.find(x: Name); |
| 1367 | if (I != ForwardRefVals.end()) { |
| 1368 | GVal = I->second.first; |
| 1369 | ForwardRefVals.erase(x: Name); |
| 1370 | } else if (M->getNamedValue(Name)) { |
| 1371 | return error(L: NameLoc, Msg: "redefinition of global '@" + Name + "'" ); |
| 1372 | } |
| 1373 | } else { |
| 1374 | auto I = ForwardRefValIDs.find(x: NameID); |
| 1375 | if (I != ForwardRefValIDs.end()) { |
| 1376 | GVal = I->second.first; |
| 1377 | ForwardRefValIDs.erase(position: I); |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | // Okay, create the alias/ifunc but do not insert it into the module yet. |
| 1382 | std::unique_ptr<GlobalAlias> GA; |
| 1383 | std::unique_ptr<GlobalIFunc> GI; |
| 1384 | GlobalValue *GV; |
| 1385 | if (IsAlias) { |
| 1386 | GA.reset(p: GlobalAlias::create(Ty, AddressSpace: AddrSpace, Linkage, Name, Aliasee, |
| 1387 | /*Parent=*/nullptr)); |
| 1388 | GV = GA.get(); |
| 1389 | } else { |
| 1390 | GI.reset(p: GlobalIFunc::create(Ty, AddressSpace: AddrSpace, Linkage, Name, Resolver: Aliasee, |
| 1391 | /*Parent=*/nullptr)); |
| 1392 | GV = GI.get(); |
| 1393 | } |
| 1394 | GV->setThreadLocalMode(TLM); |
| 1395 | GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
| 1396 | GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
| 1397 | GV->setUnnamedAddr(UnnamedAddr); |
| 1398 | maybeSetDSOLocal(DSOLocal, GV&: *GV); |
| 1399 | |
| 1400 | // At this point we've parsed everything except for the IndirectSymbolAttrs. |
| 1401 | // Now parse them if there are any. |
| 1402 | while (Lex.getKind() == lltok::comma) { |
| 1403 | Lex.Lex(); |
| 1404 | |
| 1405 | if (Lex.getKind() == lltok::kw_partition) { |
| 1406 | Lex.Lex(); |
| 1407 | GV->setPartition(Lex.getStrVal()); |
| 1408 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected partition string" )) |
| 1409 | return true; |
| 1410 | } else if (!IsAlias && Lex.getKind() == lltok::MetadataVar) { |
| 1411 | if (parseGlobalObjectMetadataAttachment(GO&: *GI)) |
| 1412 | return true; |
| 1413 | } else { |
| 1414 | return tokError(Msg: "unknown alias or ifunc property!" ); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | if (Name.empty()) |
| 1419 | NumberedVals.add(ID: NameID, V: GV); |
| 1420 | |
| 1421 | if (GVal) { |
| 1422 | // Verify that types agree. |
| 1423 | if (GVal->getType() != GV->getType()) |
| 1424 | return error( |
| 1425 | L: ExplicitTypeLoc, |
| 1426 | Msg: "forward reference and definition of alias have different types" ); |
| 1427 | |
| 1428 | // If they agree, just RAUW the old value with the alias and remove the |
| 1429 | // forward ref info. |
| 1430 | GVal->replaceAllUsesWith(V: GV); |
| 1431 | GVal->eraseFromParent(); |
| 1432 | } |
| 1433 | |
| 1434 | // Insert into the module, we know its name won't collide now. |
| 1435 | if (IsAlias) |
| 1436 | M->insertAlias(Alias: GA.release()); |
| 1437 | else |
| 1438 | M->insertIFunc(IFunc: GI.release()); |
| 1439 | assert(GV->getName() == Name && "Should not be a name conflict!" ); |
| 1440 | |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
| 1444 | static bool isSanitizer(lltok::Kind Kind) { |
| 1445 | switch (Kind) { |
| 1446 | case lltok::kw_no_sanitize_address: |
| 1447 | case lltok::kw_no_sanitize_hwaddress: |
| 1448 | case lltok::kw_sanitize_memtag: |
| 1449 | case lltok::kw_sanitize_address_dyninit: |
| 1450 | return true; |
| 1451 | default: |
| 1452 | return false; |
| 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | bool LLParser::parseSanitizer(GlobalVariable *GV) { |
| 1457 | using SanitizerMetadata = GlobalValue::SanitizerMetadata; |
| 1458 | SanitizerMetadata Meta; |
| 1459 | if (GV->hasSanitizerMetadata()) |
| 1460 | Meta = GV->getSanitizerMetadata(); |
| 1461 | |
| 1462 | switch (Lex.getKind()) { |
| 1463 | case lltok::kw_no_sanitize_address: |
| 1464 | Meta.NoAddress = true; |
| 1465 | break; |
| 1466 | case lltok::kw_no_sanitize_hwaddress: |
| 1467 | Meta.NoHWAddress = true; |
| 1468 | break; |
| 1469 | case lltok::kw_sanitize_memtag: |
| 1470 | Meta.Memtag = true; |
| 1471 | break; |
| 1472 | case lltok::kw_sanitize_address_dyninit: |
| 1473 | Meta.IsDynInit = true; |
| 1474 | break; |
| 1475 | default: |
| 1476 | return tokError(Msg: "non-sanitizer token passed to LLParser::parseSanitizer()" ); |
| 1477 | } |
| 1478 | GV->setSanitizerMetadata(Meta); |
| 1479 | Lex.Lex(); |
| 1480 | return false; |
| 1481 | } |
| 1482 | |
| 1483 | /// parseGlobal |
| 1484 | /// ::= GlobalVar '=' OptionalLinkage OptionalPreemptionSpecifier |
| 1485 | /// OptionalVisibility OptionalDLLStorageClass |
| 1486 | /// OptionalThreadLocal OptionalUnnamedAddr OptionalAddrSpace |
| 1487 | /// OptionalExternallyInitialized GlobalType Type Const OptionalAttrs |
| 1488 | /// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility |
| 1489 | /// OptionalDLLStorageClass OptionalThreadLocal OptionalUnnamedAddr |
| 1490 | /// OptionalAddrSpace OptionalExternallyInitialized GlobalType Type |
| 1491 | /// Const OptionalAttrs |
| 1492 | /// |
| 1493 | /// Everything up to and including OptionalUnnamedAddr has been parsed |
| 1494 | /// already. |
| 1495 | /// |
| 1496 | bool LLParser::parseGlobal(const std::string &Name, unsigned NameID, |
| 1497 | LocTy NameLoc, unsigned Linkage, bool HasLinkage, |
| 1498 | unsigned Visibility, unsigned DLLStorageClass, |
| 1499 | bool DSOLocal, GlobalVariable::ThreadLocalMode TLM, |
| 1500 | GlobalVariable::UnnamedAddr UnnamedAddr) { |
| 1501 | if (!isValidVisibilityForLinkage(V: Visibility, L: Linkage)) |
| 1502 | return error(L: NameLoc, |
| 1503 | Msg: "symbol with local linkage must have default visibility" ); |
| 1504 | |
| 1505 | if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L: Linkage)) |
| 1506 | return error(L: NameLoc, |
| 1507 | Msg: "symbol with local linkage cannot have a DLL storage class" ); |
| 1508 | |
| 1509 | unsigned AddrSpace; |
| 1510 | bool IsConstant, IsExternallyInitialized; |
| 1511 | LocTy IsExternallyInitializedLoc; |
| 1512 | LocTy TyLoc; |
| 1513 | |
| 1514 | Type *Ty = nullptr; |
| 1515 | if (parseOptionalAddrSpace(AddrSpace) || |
| 1516 | parseOptionalToken(T: lltok::kw_externally_initialized, |
| 1517 | Present&: IsExternallyInitialized, |
| 1518 | Loc: &IsExternallyInitializedLoc) || |
| 1519 | parseGlobalType(IsConstant) || parseType(Result&: Ty, Loc&: TyLoc)) |
| 1520 | return true; |
| 1521 | |
| 1522 | // If the linkage is specified and is external, then no initializer is |
| 1523 | // present. |
| 1524 | Constant *Init = nullptr; |
| 1525 | if (!HasLinkage || |
| 1526 | !GlobalValue::isValidDeclarationLinkage( |
| 1527 | Linkage: (GlobalValue::LinkageTypes)Linkage)) { |
| 1528 | if (parseGlobalValue(Ty, C&: Init)) |
| 1529 | return true; |
| 1530 | } |
| 1531 | |
| 1532 | if (Ty->isFunctionTy() || !PointerType::isValidElementType(ElemTy: Ty)) |
| 1533 | return error(L: TyLoc, Msg: "invalid type for global variable" ); |
| 1534 | |
| 1535 | GlobalValue *GVal = nullptr; |
| 1536 | |
| 1537 | // See if the global was forward referenced, if so, use the global. |
| 1538 | if (!Name.empty()) { |
| 1539 | auto I = ForwardRefVals.find(x: Name); |
| 1540 | if (I != ForwardRefVals.end()) { |
| 1541 | GVal = I->second.first; |
| 1542 | ForwardRefVals.erase(position: I); |
| 1543 | } else if (M->getNamedValue(Name)) { |
| 1544 | return error(L: NameLoc, Msg: "redefinition of global '@" + Name + "'" ); |
| 1545 | } |
| 1546 | } else { |
| 1547 | // Handle @"", where a name is syntactically specified, but semantically |
| 1548 | // missing. |
| 1549 | if (NameID == (unsigned)-1) |
| 1550 | NameID = NumberedVals.getNext(); |
| 1551 | |
| 1552 | auto I = ForwardRefValIDs.find(x: NameID); |
| 1553 | if (I != ForwardRefValIDs.end()) { |
| 1554 | GVal = I->second.first; |
| 1555 | ForwardRefValIDs.erase(position: I); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | GlobalVariable *GV = new GlobalVariable( |
| 1560 | *M, Ty, false, GlobalValue::ExternalLinkage, nullptr, Name, nullptr, |
| 1561 | GlobalVariable::NotThreadLocal, AddrSpace); |
| 1562 | |
| 1563 | if (Name.empty()) |
| 1564 | NumberedVals.add(ID: NameID, V: GV); |
| 1565 | |
| 1566 | // Set the parsed properties on the global. |
| 1567 | if (Init) |
| 1568 | GV->setInitializer(Init); |
| 1569 | GV->setConstant(IsConstant); |
| 1570 | GV->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 1571 | maybeSetDSOLocal(DSOLocal, GV&: *GV); |
| 1572 | GV->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
| 1573 | GV->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
| 1574 | GV->setExternallyInitialized(IsExternallyInitialized); |
| 1575 | GV->setThreadLocalMode(TLM); |
| 1576 | GV->setUnnamedAddr(UnnamedAddr); |
| 1577 | |
| 1578 | if (GVal) { |
| 1579 | if (GVal->getAddressSpace() != AddrSpace) |
| 1580 | return error( |
| 1581 | L: TyLoc, |
| 1582 | Msg: "forward reference and definition of global have different types" ); |
| 1583 | |
| 1584 | GVal->replaceAllUsesWith(V: GV); |
| 1585 | GVal->eraseFromParent(); |
| 1586 | } |
| 1587 | |
| 1588 | // parse attributes on the global. |
| 1589 | while (Lex.getKind() == lltok::comma) { |
| 1590 | Lex.Lex(); |
| 1591 | |
| 1592 | if (Lex.getKind() == lltok::kw_section) { |
| 1593 | Lex.Lex(); |
| 1594 | GV->setSection(Lex.getStrVal()); |
| 1595 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected global section string" )) |
| 1596 | return true; |
| 1597 | } else if (Lex.getKind() == lltok::kw_partition) { |
| 1598 | Lex.Lex(); |
| 1599 | GV->setPartition(Lex.getStrVal()); |
| 1600 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected partition string" )) |
| 1601 | return true; |
| 1602 | } else if (Lex.getKind() == lltok::kw_align) { |
| 1603 | MaybeAlign Alignment; |
| 1604 | if (parseOptionalAlignment(Alignment)) |
| 1605 | return true; |
| 1606 | if (Alignment) |
| 1607 | GV->setAlignment(*Alignment); |
| 1608 | } else if (Lex.getKind() == lltok::kw_code_model) { |
| 1609 | CodeModel::Model CodeModel; |
| 1610 | if (parseOptionalCodeModel(model&: CodeModel)) |
| 1611 | return true; |
| 1612 | GV->setCodeModel(CodeModel); |
| 1613 | } else if (Lex.getKind() == lltok::MetadataVar) { |
| 1614 | if (parseGlobalObjectMetadataAttachment(GO&: *GV)) |
| 1615 | return true; |
| 1616 | } else if (isSanitizer(Kind: Lex.getKind())) { |
| 1617 | if (parseSanitizer(GV)) |
| 1618 | return true; |
| 1619 | } else { |
| 1620 | Comdat *C; |
| 1621 | if (parseOptionalComdat(GlobalName: Name, C)) |
| 1622 | return true; |
| 1623 | if (C) |
| 1624 | GV->setComdat(C); |
| 1625 | else |
| 1626 | return tokError(Msg: "unknown global variable property!" ); |
| 1627 | } |
| 1628 | } |
| 1629 | |
| 1630 | AttrBuilder Attrs(M->getContext()); |
| 1631 | LocTy BuiltinLoc; |
| 1632 | std::vector<unsigned> FwdRefAttrGrps; |
| 1633 | if (parseFnAttributeValuePairs(B&: Attrs, FwdRefAttrGrps, inAttrGrp: false, BuiltinLoc)) |
| 1634 | return true; |
| 1635 | if (Attrs.hasAttributes() || !FwdRefAttrGrps.empty()) { |
| 1636 | GV->setAttributes(AttributeSet::get(C&: Context, B: Attrs)); |
| 1637 | ForwardRefAttrGroups[GV] = FwdRefAttrGrps; |
| 1638 | } |
| 1639 | |
| 1640 | return false; |
| 1641 | } |
| 1642 | |
| 1643 | /// parseUnnamedAttrGrp |
| 1644 | /// ::= 'attributes' AttrGrpID '=' '{' AttrValPair+ '}' |
| 1645 | bool LLParser::parseUnnamedAttrGrp() { |
| 1646 | assert(Lex.getKind() == lltok::kw_attributes); |
| 1647 | LocTy AttrGrpLoc = Lex.getLoc(); |
| 1648 | Lex.Lex(); |
| 1649 | |
| 1650 | if (Lex.getKind() != lltok::AttrGrpID) |
| 1651 | return tokError(Msg: "expected attribute group id" ); |
| 1652 | |
| 1653 | unsigned VarID = Lex.getUIntVal(); |
| 1654 | std::vector<unsigned> unused; |
| 1655 | LocTy BuiltinLoc; |
| 1656 | Lex.Lex(); |
| 1657 | |
| 1658 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || |
| 1659 | parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here" )) |
| 1660 | return true; |
| 1661 | |
| 1662 | auto R = NumberedAttrBuilders.find(x: VarID); |
| 1663 | if (R == NumberedAttrBuilders.end()) |
| 1664 | R = NumberedAttrBuilders.emplace(args&: VarID, args: AttrBuilder(M->getContext())).first; |
| 1665 | |
| 1666 | if (parseFnAttributeValuePairs(B&: R->second, FwdRefAttrGrps&: unused, inAttrGrp: true, BuiltinLoc) || |
| 1667 | parseToken(T: lltok::rbrace, ErrMsg: "expected end of attribute group" )) |
| 1668 | return true; |
| 1669 | |
| 1670 | if (!R->second.hasAttributes()) |
| 1671 | return error(L: AttrGrpLoc, Msg: "attribute group has no attributes" ); |
| 1672 | |
| 1673 | return false; |
| 1674 | } |
| 1675 | |
| 1676 | static Attribute::AttrKind tokenToAttribute(lltok::Kind Kind) { |
| 1677 | switch (Kind) { |
| 1678 | #define GET_ATTR_NAMES |
| 1679 | #define ATTRIBUTE_ENUM(ENUM_NAME, DISPLAY_NAME) \ |
| 1680 | case lltok::kw_##DISPLAY_NAME: \ |
| 1681 | return Attribute::ENUM_NAME; |
| 1682 | #include "llvm/IR/Attributes.inc" |
| 1683 | default: |
| 1684 | return Attribute::None; |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B, |
| 1689 | bool InAttrGroup) { |
| 1690 | if (Attribute::isTypeAttrKind(Kind: Attr)) |
| 1691 | return parseRequiredTypeAttr(B, AttrToken: Lex.getKind(), AttrKind: Attr); |
| 1692 | |
| 1693 | switch (Attr) { |
| 1694 | case Attribute::Alignment: { |
| 1695 | MaybeAlign Alignment; |
| 1696 | if (InAttrGroup) { |
| 1697 | uint32_t Value = 0; |
| 1698 | Lex.Lex(); |
| 1699 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || parseUInt32(Val&: Value)) |
| 1700 | return true; |
| 1701 | Alignment = Align(Value); |
| 1702 | } else { |
| 1703 | if (parseOptionalAlignment(Alignment, AllowParens: true)) |
| 1704 | return true; |
| 1705 | } |
| 1706 | B.addAlignmentAttr(Align: Alignment); |
| 1707 | return false; |
| 1708 | } |
| 1709 | case Attribute::StackAlignment: { |
| 1710 | unsigned Alignment; |
| 1711 | if (InAttrGroup) { |
| 1712 | Lex.Lex(); |
| 1713 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' here" ) || |
| 1714 | parseUInt32(Val&: Alignment)) |
| 1715 | return true; |
| 1716 | } else { |
| 1717 | if (parseOptionalStackAlignment(Alignment)) |
| 1718 | return true; |
| 1719 | } |
| 1720 | B.addStackAlignmentAttr(Align: Alignment); |
| 1721 | return false; |
| 1722 | } |
| 1723 | case Attribute::AllocSize: { |
| 1724 | unsigned ElemSizeArg; |
| 1725 | std::optional<unsigned> NumElemsArg; |
| 1726 | if (parseAllocSizeArguments(BaseSizeArg&: ElemSizeArg, HowManyArg&: NumElemsArg)) |
| 1727 | return true; |
| 1728 | B.addAllocSizeAttr(ElemSizeArg, NumElemsArg); |
| 1729 | return false; |
| 1730 | } |
| 1731 | case Attribute::VScaleRange: { |
| 1732 | unsigned MinValue, MaxValue; |
| 1733 | if (parseVScaleRangeArguments(MinValue, MaxValue)) |
| 1734 | return true; |
| 1735 | B.addVScaleRangeAttr(MinValue, |
| 1736 | MaxValue: MaxValue > 0 ? MaxValue : std::optional<unsigned>()); |
| 1737 | return false; |
| 1738 | } |
| 1739 | case Attribute::Dereferenceable: { |
| 1740 | std::optional<uint64_t> Bytes; |
| 1741 | if (parseOptionalAttrBytes(AttrKind: lltok::kw_dereferenceable, Bytes)) |
| 1742 | return true; |
| 1743 | assert(Bytes.has_value()); |
| 1744 | B.addDereferenceableAttr(Bytes: Bytes.value()); |
| 1745 | return false; |
| 1746 | } |
| 1747 | case Attribute::DeadOnReturn: { |
| 1748 | std::optional<uint64_t> Bytes; |
| 1749 | if (parseOptionalAttrBytes(AttrKind: lltok::kw_dead_on_return, Bytes, |
| 1750 | /*ErrorNoBytes=*/false)) |
| 1751 | return true; |
| 1752 | if (Bytes.has_value()) { |
| 1753 | B.addDeadOnReturnAttr(Info: DeadOnReturnInfo(Bytes.value())); |
| 1754 | } else { |
| 1755 | B.addDeadOnReturnAttr(Info: DeadOnReturnInfo()); |
| 1756 | } |
| 1757 | return false; |
| 1758 | } |
| 1759 | case Attribute::DereferenceableOrNull: { |
| 1760 | std::optional<uint64_t> Bytes; |
| 1761 | if (parseOptionalAttrBytes(AttrKind: lltok::kw_dereferenceable_or_null, Bytes)) |
| 1762 | return true; |
| 1763 | assert(Bytes.has_value()); |
| 1764 | B.addDereferenceableOrNullAttr(Bytes: Bytes.value()); |
| 1765 | return false; |
| 1766 | } |
| 1767 | case Attribute::UWTable: { |
| 1768 | UWTableKind Kind; |
| 1769 | if (parseOptionalUWTableKind(Kind)) |
| 1770 | return true; |
| 1771 | B.addUWTableAttr(Kind); |
| 1772 | return false; |
| 1773 | } |
| 1774 | case Attribute::AllocKind: { |
| 1775 | AllocFnKind Kind = AllocFnKind::Unknown; |
| 1776 | if (parseAllocKind(Kind)) |
| 1777 | return true; |
| 1778 | B.addAllocKindAttr(Kind); |
| 1779 | return false; |
| 1780 | } |
| 1781 | case Attribute::Memory: { |
| 1782 | std::optional<MemoryEffects> ME = parseMemoryAttr(); |
| 1783 | if (!ME) |
| 1784 | return true; |
| 1785 | B.addMemoryAttr(ME: *ME); |
| 1786 | return false; |
| 1787 | } |
| 1788 | case Attribute::DenormalFPEnv: { |
| 1789 | std::optional<DenormalFPEnv> Mode = parseDenormalFPEnvAttr(); |
| 1790 | if (!Mode) |
| 1791 | return true; |
| 1792 | |
| 1793 | B.addDenormalFPEnvAttr(Mode: *Mode); |
| 1794 | return false; |
| 1795 | } |
| 1796 | case Attribute::NoFPClass: { |
| 1797 | if (FPClassTest NoFPClass = |
| 1798 | static_cast<FPClassTest>(parseNoFPClassAttr())) { |
| 1799 | B.addNoFPClassAttr(NoFPClassMask: NoFPClass); |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
| 1803 | return true; |
| 1804 | } |
| 1805 | case Attribute::Range: |
| 1806 | return parseRangeAttr(B); |
| 1807 | case Attribute::Initializes: |
| 1808 | return parseInitializesAttr(B); |
| 1809 | case Attribute::Captures: |
| 1810 | return parseCapturesAttr(B); |
| 1811 | default: |
| 1812 | B.addAttribute(Val: Attr); |
| 1813 | Lex.Lex(); |
| 1814 | return false; |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | static bool upgradeMemoryAttr(MemoryEffects &ME, lltok::Kind Kind) { |
| 1819 | switch (Kind) { |
| 1820 | case lltok::kw_readnone: |
| 1821 | ME &= MemoryEffects::none(); |
| 1822 | return true; |
| 1823 | case lltok::kw_readonly: |
| 1824 | ME &= MemoryEffects::readOnly(); |
| 1825 | return true; |
| 1826 | case lltok::kw_writeonly: |
| 1827 | ME &= MemoryEffects::writeOnly(); |
| 1828 | return true; |
| 1829 | case lltok::kw_argmemonly: |
| 1830 | ME &= MemoryEffects::argMemOnly(); |
| 1831 | return true; |
| 1832 | case lltok::kw_inaccessiblememonly: |
| 1833 | ME &= MemoryEffects::inaccessibleMemOnly(); |
| 1834 | return true; |
| 1835 | case lltok::kw_inaccessiblemem_or_argmemonly: |
| 1836 | ME &= MemoryEffects::inaccessibleOrArgMemOnly(); |
| 1837 | return true; |
| 1838 | default: |
| 1839 | return false; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | /// parseFnAttributeValuePairs |
| 1844 | /// ::= <attr> | <attr> '=' <value> |
| 1845 | bool LLParser::parseFnAttributeValuePairs(AttrBuilder &B, |
| 1846 | std::vector<unsigned> &FwdRefAttrGrps, |
| 1847 | bool InAttrGrp, LocTy &BuiltinLoc) { |
| 1848 | bool HaveError = false; |
| 1849 | |
| 1850 | B.clear(); |
| 1851 | |
| 1852 | MemoryEffects ME = MemoryEffects::unknown(); |
| 1853 | while (true) { |
| 1854 | lltok::Kind Token = Lex.getKind(); |
| 1855 | if (Token == lltok::rbrace) |
| 1856 | break; // Finished. |
| 1857 | |
| 1858 | if (Token == lltok::StringConstant) { |
| 1859 | if (parseStringAttribute(B)) |
| 1860 | return true; |
| 1861 | continue; |
| 1862 | } |
| 1863 | |
| 1864 | if (Token == lltok::AttrGrpID) { |
| 1865 | // Allow a function to reference an attribute group: |
| 1866 | // |
| 1867 | // define void @foo() #1 { ... } |
| 1868 | if (InAttrGrp) { |
| 1869 | HaveError |= error( |
| 1870 | L: Lex.getLoc(), |
| 1871 | Msg: "cannot have an attribute group reference in an attribute group" ); |
| 1872 | } else { |
| 1873 | // Save the reference to the attribute group. We'll fill it in later. |
| 1874 | FwdRefAttrGrps.push_back(x: Lex.getUIntVal()); |
| 1875 | } |
| 1876 | Lex.Lex(); |
| 1877 | continue; |
| 1878 | } |
| 1879 | |
| 1880 | SMLoc Loc = Lex.getLoc(); |
| 1881 | if (Token == lltok::kw_builtin) |
| 1882 | BuiltinLoc = Loc; |
| 1883 | |
| 1884 | if (upgradeMemoryAttr(ME, Kind: Token)) { |
| 1885 | Lex.Lex(); |
| 1886 | continue; |
| 1887 | } |
| 1888 | |
| 1889 | Attribute::AttrKind Attr = tokenToAttribute(Kind: Token); |
| 1890 | if (Attr == Attribute::None) { |
| 1891 | if (!InAttrGrp) |
| 1892 | break; |
| 1893 | return error(L: Lex.getLoc(), Msg: "unterminated attribute group" ); |
| 1894 | } |
| 1895 | |
| 1896 | if (parseEnumAttribute(Attr, B, InAttrGroup: InAttrGrp)) |
| 1897 | return true; |
| 1898 | |
| 1899 | // As a hack, we allow function alignment to be initially parsed as an |
| 1900 | // attribute on a function declaration/definition or added to an attribute |
| 1901 | // group and later moved to the alignment field. |
| 1902 | if (!Attribute::canUseAsFnAttr(Kind: Attr) && Attr != Attribute::Alignment) |
| 1903 | HaveError |= error(L: Loc, Msg: "this attribute does not apply to functions" ); |
| 1904 | } |
| 1905 | |
| 1906 | if (ME != MemoryEffects::unknown()) |
| 1907 | B.addMemoryAttr(ME); |
| 1908 | return HaveError; |
| 1909 | } |
| 1910 | |
| 1911 | //===----------------------------------------------------------------------===// |
| 1912 | // GlobalValue Reference/Resolution Routines. |
| 1913 | //===----------------------------------------------------------------------===// |
| 1914 | |
| 1915 | static inline GlobalValue *createGlobalFwdRef(Module *M, PointerType *PTy) { |
| 1916 | // The used global type does not matter. We will later RAUW it with a |
| 1917 | // global/function of the correct type. |
| 1918 | return new GlobalVariable(*M, Type::getInt8Ty(C&: M->getContext()), false, |
| 1919 | GlobalValue::ExternalWeakLinkage, nullptr, "" , |
| 1920 | nullptr, GlobalVariable::NotThreadLocal, |
| 1921 | PTy->getAddressSpace()); |
| 1922 | } |
| 1923 | |
| 1924 | Value *LLParser::checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty, |
| 1925 | Value *Val) { |
| 1926 | Type *ValTy = Val->getType(); |
| 1927 | if (ValTy == Ty) |
| 1928 | return Val; |
| 1929 | if (Ty->isLabelTy()) |
| 1930 | error(L: Loc, Msg: "'" + Name + "' is not a basic block" ); |
| 1931 | else |
| 1932 | error(L: Loc, Msg: "'" + Name + "' defined with type '" + |
| 1933 | getTypeString(T: Val->getType()) + "' but expected '" + |
| 1934 | getTypeString(T: Ty) + "'" ); |
| 1935 | return nullptr; |
| 1936 | } |
| 1937 | |
| 1938 | /// getGlobalVal - Get a value with the specified name or ID, creating a |
| 1939 | /// forward reference record if needed. This can return null if the value |
| 1940 | /// exists but does not have the right type. |
| 1941 | GlobalValue *LLParser::getGlobalVal(const std::string &Name, Type *Ty, |
| 1942 | LocTy Loc) { |
| 1943 | PointerType *PTy = dyn_cast<PointerType>(Val: Ty); |
| 1944 | if (!PTy) { |
| 1945 | error(L: Loc, Msg: "global variable reference must have pointer type" ); |
| 1946 | return nullptr; |
| 1947 | } |
| 1948 | |
| 1949 | // Look this name up in the normal function symbol table. |
| 1950 | GlobalValue *Val = |
| 1951 | cast_or_null<GlobalValue>(Val: M->getValueSymbolTable().lookup(Name)); |
| 1952 | |
| 1953 | // If this is a forward reference for the value, see if we already created a |
| 1954 | // forward ref record. |
| 1955 | if (!Val) { |
| 1956 | auto I = ForwardRefVals.find(x: Name); |
| 1957 | if (I != ForwardRefVals.end()) |
| 1958 | Val = I->second.first; |
| 1959 | } |
| 1960 | |
| 1961 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1962 | if (Val) |
| 1963 | return cast_or_null<GlobalValue>( |
| 1964 | Val: checkValidVariableType(Loc, Name: "@" + Name, Ty, Val)); |
| 1965 | |
| 1966 | // Otherwise, create a new forward reference for this value and remember it. |
| 1967 | GlobalValue *FwdVal = createGlobalFwdRef(M, PTy); |
| 1968 | ForwardRefVals[Name] = std::make_pair(x&: FwdVal, y&: Loc); |
| 1969 | return FwdVal; |
| 1970 | } |
| 1971 | |
| 1972 | GlobalValue *LLParser::getGlobalVal(unsigned ID, Type *Ty, LocTy Loc) { |
| 1973 | PointerType *PTy = dyn_cast<PointerType>(Val: Ty); |
| 1974 | if (!PTy) { |
| 1975 | error(L: Loc, Msg: "global variable reference must have pointer type" ); |
| 1976 | return nullptr; |
| 1977 | } |
| 1978 | |
| 1979 | GlobalValue *Val = NumberedVals.get(ID); |
| 1980 | |
| 1981 | // If this is a forward reference for the value, see if we already created a |
| 1982 | // forward ref record. |
| 1983 | if (!Val) { |
| 1984 | auto I = ForwardRefValIDs.find(x: ID); |
| 1985 | if (I != ForwardRefValIDs.end()) |
| 1986 | Val = I->second.first; |
| 1987 | } |
| 1988 | |
| 1989 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 1990 | if (Val) |
| 1991 | return cast_or_null<GlobalValue>( |
| 1992 | Val: checkValidVariableType(Loc, Name: "@" + Twine(ID), Ty, Val)); |
| 1993 | |
| 1994 | // Otherwise, create a new forward reference for this value and remember it. |
| 1995 | GlobalValue *FwdVal = createGlobalFwdRef(M, PTy); |
| 1996 | ForwardRefValIDs[ID] = std::make_pair(x&: FwdVal, y&: Loc); |
| 1997 | return FwdVal; |
| 1998 | } |
| 1999 | |
| 2000 | //===----------------------------------------------------------------------===// |
| 2001 | // Comdat Reference/Resolution Routines. |
| 2002 | //===----------------------------------------------------------------------===// |
| 2003 | |
| 2004 | Comdat *LLParser::getComdat(const std::string &Name, LocTy Loc) { |
| 2005 | // Look this name up in the comdat symbol table. |
| 2006 | Module::ComdatSymTabType &ComdatSymTab = M->getComdatSymbolTable(); |
| 2007 | Module::ComdatSymTabType::iterator I = ComdatSymTab.find(Key: Name); |
| 2008 | if (I != ComdatSymTab.end()) |
| 2009 | return &I->second; |
| 2010 | |
| 2011 | // Otherwise, create a new forward reference for this value and remember it. |
| 2012 | Comdat *C = M->getOrInsertComdat(Name); |
| 2013 | ForwardRefComdats[Name] = Loc; |
| 2014 | return C; |
| 2015 | } |
| 2016 | |
| 2017 | //===----------------------------------------------------------------------===// |
| 2018 | // Helper Routines. |
| 2019 | //===----------------------------------------------------------------------===// |
| 2020 | |
| 2021 | /// parseToken - If the current token has the specified kind, eat it and return |
| 2022 | /// success. Otherwise, emit the specified error and return failure. |
| 2023 | bool LLParser::parseToken(lltok::Kind T, const char *ErrMsg) { |
| 2024 | if (Lex.getKind() != T) |
| 2025 | return tokError(Msg: ErrMsg); |
| 2026 | Lex.Lex(); |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
| 2030 | /// parseStringConstant |
| 2031 | /// ::= StringConstant |
| 2032 | bool LLParser::parseStringConstant(std::string &Result) { |
| 2033 | if (Lex.getKind() != lltok::StringConstant) |
| 2034 | return tokError(Msg: "expected string constant" ); |
| 2035 | Result = Lex.getStrVal(); |
| 2036 | Lex.Lex(); |
| 2037 | return false; |
| 2038 | } |
| 2039 | |
| 2040 | /// parseUInt32 |
| 2041 | /// ::= uint32 |
| 2042 | bool LLParser::parseUInt32(uint32_t &Val) { |
| 2043 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 2044 | return tokError(Msg: "expected integer" ); |
| 2045 | uint64_t Val64 = Lex.getAPSIntVal().getLimitedValue(Limit: 0xFFFFFFFFULL+1); |
| 2046 | if (Val64 != unsigned(Val64)) |
| 2047 | return tokError(Msg: "expected 32-bit integer (too large)" ); |
| 2048 | Val = Val64; |
| 2049 | Lex.Lex(); |
| 2050 | return false; |
| 2051 | } |
| 2052 | |
| 2053 | /// parseUInt64 |
| 2054 | /// ::= uint64 |
| 2055 | bool LLParser::parseUInt64(uint64_t &Val) { |
| 2056 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 2057 | return tokError(Msg: "expected integer" ); |
| 2058 | Val = Lex.getAPSIntVal().getLimitedValue(); |
| 2059 | Lex.Lex(); |
| 2060 | return false; |
| 2061 | } |
| 2062 | |
| 2063 | /// parseTLSModel |
| 2064 | /// := 'localdynamic' |
| 2065 | /// := 'initialexec' |
| 2066 | /// := 'localexec' |
| 2067 | bool LLParser::parseTLSModel(GlobalVariable::ThreadLocalMode &TLM) { |
| 2068 | switch (Lex.getKind()) { |
| 2069 | default: |
| 2070 | return tokError(Msg: "expected localdynamic, initialexec or localexec" ); |
| 2071 | case lltok::kw_localdynamic: |
| 2072 | TLM = GlobalVariable::LocalDynamicTLSModel; |
| 2073 | break; |
| 2074 | case lltok::kw_initialexec: |
| 2075 | TLM = GlobalVariable::InitialExecTLSModel; |
| 2076 | break; |
| 2077 | case lltok::kw_localexec: |
| 2078 | TLM = GlobalVariable::LocalExecTLSModel; |
| 2079 | break; |
| 2080 | } |
| 2081 | |
| 2082 | Lex.Lex(); |
| 2083 | return false; |
| 2084 | } |
| 2085 | |
| 2086 | /// parseOptionalThreadLocal |
| 2087 | /// := /*empty*/ |
| 2088 | /// := 'thread_local' |
| 2089 | /// := 'thread_local' '(' tlsmodel ')' |
| 2090 | bool LLParser::parseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) { |
| 2091 | TLM = GlobalVariable::NotThreadLocal; |
| 2092 | if (!EatIfPresent(T: lltok::kw_thread_local)) |
| 2093 | return false; |
| 2094 | |
| 2095 | TLM = GlobalVariable::GeneralDynamicTLSModel; |
| 2096 | if (Lex.getKind() == lltok::lparen) { |
| 2097 | Lex.Lex(); |
| 2098 | return parseTLSModel(TLM) || |
| 2099 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' after thread local model" ); |
| 2100 | } |
| 2101 | return false; |
| 2102 | } |
| 2103 | |
| 2104 | /// parseOptionalAddrSpace |
| 2105 | /// := /*empty*/ |
| 2106 | /// := 'addrspace' '(' uint32 ')' |
| 2107 | bool LLParser::parseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS) { |
| 2108 | AddrSpace = DefaultAS; |
| 2109 | if (!EatIfPresent(T: lltok::kw_addrspace)) |
| 2110 | return false; |
| 2111 | |
| 2112 | auto ParseAddrspaceValue = [&](unsigned &AddrSpace) -> bool { |
| 2113 | if (Lex.getKind() == lltok::StringConstant) { |
| 2114 | const std::string &AddrSpaceStr = Lex.getStrVal(); |
| 2115 | if (AddrSpaceStr == "A" ) { |
| 2116 | AddrSpace = M->getDataLayout().getAllocaAddrSpace(); |
| 2117 | } else if (AddrSpaceStr == "G" ) { |
| 2118 | AddrSpace = M->getDataLayout().getDefaultGlobalsAddressSpace(); |
| 2119 | } else if (AddrSpaceStr == "P" ) { |
| 2120 | AddrSpace = M->getDataLayout().getProgramAddressSpace(); |
| 2121 | } else if (std::optional<unsigned> AS = |
| 2122 | M->getDataLayout().getNamedAddressSpace(Name: AddrSpaceStr)) { |
| 2123 | AddrSpace = *AS; |
| 2124 | } else { |
| 2125 | return tokError(Msg: "invalid symbolic addrspace '" + AddrSpaceStr + "'" ); |
| 2126 | } |
| 2127 | Lex.Lex(); |
| 2128 | return false; |
| 2129 | } |
| 2130 | if (Lex.getKind() != lltok::APSInt) |
| 2131 | return tokError(Msg: "expected integer or string constant" ); |
| 2132 | SMLoc Loc = Lex.getLoc(); |
| 2133 | if (parseUInt32(Val&: AddrSpace)) |
| 2134 | return true; |
| 2135 | if (!isUInt<24>(x: AddrSpace)) |
| 2136 | return error(L: Loc, Msg: "invalid address space, must be a 24-bit integer" ); |
| 2137 | return false; |
| 2138 | }; |
| 2139 | |
| 2140 | return parseToken(T: lltok::lparen, ErrMsg: "expected '(' in address space" ) || |
| 2141 | ParseAddrspaceValue(AddrSpace) || |
| 2142 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in address space" ); |
| 2143 | } |
| 2144 | |
| 2145 | /// parseStringAttribute |
| 2146 | /// := StringConstant |
| 2147 | /// := StringConstant '=' StringConstant |
| 2148 | bool LLParser::parseStringAttribute(AttrBuilder &B) { |
| 2149 | std::string Attr = Lex.getStrVal(); |
| 2150 | Lex.Lex(); |
| 2151 | std::string Val; |
| 2152 | if (EatIfPresent(T: lltok::equal) && parseStringConstant(Result&: Val)) |
| 2153 | return true; |
| 2154 | B.addAttribute(A: Attr, V: Val); |
| 2155 | return false; |
| 2156 | } |
| 2157 | |
| 2158 | /// Parse a potentially empty list of parameter or return attributes. |
| 2159 | bool LLParser::parseOptionalParamOrReturnAttrs(AttrBuilder &B, bool IsParam) { |
| 2160 | bool HaveError = false; |
| 2161 | |
| 2162 | B.clear(); |
| 2163 | |
| 2164 | while (true) { |
| 2165 | lltok::Kind Token = Lex.getKind(); |
| 2166 | if (Token == lltok::StringConstant) { |
| 2167 | if (parseStringAttribute(B)) |
| 2168 | return true; |
| 2169 | continue; |
| 2170 | } |
| 2171 | |
| 2172 | if (Token == lltok::kw_nocapture) { |
| 2173 | Lex.Lex(); |
| 2174 | B.addCapturesAttr(CI: CaptureInfo::none()); |
| 2175 | continue; |
| 2176 | } |
| 2177 | |
| 2178 | SMLoc Loc = Lex.getLoc(); |
| 2179 | Attribute::AttrKind Attr = tokenToAttribute(Kind: Token); |
| 2180 | if (Attr == Attribute::None) |
| 2181 | return HaveError; |
| 2182 | |
| 2183 | if (parseEnumAttribute(Attr, B, /* InAttrGroup */ false)) |
| 2184 | return true; |
| 2185 | |
| 2186 | if (IsParam && !Attribute::canUseAsParamAttr(Kind: Attr)) |
| 2187 | HaveError |= error(L: Loc, Msg: "this attribute does not apply to parameters" ); |
| 2188 | if (!IsParam && !Attribute::canUseAsRetAttr(Kind: Attr)) |
| 2189 | HaveError |= error(L: Loc, Msg: "this attribute does not apply to return values" ); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | static unsigned parseOptionalLinkageAux(lltok::Kind Kind, bool &HasLinkage) { |
| 2194 | HasLinkage = true; |
| 2195 | switch (Kind) { |
| 2196 | default: |
| 2197 | HasLinkage = false; |
| 2198 | return GlobalValue::ExternalLinkage; |
| 2199 | case lltok::kw_private: |
| 2200 | return GlobalValue::PrivateLinkage; |
| 2201 | case lltok::kw_internal: |
| 2202 | return GlobalValue::InternalLinkage; |
| 2203 | case lltok::kw_weak: |
| 2204 | return GlobalValue::WeakAnyLinkage; |
| 2205 | case lltok::kw_weak_odr: |
| 2206 | return GlobalValue::WeakODRLinkage; |
| 2207 | case lltok::kw_linkonce: |
| 2208 | return GlobalValue::LinkOnceAnyLinkage; |
| 2209 | case lltok::kw_linkonce_odr: |
| 2210 | return GlobalValue::LinkOnceODRLinkage; |
| 2211 | case lltok::kw_available_externally: |
| 2212 | return GlobalValue::AvailableExternallyLinkage; |
| 2213 | case lltok::kw_appending: |
| 2214 | return GlobalValue::AppendingLinkage; |
| 2215 | case lltok::kw_common: |
| 2216 | return GlobalValue::CommonLinkage; |
| 2217 | case lltok::kw_extern_weak: |
| 2218 | return GlobalValue::ExternalWeakLinkage; |
| 2219 | case lltok::kw_external: |
| 2220 | return GlobalValue::ExternalLinkage; |
| 2221 | } |
| 2222 | } |
| 2223 | |
| 2224 | /// parseOptionalLinkage |
| 2225 | /// ::= /*empty*/ |
| 2226 | /// ::= 'private' |
| 2227 | /// ::= 'internal' |
| 2228 | /// ::= 'weak' |
| 2229 | /// ::= 'weak_odr' |
| 2230 | /// ::= 'linkonce' |
| 2231 | /// ::= 'linkonce_odr' |
| 2232 | /// ::= 'available_externally' |
| 2233 | /// ::= 'appending' |
| 2234 | /// ::= 'common' |
| 2235 | /// ::= 'extern_weak' |
| 2236 | /// ::= 'external' |
| 2237 | bool LLParser::parseOptionalLinkage(unsigned &Res, bool &HasLinkage, |
| 2238 | unsigned &Visibility, |
| 2239 | unsigned &DLLStorageClass, bool &DSOLocal) { |
| 2240 | Res = parseOptionalLinkageAux(Kind: Lex.getKind(), HasLinkage); |
| 2241 | if (HasLinkage) |
| 2242 | Lex.Lex(); |
| 2243 | parseOptionalDSOLocal(DSOLocal); |
| 2244 | parseOptionalVisibility(Res&: Visibility); |
| 2245 | parseOptionalDLLStorageClass(Res&: DLLStorageClass); |
| 2246 | |
| 2247 | if (DSOLocal && DLLStorageClass == GlobalValue::DLLImportStorageClass) { |
| 2248 | return error(L: Lex.getLoc(), Msg: "dso_location and DLL-StorageClass mismatch" ); |
| 2249 | } |
| 2250 | |
| 2251 | return false; |
| 2252 | } |
| 2253 | |
| 2254 | void LLParser::parseOptionalDSOLocal(bool &DSOLocal) { |
| 2255 | switch (Lex.getKind()) { |
| 2256 | default: |
| 2257 | DSOLocal = false; |
| 2258 | break; |
| 2259 | case lltok::kw_dso_local: |
| 2260 | DSOLocal = true; |
| 2261 | Lex.Lex(); |
| 2262 | break; |
| 2263 | case lltok::kw_dso_preemptable: |
| 2264 | DSOLocal = false; |
| 2265 | Lex.Lex(); |
| 2266 | break; |
| 2267 | } |
| 2268 | } |
| 2269 | |
| 2270 | /// parseOptionalVisibility |
| 2271 | /// ::= /*empty*/ |
| 2272 | /// ::= 'default' |
| 2273 | /// ::= 'hidden' |
| 2274 | /// ::= 'protected' |
| 2275 | /// |
| 2276 | void LLParser::parseOptionalVisibility(unsigned &Res) { |
| 2277 | switch (Lex.getKind()) { |
| 2278 | default: |
| 2279 | Res = GlobalValue::DefaultVisibility; |
| 2280 | return; |
| 2281 | case lltok::kw_default: |
| 2282 | Res = GlobalValue::DefaultVisibility; |
| 2283 | break; |
| 2284 | case lltok::kw_hidden: |
| 2285 | Res = GlobalValue::HiddenVisibility; |
| 2286 | break; |
| 2287 | case lltok::kw_protected: |
| 2288 | Res = GlobalValue::ProtectedVisibility; |
| 2289 | break; |
| 2290 | } |
| 2291 | Lex.Lex(); |
| 2292 | } |
| 2293 | |
| 2294 | bool LLParser::parseOptionalImportType(lltok::Kind Kind, |
| 2295 | GlobalValueSummary::ImportKind &Res) { |
| 2296 | switch (Kind) { |
| 2297 | default: |
| 2298 | return tokError(Msg: "unknown import kind. Expect definition or declaration." ); |
| 2299 | case lltok::kw_definition: |
| 2300 | Res = GlobalValueSummary::Definition; |
| 2301 | return false; |
| 2302 | case lltok::kw_declaration: |
| 2303 | Res = GlobalValueSummary::Declaration; |
| 2304 | return false; |
| 2305 | } |
| 2306 | } |
| 2307 | |
| 2308 | /// parseOptionalDLLStorageClass |
| 2309 | /// ::= /*empty*/ |
| 2310 | /// ::= 'dllimport' |
| 2311 | /// ::= 'dllexport' |
| 2312 | /// |
| 2313 | void LLParser::parseOptionalDLLStorageClass(unsigned &Res) { |
| 2314 | switch (Lex.getKind()) { |
| 2315 | default: |
| 2316 | Res = GlobalValue::DefaultStorageClass; |
| 2317 | return; |
| 2318 | case lltok::kw_dllimport: |
| 2319 | Res = GlobalValue::DLLImportStorageClass; |
| 2320 | break; |
| 2321 | case lltok::kw_dllexport: |
| 2322 | Res = GlobalValue::DLLExportStorageClass; |
| 2323 | break; |
| 2324 | } |
| 2325 | Lex.Lex(); |
| 2326 | } |
| 2327 | |
| 2328 | /// parseOptionalCallingConv |
| 2329 | /// ::= /*empty*/ |
| 2330 | /// ::= 'ccc' |
| 2331 | /// ::= 'fastcc' |
| 2332 | /// ::= 'intel_ocl_bicc' |
| 2333 | /// ::= 'coldcc' |
| 2334 | /// ::= 'cfguard_checkcc' |
| 2335 | /// ::= 'x86_stdcallcc' |
| 2336 | /// ::= 'x86_fastcallcc' |
| 2337 | /// ::= 'x86_thiscallcc' |
| 2338 | /// ::= 'x86_vectorcallcc' |
| 2339 | /// ::= 'arm_apcscc' |
| 2340 | /// ::= 'arm_aapcscc' |
| 2341 | /// ::= 'arm_aapcs_vfpcc' |
| 2342 | /// ::= 'aarch64_vector_pcs' |
| 2343 | /// ::= 'aarch64_sve_vector_pcs' |
| 2344 | /// ::= 'aarch64_sme_preservemost_from_x0' |
| 2345 | /// ::= 'aarch64_sme_preservemost_from_x1' |
| 2346 | /// ::= 'aarch64_sme_preservemost_from_x2' |
| 2347 | /// ::= 'msp430_intrcc' |
| 2348 | /// ::= 'avr_intrcc' |
| 2349 | /// ::= 'avr_signalcc' |
| 2350 | /// ::= 'ptx_kernel' |
| 2351 | /// ::= 'ptx_device' |
| 2352 | /// ::= 'spir_func' |
| 2353 | /// ::= 'spir_kernel' |
| 2354 | /// ::= 'x86_64_sysvcc' |
| 2355 | /// ::= 'win64cc' |
| 2356 | /// ::= 'anyregcc' |
| 2357 | /// ::= 'preserve_mostcc' |
| 2358 | /// ::= 'preserve_allcc' |
| 2359 | /// ::= 'preserve_nonecc' |
| 2360 | /// ::= 'ghccc' |
| 2361 | /// ::= 'swiftcc' |
| 2362 | /// ::= 'swifttailcc' |
| 2363 | /// ::= 'x86_intrcc' |
| 2364 | /// ::= 'hhvmcc' |
| 2365 | /// ::= 'hhvm_ccc' |
| 2366 | /// ::= 'cxx_fast_tlscc' |
| 2367 | /// ::= 'amdgpu_vs' |
| 2368 | /// ::= 'amdgpu_ls' |
| 2369 | /// ::= 'amdgpu_hs' |
| 2370 | /// ::= 'amdgpu_es' |
| 2371 | /// ::= 'amdgpu_gs' |
| 2372 | /// ::= 'amdgpu_ps' |
| 2373 | /// ::= 'amdgpu_cs' |
| 2374 | /// ::= 'amdgpu_cs_chain' |
| 2375 | /// ::= 'amdgpu_cs_chain_preserve' |
| 2376 | /// ::= 'amdgpu_kernel' |
| 2377 | /// ::= 'tailcc' |
| 2378 | /// ::= 'm68k_rtdcc' |
| 2379 | /// ::= 'graalcc' |
| 2380 | /// ::= 'riscv_vector_cc' |
| 2381 | /// ::= 'riscv_vls_cc' |
| 2382 | /// ::= 'cc' UINT |
| 2383 | /// |
| 2384 | bool LLParser::parseOptionalCallingConv(unsigned &CC) { |
| 2385 | switch (Lex.getKind()) { |
| 2386 | default: CC = CallingConv::C; return false; |
| 2387 | case lltok::kw_ccc: CC = CallingConv::C; break; |
| 2388 | case lltok::kw_fastcc: CC = CallingConv::Fast; break; |
| 2389 | case lltok::kw_coldcc: CC = CallingConv::Cold; break; |
| 2390 | case lltok::kw_cfguard_checkcc: CC = CallingConv::CFGuard_Check; break; |
| 2391 | case lltok::kw_x86_stdcallcc: CC = CallingConv::X86_StdCall; break; |
| 2392 | case lltok::kw_x86_fastcallcc: CC = CallingConv::X86_FastCall; break; |
| 2393 | case lltok::kw_x86_regcallcc: CC = CallingConv::X86_RegCall; break; |
| 2394 | case lltok::kw_x86_thiscallcc: CC = CallingConv::X86_ThisCall; break; |
| 2395 | case lltok::kw_x86_vectorcallcc:CC = CallingConv::X86_VectorCall; break; |
| 2396 | case lltok::kw_arm_apcscc: CC = CallingConv::ARM_APCS; break; |
| 2397 | case lltok::kw_arm_aapcscc: CC = CallingConv::ARM_AAPCS; break; |
| 2398 | case lltok::kw_arm_aapcs_vfpcc:CC = CallingConv::ARM_AAPCS_VFP; break; |
| 2399 | case lltok::kw_aarch64_vector_pcs:CC = CallingConv::AArch64_VectorCall; break; |
| 2400 | case lltok::kw_aarch64_sve_vector_pcs: |
| 2401 | CC = CallingConv::AArch64_SVE_VectorCall; |
| 2402 | break; |
| 2403 | case lltok::kw_aarch64_sme_preservemost_from_x0: |
| 2404 | CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0; |
| 2405 | break; |
| 2406 | case lltok::kw_aarch64_sme_preservemost_from_x1: |
| 2407 | CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1; |
| 2408 | break; |
| 2409 | case lltok::kw_aarch64_sme_preservemost_from_x2: |
| 2410 | CC = CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2; |
| 2411 | break; |
| 2412 | case lltok::kw_msp430_intrcc: CC = CallingConv::MSP430_INTR; break; |
| 2413 | case lltok::kw_avr_intrcc: CC = CallingConv::AVR_INTR; break; |
| 2414 | case lltok::kw_avr_signalcc: CC = CallingConv::AVR_SIGNAL; break; |
| 2415 | case lltok::kw_ptx_kernel: CC = CallingConv::PTX_Kernel; break; |
| 2416 | case lltok::kw_ptx_device: CC = CallingConv::PTX_Device; break; |
| 2417 | case lltok::kw_spir_kernel: CC = CallingConv::SPIR_KERNEL; break; |
| 2418 | case lltok::kw_spir_func: CC = CallingConv::SPIR_FUNC; break; |
| 2419 | case lltok::kw_intel_ocl_bicc: CC = CallingConv::Intel_OCL_BI; break; |
| 2420 | case lltok::kw_x86_64_sysvcc: CC = CallingConv::X86_64_SysV; break; |
| 2421 | case lltok::kw_win64cc: CC = CallingConv::Win64; break; |
| 2422 | case lltok::kw_anyregcc: CC = CallingConv::AnyReg; break; |
| 2423 | case lltok::kw_preserve_mostcc:CC = CallingConv::PreserveMost; break; |
| 2424 | case lltok::kw_preserve_allcc: CC = CallingConv::PreserveAll; break; |
| 2425 | case lltok::kw_preserve_nonecc:CC = CallingConv::PreserveNone; break; |
| 2426 | case lltok::kw_ghccc: CC = CallingConv::GHC; break; |
| 2427 | case lltok::kw_swiftcc: CC = CallingConv::Swift; break; |
| 2428 | case lltok::kw_swifttailcc: CC = CallingConv::SwiftTail; break; |
| 2429 | case lltok::kw_x86_intrcc: CC = CallingConv::X86_INTR; break; |
| 2430 | case lltok::kw_hhvmcc: |
| 2431 | CC = CallingConv::DUMMY_HHVM; |
| 2432 | break; |
| 2433 | case lltok::kw_hhvm_ccc: |
| 2434 | CC = CallingConv::DUMMY_HHVM_C; |
| 2435 | break; |
| 2436 | case lltok::kw_cxx_fast_tlscc: CC = CallingConv::CXX_FAST_TLS; break; |
| 2437 | case lltok::kw_amdgpu_vs: CC = CallingConv::AMDGPU_VS; break; |
| 2438 | case lltok::kw_amdgpu_gfx: CC = CallingConv::AMDGPU_Gfx; break; |
| 2439 | case lltok::kw_amdgpu_ls: CC = CallingConv::AMDGPU_LS; break; |
| 2440 | case lltok::kw_amdgpu_hs: CC = CallingConv::AMDGPU_HS; break; |
| 2441 | case lltok::kw_amdgpu_es: CC = CallingConv::AMDGPU_ES; break; |
| 2442 | case lltok::kw_amdgpu_gs: CC = CallingConv::AMDGPU_GS; break; |
| 2443 | case lltok::kw_amdgpu_ps: CC = CallingConv::AMDGPU_PS; break; |
| 2444 | case lltok::kw_amdgpu_cs: CC = CallingConv::AMDGPU_CS; break; |
| 2445 | case lltok::kw_amdgpu_cs_chain: |
| 2446 | CC = CallingConv::AMDGPU_CS_Chain; |
| 2447 | break; |
| 2448 | case lltok::kw_amdgpu_cs_chain_preserve: |
| 2449 | CC = CallingConv::AMDGPU_CS_ChainPreserve; |
| 2450 | break; |
| 2451 | case lltok::kw_amdgpu_kernel: CC = CallingConv::AMDGPU_KERNEL; break; |
| 2452 | case lltok::kw_amdgpu_gfx_whole_wave: |
| 2453 | CC = CallingConv::AMDGPU_Gfx_WholeWave; |
| 2454 | break; |
| 2455 | case lltok::kw_tailcc: CC = CallingConv::Tail; break; |
| 2456 | case lltok::kw_m68k_rtdcc: CC = CallingConv::M68k_RTD; break; |
| 2457 | case lltok::kw_graalcc: CC = CallingConv::GRAAL; break; |
| 2458 | case lltok::kw_riscv_vector_cc: |
| 2459 | CC = CallingConv::RISCV_VectorCall; |
| 2460 | break; |
| 2461 | case lltok::kw_riscv_vls_cc: |
| 2462 | // Default ABI_VLEN |
| 2463 | CC = CallingConv::RISCV_VLSCall_128; |
| 2464 | Lex.Lex(); |
| 2465 | if (!EatIfPresent(T: lltok::lparen)) |
| 2466 | break; |
| 2467 | uint32_t ABIVlen; |
| 2468 | if (parseUInt32(Val&: ABIVlen) || !EatIfPresent(T: lltok::rparen)) |
| 2469 | return true; |
| 2470 | switch (ABIVlen) { |
| 2471 | default: |
| 2472 | return tokError(Msg: "unknown RISC-V ABI VLEN" ); |
| 2473 | #define CC_VLS_CASE(ABIVlen) \ |
| 2474 | case ABIVlen: \ |
| 2475 | CC = CallingConv::RISCV_VLSCall_##ABIVlen; \ |
| 2476 | break; |
| 2477 | CC_VLS_CASE(32) |
| 2478 | CC_VLS_CASE(64) |
| 2479 | CC_VLS_CASE(128) |
| 2480 | CC_VLS_CASE(256) |
| 2481 | CC_VLS_CASE(512) |
| 2482 | CC_VLS_CASE(1024) |
| 2483 | CC_VLS_CASE(2048) |
| 2484 | CC_VLS_CASE(4096) |
| 2485 | CC_VLS_CASE(8192) |
| 2486 | CC_VLS_CASE(16384) |
| 2487 | CC_VLS_CASE(32768) |
| 2488 | CC_VLS_CASE(65536) |
| 2489 | #undef CC_VLS_CASE |
| 2490 | } |
| 2491 | return false; |
| 2492 | case lltok::kw_cheriot_compartmentcallcc: |
| 2493 | CC = CallingConv::CHERIoT_CompartmentCall; |
| 2494 | break; |
| 2495 | case lltok::kw_cheriot_compartmentcalleecc: |
| 2496 | CC = CallingConv::CHERIoT_CompartmentCallee; |
| 2497 | break; |
| 2498 | case lltok::kw_cheriot_librarycallcc: |
| 2499 | CC = CallingConv::CHERIoT_LibraryCall; |
| 2500 | break; |
| 2501 | case lltok::kw_cc: { |
| 2502 | Lex.Lex(); |
| 2503 | return parseUInt32(Val&: CC); |
| 2504 | } |
| 2505 | } |
| 2506 | |
| 2507 | Lex.Lex(); |
| 2508 | return false; |
| 2509 | } |
| 2510 | |
| 2511 | /// parseMetadataAttachment |
| 2512 | /// ::= !dbg !42 |
| 2513 | bool LLParser::parseMetadataAttachment(unsigned &Kind, MDNode *&MD) { |
| 2514 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata attachment" ); |
| 2515 | |
| 2516 | std::string Name = Lex.getStrVal(); |
| 2517 | Kind = M->getMDKindID(Name); |
| 2518 | Lex.Lex(); |
| 2519 | |
| 2520 | return parseMDNode(N&: MD); |
| 2521 | } |
| 2522 | |
| 2523 | /// parseInstructionMetadata |
| 2524 | /// ::= !dbg !42 (',' !dbg !57)* |
| 2525 | bool LLParser::parseInstructionMetadata(Instruction &Inst) { |
| 2526 | do { |
| 2527 | if (Lex.getKind() != lltok::MetadataVar) |
| 2528 | return tokError(Msg: "expected metadata after comma" ); |
| 2529 | |
| 2530 | unsigned MDK; |
| 2531 | MDNode *N; |
| 2532 | auto Loc = Lex.getLoc(); |
| 2533 | if (parseMetadataAttachment(Kind&: MDK, MD&: N)) |
| 2534 | return true; |
| 2535 | |
| 2536 | if (MDK == LLVMContext::MD_DIAssignID) |
| 2537 | TempDIAssignIDAttachments[N].push_back(Elt: &Inst); |
| 2538 | else if (MDK == LLVMContext::MD_dbg) |
| 2539 | PendingDbgInsts.emplace_back(Args&: Loc, Args: &Inst, Args&: N); |
| 2540 | else |
| 2541 | Inst.setMetadata(KindID: MDK, Node: N); |
| 2542 | |
| 2543 | // If this is the end of the list, we're done. |
| 2544 | } while (EatIfPresent(T: lltok::comma)); |
| 2545 | return false; |
| 2546 | } |
| 2547 | |
| 2548 | /// parseGlobalObjectMetadataAttachment |
| 2549 | /// ::= !dbg !57 |
| 2550 | bool LLParser::parseGlobalObjectMetadataAttachment(GlobalObject &GO) { |
| 2551 | unsigned MDK; |
| 2552 | MDNode *N; |
| 2553 | if (parseMetadataAttachment(Kind&: MDK, MD&: N)) |
| 2554 | return true; |
| 2555 | |
| 2556 | GO.addMetadata(KindID: MDK, MD&: *N); |
| 2557 | return false; |
| 2558 | } |
| 2559 | |
| 2560 | /// parseOptionalFunctionMetadata |
| 2561 | /// ::= (!dbg !57)* |
| 2562 | bool LLParser::parseOptionalFunctionMetadata(Function &F) { |
| 2563 | while (Lex.getKind() == lltok::MetadataVar) |
| 2564 | if (parseGlobalObjectMetadataAttachment(GO&: F)) |
| 2565 | return true; |
| 2566 | return false; |
| 2567 | } |
| 2568 | |
| 2569 | /// parseOptionalAlignment |
| 2570 | /// ::= /* empty */ |
| 2571 | /// ::= 'align' 4 |
| 2572 | bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) { |
| 2573 | Alignment = std::nullopt; |
| 2574 | if (!EatIfPresent(T: lltok::kw_align)) |
| 2575 | return false; |
| 2576 | LocTy AlignLoc = Lex.getLoc(); |
| 2577 | uint64_t Value = 0; |
| 2578 | |
| 2579 | LocTy ParenLoc = Lex.getLoc(); |
| 2580 | bool HaveParens = false; |
| 2581 | if (AllowParens) { |
| 2582 | if (EatIfPresent(T: lltok::lparen)) |
| 2583 | HaveParens = true; |
| 2584 | } |
| 2585 | |
| 2586 | if (parseUInt64(Val&: Value)) |
| 2587 | return true; |
| 2588 | |
| 2589 | if (HaveParens && !EatIfPresent(T: lltok::rparen)) |
| 2590 | return error(L: ParenLoc, Msg: "expected ')'" ); |
| 2591 | |
| 2592 | if (!isPowerOf2_64(Value)) |
| 2593 | return error(L: AlignLoc, Msg: "alignment is not a power of two" ); |
| 2594 | if (Value > Value::MaximumAlignment) |
| 2595 | return error(L: AlignLoc, Msg: "huge alignments are not supported yet" ); |
| 2596 | Alignment = Align(Value); |
| 2597 | return false; |
| 2598 | } |
| 2599 | |
| 2600 | /// parseOptionalPrefAlignment |
| 2601 | /// ::= /* empty */ |
| 2602 | /// ::= 'prefalign' '(' 4 ')' |
| 2603 | bool LLParser::parseOptionalPrefAlignment(MaybeAlign &Alignment) { |
| 2604 | Alignment = std::nullopt; |
| 2605 | if (!EatIfPresent(T: lltok::kw_prefalign)) |
| 2606 | return false; |
| 2607 | LocTy AlignLoc = Lex.getLoc(); |
| 2608 | uint64_t Value = 0; |
| 2609 | |
| 2610 | LocTy ParenLoc = Lex.getLoc(); |
| 2611 | if (!EatIfPresent(T: lltok::lparen)) |
| 2612 | return error(L: ParenLoc, Msg: "expected '('" ); |
| 2613 | |
| 2614 | if (parseUInt64(Val&: Value)) |
| 2615 | return true; |
| 2616 | |
| 2617 | ParenLoc = Lex.getLoc(); |
| 2618 | if (!EatIfPresent(T: lltok::rparen)) |
| 2619 | return error(L: ParenLoc, Msg: "expected ')'" ); |
| 2620 | |
| 2621 | if (!isPowerOf2_64(Value)) |
| 2622 | return error(L: AlignLoc, Msg: "alignment is not a power of two" ); |
| 2623 | if (Value > Value::MaximumAlignment) |
| 2624 | return error(L: AlignLoc, Msg: "huge alignments are not supported yet" ); |
| 2625 | Alignment = Align(Value); |
| 2626 | return false; |
| 2627 | } |
| 2628 | |
| 2629 | /// parseOptionalCodeModel |
| 2630 | /// ::= /* empty */ |
| 2631 | /// ::= 'code_model' "large" |
| 2632 | bool LLParser::parseOptionalCodeModel(CodeModel::Model &model) { |
| 2633 | Lex.Lex(); |
| 2634 | auto StrVal = Lex.getStrVal(); |
| 2635 | auto ErrMsg = "expected global code model string" ; |
| 2636 | if (StrVal == "tiny" ) |
| 2637 | model = CodeModel::Tiny; |
| 2638 | else if (StrVal == "small" ) |
| 2639 | model = CodeModel::Small; |
| 2640 | else if (StrVal == "kernel" ) |
| 2641 | model = CodeModel::Kernel; |
| 2642 | else if (StrVal == "medium" ) |
| 2643 | model = CodeModel::Medium; |
| 2644 | else if (StrVal == "large" ) |
| 2645 | model = CodeModel::Large; |
| 2646 | else |
| 2647 | return tokError(Msg: ErrMsg); |
| 2648 | if (parseToken(T: lltok::StringConstant, ErrMsg)) |
| 2649 | return true; |
| 2650 | return false; |
| 2651 | } |
| 2652 | |
| 2653 | /// parseOptionalAttrBytes |
| 2654 | /// ::= /* empty */ |
| 2655 | /// ::= AttrKind '(' 4 ')' |
| 2656 | /// |
| 2657 | /// where AttrKind is either 'dereferenceable', 'dereferenceable_or_null', or |
| 2658 | /// 'dead_on_return' |
| 2659 | bool LLParser::parseOptionalAttrBytes(lltok::Kind AttrKind, |
| 2660 | std::optional<uint64_t> &Bytes, |
| 2661 | bool ErrorNoBytes) { |
| 2662 | assert((AttrKind == lltok::kw_dereferenceable || |
| 2663 | AttrKind == lltok::kw_dereferenceable_or_null || |
| 2664 | AttrKind == lltok::kw_dead_on_return) && |
| 2665 | "contract!" ); |
| 2666 | |
| 2667 | Bytes = 0; |
| 2668 | if (!EatIfPresent(T: AttrKind)) |
| 2669 | return false; |
| 2670 | LocTy ParenLoc = Lex.getLoc(); |
| 2671 | if (!EatIfPresent(T: lltok::lparen)) { |
| 2672 | if (ErrorNoBytes) |
| 2673 | return error(L: ParenLoc, Msg: "expected '('" ); |
| 2674 | Bytes = std::nullopt; |
| 2675 | return false; |
| 2676 | } |
| 2677 | LocTy DerefLoc = Lex.getLoc(); |
| 2678 | if (parseUInt64(Val&: Bytes.value())) |
| 2679 | return true; |
| 2680 | ParenLoc = Lex.getLoc(); |
| 2681 | if (!EatIfPresent(T: lltok::rparen)) |
| 2682 | return error(L: ParenLoc, Msg: "expected ')'" ); |
| 2683 | if (!Bytes.value()) |
| 2684 | return error(L: DerefLoc, Msg: "byte count specified must be non-zero" ); |
| 2685 | return false; |
| 2686 | } |
| 2687 | |
| 2688 | bool LLParser::parseOptionalUWTableKind(UWTableKind &Kind) { |
| 2689 | Lex.Lex(); |
| 2690 | Kind = UWTableKind::Default; |
| 2691 | if (!EatIfPresent(T: lltok::lparen)) |
| 2692 | return false; |
| 2693 | LocTy KindLoc = Lex.getLoc(); |
| 2694 | if (Lex.getKind() == lltok::kw_sync) |
| 2695 | Kind = UWTableKind::Sync; |
| 2696 | else if (Lex.getKind() == lltok::kw_async) |
| 2697 | Kind = UWTableKind::Async; |
| 2698 | else |
| 2699 | return error(L: KindLoc, Msg: "expected unwind table kind" ); |
| 2700 | Lex.Lex(); |
| 2701 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')'" ); |
| 2702 | } |
| 2703 | |
| 2704 | bool LLParser::parseAllocKind(AllocFnKind &Kind) { |
| 2705 | Lex.Lex(); |
| 2706 | LocTy ParenLoc = Lex.getLoc(); |
| 2707 | if (!EatIfPresent(T: lltok::lparen)) |
| 2708 | return error(L: ParenLoc, Msg: "expected '('" ); |
| 2709 | LocTy KindLoc = Lex.getLoc(); |
| 2710 | std::string Arg; |
| 2711 | if (parseStringConstant(Result&: Arg)) |
| 2712 | return error(L: KindLoc, Msg: "expected allockind value" ); |
| 2713 | for (StringRef A : llvm::split(Str: Arg, Separator: "," )) { |
| 2714 | if (A == "alloc" ) { |
| 2715 | Kind |= AllocFnKind::Alloc; |
| 2716 | } else if (A == "realloc" ) { |
| 2717 | Kind |= AllocFnKind::Realloc; |
| 2718 | } else if (A == "free" ) { |
| 2719 | Kind |= AllocFnKind::Free; |
| 2720 | } else if (A == "uninitialized" ) { |
| 2721 | Kind |= AllocFnKind::Uninitialized; |
| 2722 | } else if (A == "zeroed" ) { |
| 2723 | Kind |= AllocFnKind::Zeroed; |
| 2724 | } else if (A == "aligned" ) { |
| 2725 | Kind |= AllocFnKind::Aligned; |
| 2726 | } else { |
| 2727 | return error(L: KindLoc, Msg: Twine("unknown allockind " ) + A); |
| 2728 | } |
| 2729 | } |
| 2730 | ParenLoc = Lex.getLoc(); |
| 2731 | if (!EatIfPresent(T: lltok::rparen)) |
| 2732 | return error(L: ParenLoc, Msg: "expected ')'" ); |
| 2733 | if (Kind == AllocFnKind::Unknown) |
| 2734 | return error(L: KindLoc, Msg: "expected allockind value" ); |
| 2735 | return false; |
| 2736 | } |
| 2737 | |
| 2738 | static SmallVector<MemoryEffects::Location, 2> keywordToLoc(lltok::Kind Tok) { |
| 2739 | using Loc = IRMemLocation; |
| 2740 | |
| 2741 | switch (Tok) { |
| 2742 | case lltok::kw_argmem: |
| 2743 | return {Loc::ArgMem}; |
| 2744 | case lltok::kw_inaccessiblemem: |
| 2745 | return {Loc::InaccessibleMem}; |
| 2746 | case lltok::kw_errnomem: |
| 2747 | return {Loc::ErrnoMem}; |
| 2748 | case lltok::kw_target_mem0: |
| 2749 | return {Loc::TargetMem0}; |
| 2750 | case lltok::kw_target_mem1: |
| 2751 | return {Loc::TargetMem1}; |
| 2752 | case lltok::kw_target_mem: { |
| 2753 | SmallVector<MemoryEffects::Location, 2> Targets; |
| 2754 | for (auto Loc : MemoryEffects::targetMemLocations()) |
| 2755 | Targets.push_back(Elt: Loc); |
| 2756 | return Targets; |
| 2757 | } |
| 2758 | default: |
| 2759 | return {}; |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | static std::optional<ModRefInfo> keywordToModRef(lltok::Kind Tok) { |
| 2764 | switch (Tok) { |
| 2765 | case lltok::kw_none: |
| 2766 | return ModRefInfo::NoModRef; |
| 2767 | case lltok::kw_read: |
| 2768 | return ModRefInfo::Ref; |
| 2769 | case lltok::kw_write: |
| 2770 | return ModRefInfo::Mod; |
| 2771 | case lltok::kw_readwrite: |
| 2772 | return ModRefInfo::ModRef; |
| 2773 | default: |
| 2774 | return std::nullopt; |
| 2775 | } |
| 2776 | } |
| 2777 | |
| 2778 | static std::optional<DenormalMode::DenormalModeKind> |
| 2779 | keywordToDenormalModeKind(lltok::Kind Tok) { |
| 2780 | switch (Tok) { |
| 2781 | case lltok::kw_ieee: |
| 2782 | return DenormalMode::IEEE; |
| 2783 | case lltok::kw_preservesign: |
| 2784 | return DenormalMode::PreserveSign; |
| 2785 | case lltok::kw_positivezero: |
| 2786 | return DenormalMode::PositiveZero; |
| 2787 | case lltok::kw_dynamic: |
| 2788 | return DenormalMode::Dynamic; |
| 2789 | default: |
| 2790 | return std::nullopt; |
| 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | std::optional<MemoryEffects> LLParser::parseMemoryAttr() { |
| 2795 | MemoryEffects ME = MemoryEffects::none(); |
| 2796 | |
| 2797 | // We use syntax like memory(argmem: read), so the colon should not be |
| 2798 | // interpreted as a label terminator. |
| 2799 | Lex.setIgnoreColonInIdentifiers(true); |
| 2800 | llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); }); |
| 2801 | |
| 2802 | Lex.Lex(); |
| 2803 | if (!EatIfPresent(T: lltok::lparen)) { |
| 2804 | tokError(Msg: "expected '('" ); |
| 2805 | return std::nullopt; |
| 2806 | } |
| 2807 | |
| 2808 | bool SeenLoc = false; |
| 2809 | bool SeenTargetLoc = false; |
| 2810 | do { |
| 2811 | SmallVector<IRMemLocation, 2> Locs = keywordToLoc(Tok: Lex.getKind()); |
| 2812 | if (!Locs.empty()) { |
| 2813 | Lex.Lex(); |
| 2814 | if (!EatIfPresent(T: lltok::colon)) { |
| 2815 | tokError(Msg: "expected ':' after location" ); |
| 2816 | return std::nullopt; |
| 2817 | } |
| 2818 | } |
| 2819 | |
| 2820 | std::optional<ModRefInfo> MR = keywordToModRef(Tok: Lex.getKind()); |
| 2821 | if (!MR) { |
| 2822 | if (Locs.empty()) |
| 2823 | tokError(Msg: "expected memory location (argmem, inaccessiblemem, errnomem) " |
| 2824 | "or access kind (none, read, write, readwrite)" ); |
| 2825 | else |
| 2826 | tokError(Msg: "expected access kind (none, read, write, readwrite)" ); |
| 2827 | return std::nullopt; |
| 2828 | } |
| 2829 | |
| 2830 | Lex.Lex(); |
| 2831 | if (!Locs.empty()) { |
| 2832 | SeenLoc = true; |
| 2833 | for (IRMemLocation Loc : Locs) { |
| 2834 | ME = ME.getWithModRef(Loc, MR: *MR); |
| 2835 | if (ME.isTargetMemLoc(Loc) && Locs.size() == 1) |
| 2836 | SeenTargetLoc = true; |
| 2837 | } |
| 2838 | if (Locs.size() > 1 && SeenTargetLoc) { |
| 2839 | tokError(Msg: "target memory default access kind must be specified first" ); |
| 2840 | return std::nullopt; |
| 2841 | } |
| 2842 | |
| 2843 | } else { |
| 2844 | if (SeenLoc) { |
| 2845 | tokError(Msg: "default access kind must be specified first" ); |
| 2846 | return std::nullopt; |
| 2847 | } |
| 2848 | ME = MemoryEffects(*MR); |
| 2849 | } |
| 2850 | |
| 2851 | if (EatIfPresent(T: lltok::rparen)) |
| 2852 | return ME; |
| 2853 | } while (EatIfPresent(T: lltok::comma)); |
| 2854 | |
| 2855 | tokError(Msg: "unterminated memory attribute" ); |
| 2856 | return std::nullopt; |
| 2857 | } |
| 2858 | |
| 2859 | std::optional<DenormalMode> LLParser::parseDenormalFPEnvEntry() { |
| 2860 | std::optional<DenormalMode::DenormalModeKind> OutputMode = |
| 2861 | keywordToDenormalModeKind(Tok: Lex.getKind()); |
| 2862 | if (!OutputMode) { |
| 2863 | tokError(Msg: "expected denormal behavior kind (ieee, preservesign, " |
| 2864 | "positivezero, dynamic)" ); |
| 2865 | return {}; |
| 2866 | } |
| 2867 | |
| 2868 | Lex.Lex(); |
| 2869 | |
| 2870 | std::optional<DenormalMode::DenormalModeKind> InputMode; |
| 2871 | if (EatIfPresent(T: lltok::bar)) { |
| 2872 | InputMode = keywordToDenormalModeKind(Tok: Lex.getKind()); |
| 2873 | if (!InputMode) { |
| 2874 | tokError(Msg: "expected denormal behavior kind (ieee, preservesign, " |
| 2875 | "positivezero, dynamic)" ); |
| 2876 | return {}; |
| 2877 | } |
| 2878 | |
| 2879 | Lex.Lex(); |
| 2880 | } else { |
| 2881 | // Single item, input == output mode |
| 2882 | InputMode = OutputMode; |
| 2883 | } |
| 2884 | |
| 2885 | return DenormalMode(*OutputMode, *InputMode); |
| 2886 | } |
| 2887 | |
| 2888 | std::optional<DenormalFPEnv> LLParser::parseDenormalFPEnvAttr() { |
| 2889 | // We use syntax like denormal_fpenv(float: preservesign), so the colon should |
| 2890 | // not be interpreted as a label terminator. |
| 2891 | Lex.setIgnoreColonInIdentifiers(true); |
| 2892 | llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); }); |
| 2893 | |
| 2894 | Lex.Lex(); |
| 2895 | |
| 2896 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" )) |
| 2897 | return {}; |
| 2898 | |
| 2899 | DenormalMode DefaultMode = DenormalMode::getIEEE(); |
| 2900 | DenormalMode F32Mode = DenormalMode::getInvalid(); |
| 2901 | |
| 2902 | bool HasDefaultSection = false; |
| 2903 | if (Lex.getKind() != lltok::Type) { |
| 2904 | std::optional<DenormalMode> ParsedDefaultMode = parseDenormalFPEnvEntry(); |
| 2905 | if (!ParsedDefaultMode) |
| 2906 | return {}; |
| 2907 | DefaultMode = *ParsedDefaultMode; |
| 2908 | HasDefaultSection = true; |
| 2909 | } |
| 2910 | |
| 2911 | bool HasComma = EatIfPresent(T: lltok::comma); |
| 2912 | if (Lex.getKind() == lltok::Type) { |
| 2913 | if (HasDefaultSection && !HasComma) { |
| 2914 | tokError(Msg: "expected ',' before float:" ); |
| 2915 | return {}; |
| 2916 | } |
| 2917 | |
| 2918 | Type *Ty = nullptr; |
| 2919 | if (parseType(Result&: Ty) || !Ty->isFloatTy()) { |
| 2920 | tokError(Msg: "expected float:" ); |
| 2921 | return {}; |
| 2922 | } |
| 2923 | |
| 2924 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' before float denormal_fpenv" )) |
| 2925 | return {}; |
| 2926 | |
| 2927 | std::optional<DenormalMode> ParsedF32Mode = parseDenormalFPEnvEntry(); |
| 2928 | if (!ParsedF32Mode) |
| 2929 | return {}; |
| 2930 | |
| 2931 | F32Mode = *ParsedF32Mode; |
| 2932 | } |
| 2933 | |
| 2934 | if (parseToken(T: lltok::rparen, ErrMsg: "unterminated denormal_fpenv" )) |
| 2935 | return {}; |
| 2936 | |
| 2937 | return DenormalFPEnv(DefaultMode, F32Mode); |
| 2938 | } |
| 2939 | |
| 2940 | static unsigned keywordToFPClassTest(lltok::Kind Tok) { |
| 2941 | switch (Tok) { |
| 2942 | case lltok::kw_all: |
| 2943 | return fcAllFlags; |
| 2944 | case lltok::kw_nan: |
| 2945 | return fcNan; |
| 2946 | case lltok::kw_snan: |
| 2947 | return fcSNan; |
| 2948 | case lltok::kw_qnan: |
| 2949 | return fcQNan; |
| 2950 | case lltok::kw_inf: |
| 2951 | return fcInf; |
| 2952 | case lltok::kw_ninf: |
| 2953 | return fcNegInf; |
| 2954 | case lltok::kw_pinf: |
| 2955 | return fcPosInf; |
| 2956 | case lltok::kw_norm: |
| 2957 | return fcNormal; |
| 2958 | case lltok::kw_nnorm: |
| 2959 | return fcNegNormal; |
| 2960 | case lltok::kw_pnorm: |
| 2961 | return fcPosNormal; |
| 2962 | case lltok::kw_sub: |
| 2963 | return fcSubnormal; |
| 2964 | case lltok::kw_nsub: |
| 2965 | return fcNegSubnormal; |
| 2966 | case lltok::kw_psub: |
| 2967 | return fcPosSubnormal; |
| 2968 | case lltok::kw_zero: |
| 2969 | return fcZero; |
| 2970 | case lltok::kw_nzero: |
| 2971 | return fcNegZero; |
| 2972 | case lltok::kw_pzero: |
| 2973 | return fcPosZero; |
| 2974 | default: |
| 2975 | return 0; |
| 2976 | } |
| 2977 | } |
| 2978 | |
| 2979 | unsigned LLParser::parseNoFPClassAttr() { |
| 2980 | unsigned Mask = fcNone; |
| 2981 | |
| 2982 | Lex.Lex(); |
| 2983 | if (!EatIfPresent(T: lltok::lparen)) { |
| 2984 | tokError(Msg: "expected '('" ); |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
| 2988 | do { |
| 2989 | uint64_t Value = 0; |
| 2990 | unsigned TestMask = keywordToFPClassTest(Tok: Lex.getKind()); |
| 2991 | if (TestMask != 0) { |
| 2992 | Mask |= TestMask; |
| 2993 | // TODO: Disallow overlapping masks to avoid copy paste errors |
| 2994 | } else if (Mask == 0 && Lex.getKind() == lltok::APSInt && |
| 2995 | !parseUInt64(Val&: Value)) { |
| 2996 | if (Value == 0 || (Value & ~static_cast<unsigned>(fcAllFlags)) != 0) { |
| 2997 | error(L: Lex.getLoc(), Msg: "invalid mask value for 'nofpclass'" ); |
| 2998 | return 0; |
| 2999 | } |
| 3000 | |
| 3001 | if (!EatIfPresent(T: lltok::rparen)) { |
| 3002 | error(L: Lex.getLoc(), Msg: "expected ')'" ); |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
| 3006 | return Value; |
| 3007 | } else { |
| 3008 | error(L: Lex.getLoc(), Msg: "expected nofpclass test mask" ); |
| 3009 | return 0; |
| 3010 | } |
| 3011 | |
| 3012 | Lex.Lex(); |
| 3013 | if (EatIfPresent(T: lltok::rparen)) |
| 3014 | return Mask; |
| 3015 | } while (1); |
| 3016 | |
| 3017 | llvm_unreachable("unterminated nofpclass attribute" ); |
| 3018 | } |
| 3019 | |
| 3020 | /// parseOptionalCommaAlign |
| 3021 | /// ::= |
| 3022 | /// ::= ',' align 4 |
| 3023 | /// |
| 3024 | /// This returns with AteExtraComma set to true if it ate an excess comma at the |
| 3025 | /// end. |
| 3026 | bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment, |
| 3027 | bool &) { |
| 3028 | AteExtraComma = false; |
| 3029 | while (EatIfPresent(T: lltok::comma)) { |
| 3030 | // Metadata at the end is an early exit. |
| 3031 | if (Lex.getKind() == lltok::MetadataVar) { |
| 3032 | AteExtraComma = true; |
| 3033 | return false; |
| 3034 | } |
| 3035 | |
| 3036 | if (Lex.getKind() != lltok::kw_align) |
| 3037 | return error(L: Lex.getLoc(), Msg: "expected metadata or 'align'" ); |
| 3038 | |
| 3039 | if (parseOptionalAlignment(Alignment)) |
| 3040 | return true; |
| 3041 | } |
| 3042 | |
| 3043 | return false; |
| 3044 | } |
| 3045 | |
| 3046 | /// parseOptionalCommaAddrSpace |
| 3047 | /// ::= |
| 3048 | /// ::= ',' addrspace(1) |
| 3049 | /// |
| 3050 | /// This returns with AteExtraComma set to true if it ate an excess comma at the |
| 3051 | /// end. |
| 3052 | bool LLParser::parseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc, |
| 3053 | bool &) { |
| 3054 | AteExtraComma = false; |
| 3055 | while (EatIfPresent(T: lltok::comma)) { |
| 3056 | // Metadata at the end is an early exit. |
| 3057 | if (Lex.getKind() == lltok::MetadataVar) { |
| 3058 | AteExtraComma = true; |
| 3059 | return false; |
| 3060 | } |
| 3061 | |
| 3062 | Loc = Lex.getLoc(); |
| 3063 | if (Lex.getKind() != lltok::kw_addrspace) |
| 3064 | return error(L: Lex.getLoc(), Msg: "expected metadata or 'addrspace'" ); |
| 3065 | |
| 3066 | if (parseOptionalAddrSpace(AddrSpace)) |
| 3067 | return true; |
| 3068 | } |
| 3069 | |
| 3070 | return false; |
| 3071 | } |
| 3072 | |
| 3073 | bool LLParser::parseAllocSizeArguments(unsigned &BaseSizeArg, |
| 3074 | std::optional<unsigned> &HowManyArg) { |
| 3075 | Lex.Lex(); |
| 3076 | |
| 3077 | auto StartParen = Lex.getLoc(); |
| 3078 | if (!EatIfPresent(T: lltok::lparen)) |
| 3079 | return error(L: StartParen, Msg: "expected '('" ); |
| 3080 | |
| 3081 | if (parseUInt32(Val&: BaseSizeArg)) |
| 3082 | return true; |
| 3083 | |
| 3084 | if (EatIfPresent(T: lltok::comma)) { |
| 3085 | auto HowManyAt = Lex.getLoc(); |
| 3086 | unsigned HowMany; |
| 3087 | if (parseUInt32(Val&: HowMany)) |
| 3088 | return true; |
| 3089 | if (HowMany == BaseSizeArg) |
| 3090 | return error(L: HowManyAt, |
| 3091 | Msg: "'allocsize' indices can't refer to the same parameter" ); |
| 3092 | HowManyArg = HowMany; |
| 3093 | } else |
| 3094 | HowManyArg = std::nullopt; |
| 3095 | |
| 3096 | auto EndParen = Lex.getLoc(); |
| 3097 | if (!EatIfPresent(T: lltok::rparen)) |
| 3098 | return error(L: EndParen, Msg: "expected ')'" ); |
| 3099 | return false; |
| 3100 | } |
| 3101 | |
| 3102 | bool LLParser::parseVScaleRangeArguments(unsigned &MinValue, |
| 3103 | unsigned &MaxValue) { |
| 3104 | Lex.Lex(); |
| 3105 | |
| 3106 | auto StartParen = Lex.getLoc(); |
| 3107 | if (!EatIfPresent(T: lltok::lparen)) |
| 3108 | return error(L: StartParen, Msg: "expected '('" ); |
| 3109 | |
| 3110 | if (parseUInt32(Val&: MinValue)) |
| 3111 | return true; |
| 3112 | |
| 3113 | if (EatIfPresent(T: lltok::comma)) { |
| 3114 | if (parseUInt32(Val&: MaxValue)) |
| 3115 | return true; |
| 3116 | } else |
| 3117 | MaxValue = MinValue; |
| 3118 | |
| 3119 | auto EndParen = Lex.getLoc(); |
| 3120 | if (!EatIfPresent(T: lltok::rparen)) |
| 3121 | return error(L: EndParen, Msg: "expected ')'" ); |
| 3122 | return false; |
| 3123 | } |
| 3124 | |
| 3125 | /// parseScopeAndOrdering |
| 3126 | /// if isAtomic: ::= SyncScope? AtomicOrdering |
| 3127 | /// else: ::= |
| 3128 | /// |
| 3129 | /// This sets Scope and Ordering to the parsed values. |
| 3130 | bool LLParser::parseScopeAndOrdering(bool IsAtomic, SyncScope::ID &SSID, |
| 3131 | AtomicOrdering &Ordering) { |
| 3132 | if (!IsAtomic) |
| 3133 | return false; |
| 3134 | |
| 3135 | return parseScope(SSID) || parseOrdering(Ordering); |
| 3136 | } |
| 3137 | |
| 3138 | /// parseScope |
| 3139 | /// ::= syncscope("singlethread" | "<target scope>")? |
| 3140 | /// |
| 3141 | /// This sets synchronization scope ID to the ID of the parsed value. |
| 3142 | bool LLParser::parseScope(SyncScope::ID &SSID) { |
| 3143 | SSID = SyncScope::System; |
| 3144 | if (EatIfPresent(T: lltok::kw_syncscope)) { |
| 3145 | auto StartParenAt = Lex.getLoc(); |
| 3146 | if (!EatIfPresent(T: lltok::lparen)) |
| 3147 | return error(L: StartParenAt, Msg: "Expected '(' in syncscope" ); |
| 3148 | |
| 3149 | std::string SSN; |
| 3150 | auto SSNAt = Lex.getLoc(); |
| 3151 | if (parseStringConstant(Result&: SSN)) |
| 3152 | return error(L: SSNAt, Msg: "Expected synchronization scope name" ); |
| 3153 | |
| 3154 | auto EndParenAt = Lex.getLoc(); |
| 3155 | if (!EatIfPresent(T: lltok::rparen)) |
| 3156 | return error(L: EndParenAt, Msg: "Expected ')' in syncscope" ); |
| 3157 | |
| 3158 | SSID = Context.getOrInsertSyncScopeID(SSN); |
| 3159 | } |
| 3160 | |
| 3161 | return false; |
| 3162 | } |
| 3163 | |
| 3164 | /// parseOrdering |
| 3165 | /// ::= AtomicOrdering |
| 3166 | /// |
| 3167 | /// This sets Ordering to the parsed value. |
| 3168 | bool LLParser::parseOrdering(AtomicOrdering &Ordering) { |
| 3169 | switch (Lex.getKind()) { |
| 3170 | default: |
| 3171 | return tokError(Msg: "Expected ordering on atomic instruction" ); |
| 3172 | case lltok::kw_unordered: Ordering = AtomicOrdering::Unordered; break; |
| 3173 | case lltok::kw_monotonic: Ordering = AtomicOrdering::Monotonic; break; |
| 3174 | // Not specified yet: |
| 3175 | // case lltok::kw_consume: Ordering = AtomicOrdering::Consume; break; |
| 3176 | case lltok::kw_acquire: Ordering = AtomicOrdering::Acquire; break; |
| 3177 | case lltok::kw_release: Ordering = AtomicOrdering::Release; break; |
| 3178 | case lltok::kw_acq_rel: Ordering = AtomicOrdering::AcquireRelease; break; |
| 3179 | case lltok::kw_seq_cst: |
| 3180 | Ordering = AtomicOrdering::SequentiallyConsistent; |
| 3181 | break; |
| 3182 | } |
| 3183 | Lex.Lex(); |
| 3184 | return false; |
| 3185 | } |
| 3186 | |
| 3187 | /// parseOptionalStackAlignment |
| 3188 | /// ::= /* empty */ |
| 3189 | /// ::= 'alignstack' '(' 4 ')' |
| 3190 | bool LLParser::parseOptionalStackAlignment(unsigned &Alignment) { |
| 3191 | Alignment = 0; |
| 3192 | if (!EatIfPresent(T: lltok::kw_alignstack)) |
| 3193 | return false; |
| 3194 | LocTy ParenLoc = Lex.getLoc(); |
| 3195 | if (!EatIfPresent(T: lltok::lparen)) |
| 3196 | return error(L: ParenLoc, Msg: "expected '('" ); |
| 3197 | LocTy AlignLoc = Lex.getLoc(); |
| 3198 | if (parseUInt32(Val&: Alignment)) |
| 3199 | return true; |
| 3200 | ParenLoc = Lex.getLoc(); |
| 3201 | if (!EatIfPresent(T: lltok::rparen)) |
| 3202 | return error(L: ParenLoc, Msg: "expected ')'" ); |
| 3203 | if (!isPowerOf2_32(Value: Alignment)) |
| 3204 | return error(L: AlignLoc, Msg: "stack alignment is not a power of two" ); |
| 3205 | return false; |
| 3206 | } |
| 3207 | |
| 3208 | /// parseIndexList - This parses the index list for an insert/extractvalue |
| 3209 | /// instruction. This sets AteExtraComma in the case where we eat an extra |
| 3210 | /// comma at the end of the line and find that it is followed by metadata. |
| 3211 | /// Clients that don't allow metadata can call the version of this function that |
| 3212 | /// only takes one argument. |
| 3213 | /// |
| 3214 | /// parseIndexList |
| 3215 | /// ::= (',' uint32)+ |
| 3216 | /// |
| 3217 | bool LLParser::parseIndexList(SmallVectorImpl<unsigned> &Indices, |
| 3218 | bool &) { |
| 3219 | AteExtraComma = false; |
| 3220 | |
| 3221 | if (Lex.getKind() != lltok::comma) |
| 3222 | return tokError(Msg: "expected ',' as start of index list" ); |
| 3223 | |
| 3224 | while (EatIfPresent(T: lltok::comma)) { |
| 3225 | if (Lex.getKind() == lltok::MetadataVar) { |
| 3226 | if (Indices.empty()) |
| 3227 | return tokError(Msg: "expected index" ); |
| 3228 | AteExtraComma = true; |
| 3229 | return false; |
| 3230 | } |
| 3231 | unsigned Idx = 0; |
| 3232 | if (parseUInt32(Val&: Idx)) |
| 3233 | return true; |
| 3234 | Indices.push_back(Elt: Idx); |
| 3235 | } |
| 3236 | |
| 3237 | return false; |
| 3238 | } |
| 3239 | |
| 3240 | //===----------------------------------------------------------------------===// |
| 3241 | // Type Parsing. |
| 3242 | //===----------------------------------------------------------------------===// |
| 3243 | |
| 3244 | /// parseType - parse a type. |
| 3245 | bool LLParser::parseType(Type *&Result, const Twine &Msg, bool AllowVoid) { |
| 3246 | SMLoc TypeLoc = Lex.getLoc(); |
| 3247 | switch (Lex.getKind()) { |
| 3248 | default: |
| 3249 | return tokError(Msg); |
| 3250 | case lltok::Type: |
| 3251 | // Type ::= 'float' | 'void' (etc) |
| 3252 | Result = Lex.getTyVal(); |
| 3253 | Lex.Lex(); |
| 3254 | |
| 3255 | // Handle "ptr" opaque pointer type. |
| 3256 | // |
| 3257 | // Type ::= ptr ('addrspace' '(' uint32 ')')? |
| 3258 | if (Result->isPointerTy()) { |
| 3259 | unsigned AddrSpace; |
| 3260 | if (parseOptionalAddrSpace(AddrSpace)) |
| 3261 | return true; |
| 3262 | Result = PointerType::get(C&: getContext(), AddressSpace: AddrSpace); |
| 3263 | |
| 3264 | // Give a nice error for 'ptr*'. |
| 3265 | if (Lex.getKind() == lltok::star) |
| 3266 | return tokError(Msg: "ptr* is invalid - use ptr instead" ); |
| 3267 | |
| 3268 | // Fall through to parsing the type suffixes only if this 'ptr' is a |
| 3269 | // function return. Otherwise, return success, implicitly rejecting other |
| 3270 | // suffixes. |
| 3271 | if (Lex.getKind() != lltok::lparen) |
| 3272 | return false; |
| 3273 | } |
| 3274 | break; |
| 3275 | case lltok::kw_target: { |
| 3276 | // Type ::= TargetExtType |
| 3277 | if (parseTargetExtType(Result)) |
| 3278 | return true; |
| 3279 | break; |
| 3280 | } |
| 3281 | case lltok::lbrace: |
| 3282 | // Type ::= StructType |
| 3283 | if (parseAnonStructType(Result, Packed: false)) |
| 3284 | return true; |
| 3285 | break; |
| 3286 | case lltok::lsquare: |
| 3287 | // Type ::= '[' ... ']' |
| 3288 | Lex.Lex(); // eat the lsquare. |
| 3289 | if (parseArrayVectorType(Result, IsVector: false)) |
| 3290 | return true; |
| 3291 | break; |
| 3292 | case lltok::less: // Either vector or packed struct. |
| 3293 | // Type ::= '<' ... '>' |
| 3294 | Lex.Lex(); |
| 3295 | if (Lex.getKind() == lltok::lbrace) { |
| 3296 | if (parseAnonStructType(Result, Packed: true) || |
| 3297 | parseToken(T: lltok::greater, ErrMsg: "expected '>' at end of packed struct" )) |
| 3298 | return true; |
| 3299 | } else if (parseArrayVectorType(Result, IsVector: true)) |
| 3300 | return true; |
| 3301 | break; |
| 3302 | case lltok::LocalVar: { |
| 3303 | // Type ::= %foo |
| 3304 | std::pair<Type*, LocTy> &Entry = NamedTypes[Lex.getStrVal()]; |
| 3305 | |
| 3306 | // If the type hasn't been defined yet, create a forward definition and |
| 3307 | // remember where that forward def'n was seen (in case it never is defined). |
| 3308 | if (!Entry.first) { |
| 3309 | Entry.first = StructType::create(Context, Name: Lex.getStrVal()); |
| 3310 | Entry.second = Lex.getLoc(); |
| 3311 | } |
| 3312 | Result = Entry.first; |
| 3313 | Lex.Lex(); |
| 3314 | break; |
| 3315 | } |
| 3316 | |
| 3317 | case lltok::LocalVarID: { |
| 3318 | // Type ::= %4 |
| 3319 | std::pair<Type*, LocTy> &Entry = NumberedTypes[Lex.getUIntVal()]; |
| 3320 | |
| 3321 | // If the type hasn't been defined yet, create a forward definition and |
| 3322 | // remember where that forward def'n was seen (in case it never is defined). |
| 3323 | if (!Entry.first) { |
| 3324 | Entry.first = StructType::create(Context); |
| 3325 | Entry.second = Lex.getLoc(); |
| 3326 | } |
| 3327 | Result = Entry.first; |
| 3328 | Lex.Lex(); |
| 3329 | break; |
| 3330 | } |
| 3331 | } |
| 3332 | |
| 3333 | // parse the type suffixes. |
| 3334 | while (true) { |
| 3335 | switch (Lex.getKind()) { |
| 3336 | // End of type. |
| 3337 | default: |
| 3338 | if (!AllowVoid && Result->isVoidTy()) |
| 3339 | return error(L: TypeLoc, Msg: "void type only allowed for function results" ); |
| 3340 | return false; |
| 3341 | |
| 3342 | // Type ::= Type '*' |
| 3343 | case lltok::star: |
| 3344 | if (Result->isLabelTy()) |
| 3345 | return tokError(Msg: "basic block pointers are invalid" ); |
| 3346 | if (Result->isVoidTy()) |
| 3347 | return tokError(Msg: "pointers to void are invalid - use i8* instead" ); |
| 3348 | if (!PointerType::isValidElementType(ElemTy: Result)) |
| 3349 | return tokError(Msg: "pointer to this type is invalid" ); |
| 3350 | Result = PointerType::getUnqual(C&: Context); |
| 3351 | Lex.Lex(); |
| 3352 | break; |
| 3353 | |
| 3354 | // Type ::= Type 'addrspace' '(' uint32 ')' '*' |
| 3355 | case lltok::kw_addrspace: { |
| 3356 | if (Result->isLabelTy()) |
| 3357 | return tokError(Msg: "basic block pointers are invalid" ); |
| 3358 | if (Result->isVoidTy()) |
| 3359 | return tokError(Msg: "pointers to void are invalid; use i8* instead" ); |
| 3360 | if (!PointerType::isValidElementType(ElemTy: Result)) |
| 3361 | return tokError(Msg: "pointer to this type is invalid" ); |
| 3362 | unsigned AddrSpace; |
| 3363 | if (parseOptionalAddrSpace(AddrSpace) || |
| 3364 | parseToken(T: lltok::star, ErrMsg: "expected '*' in address space" )) |
| 3365 | return true; |
| 3366 | |
| 3367 | Result = PointerType::get(C&: Context, AddressSpace: AddrSpace); |
| 3368 | break; |
| 3369 | } |
| 3370 | |
| 3371 | /// Types '(' ArgTypeListI ')' OptFuncAttrs |
| 3372 | case lltok::lparen: |
| 3373 | if (parseFunctionType(Result)) |
| 3374 | return true; |
| 3375 | break; |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | |
| 3380 | /// parseParameterList |
| 3381 | /// ::= '(' ')' |
| 3382 | /// ::= '(' Arg (',' Arg)* ')' |
| 3383 | /// Arg |
| 3384 | /// ::= Type OptionalAttributes Value OptionalAttributes |
| 3385 | bool LLParser::parseParameterList(SmallVectorImpl<ParamInfo> &ArgList, |
| 3386 | PerFunctionState &PFS, bool IsMustTailCall, |
| 3387 | bool InVarArgsFunc) { |
| 3388 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in call" )) |
| 3389 | return true; |
| 3390 | |
| 3391 | while (Lex.getKind() != lltok::rparen) { |
| 3392 | // If this isn't the first argument, we need a comma. |
| 3393 | if (!ArgList.empty() && |
| 3394 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in argument list" )) |
| 3395 | return true; |
| 3396 | |
| 3397 | // parse an ellipsis if this is a musttail call in a variadic function. |
| 3398 | if (Lex.getKind() == lltok::dotdotdot) { |
| 3399 | const char *Msg = "unexpected ellipsis in argument list for " ; |
| 3400 | if (!IsMustTailCall) |
| 3401 | return tokError(Msg: Twine(Msg) + "non-musttail call" ); |
| 3402 | if (!InVarArgsFunc) |
| 3403 | return tokError(Msg: Twine(Msg) + "musttail call in non-varargs function" ); |
| 3404 | Lex.Lex(); // Lex the '...', it is purely for readability. |
| 3405 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of argument list" ); |
| 3406 | } |
| 3407 | |
| 3408 | // parse the argument. |
| 3409 | LocTy ArgLoc; |
| 3410 | Type *ArgTy = nullptr; |
| 3411 | Value *V; |
| 3412 | if (parseType(Result&: ArgTy, Loc&: ArgLoc)) |
| 3413 | return true; |
| 3414 | if (!FunctionType::isValidArgumentType(ArgTy)) |
| 3415 | return error(L: ArgLoc, Msg: "invalid type for function argument" ); |
| 3416 | |
| 3417 | AttrBuilder ArgAttrs(M->getContext()); |
| 3418 | |
| 3419 | if (ArgTy->isMetadataTy()) { |
| 3420 | if (parseMetadataAsValue(V, PFS)) |
| 3421 | return true; |
| 3422 | } else { |
| 3423 | // Otherwise, handle normal operands. |
| 3424 | if (parseOptionalParamAttrs(B&: ArgAttrs) || parseValue(Ty: ArgTy, V, PFS)) |
| 3425 | return true; |
| 3426 | } |
| 3427 | ArgList.push_back(Elt: ParamInfo( |
| 3428 | ArgLoc, V, AttributeSet::get(C&: V->getContext(), B: ArgAttrs))); |
| 3429 | } |
| 3430 | |
| 3431 | if (IsMustTailCall && InVarArgsFunc) |
| 3432 | return tokError(Msg: "expected '...' at end of argument list for musttail call " |
| 3433 | "in varargs function" ); |
| 3434 | |
| 3435 | Lex.Lex(); // Lex the ')'. |
| 3436 | return false; |
| 3437 | } |
| 3438 | |
| 3439 | /// parseRequiredTypeAttr |
| 3440 | /// ::= attrname(<ty>) |
| 3441 | bool LLParser::parseRequiredTypeAttr(AttrBuilder &B, lltok::Kind AttrToken, |
| 3442 | Attribute::AttrKind AttrKind) { |
| 3443 | Type *Ty = nullptr; |
| 3444 | if (!EatIfPresent(T: AttrToken)) |
| 3445 | return true; |
| 3446 | if (!EatIfPresent(T: lltok::lparen)) |
| 3447 | return error(L: Lex.getLoc(), Msg: "expected '('" ); |
| 3448 | if (parseType(Result&: Ty)) |
| 3449 | return true; |
| 3450 | if (!EatIfPresent(T: lltok::rparen)) |
| 3451 | return error(L: Lex.getLoc(), Msg: "expected ')'" ); |
| 3452 | |
| 3453 | B.addTypeAttr(Kind: AttrKind, Ty); |
| 3454 | return false; |
| 3455 | } |
| 3456 | |
| 3457 | /// parseRangeAttr |
| 3458 | /// ::= range(<ty> <n>,<n>) |
| 3459 | bool LLParser::parseRangeAttr(AttrBuilder &B) { |
| 3460 | Lex.Lex(); |
| 3461 | |
| 3462 | APInt Lower; |
| 3463 | APInt Upper; |
| 3464 | Type *Ty = nullptr; |
| 3465 | LocTy TyLoc; |
| 3466 | |
| 3467 | auto ParseAPSInt = [&](unsigned BitWidth, APInt &Val) { |
| 3468 | if (Lex.getKind() != lltok::APSInt) |
| 3469 | return tokError(Msg: "expected integer" ); |
| 3470 | if (Lex.getAPSIntVal().getBitWidth() > BitWidth) |
| 3471 | return tokError( |
| 3472 | Msg: "integer is too large for the bit width of specified type" ); |
| 3473 | Val = Lex.getAPSIntVal().extend(width: BitWidth); |
| 3474 | Lex.Lex(); |
| 3475 | return false; |
| 3476 | }; |
| 3477 | |
| 3478 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" ) || parseType(Result&: Ty, Loc&: TyLoc)) |
| 3479 | return true; |
| 3480 | if (!Ty->isIntegerTy()) |
| 3481 | return error(L: TyLoc, Msg: "the range must have integer type!" ); |
| 3482 | |
| 3483 | unsigned BitWidth = Ty->getPrimitiveSizeInBits(); |
| 3484 | |
| 3485 | if (ParseAPSInt(BitWidth, Lower) || |
| 3486 | parseToken(T: lltok::comma, ErrMsg: "expected ','" ) || ParseAPSInt(BitWidth, Upper)) |
| 3487 | return true; |
| 3488 | if (Lower == Upper && !Lower.isZero()) |
| 3489 | return tokError(Msg: "the range represent the empty set but limits aren't 0!" ); |
| 3490 | |
| 3491 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'" )) |
| 3492 | return true; |
| 3493 | |
| 3494 | B.addRangeAttr(CR: ConstantRange(Lower, Upper)); |
| 3495 | return false; |
| 3496 | } |
| 3497 | |
| 3498 | /// parseInitializesAttr |
| 3499 | /// ::= initializes((Lo1,Hi1),(Lo2,Hi2),...) |
| 3500 | bool LLParser::parseInitializesAttr(AttrBuilder &B) { |
| 3501 | Lex.Lex(); |
| 3502 | |
| 3503 | auto ParseAPSInt = [&](APInt &Val) { |
| 3504 | if (Lex.getKind() != lltok::APSInt) |
| 3505 | return tokError(Msg: "expected integer" ); |
| 3506 | Val = Lex.getAPSIntVal().extend(width: 64); |
| 3507 | Lex.Lex(); |
| 3508 | return false; |
| 3509 | }; |
| 3510 | |
| 3511 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" )) |
| 3512 | return true; |
| 3513 | |
| 3514 | SmallVector<ConstantRange, 2> RangeList; |
| 3515 | // Parse each constant range. |
| 3516 | do { |
| 3517 | APInt Lower, Upper; |
| 3518 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" )) |
| 3519 | return true; |
| 3520 | |
| 3521 | if (ParseAPSInt(Lower) || parseToken(T: lltok::comma, ErrMsg: "expected ','" ) || |
| 3522 | ParseAPSInt(Upper)) |
| 3523 | return true; |
| 3524 | |
| 3525 | if (Lower == Upper) |
| 3526 | return tokError(Msg: "the range should not represent the full or empty set!" ); |
| 3527 | |
| 3528 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'" )) |
| 3529 | return true; |
| 3530 | |
| 3531 | RangeList.push_back(Elt: ConstantRange(Lower, Upper)); |
| 3532 | } while (EatIfPresent(T: lltok::comma)); |
| 3533 | |
| 3534 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'" )) |
| 3535 | return true; |
| 3536 | |
| 3537 | auto CRLOrNull = ConstantRangeList::getConstantRangeList(RangesRef: RangeList); |
| 3538 | if (!CRLOrNull.has_value()) |
| 3539 | return tokError(Msg: "Invalid (unordered or overlapping) range list" ); |
| 3540 | B.addInitializesAttr(CRL: *CRLOrNull); |
| 3541 | return false; |
| 3542 | } |
| 3543 | |
| 3544 | bool LLParser::parseCapturesAttr(AttrBuilder &B) { |
| 3545 | CaptureComponents Other = CaptureComponents::None; |
| 3546 | std::optional<CaptureComponents> Ret; |
| 3547 | |
| 3548 | // We use syntax like captures(ret: address, provenance), so the colon |
| 3549 | // should not be interpreted as a label terminator. |
| 3550 | Lex.setIgnoreColonInIdentifiers(true); |
| 3551 | llvm::scope_exit _([&] { Lex.setIgnoreColonInIdentifiers(false); }); |
| 3552 | |
| 3553 | Lex.Lex(); |
| 3554 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" )) |
| 3555 | return true; |
| 3556 | |
| 3557 | CaptureComponents *Current = &Other; |
| 3558 | bool SeenComponent = false; |
| 3559 | while (true) { |
| 3560 | if (EatIfPresent(T: lltok::kw_ret)) { |
| 3561 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 3562 | return true; |
| 3563 | if (Ret) |
| 3564 | return tokError(Msg: "duplicate 'ret' location" ); |
| 3565 | Ret = CaptureComponents::None; |
| 3566 | Current = &*Ret; |
| 3567 | SeenComponent = false; |
| 3568 | } |
| 3569 | |
| 3570 | if (EatIfPresent(T: lltok::kw_none)) { |
| 3571 | if (SeenComponent) |
| 3572 | return tokError(Msg: "cannot use 'none' with other component" ); |
| 3573 | *Current = CaptureComponents::None; |
| 3574 | } else { |
| 3575 | if (SeenComponent && capturesNothing(CC: *Current)) |
| 3576 | return tokError(Msg: "cannot use 'none' with other component" ); |
| 3577 | |
| 3578 | if (EatIfPresent(T: lltok::kw_address_is_null)) |
| 3579 | *Current |= CaptureComponents::AddressIsNull; |
| 3580 | else if (EatIfPresent(T: lltok::kw_address)) |
| 3581 | *Current |= CaptureComponents::Address; |
| 3582 | else if (EatIfPresent(T: lltok::kw_provenance)) |
| 3583 | *Current |= CaptureComponents::Provenance; |
| 3584 | else if (EatIfPresent(T: lltok::kw_read_provenance)) |
| 3585 | *Current |= CaptureComponents::ReadProvenance; |
| 3586 | else |
| 3587 | return tokError(Msg: "expected one of 'none', 'address', 'address_is_null', " |
| 3588 | "'provenance' or 'read_provenance'" ); |
| 3589 | } |
| 3590 | |
| 3591 | SeenComponent = true; |
| 3592 | if (EatIfPresent(T: lltok::rparen)) |
| 3593 | break; |
| 3594 | |
| 3595 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' or ')'" )) |
| 3596 | return true; |
| 3597 | } |
| 3598 | |
| 3599 | B.addCapturesAttr(CI: CaptureInfo(Other, Ret.value_or(u&: Other))); |
| 3600 | return false; |
| 3601 | } |
| 3602 | |
| 3603 | /// parseOptionalOperandBundles |
| 3604 | /// ::= /*empty*/ |
| 3605 | /// ::= '[' OperandBundle [, OperandBundle ]* ']' |
| 3606 | /// |
| 3607 | /// OperandBundle |
| 3608 | /// ::= bundle-tag '(' ')' |
| 3609 | /// ::= bundle-tag '(' Type Value [, Type Value ]* ')' |
| 3610 | /// |
| 3611 | /// bundle-tag ::= String Constant |
| 3612 | bool LLParser::parseOptionalOperandBundles( |
| 3613 | SmallVectorImpl<OperandBundleDef> &BundleList, PerFunctionState &PFS) { |
| 3614 | LocTy BeginLoc = Lex.getLoc(); |
| 3615 | if (!EatIfPresent(T: lltok::lsquare)) |
| 3616 | return false; |
| 3617 | |
| 3618 | while (Lex.getKind() != lltok::rsquare) { |
| 3619 | // If this isn't the first operand bundle, we need a comma. |
| 3620 | if (!BundleList.empty() && |
| 3621 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in input list" )) |
| 3622 | return true; |
| 3623 | |
| 3624 | std::string Tag; |
| 3625 | if (parseStringConstant(Result&: Tag)) |
| 3626 | return true; |
| 3627 | |
| 3628 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in operand bundle" )) |
| 3629 | return true; |
| 3630 | |
| 3631 | std::vector<Value *> Inputs; |
| 3632 | while (Lex.getKind() != lltok::rparen) { |
| 3633 | // If this isn't the first input, we need a comma. |
| 3634 | if (!Inputs.empty() && |
| 3635 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in input list" )) |
| 3636 | return true; |
| 3637 | |
| 3638 | Type *Ty = nullptr; |
| 3639 | Value *Input = nullptr; |
| 3640 | if (parseType(Result&: Ty)) |
| 3641 | return true; |
| 3642 | if (Ty->isMetadataTy()) { |
| 3643 | if (parseMetadataAsValue(V&: Input, PFS)) |
| 3644 | return true; |
| 3645 | } else if (parseValue(Ty, V&: Input, PFS)) { |
| 3646 | return true; |
| 3647 | } |
| 3648 | Inputs.push_back(x: Input); |
| 3649 | } |
| 3650 | |
| 3651 | BundleList.emplace_back(Args: std::move(Tag), Args: std::move(Inputs)); |
| 3652 | |
| 3653 | Lex.Lex(); // Lex the ')'. |
| 3654 | } |
| 3655 | |
| 3656 | if (BundleList.empty()) |
| 3657 | return error(L: BeginLoc, Msg: "operand bundle set must not be empty" ); |
| 3658 | |
| 3659 | Lex.Lex(); // Lex the ']'. |
| 3660 | return false; |
| 3661 | } |
| 3662 | |
| 3663 | bool LLParser::checkValueID(LocTy Loc, StringRef Kind, StringRef Prefix, |
| 3664 | unsigned NextID, unsigned ID) { |
| 3665 | if (ID < NextID) |
| 3666 | return error(L: Loc, Msg: Kind + " expected to be numbered '" + Prefix + |
| 3667 | Twine(NextID) + "' or greater" ); |
| 3668 | |
| 3669 | return false; |
| 3670 | } |
| 3671 | |
| 3672 | /// parseArgumentList - parse the argument list for a function type or function |
| 3673 | /// prototype. |
| 3674 | /// ::= '(' ArgTypeListI ')' |
| 3675 | /// ArgTypeListI |
| 3676 | /// ::= /*empty*/ |
| 3677 | /// ::= '...' |
| 3678 | /// ::= ArgTypeList ',' '...' |
| 3679 | /// ::= ArgType (',' ArgType)* |
| 3680 | /// |
| 3681 | bool LLParser::parseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, |
| 3682 | SmallVectorImpl<unsigned> &UnnamedArgNums, |
| 3683 | bool &IsVarArg) { |
| 3684 | unsigned CurValID = 0; |
| 3685 | IsVarArg = false; |
| 3686 | assert(Lex.getKind() == lltok::lparen); |
| 3687 | Lex.Lex(); // eat the (. |
| 3688 | |
| 3689 | if (Lex.getKind() != lltok::rparen) { |
| 3690 | do { |
| 3691 | // Handle ... at end of arg list. |
| 3692 | if (EatIfPresent(T: lltok::dotdotdot)) { |
| 3693 | IsVarArg = true; |
| 3694 | break; |
| 3695 | } |
| 3696 | |
| 3697 | // Otherwise must be an argument type. |
| 3698 | LocTy TypeLoc = Lex.getLoc(); |
| 3699 | Type *ArgTy = nullptr; |
| 3700 | AttrBuilder Attrs(M->getContext()); |
| 3701 | if (parseType(Result&: ArgTy) || parseOptionalParamAttrs(B&: Attrs)) |
| 3702 | return true; |
| 3703 | |
| 3704 | if (ArgTy->isVoidTy()) |
| 3705 | return error(L: TypeLoc, Msg: "argument can not have void type" ); |
| 3706 | |
| 3707 | std::string Name; |
| 3708 | FileLoc IdentStart; |
| 3709 | FileLoc IdentEnd; |
| 3710 | bool Unnamed = false; |
| 3711 | if (Lex.getKind() == lltok::LocalVar) { |
| 3712 | Name = Lex.getStrVal(); |
| 3713 | IdentStart = getTokLineColumnPos(); |
| 3714 | Lex.Lex(); |
| 3715 | IdentEnd = getPrevTokEndLineColumnPos(); |
| 3716 | } else { |
| 3717 | unsigned ArgID; |
| 3718 | if (Lex.getKind() == lltok::LocalVarID) { |
| 3719 | ArgID = Lex.getUIntVal(); |
| 3720 | IdentStart = getTokLineColumnPos(); |
| 3721 | if (checkValueID(Loc: TypeLoc, Kind: "argument" , Prefix: "%" , NextID: CurValID, ID: ArgID)) |
| 3722 | return true; |
| 3723 | Lex.Lex(); |
| 3724 | IdentEnd = getPrevTokEndLineColumnPos(); |
| 3725 | } else { |
| 3726 | ArgID = CurValID; |
| 3727 | Unnamed = true; |
| 3728 | } |
| 3729 | UnnamedArgNums.push_back(Elt: ArgID); |
| 3730 | CurValID = ArgID + 1; |
| 3731 | } |
| 3732 | |
| 3733 | if (!FunctionType::isValidArgumentType(ArgTy)) |
| 3734 | return error(L: TypeLoc, Msg: "invalid type for function argument" ); |
| 3735 | |
| 3736 | ArgList.emplace_back( |
| 3737 | Args&: TypeLoc, Args&: ArgTy, |
| 3738 | Args: Unnamed ? std::nullopt |
| 3739 | : std::make_optional(t: FileLocRange(IdentStart, IdentEnd)), |
| 3740 | Args: AttributeSet::get(C&: ArgTy->getContext(), B: Attrs), Args: std::move(Name)); |
| 3741 | } while (EatIfPresent(T: lltok::comma)); |
| 3742 | } |
| 3743 | |
| 3744 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of argument list" ); |
| 3745 | } |
| 3746 | |
| 3747 | /// parseFunctionType |
| 3748 | /// ::= Type ArgumentList OptionalAttrs |
| 3749 | bool LLParser::parseFunctionType(Type *&Result) { |
| 3750 | assert(Lex.getKind() == lltok::lparen); |
| 3751 | |
| 3752 | if (!FunctionType::isValidReturnType(RetTy: Result)) |
| 3753 | return tokError(Msg: "invalid function return type" ); |
| 3754 | |
| 3755 | SmallVector<ArgInfo, 8> ArgList; |
| 3756 | bool IsVarArg; |
| 3757 | SmallVector<unsigned> UnnamedArgNums; |
| 3758 | if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg)) |
| 3759 | return true; |
| 3760 | |
| 3761 | // Reject names on the arguments lists. |
| 3762 | for (const ArgInfo &Arg : ArgList) { |
| 3763 | if (!Arg.Name.empty()) |
| 3764 | return error(L: Arg.Loc, Msg: "argument name invalid in function type" ); |
| 3765 | if (Arg.Attrs.hasAttributes()) |
| 3766 | return error(L: Arg.Loc, Msg: "argument attributes invalid in function type" ); |
| 3767 | } |
| 3768 | |
| 3769 | SmallVector<Type*, 16> ArgListTy; |
| 3770 | for (const ArgInfo &Arg : ArgList) |
| 3771 | ArgListTy.push_back(Elt: Arg.Ty); |
| 3772 | |
| 3773 | Result = FunctionType::get(Result, Params: ArgListTy, isVarArg: IsVarArg); |
| 3774 | return false; |
| 3775 | } |
| 3776 | |
| 3777 | /// parseAnonStructType - parse an anonymous struct type, which is inlined into |
| 3778 | /// other structs. |
| 3779 | bool LLParser::parseAnonStructType(Type *&Result, bool Packed) { |
| 3780 | SmallVector<Type*, 8> Elts; |
| 3781 | if (parseStructBody(Body&: Elts)) |
| 3782 | return true; |
| 3783 | |
| 3784 | Result = StructType::get(Context, Elements: Elts, isPacked: Packed); |
| 3785 | return false; |
| 3786 | } |
| 3787 | |
| 3788 | /// parseStructDefinition - parse a struct in a 'type' definition. |
| 3789 | bool LLParser::parseStructDefinition(SMLoc TypeLoc, StringRef Name, |
| 3790 | std::pair<Type *, LocTy> &Entry, |
| 3791 | Type *&ResultTy) { |
| 3792 | // If the type was already defined, diagnose the redefinition. |
| 3793 | if (Entry.first && !Entry.second.isValid()) |
| 3794 | return error(L: TypeLoc, Msg: "redefinition of type" ); |
| 3795 | |
| 3796 | // If we have opaque, just return without filling in the definition for the |
| 3797 | // struct. This counts as a definition as far as the .ll file goes. |
| 3798 | if (EatIfPresent(T: lltok::kw_opaque)) { |
| 3799 | // This type is being defined, so clear the location to indicate this. |
| 3800 | Entry.second = SMLoc(); |
| 3801 | |
| 3802 | // If this type number has never been uttered, create it. |
| 3803 | if (!Entry.first) |
| 3804 | Entry.first = StructType::create(Context, Name); |
| 3805 | ResultTy = Entry.first; |
| 3806 | return false; |
| 3807 | } |
| 3808 | |
| 3809 | // If the type starts with '<', then it is either a packed struct or a vector. |
| 3810 | bool isPacked = EatIfPresent(T: lltok::less); |
| 3811 | |
| 3812 | // If we don't have a struct, then we have a random type alias, which we |
| 3813 | // accept for compatibility with old files. These types are not allowed to be |
| 3814 | // forward referenced and not allowed to be recursive. |
| 3815 | if (Lex.getKind() != lltok::lbrace) { |
| 3816 | if (Entry.first) |
| 3817 | return error(L: TypeLoc, Msg: "forward references to non-struct type" ); |
| 3818 | |
| 3819 | ResultTy = nullptr; |
| 3820 | if (isPacked) |
| 3821 | return parseArrayVectorType(Result&: ResultTy, IsVector: true); |
| 3822 | return parseType(Result&: ResultTy); |
| 3823 | } |
| 3824 | |
| 3825 | // This type is being defined, so clear the location to indicate this. |
| 3826 | Entry.second = SMLoc(); |
| 3827 | |
| 3828 | // If this type number has never been uttered, create it. |
| 3829 | if (!Entry.first) |
| 3830 | Entry.first = StructType::create(Context, Name); |
| 3831 | |
| 3832 | StructType *STy = cast<StructType>(Val: Entry.first); |
| 3833 | |
| 3834 | SmallVector<Type*, 8> Body; |
| 3835 | if (parseStructBody(Body) || |
| 3836 | (isPacked && parseToken(T: lltok::greater, ErrMsg: "expected '>' in packed struct" ))) |
| 3837 | return true; |
| 3838 | |
| 3839 | if (auto E = STy->setBodyOrError(Elements: Body, isPacked)) |
| 3840 | return tokError(Msg: toString(E: std::move(E))); |
| 3841 | |
| 3842 | ResultTy = STy; |
| 3843 | return false; |
| 3844 | } |
| 3845 | |
| 3846 | /// parseStructType: Handles packed and unpacked types. </> parsed elsewhere. |
| 3847 | /// StructType |
| 3848 | /// ::= '{' '}' |
| 3849 | /// ::= '{' Type (',' Type)* '}' |
| 3850 | /// ::= '<' '{' '}' '>' |
| 3851 | /// ::= '<' '{' Type (',' Type)* '}' '>' |
| 3852 | bool LLParser::parseStructBody(SmallVectorImpl<Type *> &Body) { |
| 3853 | assert(Lex.getKind() == lltok::lbrace); |
| 3854 | Lex.Lex(); // Consume the '{' |
| 3855 | |
| 3856 | // Handle the empty struct. |
| 3857 | if (EatIfPresent(T: lltok::rbrace)) |
| 3858 | return false; |
| 3859 | |
| 3860 | LocTy EltTyLoc = Lex.getLoc(); |
| 3861 | Type *Ty = nullptr; |
| 3862 | if (parseType(Result&: Ty)) |
| 3863 | return true; |
| 3864 | Body.push_back(Elt: Ty); |
| 3865 | |
| 3866 | if (!StructType::isValidElementType(ElemTy: Ty)) |
| 3867 | return error(L: EltTyLoc, Msg: "invalid element type for struct" ); |
| 3868 | |
| 3869 | while (EatIfPresent(T: lltok::comma)) { |
| 3870 | EltTyLoc = Lex.getLoc(); |
| 3871 | if (parseType(Result&: Ty)) |
| 3872 | return true; |
| 3873 | |
| 3874 | if (!StructType::isValidElementType(ElemTy: Ty)) |
| 3875 | return error(L: EltTyLoc, Msg: "invalid element type for struct" ); |
| 3876 | |
| 3877 | Body.push_back(Elt: Ty); |
| 3878 | } |
| 3879 | |
| 3880 | return parseToken(T: lltok::rbrace, ErrMsg: "expected '}' at end of struct" ); |
| 3881 | } |
| 3882 | |
| 3883 | /// parseArrayVectorType - parse an array or vector type, assuming the first |
| 3884 | /// token has already been consumed. |
| 3885 | /// Type |
| 3886 | /// ::= '[' APSINTVAL 'x' Types ']' |
| 3887 | /// ::= '<' APSINTVAL 'x' Types '>' |
| 3888 | /// ::= '<' 'vscale' 'x' APSINTVAL 'x' Types '>' |
| 3889 | bool LLParser::parseArrayVectorType(Type *&Result, bool IsVector) { |
| 3890 | bool Scalable = false; |
| 3891 | |
| 3892 | if (IsVector && Lex.getKind() == lltok::kw_vscale) { |
| 3893 | Lex.Lex(); // consume the 'vscale' |
| 3894 | if (parseToken(T: lltok::kw_x, ErrMsg: "expected 'x' after vscale" )) |
| 3895 | return true; |
| 3896 | |
| 3897 | Scalable = true; |
| 3898 | } |
| 3899 | |
| 3900 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned() || |
| 3901 | Lex.getAPSIntVal().getBitWidth() > 64) |
| 3902 | return tokError(Msg: "expected number in address space" ); |
| 3903 | |
| 3904 | LocTy SizeLoc = Lex.getLoc(); |
| 3905 | uint64_t Size = Lex.getAPSIntVal().getZExtValue(); |
| 3906 | Lex.Lex(); |
| 3907 | |
| 3908 | if (parseToken(T: lltok::kw_x, ErrMsg: "expected 'x' after element count" )) |
| 3909 | return true; |
| 3910 | |
| 3911 | LocTy TypeLoc = Lex.getLoc(); |
| 3912 | Type *EltTy = nullptr; |
| 3913 | if (parseType(Result&: EltTy)) |
| 3914 | return true; |
| 3915 | |
| 3916 | if (parseToken(T: IsVector ? lltok::greater : lltok::rsquare, |
| 3917 | ErrMsg: "expected end of sequential type" )) |
| 3918 | return true; |
| 3919 | |
| 3920 | if (IsVector) { |
| 3921 | if (Size == 0) |
| 3922 | return error(L: SizeLoc, Msg: "zero element vector is illegal" ); |
| 3923 | if ((unsigned)Size != Size) |
| 3924 | return error(L: SizeLoc, Msg: "size too large for vector" ); |
| 3925 | if (!VectorType::isValidElementType(ElemTy: EltTy)) |
| 3926 | return error(L: TypeLoc, Msg: "invalid vector element type" ); |
| 3927 | Result = VectorType::get(ElementType: EltTy, NumElements: unsigned(Size), Scalable); |
| 3928 | } else { |
| 3929 | if (!ArrayType::isValidElementType(ElemTy: EltTy)) |
| 3930 | return error(L: TypeLoc, Msg: "invalid array element type" ); |
| 3931 | Result = ArrayType::get(ElementType: EltTy, NumElements: Size); |
| 3932 | } |
| 3933 | return false; |
| 3934 | } |
| 3935 | |
| 3936 | /// parseTargetExtType - handle target extension type syntax |
| 3937 | /// TargetExtType |
| 3938 | /// ::= 'target' '(' STRINGCONSTANT TargetExtTypeParams TargetExtIntParams ')' |
| 3939 | /// |
| 3940 | /// TargetExtTypeParams |
| 3941 | /// ::= /*empty*/ |
| 3942 | /// ::= ',' Type TargetExtTypeParams |
| 3943 | /// |
| 3944 | /// TargetExtIntParams |
| 3945 | /// ::= /*empty*/ |
| 3946 | /// ::= ',' uint32 TargetExtIntParams |
| 3947 | bool LLParser::parseTargetExtType(Type *&Result) { |
| 3948 | Lex.Lex(); // Eat the 'target' keyword. |
| 3949 | |
| 3950 | // Get the mandatory type name. |
| 3951 | std::string TypeName; |
| 3952 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in target extension type" ) || |
| 3953 | parseStringConstant(Result&: TypeName)) |
| 3954 | return true; |
| 3955 | |
| 3956 | // Parse all of the integer and type parameters at the same time; the use of |
| 3957 | // SeenInt will allow us to catch cases where type parameters follow integer |
| 3958 | // parameters. |
| 3959 | SmallVector<Type *> TypeParams; |
| 3960 | SmallVector<unsigned> IntParams; |
| 3961 | bool SeenInt = false; |
| 3962 | while (Lex.getKind() == lltok::comma) { |
| 3963 | Lex.Lex(); // Eat the comma. |
| 3964 | |
| 3965 | if (Lex.getKind() == lltok::APSInt) { |
| 3966 | SeenInt = true; |
| 3967 | unsigned IntVal; |
| 3968 | if (parseUInt32(Val&: IntVal)) |
| 3969 | return true; |
| 3970 | IntParams.push_back(Elt: IntVal); |
| 3971 | } else if (SeenInt) { |
| 3972 | // The only other kind of parameter we support is type parameters, which |
| 3973 | // must precede the integer parameters. This is therefore an error. |
| 3974 | return tokError(Msg: "expected uint32 param" ); |
| 3975 | } else { |
| 3976 | Type *TypeParam; |
| 3977 | if (parseType(Result&: TypeParam, /*AllowVoid=*/true)) |
| 3978 | return true; |
| 3979 | TypeParams.push_back(Elt: TypeParam); |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in target extension type" )) |
| 3984 | return true; |
| 3985 | |
| 3986 | auto TTy = |
| 3987 | TargetExtType::getOrError(Context, Name: TypeName, Types: TypeParams, Ints: IntParams); |
| 3988 | if (auto E = TTy.takeError()) |
| 3989 | return tokError(Msg: toString(E: std::move(E))); |
| 3990 | |
| 3991 | Result = *TTy; |
| 3992 | return false; |
| 3993 | } |
| 3994 | |
| 3995 | //===----------------------------------------------------------------------===// |
| 3996 | // Function Semantic Analysis. |
| 3997 | //===----------------------------------------------------------------------===// |
| 3998 | |
| 3999 | LLParser::PerFunctionState::PerFunctionState(LLParser &p, Function &f, |
| 4000 | int functionNumber, |
| 4001 | ArrayRef<unsigned> UnnamedArgNums) |
| 4002 | : P(p), F(f), FunctionNumber(functionNumber) { |
| 4003 | |
| 4004 | // Insert unnamed arguments into the NumberedVals list. |
| 4005 | auto It = UnnamedArgNums.begin(); |
| 4006 | for (Argument &A : F.args()) { |
| 4007 | if (!A.hasName()) { |
| 4008 | unsigned ArgNum = *It++; |
| 4009 | NumberedVals.add(ID: ArgNum, V: &A); |
| 4010 | } |
| 4011 | } |
| 4012 | } |
| 4013 | |
| 4014 | LLParser::PerFunctionState::~PerFunctionState() { |
| 4015 | // If there were any forward referenced non-basicblock values, delete them. |
| 4016 | |
| 4017 | for (const auto &P : ForwardRefVals) { |
| 4018 | if (isa<BasicBlock>(Val: P.second.first)) |
| 4019 | continue; |
| 4020 | P.second.first->replaceAllUsesWith( |
| 4021 | V: PoisonValue::get(T: P.second.first->getType())); |
| 4022 | P.second.first->deleteValue(); |
| 4023 | } |
| 4024 | |
| 4025 | for (const auto &P : ForwardRefValIDs) { |
| 4026 | if (isa<BasicBlock>(Val: P.second.first)) |
| 4027 | continue; |
| 4028 | P.second.first->replaceAllUsesWith( |
| 4029 | V: PoisonValue::get(T: P.second.first->getType())); |
| 4030 | P.second.first->deleteValue(); |
| 4031 | } |
| 4032 | } |
| 4033 | |
| 4034 | bool LLParser::PerFunctionState::finishFunction() { |
| 4035 | if (!ForwardRefVals.empty()) |
| 4036 | return P.error(L: ForwardRefVals.begin()->second.second, |
| 4037 | Msg: "use of undefined value '%" + ForwardRefVals.begin()->first + |
| 4038 | "'" ); |
| 4039 | if (!ForwardRefValIDs.empty()) |
| 4040 | return P.error(L: ForwardRefValIDs.begin()->second.second, |
| 4041 | Msg: "use of undefined value '%" + |
| 4042 | Twine(ForwardRefValIDs.begin()->first) + "'" ); |
| 4043 | return false; |
| 4044 | } |
| 4045 | |
| 4046 | /// getVal - Get a value with the specified name or ID, creating a |
| 4047 | /// forward reference record if needed. This can return null if the value |
| 4048 | /// exists but does not have the right type. |
| 4049 | Value *LLParser::PerFunctionState::getVal(const std::string &Name, Type *Ty, |
| 4050 | LocTy Loc) { |
| 4051 | // Look this name up in the normal function symbol table. |
| 4052 | Value *Val = F.getValueSymbolTable()->lookup(Name); |
| 4053 | |
| 4054 | // If this is a forward reference for the value, see if we already created a |
| 4055 | // forward ref record. |
| 4056 | if (!Val) { |
| 4057 | auto I = ForwardRefVals.find(x: Name); |
| 4058 | if (I != ForwardRefVals.end()) |
| 4059 | Val = I->second.first; |
| 4060 | } |
| 4061 | |
| 4062 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 4063 | if (Val) |
| 4064 | return P.checkValidVariableType(Loc, Name: "%" + Name, Ty, Val); |
| 4065 | |
| 4066 | // Don't make placeholders with invalid type. |
| 4067 | if (!Ty->isFirstClassType()) { |
| 4068 | P.error(L: Loc, Msg: "invalid use of a non-first-class type" ); |
| 4069 | return nullptr; |
| 4070 | } |
| 4071 | |
| 4072 | // Otherwise, create a new forward reference for this value and remember it. |
| 4073 | Value *FwdVal; |
| 4074 | if (Ty->isLabelTy()) { |
| 4075 | FwdVal = BasicBlock::Create(Context&: F.getContext(), Name, Parent: &F); |
| 4076 | } else { |
| 4077 | FwdVal = new Argument(Ty, Name); |
| 4078 | } |
| 4079 | if (FwdVal->getName() != Name) { |
| 4080 | P.error(L: Loc, Msg: "name is too long which can result in name collisions, " |
| 4081 | "consider making the name shorter or " |
| 4082 | "increasing -non-global-value-max-name-size" ); |
| 4083 | return nullptr; |
| 4084 | } |
| 4085 | |
| 4086 | ForwardRefVals[Name] = std::make_pair(x&: FwdVal, y&: Loc); |
| 4087 | return FwdVal; |
| 4088 | } |
| 4089 | |
| 4090 | Value *LLParser::PerFunctionState::getVal(unsigned ID, Type *Ty, LocTy Loc) { |
| 4091 | // Look this name up in the normal function symbol table. |
| 4092 | Value *Val = NumberedVals.get(ID); |
| 4093 | |
| 4094 | // If this is a forward reference for the value, see if we already created a |
| 4095 | // forward ref record. |
| 4096 | if (!Val) { |
| 4097 | auto I = ForwardRefValIDs.find(x: ID); |
| 4098 | if (I != ForwardRefValIDs.end()) |
| 4099 | Val = I->second.first; |
| 4100 | } |
| 4101 | |
| 4102 | // If we have the value in the symbol table or fwd-ref table, return it. |
| 4103 | if (Val) |
| 4104 | return P.checkValidVariableType(Loc, Name: "%" + Twine(ID), Ty, Val); |
| 4105 | |
| 4106 | if (!Ty->isFirstClassType()) { |
| 4107 | P.error(L: Loc, Msg: "invalid use of a non-first-class type" ); |
| 4108 | return nullptr; |
| 4109 | } |
| 4110 | |
| 4111 | // Otherwise, create a new forward reference for this value and remember it. |
| 4112 | Value *FwdVal; |
| 4113 | if (Ty->isLabelTy()) { |
| 4114 | FwdVal = BasicBlock::Create(Context&: F.getContext(), Name: "" , Parent: &F); |
| 4115 | } else { |
| 4116 | FwdVal = new Argument(Ty); |
| 4117 | } |
| 4118 | |
| 4119 | ForwardRefValIDs[ID] = std::make_pair(x&: FwdVal, y&: Loc); |
| 4120 | return FwdVal; |
| 4121 | } |
| 4122 | |
| 4123 | /// setInstName - After an instruction is parsed and inserted into its |
| 4124 | /// basic block, this installs its name. |
| 4125 | bool LLParser::PerFunctionState::setInstName(int NameID, |
| 4126 | const std::string &NameStr, |
| 4127 | LocTy NameLoc, Instruction *Inst) { |
| 4128 | // If this instruction has void type, it cannot have a name or ID specified. |
| 4129 | if (Inst->getType()->isVoidTy()) { |
| 4130 | if (NameID != -1 || !NameStr.empty()) |
| 4131 | return P.error(L: NameLoc, Msg: "instructions returning void cannot have a name" ); |
| 4132 | return false; |
| 4133 | } |
| 4134 | |
| 4135 | // If this was a numbered instruction, verify that the instruction is the |
| 4136 | // expected value and resolve any forward references. |
| 4137 | if (NameStr.empty()) { |
| 4138 | // If neither a name nor an ID was specified, just use the next ID. |
| 4139 | if (NameID == -1) |
| 4140 | NameID = NumberedVals.getNext(); |
| 4141 | |
| 4142 | if (P.checkValueID(Loc: NameLoc, Kind: "instruction" , Prefix: "%" , NextID: NumberedVals.getNext(), |
| 4143 | ID: NameID)) |
| 4144 | return true; |
| 4145 | |
| 4146 | auto FI = ForwardRefValIDs.find(x: NameID); |
| 4147 | if (FI != ForwardRefValIDs.end()) { |
| 4148 | Value *Sentinel = FI->second.first; |
| 4149 | if (Sentinel->getType() != Inst->getType()) |
| 4150 | return P.error(L: NameLoc, Msg: "instruction forward referenced with type '" + |
| 4151 | getTypeString(T: FI->second.first->getType()) + |
| 4152 | "'" ); |
| 4153 | |
| 4154 | Sentinel->replaceAllUsesWith(V: Inst); |
| 4155 | Sentinel->deleteValue(); |
| 4156 | ForwardRefValIDs.erase(position: FI); |
| 4157 | } |
| 4158 | |
| 4159 | NumberedVals.add(ID: NameID, V: Inst); |
| 4160 | return false; |
| 4161 | } |
| 4162 | |
| 4163 | // Otherwise, the instruction had a name. Resolve forward refs and set it. |
| 4164 | auto FI = ForwardRefVals.find(x: NameStr); |
| 4165 | if (FI != ForwardRefVals.end()) { |
| 4166 | Value *Sentinel = FI->second.first; |
| 4167 | if (Sentinel->getType() != Inst->getType()) |
| 4168 | return P.error(L: NameLoc, Msg: "instruction forward referenced with type '" + |
| 4169 | getTypeString(T: FI->second.first->getType()) + |
| 4170 | "'" ); |
| 4171 | |
| 4172 | Sentinel->replaceAllUsesWith(V: Inst); |
| 4173 | Sentinel->deleteValue(); |
| 4174 | ForwardRefVals.erase(position: FI); |
| 4175 | } |
| 4176 | |
| 4177 | // Set the name on the instruction. |
| 4178 | Inst->setName(NameStr); |
| 4179 | |
| 4180 | if (Inst->getName() != NameStr) |
| 4181 | return P.error(L: NameLoc, Msg: "multiple definition of local value named '" + |
| 4182 | NameStr + "'" ); |
| 4183 | return false; |
| 4184 | } |
| 4185 | |
| 4186 | /// getBB - Get a basic block with the specified name or ID, creating a |
| 4187 | /// forward reference record if needed. |
| 4188 | BasicBlock *LLParser::PerFunctionState::getBB(const std::string &Name, |
| 4189 | LocTy Loc) { |
| 4190 | return dyn_cast_or_null<BasicBlock>( |
| 4191 | Val: getVal(Name, Ty: Type::getLabelTy(C&: F.getContext()), Loc)); |
| 4192 | } |
| 4193 | |
| 4194 | BasicBlock *LLParser::PerFunctionState::getBB(unsigned ID, LocTy Loc) { |
| 4195 | return dyn_cast_or_null<BasicBlock>( |
| 4196 | Val: getVal(ID, Ty: Type::getLabelTy(C&: F.getContext()), Loc)); |
| 4197 | } |
| 4198 | |
| 4199 | /// defineBB - Define the specified basic block, which is either named or |
| 4200 | /// unnamed. If there is an error, this returns null otherwise it returns |
| 4201 | /// the block being defined. |
| 4202 | BasicBlock *LLParser::PerFunctionState::defineBB(const std::string &Name, |
| 4203 | int NameID, LocTy Loc) { |
| 4204 | BasicBlock *BB; |
| 4205 | if (Name.empty()) { |
| 4206 | if (NameID != -1) { |
| 4207 | if (P.checkValueID(Loc, Kind: "label" , Prefix: "" , NextID: NumberedVals.getNext(), ID: NameID)) |
| 4208 | return nullptr; |
| 4209 | } else { |
| 4210 | NameID = NumberedVals.getNext(); |
| 4211 | } |
| 4212 | BB = getBB(ID: NameID, Loc); |
| 4213 | if (!BB) { |
| 4214 | P.error(L: Loc, Msg: "unable to create block numbered '" + Twine(NameID) + "'" ); |
| 4215 | return nullptr; |
| 4216 | } |
| 4217 | } else { |
| 4218 | BB = getBB(Name, Loc); |
| 4219 | if (!BB) { |
| 4220 | P.error(L: Loc, Msg: "unable to create block named '" + Name + "'" ); |
| 4221 | return nullptr; |
| 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | // Move the block to the end of the function. Forward ref'd blocks are |
| 4226 | // inserted wherever they happen to be referenced. |
| 4227 | F.splice(ToIt: F.end(), FromF: &F, FromIt: BB->getIterator()); |
| 4228 | |
| 4229 | // Remove the block from forward ref sets. |
| 4230 | if (Name.empty()) { |
| 4231 | ForwardRefValIDs.erase(x: NameID); |
| 4232 | NumberedVals.add(ID: NameID, V: BB); |
| 4233 | } else { |
| 4234 | // BB forward references are already in the function symbol table. |
| 4235 | ForwardRefVals.erase(x: Name); |
| 4236 | } |
| 4237 | |
| 4238 | return BB; |
| 4239 | } |
| 4240 | |
| 4241 | //===----------------------------------------------------------------------===// |
| 4242 | // Constants. |
| 4243 | //===----------------------------------------------------------------------===// |
| 4244 | |
| 4245 | /// parseValID - parse an abstract value that doesn't necessarily have a |
| 4246 | /// type implied. For example, if we parse "4" we don't know what integer type |
| 4247 | /// it has. The value will later be combined with its type and checked for |
| 4248 | /// basic correctness. PFS is used to convert function-local operands of |
| 4249 | /// metadata (since metadata operands are not just parsed here but also |
| 4250 | /// converted to values). PFS can be null when we are not parsing metadata |
| 4251 | /// values inside a function. |
| 4252 | bool LLParser::parseValID(ValID &ID, PerFunctionState *PFS, Type *ExpectedTy) { |
| 4253 | ID.Loc = Lex.getLoc(); |
| 4254 | switch (Lex.getKind()) { |
| 4255 | default: |
| 4256 | return tokError(Msg: "expected value token" ); |
| 4257 | case lltok::GlobalID: // @42 |
| 4258 | ID.UIntVal = Lex.getUIntVal(); |
| 4259 | ID.Kind = ValID::t_GlobalID; |
| 4260 | break; |
| 4261 | case lltok::GlobalVar: // @foo |
| 4262 | ID.StrVal = Lex.getStrVal(); |
| 4263 | ID.Kind = ValID::t_GlobalName; |
| 4264 | break; |
| 4265 | case lltok::LocalVarID: // %42 |
| 4266 | ID.UIntVal = Lex.getUIntVal(); |
| 4267 | ID.Kind = ValID::t_LocalID; |
| 4268 | break; |
| 4269 | case lltok::LocalVar: // %foo |
| 4270 | ID.StrVal = Lex.getStrVal(); |
| 4271 | ID.Kind = ValID::t_LocalName; |
| 4272 | break; |
| 4273 | case lltok::APSInt: |
| 4274 | ID.APSIntVal = Lex.getAPSIntVal(); |
| 4275 | ID.Kind = ValID::t_APSInt; |
| 4276 | break; |
| 4277 | case lltok::APFloat: { |
| 4278 | ID.APFloatVal = Lex.getAPFloatVal(); |
| 4279 | ID.Kind = ValID::t_APFloat; |
| 4280 | break; |
| 4281 | } |
| 4282 | case lltok::FloatLiteral: { |
| 4283 | if (!ExpectedTy) |
| 4284 | return error(L: ID.Loc, Msg: "unexpected floating-point literal" ); |
| 4285 | if (!ExpectedTy->isFloatingPointTy()) |
| 4286 | return error(L: ID.Loc, Msg: "floating-point constant invalid for type" ); |
| 4287 | ID.APFloatVal = APFloat(ExpectedTy->getFltSemantics()); |
| 4288 | APFloat::opStatus Except = |
| 4289 | cantFail(ValOrErr: ID.APFloatVal.convertFromString( |
| 4290 | Lex.getStrVal(), RoundingMode::NearestTiesToEven), |
| 4291 | Msg: "Invalid float strings should be caught by the lexer" ); |
| 4292 | // Forbid overflowing and underflowing literals, but permit inexact |
| 4293 | // literals. Underflow is thrown when the result is denormal, so to allow |
| 4294 | // denormals, only reject underflowing literals that resulted in a zero. |
| 4295 | if (Except & APFloat::opOverflow) |
| 4296 | return error(L: ID.Loc, Msg: "floating-point constant overflowed type" ); |
| 4297 | if ((Except & APFloat::opUnderflow) && ID.APFloatVal.isZero()) |
| 4298 | return error(L: ID.Loc, Msg: "floating-point constant underflowed type" ); |
| 4299 | ID.Kind = ValID::t_APFloat; |
| 4300 | break; |
| 4301 | } |
| 4302 | case lltok::FloatHexLiteral: { |
| 4303 | if (!ExpectedTy) |
| 4304 | return error(L: ID.Loc, Msg: "unexpected floating-point literal" ); |
| 4305 | const auto &Semantics = ExpectedTy->getFltSemantics(); |
| 4306 | const APInt &Bits = Lex.getAPSIntVal(); |
| 4307 | if (APFloat::getSizeInBits(Sem: Semantics) != Bits.getBitWidth()) |
| 4308 | return error(L: ID.Loc, Msg: "float hex literal has incorrect number of bits" ); |
| 4309 | ID.APFloatVal = APFloat(Semantics, Bits); |
| 4310 | ID.Kind = ValID::t_APFloat; |
| 4311 | break; |
| 4312 | } |
| 4313 | case lltok::kw_true: |
| 4314 | ID.ConstantVal = ConstantInt::getTrue(Context); |
| 4315 | ID.Kind = ValID::t_Constant; |
| 4316 | break; |
| 4317 | case lltok::kw_false: |
| 4318 | ID.ConstantVal = ConstantInt::getFalse(Context); |
| 4319 | ID.Kind = ValID::t_Constant; |
| 4320 | break; |
| 4321 | case lltok::kw_null: ID.Kind = ValID::t_Null; break; |
| 4322 | case lltok::kw_undef: ID.Kind = ValID::t_Undef; break; |
| 4323 | case lltok::kw_poison: ID.Kind = ValID::t_Poison; break; |
| 4324 | case lltok::kw_zeroinitializer: ID.Kind = ValID::t_Zero; break; |
| 4325 | case lltok::kw_none: ID.Kind = ValID::t_None; break; |
| 4326 | |
| 4327 | case lltok::lbrace: { |
| 4328 | // ValID ::= '{' ConstVector '}' |
| 4329 | Lex.Lex(); |
| 4330 | SmallVector<Constant*, 16> Elts; |
| 4331 | if (parseGlobalValueVector(Elts) || |
| 4332 | parseToken(T: lltok::rbrace, ErrMsg: "expected end of struct constant" )) |
| 4333 | return true; |
| 4334 | |
| 4335 | ID.ConstantStructElts = std::make_unique<Constant *[]>(num: Elts.size()); |
| 4336 | ID.UIntVal = Elts.size(); |
| 4337 | memcpy(dest: ID.ConstantStructElts.get(), src: Elts.data(), |
| 4338 | n: Elts.size() * sizeof(Elts[0])); |
| 4339 | ID.Kind = ValID::t_ConstantStruct; |
| 4340 | return false; |
| 4341 | } |
| 4342 | case lltok::less: { |
| 4343 | // ValID ::= '<' ConstVector '>' --> Vector. |
| 4344 | // ValID ::= '<' '{' ConstVector '}' '>' --> Packed Struct. |
| 4345 | Lex.Lex(); |
| 4346 | bool isPackedStruct = EatIfPresent(T: lltok::lbrace); |
| 4347 | |
| 4348 | SmallVector<Constant*, 16> Elts; |
| 4349 | LocTy FirstEltLoc = Lex.getLoc(); |
| 4350 | if (parseGlobalValueVector(Elts) || |
| 4351 | (isPackedStruct && |
| 4352 | parseToken(T: lltok::rbrace, ErrMsg: "expected end of packed struct" )) || |
| 4353 | parseToken(T: lltok::greater, ErrMsg: "expected end of constant" )) |
| 4354 | return true; |
| 4355 | |
| 4356 | if (isPackedStruct) { |
| 4357 | ID.ConstantStructElts = std::make_unique<Constant *[]>(num: Elts.size()); |
| 4358 | memcpy(dest: ID.ConstantStructElts.get(), src: Elts.data(), |
| 4359 | n: Elts.size() * sizeof(Elts[0])); |
| 4360 | ID.UIntVal = Elts.size(); |
| 4361 | ID.Kind = ValID::t_PackedConstantStruct; |
| 4362 | return false; |
| 4363 | } |
| 4364 | |
| 4365 | if (Elts.empty()) |
| 4366 | return error(L: ID.Loc, Msg: "constant vector must not be empty" ); |
| 4367 | |
| 4368 | if (!Elts[0]->getType()->isIntegerTy() && !Elts[0]->getType()->isByteTy() && |
| 4369 | !Elts[0]->getType()->isFloatingPointTy() && |
| 4370 | !Elts[0]->getType()->isPointerTy()) |
| 4371 | return error( |
| 4372 | L: FirstEltLoc, |
| 4373 | Msg: "vector elements must have integer, byte, pointer or floating point " |
| 4374 | "type" ); |
| 4375 | |
| 4376 | // Verify that all the vector elements have the same type. |
| 4377 | for (unsigned i = 1, e = Elts.size(); i != e; ++i) |
| 4378 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 4379 | return error(L: FirstEltLoc, Msg: "vector element #" + Twine(i) + |
| 4380 | " is not of type '" + |
| 4381 | getTypeString(T: Elts[0]->getType())); |
| 4382 | |
| 4383 | ID.ConstantVal = ConstantVector::get(V: Elts); |
| 4384 | ID.Kind = ValID::t_Constant; |
| 4385 | return false; |
| 4386 | } |
| 4387 | case lltok::lsquare: { // Array Constant |
| 4388 | Lex.Lex(); |
| 4389 | SmallVector<Constant*, 16> Elts; |
| 4390 | LocTy FirstEltLoc = Lex.getLoc(); |
| 4391 | if (parseGlobalValueVector(Elts) || |
| 4392 | parseToken(T: lltok::rsquare, ErrMsg: "expected end of array constant" )) |
| 4393 | return true; |
| 4394 | |
| 4395 | // Handle empty element. |
| 4396 | if (Elts.empty()) { |
| 4397 | // Use undef instead of an array because it's inconvenient to determine |
| 4398 | // the element type at this point, there being no elements to examine. |
| 4399 | ID.Kind = ValID::t_EmptyArray; |
| 4400 | return false; |
| 4401 | } |
| 4402 | |
| 4403 | if (!Elts[0]->getType()->isFirstClassType()) |
| 4404 | return error(L: FirstEltLoc, Msg: "invalid array element type: " + |
| 4405 | getTypeString(T: Elts[0]->getType())); |
| 4406 | |
| 4407 | ArrayType *ATy = ArrayType::get(ElementType: Elts[0]->getType(), NumElements: Elts.size()); |
| 4408 | |
| 4409 | // Verify all elements are correct type! |
| 4410 | for (unsigned i = 0, e = Elts.size(); i != e; ++i) { |
| 4411 | if (Elts[i]->getType() != Elts[0]->getType()) |
| 4412 | return error(L: FirstEltLoc, Msg: "array element #" + Twine(i) + |
| 4413 | " is not of type '" + |
| 4414 | getTypeString(T: Elts[0]->getType())); |
| 4415 | } |
| 4416 | |
| 4417 | ID.ConstantVal = ConstantArray::get(T: ATy, V: Elts); |
| 4418 | ID.Kind = ValID::t_Constant; |
| 4419 | return false; |
| 4420 | } |
| 4421 | case lltok::kw_c: { // c "foo" |
| 4422 | Lex.Lex(); |
| 4423 | ArrayType *ATy = cast<ArrayType>(Val: ExpectedTy); |
| 4424 | ID.ConstantVal = ConstantDataArray::getString( |
| 4425 | Context, Initializer: Lex.getStrVal(), AddNull: false, ByteString: ATy->getElementType()->isByteTy()); |
| 4426 | if (parseToken(T: lltok::StringConstant, ErrMsg: "expected string" )) |
| 4427 | return true; |
| 4428 | ID.Kind = ValID::t_Constant; |
| 4429 | return false; |
| 4430 | } |
| 4431 | case lltok::kw_asm: { |
| 4432 | // ValID ::= 'asm' SideEffect? AlignStack? IntelDialect? STRINGCONSTANT ',' |
| 4433 | // STRINGCONSTANT |
| 4434 | bool HasSideEffect, AlignStack, AsmDialect, CanThrow; |
| 4435 | Lex.Lex(); |
| 4436 | if (parseOptionalToken(T: lltok::kw_sideeffect, Present&: HasSideEffect) || |
| 4437 | parseOptionalToken(T: lltok::kw_alignstack, Present&: AlignStack) || |
| 4438 | parseOptionalToken(T: lltok::kw_inteldialect, Present&: AsmDialect) || |
| 4439 | parseOptionalToken(T: lltok::kw_unwind, Present&: CanThrow) || |
| 4440 | parseStringConstant(Result&: ID.StrVal) || |
| 4441 | parseToken(T: lltok::comma, ErrMsg: "expected comma in inline asm expression" ) || |
| 4442 | parseToken(T: lltok::StringConstant, ErrMsg: "expected constraint string" )) |
| 4443 | return true; |
| 4444 | ID.StrVal2 = Lex.getStrVal(); |
| 4445 | ID.UIntVal = unsigned(HasSideEffect) | (unsigned(AlignStack) << 1) | |
| 4446 | (unsigned(AsmDialect) << 2) | (unsigned(CanThrow) << 3); |
| 4447 | ID.Kind = ValID::t_InlineAsm; |
| 4448 | return false; |
| 4449 | } |
| 4450 | |
| 4451 | case lltok::kw_blockaddress: { |
| 4452 | // ValID ::= 'blockaddress' '(' @foo ',' %bar ')' |
| 4453 | Lex.Lex(); |
| 4454 | |
| 4455 | ValID Fn, Label; |
| 4456 | |
| 4457 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in block address expression" ) || |
| 4458 | parseValID(ID&: Fn, PFS) || |
| 4459 | parseToken(T: lltok::comma, |
| 4460 | ErrMsg: "expected comma in block address expression" ) || |
| 4461 | parseValID(ID&: Label, PFS) || |
| 4462 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in block address expression" )) |
| 4463 | return true; |
| 4464 | |
| 4465 | if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) |
| 4466 | return error(L: Fn.Loc, Msg: "expected function name in blockaddress" ); |
| 4467 | if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName) |
| 4468 | return error(L: Label.Loc, Msg: "expected basic block name in blockaddress" ); |
| 4469 | |
| 4470 | // Try to find the function (but skip it if it's forward-referenced). |
| 4471 | GlobalValue *GV = nullptr; |
| 4472 | if (Fn.Kind == ValID::t_GlobalID) { |
| 4473 | GV = NumberedVals.get(ID: Fn.UIntVal); |
| 4474 | } else if (!ForwardRefVals.count(x: Fn.StrVal)) { |
| 4475 | GV = M->getNamedValue(Name: Fn.StrVal); |
| 4476 | } |
| 4477 | Function *F = nullptr; |
| 4478 | if (GV) { |
| 4479 | // Confirm that it's actually a function with a definition. |
| 4480 | if (!isa<Function>(Val: GV)) |
| 4481 | return error(L: Fn.Loc, Msg: "expected function name in blockaddress" ); |
| 4482 | F = cast<Function>(Val: GV); |
| 4483 | if (F->isDeclaration()) |
| 4484 | return error(L: Fn.Loc, Msg: "cannot take blockaddress inside a declaration" ); |
| 4485 | } |
| 4486 | |
| 4487 | if (!F) { |
| 4488 | // Make a global variable as a placeholder for this reference. |
| 4489 | GlobalValue *&FwdRef = |
| 4490 | ForwardRefBlockAddresses[std::move(Fn)][std::move(Label)]; |
| 4491 | if (!FwdRef) { |
| 4492 | unsigned FwdDeclAS; |
| 4493 | if (ExpectedTy) { |
| 4494 | // If we know the type that the blockaddress is being assigned to, |
| 4495 | // we can use the address space of that type. |
| 4496 | if (!ExpectedTy->isPointerTy()) |
| 4497 | return error(L: ID.Loc, |
| 4498 | Msg: "type of blockaddress must be a pointer and not '" + |
| 4499 | getTypeString(T: ExpectedTy) + "'" ); |
| 4500 | FwdDeclAS = ExpectedTy->getPointerAddressSpace(); |
| 4501 | } else if (PFS) { |
| 4502 | // Otherwise, we default the address space of the current function. |
| 4503 | FwdDeclAS = PFS->getFunction().getAddressSpace(); |
| 4504 | } else { |
| 4505 | llvm_unreachable("Unknown address space for blockaddress" ); |
| 4506 | } |
| 4507 | FwdRef = new GlobalVariable( |
| 4508 | *M, Type::getInt8Ty(C&: Context), false, GlobalValue::InternalLinkage, |
| 4509 | nullptr, "" , nullptr, GlobalValue::NotThreadLocal, FwdDeclAS); |
| 4510 | } |
| 4511 | |
| 4512 | ID.ConstantVal = FwdRef; |
| 4513 | ID.Kind = ValID::t_Constant; |
| 4514 | return false; |
| 4515 | } |
| 4516 | |
| 4517 | // We found the function; now find the basic block. Don't use PFS, since we |
| 4518 | // might be inside a constant expression. |
| 4519 | BasicBlock *BB; |
| 4520 | if (BlockAddressPFS && F == &BlockAddressPFS->getFunction()) { |
| 4521 | if (Label.Kind == ValID::t_LocalID) |
| 4522 | BB = BlockAddressPFS->getBB(ID: Label.UIntVal, Loc: Label.Loc); |
| 4523 | else |
| 4524 | BB = BlockAddressPFS->getBB(Name: Label.StrVal, Loc: Label.Loc); |
| 4525 | if (!BB) |
| 4526 | return error(L: Label.Loc, Msg: "referenced value is not a basic block" ); |
| 4527 | } else { |
| 4528 | if (Label.Kind == ValID::t_LocalID) |
| 4529 | return error(L: Label.Loc, Msg: "cannot take address of numeric label after " |
| 4530 | "the function is defined" ); |
| 4531 | BB = dyn_cast_or_null<BasicBlock>( |
| 4532 | Val: F->getValueSymbolTable()->lookup(Name: Label.StrVal)); |
| 4533 | if (!BB) |
| 4534 | return error(L: Label.Loc, Msg: "referenced value is not a basic block" ); |
| 4535 | } |
| 4536 | |
| 4537 | ID.ConstantVal = BlockAddress::get(F, BB); |
| 4538 | ID.Kind = ValID::t_Constant; |
| 4539 | return false; |
| 4540 | } |
| 4541 | |
| 4542 | case lltok::kw_dso_local_equivalent: { |
| 4543 | // ValID ::= 'dso_local_equivalent' @foo |
| 4544 | Lex.Lex(); |
| 4545 | |
| 4546 | ValID Fn; |
| 4547 | |
| 4548 | if (parseValID(ID&: Fn, PFS)) |
| 4549 | return true; |
| 4550 | |
| 4551 | if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName) |
| 4552 | return error(L: Fn.Loc, |
| 4553 | Msg: "expected global value name in dso_local_equivalent" ); |
| 4554 | |
| 4555 | // Try to find the function (but skip it if it's forward-referenced). |
| 4556 | GlobalValue *GV = nullptr; |
| 4557 | if (Fn.Kind == ValID::t_GlobalID) { |
| 4558 | GV = NumberedVals.get(ID: Fn.UIntVal); |
| 4559 | } else if (!ForwardRefVals.count(x: Fn.StrVal)) { |
| 4560 | GV = M->getNamedValue(Name: Fn.StrVal); |
| 4561 | } |
| 4562 | |
| 4563 | if (!GV) { |
| 4564 | // Make a placeholder global variable as a placeholder for this reference. |
| 4565 | auto &FwdRefMap = (Fn.Kind == ValID::t_GlobalID) |
| 4566 | ? ForwardRefDSOLocalEquivalentIDs |
| 4567 | : ForwardRefDSOLocalEquivalentNames; |
| 4568 | GlobalValue *&FwdRef = FwdRefMap[Fn]; |
| 4569 | if (!FwdRef) { |
| 4570 | FwdRef = new GlobalVariable(*M, Type::getInt8Ty(C&: Context), false, |
| 4571 | GlobalValue::InternalLinkage, nullptr, "" , |
| 4572 | nullptr, GlobalValue::NotThreadLocal); |
| 4573 | } |
| 4574 | |
| 4575 | ID.ConstantVal = FwdRef; |
| 4576 | ID.Kind = ValID::t_Constant; |
| 4577 | return false; |
| 4578 | } |
| 4579 | |
| 4580 | if (!GV->getValueType()->isFunctionTy()) |
| 4581 | return error(L: Fn.Loc, Msg: "expected a function, alias to function, or ifunc " |
| 4582 | "in dso_local_equivalent" ); |
| 4583 | |
| 4584 | ID.ConstantVal = DSOLocalEquivalent::get(GV); |
| 4585 | ID.Kind = ValID::t_Constant; |
| 4586 | return false; |
| 4587 | } |
| 4588 | |
| 4589 | case lltok::kw_no_cfi: { |
| 4590 | // ValID ::= 'no_cfi' @foo |
| 4591 | Lex.Lex(); |
| 4592 | |
| 4593 | if (parseValID(ID, PFS)) |
| 4594 | return true; |
| 4595 | |
| 4596 | if (ID.Kind != ValID::t_GlobalID && ID.Kind != ValID::t_GlobalName) |
| 4597 | return error(L: ID.Loc, Msg: "expected global value name in no_cfi" ); |
| 4598 | |
| 4599 | ID.NoCFI = true; |
| 4600 | return false; |
| 4601 | } |
| 4602 | case lltok::kw_ptrauth: { |
| 4603 | // ValID ::= 'ptrauth' '(' ptr @foo ',' i32 <key> |
| 4604 | // (',' i64 <disc> (',' ptr addrdisc (',' ptr ds)? |
| 4605 | // )? )? ')' |
| 4606 | Lex.Lex(); |
| 4607 | |
| 4608 | Constant *Ptr, *Key; |
| 4609 | Constant *Disc = nullptr, *AddrDisc = nullptr, |
| 4610 | *DeactivationSymbol = nullptr; |
| 4611 | |
| 4612 | if (parseToken(T: lltok::lparen, |
| 4613 | ErrMsg: "expected '(' in constant ptrauth expression" ) || |
| 4614 | parseGlobalTypeAndValue(V&: Ptr) || |
| 4615 | parseToken(T: lltok::comma, |
| 4616 | ErrMsg: "expected comma in constant ptrauth expression" ) || |
| 4617 | parseGlobalTypeAndValue(V&: Key)) |
| 4618 | return true; |
| 4619 | // If present, parse the optional disc/addrdisc/ds. |
| 4620 | if (EatIfPresent(T: lltok::comma) && parseGlobalTypeAndValue(V&: Disc)) |
| 4621 | return true; |
| 4622 | if (EatIfPresent(T: lltok::comma) && parseGlobalTypeAndValue(V&: AddrDisc)) |
| 4623 | return true; |
| 4624 | if (EatIfPresent(T: lltok::comma) && |
| 4625 | parseGlobalTypeAndValue(V&: DeactivationSymbol)) |
| 4626 | return true; |
| 4627 | if (parseToken(T: lltok::rparen, |
| 4628 | ErrMsg: "expected ')' in constant ptrauth expression" )) |
| 4629 | return true; |
| 4630 | |
| 4631 | if (!Ptr->getType()->isPointerTy()) |
| 4632 | return error(L: ID.Loc, Msg: "constant ptrauth base pointer must be a pointer" ); |
| 4633 | |
| 4634 | auto *KeyC = dyn_cast<ConstantInt>(Val: Key); |
| 4635 | if (!KeyC || KeyC->getBitWidth() != 32) |
| 4636 | return error(L: ID.Loc, Msg: "constant ptrauth key must be i32 constant" ); |
| 4637 | |
| 4638 | ConstantInt *DiscC = nullptr; |
| 4639 | if (Disc) { |
| 4640 | DiscC = dyn_cast<ConstantInt>(Val: Disc); |
| 4641 | if (!DiscC || DiscC->getBitWidth() != 64) |
| 4642 | return error( |
| 4643 | L: ID.Loc, |
| 4644 | Msg: "constant ptrauth integer discriminator must be i64 constant" ); |
| 4645 | } else { |
| 4646 | DiscC = ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: 0); |
| 4647 | } |
| 4648 | |
| 4649 | if (AddrDisc) { |
| 4650 | if (!AddrDisc->getType()->isPointerTy()) |
| 4651 | return error( |
| 4652 | L: ID.Loc, Msg: "constant ptrauth address discriminator must be a pointer" ); |
| 4653 | } else { |
| 4654 | AddrDisc = ConstantPointerNull::get(T: PointerType::get(C&: Context, AddressSpace: 0)); |
| 4655 | } |
| 4656 | |
| 4657 | if (!DeactivationSymbol) |
| 4658 | DeactivationSymbol = |
| 4659 | ConstantPointerNull::get(T: PointerType::get(C&: Context, AddressSpace: 0)); |
| 4660 | if (!DeactivationSymbol->getType()->isPointerTy()) |
| 4661 | return error(L: ID.Loc, |
| 4662 | Msg: "constant ptrauth deactivation symbol must be a pointer" ); |
| 4663 | |
| 4664 | ID.ConstantVal = |
| 4665 | ConstantPtrAuth::get(Ptr, Key: KeyC, Disc: DiscC, AddrDisc, DeactivationSymbol); |
| 4666 | ID.Kind = ValID::t_Constant; |
| 4667 | return false; |
| 4668 | } |
| 4669 | |
| 4670 | case lltok::kw_trunc: |
| 4671 | case lltok::kw_bitcast: |
| 4672 | case lltok::kw_addrspacecast: |
| 4673 | case lltok::kw_inttoptr: |
| 4674 | case lltok::kw_ptrtoaddr: |
| 4675 | case lltok::kw_ptrtoint: { |
| 4676 | unsigned Opc = Lex.getUIntVal(); |
| 4677 | Type *DestTy = nullptr; |
| 4678 | Constant *SrcVal; |
| 4679 | Lex.Lex(); |
| 4680 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' after constantexpr cast" ) || |
| 4681 | parseGlobalTypeAndValue(V&: SrcVal) || |
| 4682 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in constantexpr cast" ) || |
| 4683 | parseType(Result&: DestTy) || |
| 4684 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of constantexpr cast" )) |
| 4685 | return true; |
| 4686 | if (!CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: SrcVal, DstTy: DestTy)) |
| 4687 | return error(L: ID.Loc, Msg: "invalid cast opcode for cast from '" + |
| 4688 | getTypeString(T: SrcVal->getType()) + "' to '" + |
| 4689 | getTypeString(T: DestTy) + "'" ); |
| 4690 | ID.ConstantVal = ConstantExpr::getCast(ops: (Instruction::CastOps)Opc, |
| 4691 | C: SrcVal, Ty: DestTy); |
| 4692 | ID.Kind = ValID::t_Constant; |
| 4693 | return false; |
| 4694 | } |
| 4695 | case lltok::kw_extractvalue: |
| 4696 | return error(L: ID.Loc, Msg: "extractvalue constexprs are no longer supported" ); |
| 4697 | case lltok::kw_insertvalue: |
| 4698 | return error(L: ID.Loc, Msg: "insertvalue constexprs are no longer supported" ); |
| 4699 | case lltok::kw_udiv: |
| 4700 | return error(L: ID.Loc, Msg: "udiv constexprs are no longer supported" ); |
| 4701 | case lltok::kw_sdiv: |
| 4702 | return error(L: ID.Loc, Msg: "sdiv constexprs are no longer supported" ); |
| 4703 | case lltok::kw_urem: |
| 4704 | return error(L: ID.Loc, Msg: "urem constexprs are no longer supported" ); |
| 4705 | case lltok::kw_srem: |
| 4706 | return error(L: ID.Loc, Msg: "srem constexprs are no longer supported" ); |
| 4707 | case lltok::kw_fadd: |
| 4708 | return error(L: ID.Loc, Msg: "fadd constexprs are no longer supported" ); |
| 4709 | case lltok::kw_fsub: |
| 4710 | return error(L: ID.Loc, Msg: "fsub constexprs are no longer supported" ); |
| 4711 | case lltok::kw_fmul: |
| 4712 | return error(L: ID.Loc, Msg: "fmul constexprs are no longer supported" ); |
| 4713 | case lltok::kw_fdiv: |
| 4714 | return error(L: ID.Loc, Msg: "fdiv constexprs are no longer supported" ); |
| 4715 | case lltok::kw_frem: |
| 4716 | return error(L: ID.Loc, Msg: "frem constexprs are no longer supported" ); |
| 4717 | case lltok::kw_and: |
| 4718 | return error(L: ID.Loc, Msg: "and constexprs are no longer supported" ); |
| 4719 | case lltok::kw_or: |
| 4720 | return error(L: ID.Loc, Msg: "or constexprs are no longer supported" ); |
| 4721 | case lltok::kw_lshr: |
| 4722 | return error(L: ID.Loc, Msg: "lshr constexprs are no longer supported" ); |
| 4723 | case lltok::kw_ashr: |
| 4724 | return error(L: ID.Loc, Msg: "ashr constexprs are no longer supported" ); |
| 4725 | case lltok::kw_shl: |
| 4726 | return error(L: ID.Loc, Msg: "shl constexprs are no longer supported" ); |
| 4727 | case lltok::kw_mul: |
| 4728 | return error(L: ID.Loc, Msg: "mul constexprs are no longer supported" ); |
| 4729 | case lltok::kw_fneg: |
| 4730 | return error(L: ID.Loc, Msg: "fneg constexprs are no longer supported" ); |
| 4731 | case lltok::kw_select: |
| 4732 | return error(L: ID.Loc, Msg: "select constexprs are no longer supported" ); |
| 4733 | case lltok::kw_zext: |
| 4734 | return error(L: ID.Loc, Msg: "zext constexprs are no longer supported" ); |
| 4735 | case lltok::kw_sext: |
| 4736 | return error(L: ID.Loc, Msg: "sext constexprs are no longer supported" ); |
| 4737 | case lltok::kw_fptrunc: |
| 4738 | return error(L: ID.Loc, Msg: "fptrunc constexprs are no longer supported" ); |
| 4739 | case lltok::kw_fpext: |
| 4740 | return error(L: ID.Loc, Msg: "fpext constexprs are no longer supported" ); |
| 4741 | case lltok::kw_uitofp: |
| 4742 | return error(L: ID.Loc, Msg: "uitofp constexprs are no longer supported" ); |
| 4743 | case lltok::kw_sitofp: |
| 4744 | return error(L: ID.Loc, Msg: "sitofp constexprs are no longer supported" ); |
| 4745 | case lltok::kw_fptoui: |
| 4746 | return error(L: ID.Loc, Msg: "fptoui constexprs are no longer supported" ); |
| 4747 | case lltok::kw_fptosi: |
| 4748 | return error(L: ID.Loc, Msg: "fptosi constexprs are no longer supported" ); |
| 4749 | case lltok::kw_icmp: |
| 4750 | return error(L: ID.Loc, Msg: "icmp constexprs are no longer supported" ); |
| 4751 | case lltok::kw_fcmp: |
| 4752 | return error(L: ID.Loc, Msg: "fcmp constexprs are no longer supported" ); |
| 4753 | |
| 4754 | // Binary Operators. |
| 4755 | case lltok::kw_add: |
| 4756 | case lltok::kw_sub: |
| 4757 | case lltok::kw_xor: { |
| 4758 | bool NUW = false; |
| 4759 | bool NSW = false; |
| 4760 | unsigned Opc = Lex.getUIntVal(); |
| 4761 | Constant *Val0, *Val1; |
| 4762 | Lex.Lex(); |
| 4763 | if (Opc == Instruction::Add || Opc == Instruction::Sub || |
| 4764 | Opc == Instruction::Mul) { |
| 4765 | if (EatIfPresent(T: lltok::kw_nuw)) |
| 4766 | NUW = true; |
| 4767 | if (EatIfPresent(T: lltok::kw_nsw)) { |
| 4768 | NSW = true; |
| 4769 | if (EatIfPresent(T: lltok::kw_nuw)) |
| 4770 | NUW = true; |
| 4771 | } |
| 4772 | } |
| 4773 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in binary constantexpr" ) || |
| 4774 | parseGlobalTypeAndValue(V&: Val0) || |
| 4775 | parseToken(T: lltok::comma, ErrMsg: "expected comma in binary constantexpr" ) || |
| 4776 | parseGlobalTypeAndValue(V&: Val1) || |
| 4777 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in binary constantexpr" )) |
| 4778 | return true; |
| 4779 | if (Val0->getType() != Val1->getType()) |
| 4780 | return error(L: ID.Loc, Msg: "operands of constexpr must have same type" ); |
| 4781 | // Check that the type is valid for the operator. |
| 4782 | if (!Val0->getType()->isIntOrIntVectorTy()) |
| 4783 | return error(L: ID.Loc, |
| 4784 | Msg: "constexpr requires integer or integer vector operands" ); |
| 4785 | unsigned Flags = 0; |
| 4786 | if (NUW) Flags |= OverflowingBinaryOperator::NoUnsignedWrap; |
| 4787 | if (NSW) Flags |= OverflowingBinaryOperator::NoSignedWrap; |
| 4788 | ID.ConstantVal = ConstantExpr::get(Opcode: Opc, C1: Val0, C2: Val1, Flags); |
| 4789 | ID.Kind = ValID::t_Constant; |
| 4790 | return false; |
| 4791 | } |
| 4792 | |
| 4793 | case lltok::kw_splat: { |
| 4794 | Lex.Lex(); |
| 4795 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' after vector splat" )) |
| 4796 | return true; |
| 4797 | Constant *C; |
| 4798 | if (parseGlobalTypeAndValue(V&: C)) |
| 4799 | return true; |
| 4800 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' at end of vector splat" )) |
| 4801 | return true; |
| 4802 | |
| 4803 | ID.ConstantVal = C; |
| 4804 | ID.Kind = ValID::t_ConstantSplat; |
| 4805 | return false; |
| 4806 | } |
| 4807 | |
| 4808 | case lltok::kw_getelementptr: |
| 4809 | case lltok::kw_shufflevector: |
| 4810 | case lltok::kw_insertelement: |
| 4811 | case lltok::kw_extractelement: { |
| 4812 | unsigned Opc = Lex.getUIntVal(); |
| 4813 | SmallVector<Constant*, 16> Elts; |
| 4814 | GEPNoWrapFlags NW; |
| 4815 | bool HasInRange = false; |
| 4816 | APSInt InRangeStart; |
| 4817 | APSInt InRangeEnd; |
| 4818 | Type *Ty; |
| 4819 | Lex.Lex(); |
| 4820 | |
| 4821 | if (Opc == Instruction::GetElementPtr) { |
| 4822 | while (true) { |
| 4823 | if (EatIfPresent(T: lltok::kw_inbounds)) |
| 4824 | NW |= GEPNoWrapFlags::inBounds(); |
| 4825 | else if (EatIfPresent(T: lltok::kw_nusw)) |
| 4826 | NW |= GEPNoWrapFlags::noUnsignedSignedWrap(); |
| 4827 | else if (EatIfPresent(T: lltok::kw_nuw)) |
| 4828 | NW |= GEPNoWrapFlags::noUnsignedWrap(); |
| 4829 | else |
| 4830 | break; |
| 4831 | } |
| 4832 | |
| 4833 | if (EatIfPresent(T: lltok::kw_inrange)) { |
| 4834 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '('" )) |
| 4835 | return true; |
| 4836 | if (Lex.getKind() != lltok::APSInt) |
| 4837 | return tokError(Msg: "expected integer" ); |
| 4838 | InRangeStart = Lex.getAPSIntVal(); |
| 4839 | Lex.Lex(); |
| 4840 | if (parseToken(T: lltok::comma, ErrMsg: "expected ','" )) |
| 4841 | return true; |
| 4842 | if (Lex.getKind() != lltok::APSInt) |
| 4843 | return tokError(Msg: "expected integer" ); |
| 4844 | InRangeEnd = Lex.getAPSIntVal(); |
| 4845 | Lex.Lex(); |
| 4846 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')'" )) |
| 4847 | return true; |
| 4848 | HasInRange = true; |
| 4849 | } |
| 4850 | } |
| 4851 | |
| 4852 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in constantexpr" )) |
| 4853 | return true; |
| 4854 | |
| 4855 | if (Opc == Instruction::GetElementPtr) { |
| 4856 | if (parseType(Result&: Ty) || |
| 4857 | parseToken(T: lltok::comma, ErrMsg: "expected comma after getelementptr's type" )) |
| 4858 | return true; |
| 4859 | } |
| 4860 | |
| 4861 | if (parseGlobalValueVector(Elts) || |
| 4862 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' in constantexpr" )) |
| 4863 | return true; |
| 4864 | |
| 4865 | if (Opc == Instruction::GetElementPtr) { |
| 4866 | if (Elts.size() == 0 || |
| 4867 | !Elts[0]->getType()->isPtrOrPtrVectorTy()) |
| 4868 | return error(L: ID.Loc, Msg: "base of getelementptr must be a pointer" ); |
| 4869 | |
| 4870 | Type *BaseType = Elts[0]->getType(); |
| 4871 | std::optional<ConstantRange> InRange; |
| 4872 | if (HasInRange) { |
| 4873 | unsigned IndexWidth = |
| 4874 | M->getDataLayout().getIndexTypeSizeInBits(Ty: BaseType); |
| 4875 | InRangeStart = InRangeStart.extOrTrunc(width: IndexWidth); |
| 4876 | InRangeEnd = InRangeEnd.extOrTrunc(width: IndexWidth); |
| 4877 | if (InRangeStart.sge(RHS: InRangeEnd)) |
| 4878 | return error(L: ID.Loc, Msg: "expected end to be larger than start" ); |
| 4879 | InRange = ConstantRange::getNonEmpty(Lower: InRangeStart, Upper: InRangeEnd); |
| 4880 | } |
| 4881 | |
| 4882 | unsigned GEPWidth = |
| 4883 | BaseType->isVectorTy() |
| 4884 | ? cast<FixedVectorType>(Val: BaseType)->getNumElements() |
| 4885 | : 0; |
| 4886 | |
| 4887 | ArrayRef<Constant *> Indices(Elts.begin() + 1, Elts.end()); |
| 4888 | for (Constant *Val : Indices) { |
| 4889 | Type *ValTy = Val->getType(); |
| 4890 | if (!ValTy->isIntOrIntVectorTy()) |
| 4891 | return error(L: ID.Loc, Msg: "getelementptr index must be an integer" ); |
| 4892 | if (auto *ValVTy = dyn_cast<VectorType>(Val: ValTy)) { |
| 4893 | unsigned ValNumEl = cast<FixedVectorType>(Val: ValVTy)->getNumElements(); |
| 4894 | if (GEPWidth && (ValNumEl != GEPWidth)) |
| 4895 | return error( |
| 4896 | L: ID.Loc, |
| 4897 | Msg: "getelementptr vector index has a wrong number of elements" ); |
| 4898 | // GEPWidth may have been unknown because the base is a scalar, |
| 4899 | // but it is known now. |
| 4900 | GEPWidth = ValNumEl; |
| 4901 | } |
| 4902 | } |
| 4903 | |
| 4904 | if (!Indices.empty() && !Ty->isSized()) |
| 4905 | return error(L: ID.Loc, Msg: "base element of getelementptr must be sized" ); |
| 4906 | |
| 4907 | if (!ConstantExpr::isSupportedGetElementPtr(SrcElemTy: Ty)) |
| 4908 | return error(L: ID.Loc, Msg: "invalid base element for constant getelementptr" ); |
| 4909 | |
| 4910 | if (!GetElementPtrInst::getIndexedType(Ty, IdxList: Indices)) |
| 4911 | return error(L: ID.Loc, Msg: "invalid getelementptr indices" ); |
| 4912 | |
| 4913 | LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH |
| 4914 | ID.ConstantVal = |
| 4915 | ConstantExpr::getGetElementPtr(Ty, C: Elts[0], IdxList: Indices, NW, InRange); |
| 4916 | LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP |
| 4917 | } else if (Opc == Instruction::ShuffleVector) { |
| 4918 | if (Elts.size() != 3) |
| 4919 | return error(L: ID.Loc, Msg: "expected three operands to shufflevector" ); |
| 4920 | if (!ShuffleVectorInst::isValidOperands(V1: Elts[0], V2: Elts[1], Mask: Elts[2])) |
| 4921 | return error(L: ID.Loc, Msg: "invalid operands to shufflevector" ); |
| 4922 | SmallVector<int, 16> Mask; |
| 4923 | ShuffleVectorInst::getShuffleMask(Mask: cast<Constant>(Val: Elts[2]), Result&: Mask); |
| 4924 | ID.ConstantVal = ConstantExpr::getShuffleVector(V1: Elts[0], V2: Elts[1], Mask); |
| 4925 | } else if (Opc == Instruction::ExtractElement) { |
| 4926 | if (Elts.size() != 2) |
| 4927 | return error(L: ID.Loc, Msg: "expected two operands to extractelement" ); |
| 4928 | if (!ExtractElementInst::isValidOperands(Vec: Elts[0], Idx: Elts[1])) |
| 4929 | return error(L: ID.Loc, Msg: "invalid extractelement operands" ); |
| 4930 | ID.ConstantVal = ConstantExpr::getExtractElement(Vec: Elts[0], Idx: Elts[1]); |
| 4931 | } else { |
| 4932 | assert(Opc == Instruction::InsertElement && "Unknown opcode" ); |
| 4933 | if (Elts.size() != 3) |
| 4934 | return error(L: ID.Loc, Msg: "expected three operands to insertelement" ); |
| 4935 | if (!InsertElementInst::isValidOperands(Vec: Elts[0], NewElt: Elts[1], Idx: Elts[2])) |
| 4936 | return error(L: ID.Loc, Msg: "invalid insertelement operands" ); |
| 4937 | ID.ConstantVal = |
| 4938 | ConstantExpr::getInsertElement(Vec: Elts[0], Elt: Elts[1],Idx: Elts[2]); |
| 4939 | } |
| 4940 | |
| 4941 | ID.Kind = ValID::t_Constant; |
| 4942 | return false; |
| 4943 | } |
| 4944 | } |
| 4945 | |
| 4946 | Lex.Lex(); |
| 4947 | return false; |
| 4948 | } |
| 4949 | |
| 4950 | /// parseGlobalValue - parse a global value with the specified type. |
| 4951 | bool LLParser::parseGlobalValue(Type *Ty, Constant *&C) { |
| 4952 | C = nullptr; |
| 4953 | ValID ID; |
| 4954 | Value *V = nullptr; |
| 4955 | bool Parsed = parseValID(ID, /*PFS=*/nullptr, ExpectedTy: Ty) || |
| 4956 | convertValIDToValue(Ty, ID, V, PFS: nullptr); |
| 4957 | if (V && !(C = dyn_cast<Constant>(Val: V))) |
| 4958 | return error(L: ID.Loc, Msg: "global values must be constants" ); |
| 4959 | return Parsed; |
| 4960 | } |
| 4961 | |
| 4962 | bool LLParser::parseGlobalTypeAndValue(Constant *&V) { |
| 4963 | Type *Ty = nullptr; |
| 4964 | return parseType(Result&: Ty) || parseGlobalValue(Ty, C&: V); |
| 4965 | } |
| 4966 | |
| 4967 | bool LLParser::parseOptionalComdat(StringRef GlobalName, Comdat *&C) { |
| 4968 | C = nullptr; |
| 4969 | |
| 4970 | LocTy KwLoc = Lex.getLoc(); |
| 4971 | if (!EatIfPresent(T: lltok::kw_comdat)) |
| 4972 | return false; |
| 4973 | |
| 4974 | if (EatIfPresent(T: lltok::lparen)) { |
| 4975 | if (Lex.getKind() != lltok::ComdatVar) |
| 4976 | return tokError(Msg: "expected comdat variable" ); |
| 4977 | C = getComdat(Name: Lex.getStrVal(), Loc: Lex.getLoc()); |
| 4978 | Lex.Lex(); |
| 4979 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' after comdat var" )) |
| 4980 | return true; |
| 4981 | } else { |
| 4982 | if (GlobalName.empty()) |
| 4983 | return tokError(Msg: "comdat cannot be unnamed" ); |
| 4984 | C = getComdat(Name: std::string(GlobalName), Loc: KwLoc); |
| 4985 | } |
| 4986 | |
| 4987 | return false; |
| 4988 | } |
| 4989 | |
| 4990 | /// parseGlobalValueVector |
| 4991 | /// ::= /*empty*/ |
| 4992 | /// ::= TypeAndValue (',' TypeAndValue)* |
| 4993 | bool LLParser::parseGlobalValueVector(SmallVectorImpl<Constant *> &Elts) { |
| 4994 | // Empty list. |
| 4995 | if (Lex.getKind() == lltok::rbrace || |
| 4996 | Lex.getKind() == lltok::rsquare || |
| 4997 | Lex.getKind() == lltok::greater || |
| 4998 | Lex.getKind() == lltok::rparen) |
| 4999 | return false; |
| 5000 | |
| 5001 | do { |
| 5002 | // Let the caller deal with inrange. |
| 5003 | if (Lex.getKind() == lltok::kw_inrange) |
| 5004 | return false; |
| 5005 | |
| 5006 | Constant *C; |
| 5007 | if (parseGlobalTypeAndValue(V&: C)) |
| 5008 | return true; |
| 5009 | Elts.push_back(Elt: C); |
| 5010 | } while (EatIfPresent(T: lltok::comma)); |
| 5011 | |
| 5012 | return false; |
| 5013 | } |
| 5014 | |
| 5015 | bool LLParser::parseMDTuple(MDNode *&MD, bool IsDistinct) { |
| 5016 | SmallVector<Metadata *, 16> Elts; |
| 5017 | if (parseMDNodeVector(Elts)) |
| 5018 | return true; |
| 5019 | |
| 5020 | MD = (IsDistinct ? MDTuple::getDistinct : MDTuple::get)(Context, Elts); |
| 5021 | return false; |
| 5022 | } |
| 5023 | |
| 5024 | /// MDNode: |
| 5025 | /// ::= !{ ... } |
| 5026 | /// ::= !7 |
| 5027 | /// ::= !DILocation(...) |
| 5028 | bool LLParser::parseMDNode(MDNode *&N) { |
| 5029 | if (Lex.getKind() == lltok::MetadataVar) |
| 5030 | return parseSpecializedMDNode(N); |
| 5031 | |
| 5032 | return parseToken(T: lltok::exclaim, ErrMsg: "expected '!' here" ) || parseMDNodeTail(N); |
| 5033 | } |
| 5034 | |
| 5035 | bool LLParser::parseMDNodeTail(MDNode *&N) { |
| 5036 | // !{ ... } |
| 5037 | if (Lex.getKind() == lltok::lbrace) |
| 5038 | return parseMDTuple(MD&: N); |
| 5039 | |
| 5040 | // !42 |
| 5041 | return parseMDNodeID(Result&: N); |
| 5042 | } |
| 5043 | |
| 5044 | namespace { |
| 5045 | |
| 5046 | /// Structure to represent an optional metadata field. |
| 5047 | template <class FieldTy> struct MDFieldImpl { |
| 5048 | typedef MDFieldImpl ImplTy; |
| 5049 | FieldTy Val; |
| 5050 | bool Seen; |
| 5051 | |
| 5052 | void assign(FieldTy Val) { |
| 5053 | Seen = true; |
| 5054 | this->Val = std::move(Val); |
| 5055 | } |
| 5056 | |
| 5057 | explicit MDFieldImpl(FieldTy Default) |
| 5058 | : Val(std::move(Default)), Seen(false) {} |
| 5059 | }; |
| 5060 | |
| 5061 | /// Structure to represent an optional metadata field that |
| 5062 | /// can be of either type (A or B) and encapsulates the |
| 5063 | /// MD<typeofA>Field and MD<typeofB>Field structs, so not |
| 5064 | /// to reimplement the specifics for representing each Field. |
| 5065 | template <class FieldTypeA, class FieldTypeB> struct MDEitherFieldImpl { |
| 5066 | typedef MDEitherFieldImpl<FieldTypeA, FieldTypeB> ImplTy; |
| 5067 | FieldTypeA A; |
| 5068 | FieldTypeB B; |
| 5069 | bool Seen; |
| 5070 | |
| 5071 | enum { |
| 5072 | IsInvalid = 0, |
| 5073 | IsTypeA = 1, |
| 5074 | IsTypeB = 2 |
| 5075 | } WhatIs; |
| 5076 | |
| 5077 | void assign(FieldTypeA A) { |
| 5078 | Seen = true; |
| 5079 | this->A = std::move(A); |
| 5080 | WhatIs = IsTypeA; |
| 5081 | } |
| 5082 | |
| 5083 | void assign(FieldTypeB B) { |
| 5084 | Seen = true; |
| 5085 | this->B = std::move(B); |
| 5086 | WhatIs = IsTypeB; |
| 5087 | } |
| 5088 | |
| 5089 | explicit MDEitherFieldImpl(FieldTypeA DefaultA, FieldTypeB DefaultB) |
| 5090 | : A(std::move(DefaultA)), B(std::move(DefaultB)), Seen(false), |
| 5091 | WhatIs(IsInvalid) {} |
| 5092 | }; |
| 5093 | |
| 5094 | struct MDUnsignedField : public MDFieldImpl<uint64_t> { |
| 5095 | uint64_t Max; |
| 5096 | |
| 5097 | MDUnsignedField(uint64_t Default = 0, uint64_t Max = UINT64_MAX) |
| 5098 | : ImplTy(Default), Max(Max) {} |
| 5099 | }; |
| 5100 | |
| 5101 | struct LineField : public MDUnsignedField { |
| 5102 | LineField() : MDUnsignedField(0, UINT32_MAX) {} |
| 5103 | }; |
| 5104 | |
| 5105 | struct ColumnField : public MDUnsignedField { |
| 5106 | ColumnField() : MDUnsignedField(0, UINT16_MAX) {} |
| 5107 | }; |
| 5108 | |
| 5109 | struct DwarfTagField : public MDUnsignedField { |
| 5110 | DwarfTagField() : MDUnsignedField(0, dwarf::DW_TAG_hi_user) {} |
| 5111 | DwarfTagField(dwarf::Tag DefaultTag) |
| 5112 | : MDUnsignedField(DefaultTag, dwarf::DW_TAG_hi_user) {} |
| 5113 | }; |
| 5114 | |
| 5115 | struct DwarfMacinfoTypeField : public MDUnsignedField { |
| 5116 | DwarfMacinfoTypeField() : MDUnsignedField(0, dwarf::DW_MACINFO_vendor_ext) {} |
| 5117 | DwarfMacinfoTypeField(dwarf::MacinfoRecordType DefaultType) |
| 5118 | : MDUnsignedField(DefaultType, dwarf::DW_MACINFO_vendor_ext) {} |
| 5119 | }; |
| 5120 | |
| 5121 | struct DwarfAttEncodingField : public MDUnsignedField { |
| 5122 | DwarfAttEncodingField() : MDUnsignedField(0, dwarf::DW_ATE_hi_user) {} |
| 5123 | }; |
| 5124 | |
| 5125 | struct DwarfVirtualityField : public MDUnsignedField { |
| 5126 | DwarfVirtualityField() : MDUnsignedField(0, dwarf::DW_VIRTUALITY_max) {} |
| 5127 | }; |
| 5128 | |
| 5129 | struct DwarfLangField : public MDUnsignedField { |
| 5130 | DwarfLangField() : MDUnsignedField(0, dwarf::DW_LANG_hi_user) {} |
| 5131 | }; |
| 5132 | |
| 5133 | struct DwarfSourceLangNameField : public MDUnsignedField { |
| 5134 | DwarfSourceLangNameField() : MDUnsignedField(0, UINT32_MAX) {} |
| 5135 | }; |
| 5136 | |
| 5137 | struct DwarfLangDialectField : public MDUnsignedField { |
| 5138 | DwarfLangDialectField() |
| 5139 | : MDUnsignedField(0, dwarf::DW_LLVM_LANG_DIALECT_max) {} |
| 5140 | }; |
| 5141 | |
| 5142 | struct DwarfCCField : public MDUnsignedField { |
| 5143 | DwarfCCField() : MDUnsignedField(0, dwarf::DW_CC_hi_user) {} |
| 5144 | }; |
| 5145 | |
| 5146 | struct DwarfEnumKindField : public MDUnsignedField { |
| 5147 | DwarfEnumKindField() |
| 5148 | : MDUnsignedField(dwarf::DW_APPLE_ENUM_KIND_invalid, |
| 5149 | dwarf::DW_APPLE_ENUM_KIND_max) {} |
| 5150 | }; |
| 5151 | |
| 5152 | struct EmissionKindField : public MDUnsignedField { |
| 5153 | EmissionKindField() : MDUnsignedField(0, DICompileUnit::LastEmissionKind) {} |
| 5154 | }; |
| 5155 | |
| 5156 | struct FixedPointKindField : public MDUnsignedField { |
| 5157 | FixedPointKindField() |
| 5158 | : MDUnsignedField(0, DIFixedPointType::LastFixedPointKind) {} |
| 5159 | }; |
| 5160 | |
| 5161 | struct NameTableKindField : public MDUnsignedField { |
| 5162 | NameTableKindField() |
| 5163 | : MDUnsignedField( |
| 5164 | 0, (unsigned) |
| 5165 | DICompileUnit::DebugNameTableKind::LastDebugNameTableKind) {} |
| 5166 | }; |
| 5167 | |
| 5168 | struct DIFlagField : public MDFieldImpl<DINode::DIFlags> { |
| 5169 | DIFlagField() : MDFieldImpl(DINode::FlagZero) {} |
| 5170 | }; |
| 5171 | |
| 5172 | struct DISPFlagField : public MDFieldImpl<DISubprogram::DISPFlags> { |
| 5173 | DISPFlagField() : MDFieldImpl(DISubprogram::SPFlagZero) {} |
| 5174 | }; |
| 5175 | |
| 5176 | struct MDAPSIntField : public MDFieldImpl<APSInt> { |
| 5177 | MDAPSIntField() : ImplTy(APSInt()) {} |
| 5178 | }; |
| 5179 | |
| 5180 | struct MDSignedField : public MDFieldImpl<int64_t> { |
| 5181 | int64_t Min = INT64_MIN; |
| 5182 | int64_t Max = INT64_MAX; |
| 5183 | |
| 5184 | MDSignedField(int64_t Default = 0) |
| 5185 | : ImplTy(Default) {} |
| 5186 | MDSignedField(int64_t Default, int64_t Min, int64_t Max) |
| 5187 | : ImplTy(Default), Min(Min), Max(Max) {} |
| 5188 | }; |
| 5189 | |
| 5190 | struct MDBoolField : public MDFieldImpl<bool> { |
| 5191 | MDBoolField(bool Default = false) : ImplTy(Default) {} |
| 5192 | }; |
| 5193 | |
| 5194 | struct MDField : public MDFieldImpl<Metadata *> { |
| 5195 | bool AllowNull; |
| 5196 | |
| 5197 | MDField(bool AllowNull = true) : ImplTy(nullptr), AllowNull(AllowNull) {} |
| 5198 | }; |
| 5199 | |
| 5200 | struct MDStringField : public MDFieldImpl<MDString *> { |
| 5201 | enum class EmptyIs { |
| 5202 | Null, //< Allow empty input string, map to nullptr |
| 5203 | Empty, //< Allow empty input string, map to an empty MDString |
| 5204 | Error, //< Disallow empty string, map to an error |
| 5205 | } EmptyIs; |
| 5206 | MDStringField(enum EmptyIs EmptyIs = EmptyIs::Null) |
| 5207 | : ImplTy(nullptr), EmptyIs(EmptyIs) {} |
| 5208 | }; |
| 5209 | |
| 5210 | struct MDFieldList : public MDFieldImpl<SmallVector<Metadata *, 4>> { |
| 5211 | MDFieldList() : ImplTy(SmallVector<Metadata *, 4>()) {} |
| 5212 | }; |
| 5213 | |
| 5214 | struct ChecksumKindField : public MDFieldImpl<DIFile::ChecksumKind> { |
| 5215 | ChecksumKindField(DIFile::ChecksumKind CSKind) : ImplTy(CSKind) {} |
| 5216 | }; |
| 5217 | |
| 5218 | struct MDSignedOrMDField : MDEitherFieldImpl<MDSignedField, MDField> { |
| 5219 | MDSignedOrMDField(int64_t Default = 0, bool AllowNull = true) |
| 5220 | : ImplTy(MDSignedField(Default), MDField(AllowNull)) {} |
| 5221 | |
| 5222 | MDSignedOrMDField(int64_t Default, int64_t Min, int64_t Max, |
| 5223 | bool AllowNull = true) |
| 5224 | : ImplTy(MDSignedField(Default, Min, Max), MDField(AllowNull)) {} |
| 5225 | |
| 5226 | bool isMDSignedField() const { return WhatIs == IsTypeA; } |
| 5227 | bool isMDField() const { return WhatIs == IsTypeB; } |
| 5228 | int64_t getMDSignedValue() const { |
| 5229 | assert(isMDSignedField() && "Wrong field type" ); |
| 5230 | return A.Val; |
| 5231 | } |
| 5232 | Metadata *getMDFieldValue() const { |
| 5233 | assert(isMDField() && "Wrong field type" ); |
| 5234 | return B.Val; |
| 5235 | } |
| 5236 | }; |
| 5237 | |
| 5238 | struct MDUnsignedOrMDField : MDEitherFieldImpl<MDUnsignedField, MDField> { |
| 5239 | MDUnsignedOrMDField(uint64_t Default = 0, bool AllowNull = true) |
| 5240 | : ImplTy(MDUnsignedField(Default), MDField(AllowNull)) {} |
| 5241 | |
| 5242 | MDUnsignedOrMDField(uint64_t Default, uint64_t Max, bool AllowNull = true) |
| 5243 | : ImplTy(MDUnsignedField(Default, Max), MDField(AllowNull)) {} |
| 5244 | |
| 5245 | bool isMDUnsignedField() const { return WhatIs == IsTypeA; } |
| 5246 | bool isMDField() const { return WhatIs == IsTypeB; } |
| 5247 | uint64_t getMDUnsignedValue() const { |
| 5248 | assert(isMDUnsignedField() && "Wrong field type" ); |
| 5249 | return A.Val; |
| 5250 | } |
| 5251 | Metadata *getMDFieldValue() const { |
| 5252 | assert(isMDField() && "Wrong field type" ); |
| 5253 | return B.Val; |
| 5254 | } |
| 5255 | |
| 5256 | Metadata *getValueAsMetadata(LLVMContext &Context) const { |
| 5257 | if (isMDUnsignedField()) |
| 5258 | return ConstantAsMetadata::get( |
| 5259 | C: ConstantInt::get(Ty: Type::getInt64Ty(C&: Context), V: getMDUnsignedValue())); |
| 5260 | if (isMDField()) |
| 5261 | return getMDFieldValue(); |
| 5262 | return nullptr; |
| 5263 | } |
| 5264 | }; |
| 5265 | |
| 5266 | } // end anonymous namespace |
| 5267 | |
| 5268 | namespace llvm { |
| 5269 | |
| 5270 | template <> |
| 5271 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDAPSIntField &Result) { |
| 5272 | if (Lex.getKind() != lltok::APSInt) |
| 5273 | return tokError(Msg: "expected integer" ); |
| 5274 | |
| 5275 | Result.assign(Val: Lex.getAPSIntVal()); |
| 5276 | Lex.Lex(); |
| 5277 | return false; |
| 5278 | } |
| 5279 | |
| 5280 | template <> |
| 5281 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5282 | MDUnsignedField &Result) { |
| 5283 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 5284 | return tokError(Msg: "expected unsigned integer" ); |
| 5285 | |
| 5286 | auto &U = Lex.getAPSIntVal(); |
| 5287 | if (U.ugt(RHS: Result.Max)) |
| 5288 | return tokError(Msg: "value for '" + Name + "' too large, limit is " + |
| 5289 | Twine(Result.Max)); |
| 5290 | Result.assign(Val: U.getZExtValue()); |
| 5291 | assert(Result.Val <= Result.Max && "Expected value in range" ); |
| 5292 | Lex.Lex(); |
| 5293 | return false; |
| 5294 | } |
| 5295 | |
| 5296 | template <> |
| 5297 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, LineField &Result) { |
| 5298 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5299 | } |
| 5300 | template <> |
| 5301 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, ColumnField &Result) { |
| 5302 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5303 | } |
| 5304 | |
| 5305 | template <> |
| 5306 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfTagField &Result) { |
| 5307 | if (Lex.getKind() == lltok::APSInt) |
| 5308 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5309 | |
| 5310 | if (Lex.getKind() != lltok::DwarfTag) |
| 5311 | return tokError(Msg: "expected DWARF tag" ); |
| 5312 | |
| 5313 | unsigned Tag = dwarf::getTag(TagString: Lex.getStrVal()); |
| 5314 | if (Tag == dwarf::DW_TAG_invalid) |
| 5315 | return tokError(Msg: "invalid DWARF tag" + Twine(" '" ) + Lex.getStrVal() + "'" ); |
| 5316 | assert(Tag <= Result.Max && "Expected valid DWARF tag" ); |
| 5317 | |
| 5318 | Result.assign(Val: Tag); |
| 5319 | Lex.Lex(); |
| 5320 | return false; |
| 5321 | } |
| 5322 | |
| 5323 | template <> |
| 5324 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5325 | DwarfMacinfoTypeField &Result) { |
| 5326 | if (Lex.getKind() == lltok::APSInt) |
| 5327 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5328 | |
| 5329 | if (Lex.getKind() != lltok::DwarfMacinfo) |
| 5330 | return tokError(Msg: "expected DWARF macinfo type" ); |
| 5331 | |
| 5332 | unsigned Macinfo = dwarf::getMacinfo(MacinfoString: Lex.getStrVal()); |
| 5333 | if (Macinfo == dwarf::DW_MACINFO_invalid) |
| 5334 | return tokError(Msg: "invalid DWARF macinfo type" + Twine(" '" ) + |
| 5335 | Lex.getStrVal() + "'" ); |
| 5336 | assert(Macinfo <= Result.Max && "Expected valid DWARF macinfo type" ); |
| 5337 | |
| 5338 | Result.assign(Val: Macinfo); |
| 5339 | Lex.Lex(); |
| 5340 | return false; |
| 5341 | } |
| 5342 | |
| 5343 | template <> |
| 5344 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5345 | DwarfVirtualityField &Result) { |
| 5346 | if (Lex.getKind() == lltok::APSInt) |
| 5347 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5348 | |
| 5349 | if (Lex.getKind() != lltok::DwarfVirtuality) |
| 5350 | return tokError(Msg: "expected DWARF virtuality code" ); |
| 5351 | |
| 5352 | unsigned Virtuality = dwarf::getVirtuality(VirtualityString: Lex.getStrVal()); |
| 5353 | if (Virtuality == dwarf::DW_VIRTUALITY_invalid) |
| 5354 | return tokError(Msg: "invalid DWARF virtuality code" + Twine(" '" ) + |
| 5355 | Lex.getStrVal() + "'" ); |
| 5356 | assert(Virtuality <= Result.Max && "Expected valid DWARF virtuality code" ); |
| 5357 | Result.assign(Val: Virtuality); |
| 5358 | Lex.Lex(); |
| 5359 | return false; |
| 5360 | } |
| 5361 | |
| 5362 | template <> |
| 5363 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5364 | DwarfEnumKindField &Result) { |
| 5365 | if (Lex.getKind() == lltok::APSInt) |
| 5366 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5367 | |
| 5368 | if (Lex.getKind() != lltok::DwarfEnumKind) |
| 5369 | return tokError(Msg: "expected DWARF enum kind code" ); |
| 5370 | |
| 5371 | unsigned EnumKind = dwarf::getEnumKind(EnumKindString: Lex.getStrVal()); |
| 5372 | if (EnumKind == dwarf::DW_APPLE_ENUM_KIND_invalid) |
| 5373 | return tokError(Msg: "invalid DWARF enum kind code" + Twine(" '" ) + |
| 5374 | Lex.getStrVal() + "'" ); |
| 5375 | assert(EnumKind <= Result.Max && "Expected valid DWARF enum kind code" ); |
| 5376 | Result.assign(Val: EnumKind); |
| 5377 | Lex.Lex(); |
| 5378 | return false; |
| 5379 | } |
| 5380 | |
| 5381 | template <> |
| 5382 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfLangField &Result) { |
| 5383 | if (Lex.getKind() == lltok::APSInt) |
| 5384 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5385 | |
| 5386 | if (Lex.getKind() != lltok::DwarfLang) |
| 5387 | return tokError(Msg: "expected DWARF language" ); |
| 5388 | |
| 5389 | unsigned Lang = dwarf::getLanguage(LanguageString: Lex.getStrVal()); |
| 5390 | if (!Lang) |
| 5391 | return tokError(Msg: "invalid DWARF language" + Twine(" '" ) + Lex.getStrVal() + |
| 5392 | "'" ); |
| 5393 | assert(Lang <= Result.Max && "Expected valid DWARF language" ); |
| 5394 | Result.assign(Val: Lang); |
| 5395 | Lex.Lex(); |
| 5396 | return false; |
| 5397 | } |
| 5398 | |
| 5399 | template <> |
| 5400 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5401 | DwarfSourceLangNameField &Result) { |
| 5402 | if (Lex.getKind() == lltok::APSInt) |
| 5403 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5404 | |
| 5405 | if (Lex.getKind() != lltok::DwarfSourceLangName) |
| 5406 | return tokError(Msg: "expected DWARF source language name" ); |
| 5407 | |
| 5408 | unsigned Lang = dwarf::getSourceLanguageName(SourceLanguageNameString: Lex.getStrVal()); |
| 5409 | if (!Lang) |
| 5410 | return tokError(Msg: "invalid DWARF source language name" + Twine(" '" ) + |
| 5411 | Lex.getStrVal() + "'" ); |
| 5412 | assert(Lang <= Result.Max && "Expected valid DWARF source language name" ); |
| 5413 | Result.assign(Val: Lang); |
| 5414 | Lex.Lex(); |
| 5415 | return false; |
| 5416 | } |
| 5417 | |
| 5418 | template <> |
| 5419 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5420 | DwarfLangDialectField &Result) { |
| 5421 | // Specifying the dialect field requires a recognized dialect: simt or |
| 5422 | // tile (numerically 1 or 2). Omitting the field is the only way to |
| 5423 | // express "no dialect specified". |
| 5424 | if (Lex.getKind() == lltok::APSInt) { |
| 5425 | if (Lex.getAPSIntVal() == 0) |
| 5426 | return tokError(Msg: "value for 'dialect' must be a known DWARF language " |
| 5427 | "dialect (DW_LLVM_LANG_DIALECT_simt or " |
| 5428 | "DW_LLVM_LANG_DIALECT_tile)" ); |
| 5429 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5430 | } |
| 5431 | |
| 5432 | if (Lex.getKind() != lltok::DwarfLangDialect) |
| 5433 | return tokError(Msg: "expected DWARF language dialect" ); |
| 5434 | |
| 5435 | StringRef DialectString = Lex.getStrVal(); |
| 5436 | // getLanguageDialect returns a sentinel above Result.Max for unknown |
| 5437 | // spellings; only simt and tile are registered, so any unrecognized |
| 5438 | // DW_LLVM_LANG_DIALECT_* token is rejected here. |
| 5439 | unsigned Dialect = dwarf::getLanguageDialect(LanguageDialectString: DialectString); |
| 5440 | if (Dialect > Result.Max) |
| 5441 | return tokError(Msg: "invalid DWARF language dialect" + Twine(" '" ) + |
| 5442 | DialectString + "'" ); |
| 5443 | Result.assign(Val: Dialect); |
| 5444 | Lex.Lex(); |
| 5445 | return false; |
| 5446 | } |
| 5447 | |
| 5448 | template <> |
| 5449 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DwarfCCField &Result) { |
| 5450 | if (Lex.getKind() == lltok::APSInt) |
| 5451 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5452 | |
| 5453 | if (Lex.getKind() != lltok::DwarfCC) |
| 5454 | return tokError(Msg: "expected DWARF calling convention" ); |
| 5455 | |
| 5456 | unsigned CC = dwarf::getCallingConvention(LanguageString: Lex.getStrVal()); |
| 5457 | if (!CC) |
| 5458 | return tokError(Msg: "invalid DWARF calling convention" + Twine(" '" ) + |
| 5459 | Lex.getStrVal() + "'" ); |
| 5460 | assert(CC <= Result.Max && "Expected valid DWARF calling convention" ); |
| 5461 | Result.assign(Val: CC); |
| 5462 | Lex.Lex(); |
| 5463 | return false; |
| 5464 | } |
| 5465 | |
| 5466 | template <> |
| 5467 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5468 | EmissionKindField &Result) { |
| 5469 | if (Lex.getKind() == lltok::APSInt) |
| 5470 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5471 | |
| 5472 | if (Lex.getKind() != lltok::EmissionKind) |
| 5473 | return tokError(Msg: "expected emission kind" ); |
| 5474 | |
| 5475 | auto Kind = DICompileUnit::getEmissionKind(Str: Lex.getStrVal()); |
| 5476 | if (!Kind) |
| 5477 | return tokError(Msg: "invalid emission kind" + Twine(" '" ) + Lex.getStrVal() + |
| 5478 | "'" ); |
| 5479 | assert(*Kind <= Result.Max && "Expected valid emission kind" ); |
| 5480 | Result.assign(Val: *Kind); |
| 5481 | Lex.Lex(); |
| 5482 | return false; |
| 5483 | } |
| 5484 | |
| 5485 | template <> |
| 5486 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5487 | FixedPointKindField &Result) { |
| 5488 | if (Lex.getKind() == lltok::APSInt) |
| 5489 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5490 | |
| 5491 | if (Lex.getKind() != lltok::FixedPointKind) |
| 5492 | return tokError(Msg: "expected fixed-point kind" ); |
| 5493 | |
| 5494 | auto Kind = DIFixedPointType::getFixedPointKind(Str: Lex.getStrVal()); |
| 5495 | if (!Kind) |
| 5496 | return tokError(Msg: "invalid fixed-point kind" + Twine(" '" ) + Lex.getStrVal() + |
| 5497 | "'" ); |
| 5498 | assert(*Kind <= Result.Max && "Expected valid fixed-point kind" ); |
| 5499 | Result.assign(Val: *Kind); |
| 5500 | Lex.Lex(); |
| 5501 | return false; |
| 5502 | } |
| 5503 | |
| 5504 | template <> |
| 5505 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5506 | NameTableKindField &Result) { |
| 5507 | if (Lex.getKind() == lltok::APSInt) |
| 5508 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5509 | |
| 5510 | if (Lex.getKind() != lltok::NameTableKind) |
| 5511 | return tokError(Msg: "expected nameTable kind" ); |
| 5512 | |
| 5513 | auto Kind = DICompileUnit::getNameTableKind(Str: Lex.getStrVal()); |
| 5514 | if (!Kind) |
| 5515 | return tokError(Msg: "invalid nameTable kind" + Twine(" '" ) + Lex.getStrVal() + |
| 5516 | "'" ); |
| 5517 | assert(((unsigned)*Kind) <= Result.Max && "Expected valid nameTable kind" ); |
| 5518 | Result.assign(Val: (unsigned)*Kind); |
| 5519 | Lex.Lex(); |
| 5520 | return false; |
| 5521 | } |
| 5522 | |
| 5523 | template <> |
| 5524 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5525 | DwarfAttEncodingField &Result) { |
| 5526 | if (Lex.getKind() == lltok::APSInt) |
| 5527 | return parseMDField(Loc, Name, Result&: static_cast<MDUnsignedField &>(Result)); |
| 5528 | |
| 5529 | if (Lex.getKind() != lltok::DwarfAttEncoding) |
| 5530 | return tokError(Msg: "expected DWARF type attribute encoding" ); |
| 5531 | |
| 5532 | unsigned Encoding = dwarf::getAttributeEncoding(EncodingString: Lex.getStrVal()); |
| 5533 | if (!Encoding) |
| 5534 | return tokError(Msg: "invalid DWARF type attribute encoding" + Twine(" '" ) + |
| 5535 | Lex.getStrVal() + "'" ); |
| 5536 | assert(Encoding <= Result.Max && "Expected valid DWARF language" ); |
| 5537 | Result.assign(Val: Encoding); |
| 5538 | Lex.Lex(); |
| 5539 | return false; |
| 5540 | } |
| 5541 | |
| 5542 | /// DIFlagField |
| 5543 | /// ::= uint32 |
| 5544 | /// ::= DIFlagVector |
| 5545 | /// ::= DIFlagVector '|' DIFlagFwdDecl '|' uint32 '|' DIFlagPublic |
| 5546 | template <> |
| 5547 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DIFlagField &Result) { |
| 5548 | |
| 5549 | // parser for a single flag. |
| 5550 | auto parseFlag = [&](DINode::DIFlags &Val) { |
| 5551 | if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) { |
| 5552 | uint32_t TempVal = static_cast<uint32_t>(Val); |
| 5553 | bool Res = parseUInt32(Val&: TempVal); |
| 5554 | Val = static_cast<DINode::DIFlags>(TempVal); |
| 5555 | return Res; |
| 5556 | } |
| 5557 | |
| 5558 | if (Lex.getKind() != lltok::DIFlag) |
| 5559 | return tokError(Msg: "expected debug info flag" ); |
| 5560 | |
| 5561 | Val = DINode::getFlag(Flag: Lex.getStrVal()); |
| 5562 | if (!Val) |
| 5563 | return tokError(Msg: Twine("invalid debug info flag '" ) + Lex.getStrVal() + |
| 5564 | "'" ); |
| 5565 | Lex.Lex(); |
| 5566 | return false; |
| 5567 | }; |
| 5568 | |
| 5569 | // parse the flags and combine them together. |
| 5570 | DINode::DIFlags Combined = DINode::FlagZero; |
| 5571 | do { |
| 5572 | DINode::DIFlags Val; |
| 5573 | if (parseFlag(Val)) |
| 5574 | return true; |
| 5575 | Combined |= Val; |
| 5576 | } while (EatIfPresent(T: lltok::bar)); |
| 5577 | |
| 5578 | Result.assign(Val: Combined); |
| 5579 | return false; |
| 5580 | } |
| 5581 | |
| 5582 | /// DISPFlagField |
| 5583 | /// ::= uint32 |
| 5584 | /// ::= DISPFlagVector |
| 5585 | /// ::= DISPFlagVector '|' DISPFlag* '|' uint32 |
| 5586 | template <> |
| 5587 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, DISPFlagField &Result) { |
| 5588 | |
| 5589 | // parser for a single flag. |
| 5590 | auto parseFlag = [&](DISubprogram::DISPFlags &Val) { |
| 5591 | if (Lex.getKind() == lltok::APSInt && !Lex.getAPSIntVal().isSigned()) { |
| 5592 | uint32_t TempVal = static_cast<uint32_t>(Val); |
| 5593 | bool Res = parseUInt32(Val&: TempVal); |
| 5594 | Val = static_cast<DISubprogram::DISPFlags>(TempVal); |
| 5595 | return Res; |
| 5596 | } |
| 5597 | |
| 5598 | if (Lex.getKind() != lltok::DISPFlag) |
| 5599 | return tokError(Msg: "expected debug info flag" ); |
| 5600 | |
| 5601 | Val = DISubprogram::getFlag(Flag: Lex.getStrVal()); |
| 5602 | if (!Val) |
| 5603 | return tokError(Msg: Twine("invalid subprogram debug info flag '" ) + |
| 5604 | Lex.getStrVal() + "'" ); |
| 5605 | Lex.Lex(); |
| 5606 | return false; |
| 5607 | }; |
| 5608 | |
| 5609 | // parse the flags and combine them together. |
| 5610 | DISubprogram::DISPFlags Combined = DISubprogram::SPFlagZero; |
| 5611 | do { |
| 5612 | DISubprogram::DISPFlags Val; |
| 5613 | if (parseFlag(Val)) |
| 5614 | return true; |
| 5615 | Combined |= Val; |
| 5616 | } while (EatIfPresent(T: lltok::bar)); |
| 5617 | |
| 5618 | Result.assign(Val: Combined); |
| 5619 | return false; |
| 5620 | } |
| 5621 | |
| 5622 | template <> |
| 5623 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDSignedField &Result) { |
| 5624 | if (Lex.getKind() != lltok::APSInt) |
| 5625 | return tokError(Msg: "expected signed integer" ); |
| 5626 | |
| 5627 | auto &S = Lex.getAPSIntVal(); |
| 5628 | if (S < Result.Min) |
| 5629 | return tokError(Msg: "value for '" + Name + "' too small, limit is " + |
| 5630 | Twine(Result.Min)); |
| 5631 | if (S > Result.Max) |
| 5632 | return tokError(Msg: "value for '" + Name + "' too large, limit is " + |
| 5633 | Twine(Result.Max)); |
| 5634 | Result.assign(Val: S.getExtValue()); |
| 5635 | assert(Result.Val >= Result.Min && "Expected value in range" ); |
| 5636 | assert(Result.Val <= Result.Max && "Expected value in range" ); |
| 5637 | Lex.Lex(); |
| 5638 | return false; |
| 5639 | } |
| 5640 | |
| 5641 | template <> |
| 5642 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDBoolField &Result) { |
| 5643 | switch (Lex.getKind()) { |
| 5644 | default: |
| 5645 | return tokError(Msg: "expected 'true' or 'false'" ); |
| 5646 | case lltok::kw_true: |
| 5647 | Result.assign(Val: true); |
| 5648 | break; |
| 5649 | case lltok::kw_false: |
| 5650 | Result.assign(Val: false); |
| 5651 | break; |
| 5652 | } |
| 5653 | Lex.Lex(); |
| 5654 | return false; |
| 5655 | } |
| 5656 | |
| 5657 | template <> |
| 5658 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDField &Result) { |
| 5659 | if (Lex.getKind() == lltok::kw_null) { |
| 5660 | if (!Result.AllowNull) |
| 5661 | return tokError(Msg: "'" + Name + "' cannot be null" ); |
| 5662 | Lex.Lex(); |
| 5663 | Result.assign(Val: nullptr); |
| 5664 | return false; |
| 5665 | } |
| 5666 | |
| 5667 | Metadata *MD; |
| 5668 | if (parseMetadata(MD, PFS: nullptr)) |
| 5669 | return true; |
| 5670 | |
| 5671 | Result.assign(Val: MD); |
| 5672 | return false; |
| 5673 | } |
| 5674 | |
| 5675 | template <> |
| 5676 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5677 | MDSignedOrMDField &Result) { |
| 5678 | // Try to parse a signed int. |
| 5679 | if (Lex.getKind() == lltok::APSInt) { |
| 5680 | MDSignedField Res = Result.A; |
| 5681 | if (!parseMDField(Loc, Name, Result&: Res)) { |
| 5682 | Result.assign(A: Res); |
| 5683 | return false; |
| 5684 | } |
| 5685 | return true; |
| 5686 | } |
| 5687 | |
| 5688 | // Otherwise, try to parse as an MDField. |
| 5689 | MDField Res = Result.B; |
| 5690 | if (!parseMDField(Loc, Name, Result&: Res)) { |
| 5691 | Result.assign(B: Res); |
| 5692 | return false; |
| 5693 | } |
| 5694 | |
| 5695 | return true; |
| 5696 | } |
| 5697 | |
| 5698 | template <> |
| 5699 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5700 | MDUnsignedOrMDField &Result) { |
| 5701 | // Try to parse an unsigned int. |
| 5702 | if (Lex.getKind() == lltok::APSInt) { |
| 5703 | MDUnsignedField Res = Result.A; |
| 5704 | if (!parseMDField(Loc, Name, Result&: Res)) { |
| 5705 | Result.assign(A: Res); |
| 5706 | return false; |
| 5707 | } |
| 5708 | return true; |
| 5709 | } |
| 5710 | |
| 5711 | // Otherwise, try to parse as an MDField. |
| 5712 | MDField Res = Result.B; |
| 5713 | if (!parseMDField(Loc, Name, Result&: Res)) { |
| 5714 | Result.assign(B: Res); |
| 5715 | return false; |
| 5716 | } |
| 5717 | |
| 5718 | return true; |
| 5719 | } |
| 5720 | |
| 5721 | template <> |
| 5722 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDStringField &Result) { |
| 5723 | LocTy ValueLoc = Lex.getLoc(); |
| 5724 | std::string S; |
| 5725 | if (parseStringConstant(Result&: S)) |
| 5726 | return true; |
| 5727 | |
| 5728 | if (S.empty()) { |
| 5729 | switch (Result.EmptyIs) { |
| 5730 | case MDStringField::EmptyIs::Null: |
| 5731 | Result.assign(Val: nullptr); |
| 5732 | return false; |
| 5733 | case MDStringField::EmptyIs::Empty: |
| 5734 | break; |
| 5735 | case MDStringField::EmptyIs::Error: |
| 5736 | return error(L: ValueLoc, Msg: "'" + Name + "' cannot be empty" ); |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | Result.assign(Val: MDString::get(Context, Str: S)); |
| 5741 | return false; |
| 5742 | } |
| 5743 | |
| 5744 | template <> |
| 5745 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, MDFieldList &Result) { |
| 5746 | SmallVector<Metadata *, 4> MDs; |
| 5747 | if (parseMDNodeVector(Elts&: MDs)) |
| 5748 | return true; |
| 5749 | |
| 5750 | Result.assign(Val: std::move(MDs)); |
| 5751 | return false; |
| 5752 | } |
| 5753 | |
| 5754 | template <> |
| 5755 | bool LLParser::parseMDField(LocTy Loc, StringRef Name, |
| 5756 | ChecksumKindField &Result) { |
| 5757 | std::optional<DIFile::ChecksumKind> CSKind = |
| 5758 | DIFile::getChecksumKind(CSKindStr: Lex.getStrVal()); |
| 5759 | |
| 5760 | if (Lex.getKind() != lltok::ChecksumKind || !CSKind) |
| 5761 | return tokError(Msg: "invalid checksum kind" + Twine(" '" ) + Lex.getStrVal() + |
| 5762 | "'" ); |
| 5763 | |
| 5764 | Result.assign(Val: *CSKind); |
| 5765 | Lex.Lex(); |
| 5766 | return false; |
| 5767 | } |
| 5768 | |
| 5769 | } // end namespace llvm |
| 5770 | |
| 5771 | template <class ParserTy> |
| 5772 | bool LLParser::parseMDFieldsImplBody(ParserTy ParseField) { |
| 5773 | do { |
| 5774 | if (Lex.getKind() != lltok::LabelStr) |
| 5775 | return tokError(Msg: "expected field label here" ); |
| 5776 | |
| 5777 | if (ParseField()) |
| 5778 | return true; |
| 5779 | } while (EatIfPresent(T: lltok::comma)); |
| 5780 | |
| 5781 | return false; |
| 5782 | } |
| 5783 | |
| 5784 | template <class ParserTy> |
| 5785 | bool LLParser::parseMDFieldsImpl(ParserTy ParseField, LocTy &ClosingLoc) { |
| 5786 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
| 5787 | Lex.Lex(); |
| 5788 | |
| 5789 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 5790 | return true; |
| 5791 | if (Lex.getKind() != lltok::rparen) |
| 5792 | if (parseMDFieldsImplBody(ParseField)) |
| 5793 | return true; |
| 5794 | |
| 5795 | ClosingLoc = Lex.getLoc(); |
| 5796 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ); |
| 5797 | } |
| 5798 | |
| 5799 | template <class FieldTy> |
| 5800 | bool LLParser::parseMDField(StringRef Name, FieldTy &Result) { |
| 5801 | if (Result.Seen) |
| 5802 | return tokError(Msg: "field '" + Name + "' cannot be specified more than once" ); |
| 5803 | |
| 5804 | LocTy Loc = Lex.getLoc(); |
| 5805 | Lex.Lex(); |
| 5806 | return parseMDField(Loc, Name, Result); |
| 5807 | } |
| 5808 | |
| 5809 | bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) { |
| 5810 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
| 5811 | |
| 5812 | #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ |
| 5813 | if (Lex.getStrVal() == #CLASS) \ |
| 5814 | return parse##CLASS(N, IsDistinct); |
| 5815 | #include "llvm/IR/Metadata.def" |
| 5816 | |
| 5817 | return tokError(Msg: "expected metadata type" ); |
| 5818 | } |
| 5819 | |
| 5820 | #define DECLARE_FIELD(NAME, TYPE, INIT) TYPE NAME INIT |
| 5821 | #define NOP_FIELD(NAME, TYPE, INIT) |
| 5822 | #define REQUIRE_FIELD(NAME, TYPE, INIT) \ |
| 5823 | if (!NAME.Seen) \ |
| 5824 | return error(ClosingLoc, "missing required field '" #NAME "'"); |
| 5825 | #define PARSE_MD_FIELD(NAME, TYPE, DEFAULT) \ |
| 5826 | if (Lex.getStrVal() == #NAME) \ |
| 5827 | return parseMDField(#NAME, NAME); |
| 5828 | #define PARSE_MD_FIELDS() \ |
| 5829 | VISIT_MD_FIELDS(DECLARE_FIELD, DECLARE_FIELD) \ |
| 5830 | do { \ |
| 5831 | LocTy ClosingLoc; \ |
| 5832 | if (parseMDFieldsImpl( \ |
| 5833 | [&]() -> bool { \ |
| 5834 | VISIT_MD_FIELDS(PARSE_MD_FIELD, PARSE_MD_FIELD) \ |
| 5835 | return tokError(Twine("invalid field '") + Lex.getStrVal() + \ |
| 5836 | "'"); \ |
| 5837 | }, \ |
| 5838 | ClosingLoc)) \ |
| 5839 | return true; \ |
| 5840 | VISIT_MD_FIELDS(NOP_FIELD, REQUIRE_FIELD) \ |
| 5841 | } while (false) |
| 5842 | #define GET_OR_DISTINCT(CLASS, ARGS) \ |
| 5843 | (IsDistinct ? CLASS::getDistinct ARGS : CLASS::get ARGS) |
| 5844 | |
| 5845 | /// parseDILocationFields: |
| 5846 | /// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6, |
| 5847 | /// isImplicitCode: true, atomGroup: 1, atomRank: 1) |
| 5848 | bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) { |
| 5849 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 5850 | OPTIONAL(line, LineField, ); \ |
| 5851 | OPTIONAL(column, ColumnField, ); \ |
| 5852 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
| 5853 | OPTIONAL(inlinedAt, MDField, ); \ |
| 5854 | OPTIONAL(isImplicitCode, MDBoolField, (false)); \ |
| 5855 | OPTIONAL(atomGroup, MDUnsignedField, (0, UINT64_MAX)); \ |
| 5856 | OPTIONAL(atomRank, MDUnsignedField, (0, UINT8_MAX)); \ |
| 5857 | OPTIONAL(irlayers, MDField, ); |
| 5858 | PARSE_MD_FIELDS(); |
| 5859 | #undef VISIT_MD_FIELDS |
| 5860 | |
| 5861 | Result = |
| 5862 | GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val, |
| 5863 | inlinedAt.Val, isImplicitCode.Val, |
| 5864 | atomGroup.Val, atomRank.Val, irlayers.Val)); |
| 5865 | return false; |
| 5866 | } |
| 5867 | |
| 5868 | bool LLParser::parseDILayerLoc(MDNode *&Result, bool IsDistinct) { |
| 5869 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 5870 | OPTIONAL(line, LineField, ); \ |
| 5871 | OPTIONAL(column, ColumnField, ); \ |
| 5872 | REQUIRED(file, MDField, (/* AllowNull */ false)); \ |
| 5873 | REQUIRED(kind, MDStringField, ); |
| 5874 | PARSE_MD_FIELDS(); |
| 5875 | #undef VISIT_MD_FIELDS |
| 5876 | |
| 5877 | Result = GET_OR_DISTINCT(DILayerLoc, |
| 5878 | (Context, kind.Val, file.Val, line.Val, column.Val)); |
| 5879 | return false; |
| 5880 | } |
| 5881 | |
| 5882 | bool LLParser::parseDILayerLocList(MDNode *&Result, bool IsDistinct) { |
| 5883 | // ::= !DILayerLocList(!a, !b, ...) |
| 5884 | Lex.Lex(); // eat the '!DILayerLocList' type name |
| 5885 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 5886 | return true; |
| 5887 | SmallVector<Metadata *, 4> Layers; |
| 5888 | if (!EatIfPresent(T: lltok::rparen)) { |
| 5889 | do { |
| 5890 | Metadata *MD; |
| 5891 | if (parseMetadata(MD, PFS: nullptr)) |
| 5892 | return true; |
| 5893 | Layers.push_back(Elt: MD); |
| 5894 | } while (EatIfPresent(T: lltok::comma)); |
| 5895 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 5896 | return true; |
| 5897 | } |
| 5898 | Result = GET_OR_DISTINCT(DILayerLocList, (Context, Layers)); |
| 5899 | return false; |
| 5900 | } |
| 5901 | |
| 5902 | /// parseDIAssignID: |
| 5903 | /// ::= distinct !DIAssignID() |
| 5904 | bool LLParser::parseDIAssignID(MDNode *&Result, bool IsDistinct) { |
| 5905 | if (!IsDistinct) |
| 5906 | return tokError(Msg: "missing 'distinct', required for !DIAssignID()" ); |
| 5907 | |
| 5908 | Lex.Lex(); |
| 5909 | |
| 5910 | // Now eat the parens. |
| 5911 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 5912 | return true; |
| 5913 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 5914 | return true; |
| 5915 | |
| 5916 | Result = DIAssignID::getDistinct(Context); |
| 5917 | return false; |
| 5918 | } |
| 5919 | |
| 5920 | /// parseGenericDINode: |
| 5921 | /// ::= !GenericDINode(tag: 15, header: "...", operands: {...}) |
| 5922 | bool LLParser::parseGenericDINode(MDNode *&Result, bool IsDistinct) { |
| 5923 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 5924 | REQUIRED(tag, DwarfTagField, ); \ |
| 5925 | OPTIONAL(, MDStringField, ); \ |
| 5926 | OPTIONAL(operands, MDFieldList, ); |
| 5927 | PARSE_MD_FIELDS(); |
| 5928 | #undef VISIT_MD_FIELDS |
| 5929 | |
| 5930 | Result = GET_OR_DISTINCT(GenericDINode, |
| 5931 | (Context, tag.Val, header.Val, operands.Val)); |
| 5932 | return false; |
| 5933 | } |
| 5934 | |
| 5935 | /// parseDISubrangeType: |
| 5936 | /// ::= !DISubrangeType(name: "whatever", file: !0, |
| 5937 | /// line: 7, scope: !1, baseType: !2, size: 32, |
| 5938 | /// align: 32, flags: 0, lowerBound: !3 |
| 5939 | /// upperBound: !4, stride: !5, bias: !6) |
| 5940 | bool LLParser::parseDISubrangeType(MDNode *&Result, bool IsDistinct) { |
| 5941 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 5942 | OPTIONAL(name, MDStringField, ); \ |
| 5943 | OPTIONAL(file, MDField, ); \ |
| 5944 | OPTIONAL(line, LineField, ); \ |
| 5945 | OPTIONAL(scope, MDField, ); \ |
| 5946 | OPTIONAL(baseType, MDField, ); \ |
| 5947 | OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 5948 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 5949 | OPTIONAL(flags, DIFlagField, ); \ |
| 5950 | OPTIONAL(lowerBound, MDSignedOrMDField, ); \ |
| 5951 | OPTIONAL(upperBound, MDSignedOrMDField, ); \ |
| 5952 | OPTIONAL(stride, MDSignedOrMDField, ); \ |
| 5953 | OPTIONAL(bias, MDSignedOrMDField, ); |
| 5954 | PARSE_MD_FIELDS(); |
| 5955 | #undef VISIT_MD_FIELDS |
| 5956 | |
| 5957 | auto convToMetadata = [&](MDSignedOrMDField Bound) -> Metadata * { |
| 5958 | if (Bound.isMDSignedField()) |
| 5959 | return ConstantAsMetadata::get(C: ConstantInt::getSigned( |
| 5960 | Ty: Type::getInt64Ty(C&: Context), V: Bound.getMDSignedValue())); |
| 5961 | if (Bound.isMDField()) |
| 5962 | return Bound.getMDFieldValue(); |
| 5963 | return nullptr; |
| 5964 | }; |
| 5965 | |
| 5966 | Metadata *LowerBound = convToMetadata(lowerBound); |
| 5967 | Metadata *UpperBound = convToMetadata(upperBound); |
| 5968 | Metadata *Stride = convToMetadata(stride); |
| 5969 | Metadata *Bias = convToMetadata(bias); |
| 5970 | |
| 5971 | Result = GET_OR_DISTINCT( |
| 5972 | DISubrangeType, (Context, name.Val, file.Val, line.Val, scope.Val, |
| 5973 | size.getValueAsMetadata(Context), align.Val, flags.Val, |
| 5974 | baseType.Val, LowerBound, UpperBound, Stride, Bias)); |
| 5975 | |
| 5976 | return false; |
| 5977 | } |
| 5978 | |
| 5979 | /// parseDISubrange: |
| 5980 | /// ::= !DISubrange(count: 30, lowerBound: 2) |
| 5981 | /// ::= !DISubrange(count: !node, lowerBound: 2) |
| 5982 | /// ::= !DISubrange(lowerBound: !node1, upperBound: !node2, stride: !node3) |
| 5983 | bool LLParser::parseDISubrange(MDNode *&Result, bool IsDistinct) { |
| 5984 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 5985 | OPTIONAL(count, MDSignedOrMDField, (-1, -1, INT64_MAX, false)); \ |
| 5986 | OPTIONAL(lowerBound, MDSignedOrMDField, ); \ |
| 5987 | OPTIONAL(upperBound, MDSignedOrMDField, ); \ |
| 5988 | OPTIONAL(stride, MDSignedOrMDField, ); |
| 5989 | PARSE_MD_FIELDS(); |
| 5990 | #undef VISIT_MD_FIELDS |
| 5991 | |
| 5992 | Metadata *Count = nullptr; |
| 5993 | Metadata *LowerBound = nullptr; |
| 5994 | Metadata *UpperBound = nullptr; |
| 5995 | Metadata *Stride = nullptr; |
| 5996 | |
| 5997 | auto convToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * { |
| 5998 | if (Bound.isMDSignedField()) |
| 5999 | return ConstantAsMetadata::get(C: ConstantInt::getSigned( |
| 6000 | Ty: Type::getInt64Ty(C&: Context), V: Bound.getMDSignedValue())); |
| 6001 | if (Bound.isMDField()) |
| 6002 | return Bound.getMDFieldValue(); |
| 6003 | return nullptr; |
| 6004 | }; |
| 6005 | |
| 6006 | Count = convToMetadata(count); |
| 6007 | LowerBound = convToMetadata(lowerBound); |
| 6008 | UpperBound = convToMetadata(upperBound); |
| 6009 | Stride = convToMetadata(stride); |
| 6010 | |
| 6011 | Result = GET_OR_DISTINCT(DISubrange, |
| 6012 | (Context, Count, LowerBound, UpperBound, Stride)); |
| 6013 | |
| 6014 | return false; |
| 6015 | } |
| 6016 | |
| 6017 | /// parseDIGenericSubrange: |
| 6018 | /// ::= !DIGenericSubrange(lowerBound: !node1, upperBound: !node2, stride: |
| 6019 | /// !node3) |
| 6020 | bool LLParser::parseDIGenericSubrange(MDNode *&Result, bool IsDistinct) { |
| 6021 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6022 | OPTIONAL(count, MDSignedOrMDField, ); \ |
| 6023 | OPTIONAL(lowerBound, MDSignedOrMDField, ); \ |
| 6024 | OPTIONAL(upperBound, MDSignedOrMDField, ); \ |
| 6025 | OPTIONAL(stride, MDSignedOrMDField, ); |
| 6026 | PARSE_MD_FIELDS(); |
| 6027 | #undef VISIT_MD_FIELDS |
| 6028 | |
| 6029 | auto ConvToMetadata = [&](const MDSignedOrMDField &Bound) -> Metadata * { |
| 6030 | if (Bound.isMDSignedField()) |
| 6031 | return DIExpression::get( |
| 6032 | Context, Elements: {dwarf::DW_OP_consts, |
| 6033 | static_cast<uint64_t>(Bound.getMDSignedValue())}); |
| 6034 | if (Bound.isMDField()) |
| 6035 | return Bound.getMDFieldValue(); |
| 6036 | return nullptr; |
| 6037 | }; |
| 6038 | |
| 6039 | Metadata *Count = ConvToMetadata(count); |
| 6040 | Metadata *LowerBound = ConvToMetadata(lowerBound); |
| 6041 | Metadata *UpperBound = ConvToMetadata(upperBound); |
| 6042 | Metadata *Stride = ConvToMetadata(stride); |
| 6043 | |
| 6044 | Result = GET_OR_DISTINCT(DIGenericSubrange, |
| 6045 | (Context, Count, LowerBound, UpperBound, Stride)); |
| 6046 | |
| 6047 | return false; |
| 6048 | } |
| 6049 | |
| 6050 | /// parseDIEnumerator: |
| 6051 | /// ::= !DIEnumerator(value: 30, isUnsigned: true, name: "SomeKind") |
| 6052 | bool LLParser::parseDIEnumerator(MDNode *&Result, bool IsDistinct) { |
| 6053 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6054 | REQUIRED(name, MDStringField, ); \ |
| 6055 | REQUIRED(value, MDAPSIntField, ); \ |
| 6056 | OPTIONAL(isUnsigned, MDBoolField, (false)); |
| 6057 | PARSE_MD_FIELDS(); |
| 6058 | #undef VISIT_MD_FIELDS |
| 6059 | |
| 6060 | if (isUnsigned.Val && value.Val.isNegative()) |
| 6061 | return tokError(Msg: "unsigned enumerator with negative value" ); |
| 6062 | |
| 6063 | APSInt Value(value.Val); |
| 6064 | // Add a leading zero so that unsigned values with the msb set are not |
| 6065 | // mistaken for negative values when used for signed enumerators. |
| 6066 | if (!isUnsigned.Val && value.Val.isUnsigned() && value.Val.isSignBitSet()) |
| 6067 | Value = Value.zext(width: Value.getBitWidth() + 1); |
| 6068 | |
| 6069 | Result = |
| 6070 | GET_OR_DISTINCT(DIEnumerator, (Context, Value, isUnsigned.Val, name.Val)); |
| 6071 | |
| 6072 | return false; |
| 6073 | } |
| 6074 | |
| 6075 | /// parseDIBasicType: |
| 6076 | /// ::= !DIBasicType(tag: DW_TAG_base_type, name: "int", size: 32, align: 32, |
| 6077 | /// encoding: DW_ATE_encoding, flags: 0) |
| 6078 | bool LLParser::parseDIBasicType(MDNode *&Result, bool IsDistinct) { |
| 6079 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6080 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \ |
| 6081 | OPTIONAL(name, MDStringField, ); \ |
| 6082 | OPTIONAL(file, MDField, ); \ |
| 6083 | OPTIONAL(line, LineField, ); \ |
| 6084 | OPTIONAL(scope, MDField, ); \ |
| 6085 | OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6086 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6087 | OPTIONAL(dataSize, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6088 | OPTIONAL(encoding, DwarfAttEncodingField, ); \ |
| 6089 | OPTIONAL(, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6090 | OPTIONAL(flags, DIFlagField, ); |
| 6091 | PARSE_MD_FIELDS(); |
| 6092 | #undef VISIT_MD_FIELDS |
| 6093 | |
| 6094 | Result = GET_OR_DISTINCT( |
| 6095 | DIBasicType, (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, |
| 6096 | size.getValueAsMetadata(Context), align.Val, encoding.Val, |
| 6097 | num_extra_inhabitants.Val, dataSize.Val, flags.Val)); |
| 6098 | return false; |
| 6099 | } |
| 6100 | |
| 6101 | /// parseDIFixedPointType: |
| 6102 | /// ::= !DIFixedPointType(tag: DW_TAG_base_type, name: "xyz", size: 32, |
| 6103 | /// align: 32, encoding: DW_ATE_signed_fixed, |
| 6104 | /// flags: 0, kind: Rational, factor: 3, numerator: 1, |
| 6105 | /// denominator: 8) |
| 6106 | bool LLParser::parseDIFixedPointType(MDNode *&Result, bool IsDistinct) { |
| 6107 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6108 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_base_type)); \ |
| 6109 | OPTIONAL(name, MDStringField, ); \ |
| 6110 | OPTIONAL(file, MDField, ); \ |
| 6111 | OPTIONAL(line, LineField, ); \ |
| 6112 | OPTIONAL(scope, MDField, ); \ |
| 6113 | OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6114 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6115 | OPTIONAL(encoding, DwarfAttEncodingField, ); \ |
| 6116 | OPTIONAL(flags, DIFlagField, ); \ |
| 6117 | OPTIONAL(kind, FixedPointKindField, ); \ |
| 6118 | OPTIONAL(factor, MDSignedField, ); \ |
| 6119 | OPTIONAL(numerator, MDAPSIntField, ); \ |
| 6120 | OPTIONAL(denominator, MDAPSIntField, ); |
| 6121 | PARSE_MD_FIELDS(); |
| 6122 | #undef VISIT_MD_FIELDS |
| 6123 | |
| 6124 | Result = GET_OR_DISTINCT(DIFixedPointType, |
| 6125 | (Context, tag.Val, name.Val, file.Val, line.Val, |
| 6126 | scope.Val, size.getValueAsMetadata(Context), |
| 6127 | align.Val, encoding.Val, flags.Val, kind.Val, |
| 6128 | factor.Val, numerator.Val, denominator.Val)); |
| 6129 | return false; |
| 6130 | } |
| 6131 | |
| 6132 | /// parseDIStringType: |
| 6133 | /// ::= !DIStringType(name: "character(4)", size: 32, align: 32) |
| 6134 | bool LLParser::parseDIStringType(MDNode *&Result, bool IsDistinct) { |
| 6135 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6136 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_string_type)); \ |
| 6137 | OPTIONAL(name, MDStringField, ); \ |
| 6138 | OPTIONAL(stringLength, MDField, ); \ |
| 6139 | OPTIONAL(stringLengthExpression, MDField, ); \ |
| 6140 | OPTIONAL(stringLocationExpression, MDField, ); \ |
| 6141 | OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6142 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6143 | OPTIONAL(encoding, DwarfAttEncodingField, ); \ |
| 6144 | OPTIONAL(charType, MDField, ); |
| 6145 | PARSE_MD_FIELDS(); |
| 6146 | #undef VISIT_MD_FIELDS |
| 6147 | |
| 6148 | Result = GET_OR_DISTINCT( |
| 6149 | DIStringType, |
| 6150 | (Context, tag.Val, name.Val, stringLength.Val, stringLengthExpression.Val, |
| 6151 | stringLocationExpression.Val, size.getValueAsMetadata(Context), |
| 6152 | align.Val, encoding.Val, charType.Val)); |
| 6153 | return false; |
| 6154 | } |
| 6155 | |
| 6156 | /// parseDIDerivedType: |
| 6157 | /// ::= !DIDerivedType(tag: DW_TAG_pointer_type, name: "int", file: !0, |
| 6158 | /// line: 7, scope: !1, baseType: !2, size: 32, |
| 6159 | /// align: 32, offset: 0, flags: 0, extraData: !3, |
| 6160 | /// dwarfAddressSpace: 3, ptrAuthKey: 1, |
| 6161 | /// ptrAuthIsAddressDiscriminated: true, |
| 6162 | /// ptrAuthExtraDiscriminator: 0x1234, |
| 6163 | /// ptrAuthIsaPointer: 1, ptrAuthAuthenticatesNullValues:1 |
| 6164 | /// ) |
| 6165 | bool LLParser::parseDIDerivedType(MDNode *&Result, bool IsDistinct) { |
| 6166 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6167 | REQUIRED(tag, DwarfTagField, ); \ |
| 6168 | OPTIONAL(name, MDStringField, ); \ |
| 6169 | OPTIONAL(file, MDField, ); \ |
| 6170 | OPTIONAL(line, LineField, ); \ |
| 6171 | OPTIONAL(scope, MDField, ); \ |
| 6172 | REQUIRED(baseType, MDField, ); \ |
| 6173 | OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6174 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6175 | OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6176 | OPTIONAL(flags, DIFlagField, ); \ |
| 6177 | OPTIONAL(, MDField, ); \ |
| 6178 | OPTIONAL(dwarfAddressSpace, MDUnsignedField, (UINT32_MAX, UINT32_MAX)); \ |
| 6179 | OPTIONAL(annotations, MDField, ); \ |
| 6180 | OPTIONAL(ptrAuthKey, MDUnsignedField, (0, 7)); \ |
| 6181 | OPTIONAL(ptrAuthIsAddressDiscriminated, MDBoolField, ); \ |
| 6182 | OPTIONAL(, MDUnsignedField, (0, 0xffff)); \ |
| 6183 | OPTIONAL(ptrAuthIsaPointer, MDBoolField, ); \ |
| 6184 | OPTIONAL(ptrAuthAuthenticatesNullValues, MDBoolField, ); |
| 6185 | PARSE_MD_FIELDS(); |
| 6186 | #undef VISIT_MD_FIELDS |
| 6187 | |
| 6188 | std::optional<unsigned> DWARFAddressSpace; |
| 6189 | if (dwarfAddressSpace.Val != UINT32_MAX) |
| 6190 | DWARFAddressSpace = dwarfAddressSpace.Val; |
| 6191 | std::optional<DIDerivedType::PtrAuthData> PtrAuthData; |
| 6192 | if (ptrAuthKey.Val) |
| 6193 | PtrAuthData.emplace( |
| 6194 | args: (unsigned)ptrAuthKey.Val, args&: ptrAuthIsAddressDiscriminated.Val, |
| 6195 | args: (unsigned)ptrAuthExtraDiscriminator.Val, args&: ptrAuthIsaPointer.Val, |
| 6196 | args&: ptrAuthAuthenticatesNullValues.Val); |
| 6197 | |
| 6198 | Result = GET_OR_DISTINCT( |
| 6199 | DIDerivedType, (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, |
| 6200 | baseType.Val, size.getValueAsMetadata(Context), align.Val, |
| 6201 | offset.getValueAsMetadata(Context), DWARFAddressSpace, |
| 6202 | PtrAuthData, flags.Val, extraData.Val, annotations.Val)); |
| 6203 | return false; |
| 6204 | } |
| 6205 | |
| 6206 | bool LLParser::parseDICompositeType(MDNode *&Result, bool IsDistinct) { |
| 6207 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6208 | REQUIRED(tag, DwarfTagField, ); \ |
| 6209 | OPTIONAL(name, MDStringField, ); \ |
| 6210 | OPTIONAL(file, MDField, ); \ |
| 6211 | OPTIONAL(line, LineField, ); \ |
| 6212 | OPTIONAL(scope, MDField, ); \ |
| 6213 | OPTIONAL(baseType, MDField, ); \ |
| 6214 | OPTIONAL(size, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6215 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6216 | OPTIONAL(offset, MDUnsignedOrMDField, (0, UINT64_MAX)); \ |
| 6217 | OPTIONAL(flags, DIFlagField, ); \ |
| 6218 | OPTIONAL(elements, MDField, ); \ |
| 6219 | OPTIONAL(runtimeLang, DwarfLangField, ); \ |
| 6220 | OPTIONAL(enumKind, DwarfEnumKindField, ); \ |
| 6221 | OPTIONAL(vtableHolder, MDField, ); \ |
| 6222 | OPTIONAL(templateParams, MDField, ); \ |
| 6223 | OPTIONAL(identifier, MDStringField, ); \ |
| 6224 | OPTIONAL(discriminator, MDField, ); \ |
| 6225 | OPTIONAL(dataLocation, MDField, ); \ |
| 6226 | OPTIONAL(associated, MDField, ); \ |
| 6227 | OPTIONAL(allocated, MDField, ); \ |
| 6228 | OPTIONAL(rank, MDSignedOrMDField, ); \ |
| 6229 | OPTIONAL(annotations, MDField, ); \ |
| 6230 | OPTIONAL(, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6231 | OPTIONAL(specification, MDField, ); \ |
| 6232 | OPTIONAL(bitStride, MDField, ); |
| 6233 | PARSE_MD_FIELDS(); |
| 6234 | #undef VISIT_MD_FIELDS |
| 6235 | |
| 6236 | Metadata *Rank = nullptr; |
| 6237 | if (rank.isMDSignedField()) |
| 6238 | Rank = ConstantAsMetadata::get(C: ConstantInt::getSigned( |
| 6239 | Ty: Type::getInt64Ty(C&: Context), V: rank.getMDSignedValue())); |
| 6240 | else if (rank.isMDField()) |
| 6241 | Rank = rank.getMDFieldValue(); |
| 6242 | |
| 6243 | std::optional<unsigned> EnumKind; |
| 6244 | if (enumKind.Val != dwarf::DW_APPLE_ENUM_KIND_invalid) |
| 6245 | EnumKind = enumKind.Val; |
| 6246 | |
| 6247 | // If this has an identifier try to build an ODR type. |
| 6248 | if (identifier.Val) |
| 6249 | if (auto *CT = DICompositeType::buildODRType( |
| 6250 | Context, Identifier&: *identifier.Val, Tag: tag.Val, Name: name.Val, File: file.Val, Line: line.Val, |
| 6251 | Scope: scope.Val, BaseType: baseType.Val, SizeInBits: size.getValueAsMetadata(Context), |
| 6252 | AlignInBits: align.Val, OffsetInBits: offset.getValueAsMetadata(Context), Specification: specification.Val, |
| 6253 | NumExtraInhabitants: num_extra_inhabitants.Val, Flags: flags.Val, Elements: elements.Val, RuntimeLang: runtimeLang.Val, |
| 6254 | EnumKind, VTableHolder: vtableHolder.Val, TemplateParams: templateParams.Val, Discriminator: discriminator.Val, |
| 6255 | DataLocation: dataLocation.Val, Associated: associated.Val, Allocated: allocated.Val, Rank, |
| 6256 | Annotations: annotations.Val, BitStride: bitStride.Val)) { |
| 6257 | Result = CT; |
| 6258 | return false; |
| 6259 | } |
| 6260 | |
| 6261 | // Create a new node, and save it in the context if it belongs in the type |
| 6262 | // map. |
| 6263 | Result = GET_OR_DISTINCT( |
| 6264 | DICompositeType, |
| 6265 | (Context, tag.Val, name.Val, file.Val, line.Val, scope.Val, baseType.Val, |
| 6266 | size.getValueAsMetadata(Context), align.Val, |
| 6267 | offset.getValueAsMetadata(Context), flags.Val, elements.Val, |
| 6268 | runtimeLang.Val, EnumKind, vtableHolder.Val, templateParams.Val, |
| 6269 | identifier.Val, discriminator.Val, dataLocation.Val, associated.Val, |
| 6270 | allocated.Val, Rank, annotations.Val, specification.Val, |
| 6271 | num_extra_inhabitants.Val, bitStride.Val)); |
| 6272 | return false; |
| 6273 | } |
| 6274 | |
| 6275 | bool LLParser::parseDISubroutineType(MDNode *&Result, bool IsDistinct) { |
| 6276 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6277 | OPTIONAL(flags, DIFlagField, ); \ |
| 6278 | OPTIONAL(cc, DwarfCCField, ); \ |
| 6279 | REQUIRED(types, MDField, ); |
| 6280 | PARSE_MD_FIELDS(); |
| 6281 | #undef VISIT_MD_FIELDS |
| 6282 | |
| 6283 | Result = GET_OR_DISTINCT(DISubroutineType, |
| 6284 | (Context, flags.Val, cc.Val, types.Val)); |
| 6285 | return false; |
| 6286 | } |
| 6287 | |
| 6288 | /// parseDIFileType: |
| 6289 | /// ::= !DIFileType(filename: "path/to/file", directory: "/path/to/dir", |
| 6290 | /// checksumkind: CSK_MD5, |
| 6291 | /// checksum: "000102030405060708090a0b0c0d0e0f", |
| 6292 | /// source: "source file contents") |
| 6293 | bool LLParser::parseDIFile(MDNode *&Result, bool IsDistinct) { |
| 6294 | // The default constructed value for checksumkind is required, but will never |
| 6295 | // be used, as the parser checks if the field was actually Seen before using |
| 6296 | // the Val. |
| 6297 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6298 | REQUIRED(filename, MDStringField, ); \ |
| 6299 | REQUIRED(directory, MDStringField, ); \ |
| 6300 | OPTIONAL(checksumkind, ChecksumKindField, (DIFile::CSK_MD5)); \ |
| 6301 | OPTIONAL(checksum, MDStringField, ); \ |
| 6302 | OPTIONAL(source, MDStringField, (MDStringField::EmptyIs::Empty)); |
| 6303 | PARSE_MD_FIELDS(); |
| 6304 | #undef VISIT_MD_FIELDS |
| 6305 | |
| 6306 | std::optional<DIFile::ChecksumInfo<MDString *>> OptChecksum; |
| 6307 | if (checksumkind.Seen && checksum.Seen) |
| 6308 | OptChecksum.emplace(args&: checksumkind.Val, args&: checksum.Val); |
| 6309 | else if (checksumkind.Seen || checksum.Seen) |
| 6310 | return tokError(Msg: "'checksumkind' and 'checksum' must be provided together" ); |
| 6311 | |
| 6312 | MDString *Source = nullptr; |
| 6313 | if (source.Seen) |
| 6314 | Source = source.Val; |
| 6315 | Result = GET_OR_DISTINCT( |
| 6316 | DIFile, (Context, filename.Val, directory.Val, OptChecksum, Source)); |
| 6317 | return false; |
| 6318 | } |
| 6319 | |
| 6320 | /// parseDICompileUnit: |
| 6321 | /// ::= !DICompileUnit(language: DW_LANG_C99, file: !0, producer: "clang", |
| 6322 | /// isOptimized: true, flags: "-O2", runtimeVersion: 1, |
| 6323 | /// splitDebugFilename: "abc.debug", |
| 6324 | /// emissionKind: FullDebug, enums: !1, retainedTypes: !2, |
| 6325 | /// globals: !4, imports: !5, macros: !6, dwoId: 0x0abcd, |
| 6326 | /// sysroot: "/", sdk: "MacOSX.sdk", |
| 6327 | /// dialect: DW_LLVM_LANG_DIALECT_simt) |
| 6328 | bool LLParser::parseDICompileUnit(MDNode *&Result, bool IsDistinct) { |
| 6329 | if (!IsDistinct) |
| 6330 | return tokError(Msg: "missing 'distinct', required for !DICompileUnit" ); |
| 6331 | |
| 6332 | LocTy Loc = Lex.getLoc(); |
| 6333 | |
| 6334 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6335 | REQUIRED(file, MDField, (/* AllowNull */ false)); \ |
| 6336 | OPTIONAL(language, DwarfLangField, ); \ |
| 6337 | OPTIONAL(sourceLanguageName, DwarfSourceLangNameField, ); \ |
| 6338 | OPTIONAL(sourceLanguageVersion, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6339 | OPTIONAL(producer, MDStringField, ); \ |
| 6340 | OPTIONAL(isOptimized, MDBoolField, ); \ |
| 6341 | OPTIONAL(flags, MDStringField, ); \ |
| 6342 | OPTIONAL(runtimeVersion, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6343 | OPTIONAL(splitDebugFilename, MDStringField, ); \ |
| 6344 | OPTIONAL(emissionKind, EmissionKindField, ); \ |
| 6345 | OPTIONAL(enums, MDField, ); \ |
| 6346 | OPTIONAL(retainedTypes, MDField, ); \ |
| 6347 | OPTIONAL(globals, MDField, ); \ |
| 6348 | OPTIONAL(imports, MDField, ); \ |
| 6349 | OPTIONAL(macros, MDField, ); \ |
| 6350 | OPTIONAL(dwoId, MDUnsignedField, ); \ |
| 6351 | OPTIONAL(splitDebugInlining, MDBoolField, = true); \ |
| 6352 | OPTIONAL(debugInfoForProfiling, MDBoolField, = false); \ |
| 6353 | OPTIONAL(nameTableKind, NameTableKindField, ); \ |
| 6354 | OPTIONAL(rangesBaseAddress, MDBoolField, = false); \ |
| 6355 | OPTIONAL(sysroot, MDStringField, ); \ |
| 6356 | OPTIONAL(sdk, MDStringField, ); \ |
| 6357 | OPTIONAL(dialect, DwarfLangDialectField, ); |
| 6358 | PARSE_MD_FIELDS(); |
| 6359 | #undef VISIT_MD_FIELDS |
| 6360 | |
| 6361 | if (!language.Seen && !sourceLanguageName.Seen) |
| 6362 | return error(L: Loc, Msg: "missing one of 'language' or 'sourceLanguageName', " |
| 6363 | "required for !DICompileUnit" ); |
| 6364 | |
| 6365 | if (language.Seen && sourceLanguageName.Seen) |
| 6366 | return error(L: Loc, Msg: "can only specify one of 'language' and " |
| 6367 | "'sourceLanguageName' on !DICompileUnit" ); |
| 6368 | |
| 6369 | if (sourceLanguageVersion.Seen && !sourceLanguageName.Seen) |
| 6370 | return error(L: Loc, Msg: "'sourceLanguageVersion' requires an associated " |
| 6371 | "'sourceLanguageName' on !DICompileUnit" ); |
| 6372 | |
| 6373 | uint16_t Dialect = static_cast<uint16_t>(dialect.Val); |
| 6374 | DISourceLanguageName SourceLanguage = |
| 6375 | language.Seen |
| 6376 | ? DISourceLanguageName(static_cast<uint16_t>(language.Val), Dialect) |
| 6377 | : DISourceLanguageName( |
| 6378 | static_cast<uint16_t>(sourceLanguageName.Val), |
| 6379 | static_cast<uint32_t>(sourceLanguageVersion.Val), Dialect); |
| 6380 | |
| 6381 | Result = DICompileUnit::getDistinct( |
| 6382 | Context, SourceLanguage, File: file.Val, Producer: producer.Val, IsOptimized: isOptimized.Val, |
| 6383 | Flags: flags.Val, RuntimeVersion: runtimeVersion.Val, SplitDebugFilename: splitDebugFilename.Val, EmissionKind: emissionKind.Val, |
| 6384 | EnumTypes: enums.Val, RetainedTypes: retainedTypes.Val, GlobalVariables: globals.Val, ImportedEntities: imports.Val, Macros: macros.Val, |
| 6385 | DWOId: dwoId.Val, SplitDebugInlining: splitDebugInlining.Val, DebugInfoForProfiling: debugInfoForProfiling.Val, |
| 6386 | NameTableKind: nameTableKind.Val, RangesBaseAddress: rangesBaseAddress.Val, SysRoot: sysroot.Val, SDK: sdk.Val); |
| 6387 | return false; |
| 6388 | } |
| 6389 | |
| 6390 | /// parseDISubprogram: |
| 6391 | /// ::= !DISubprogram(scope: !0, name: "foo", linkageName: "_Zfoo", |
| 6392 | /// file: !1, line: 7, type: !2, isLocal: false, |
| 6393 | /// isDefinition: true, scopeLine: 8, containingType: !3, |
| 6394 | /// virtuality: DW_VIRTUALTIY_pure_virtual, |
| 6395 | /// virtualIndex: 10, thisAdjustment: 4, flags: 11, |
| 6396 | /// spFlags: 10, isOptimized: false, templateParams: !4, |
| 6397 | /// declaration: !5, retainedNodes: !6, thrownTypes: !7, |
| 6398 | /// annotations: !8) |
| 6399 | bool LLParser::parseDISubprogram(MDNode *&Result, bool IsDistinct) { |
| 6400 | auto Loc = Lex.getLoc(); |
| 6401 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6402 | OPTIONAL(scope, MDField, ); \ |
| 6403 | OPTIONAL(name, MDStringField, ); \ |
| 6404 | OPTIONAL(linkageName, MDStringField, ); \ |
| 6405 | OPTIONAL(file, MDField, ); \ |
| 6406 | OPTIONAL(line, LineField, ); \ |
| 6407 | REQUIRED(type, MDField, (/* AllowNull */ false)); \ |
| 6408 | OPTIONAL(isLocal, MDBoolField, ); \ |
| 6409 | OPTIONAL(isDefinition, MDBoolField, (true)); \ |
| 6410 | OPTIONAL(scopeLine, LineField, ); \ |
| 6411 | OPTIONAL(containingType, MDField, ); \ |
| 6412 | OPTIONAL(virtuality, DwarfVirtualityField, ); \ |
| 6413 | OPTIONAL(virtualIndex, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6414 | OPTIONAL(thisAdjustment, MDSignedField, (0, INT32_MIN, INT32_MAX)); \ |
| 6415 | OPTIONAL(flags, DIFlagField, ); \ |
| 6416 | OPTIONAL(spFlags, DISPFlagField, ); \ |
| 6417 | OPTIONAL(isOptimized, MDBoolField, ); \ |
| 6418 | OPTIONAL(unit, MDField, ); \ |
| 6419 | OPTIONAL(templateParams, MDField, ); \ |
| 6420 | OPTIONAL(declaration, MDField, ); \ |
| 6421 | OPTIONAL(retainedNodes, MDField, ); \ |
| 6422 | OPTIONAL(thrownTypes, MDField, ); \ |
| 6423 | OPTIONAL(annotations, MDField, ); \ |
| 6424 | OPTIONAL(targetFuncName, MDStringField, ); \ |
| 6425 | OPTIONAL(keyInstructions, MDBoolField, ); |
| 6426 | PARSE_MD_FIELDS(); |
| 6427 | #undef VISIT_MD_FIELDS |
| 6428 | |
| 6429 | // An explicit spFlags field takes precedence over individual fields in |
| 6430 | // older IR versions. |
| 6431 | DISubprogram::DISPFlags SPFlags = |
| 6432 | spFlags.Seen ? spFlags.Val |
| 6433 | : DISubprogram::toSPFlags(IsLocalToUnit: isLocal.Val, IsDefinition: isDefinition.Val, |
| 6434 | IsOptimized: isOptimized.Val, Virtuality: virtuality.Val); |
| 6435 | if ((SPFlags & DISubprogram::SPFlagDefinition) && !IsDistinct) |
| 6436 | return error( |
| 6437 | L: Loc, |
| 6438 | Msg: "missing 'distinct', required for !DISubprogram that is a Definition" ); |
| 6439 | Result = GET_OR_DISTINCT( |
| 6440 | DISubprogram, |
| 6441 | (Context, scope.Val, name.Val, linkageName.Val, file.Val, line.Val, |
| 6442 | type.Val, scopeLine.Val, containingType.Val, virtualIndex.Val, |
| 6443 | thisAdjustment.Val, flags.Val, SPFlags, unit.Val, templateParams.Val, |
| 6444 | declaration.Val, retainedNodes.Val, thrownTypes.Val, annotations.Val, |
| 6445 | targetFuncName.Val, keyInstructions.Val)); |
| 6446 | |
| 6447 | if (IsDistinct) |
| 6448 | NewDistinctSPs.push_back(Elt: cast<DISubprogram>(Val: Result)); |
| 6449 | |
| 6450 | return false; |
| 6451 | } |
| 6452 | |
| 6453 | /// parseDILexicalBlock: |
| 6454 | /// ::= !DILexicalBlock(scope: !0, file: !2, line: 7, column: 9) |
| 6455 | bool LLParser::parseDILexicalBlock(MDNode *&Result, bool IsDistinct) { |
| 6456 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6457 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
| 6458 | OPTIONAL(file, MDField, ); \ |
| 6459 | OPTIONAL(line, LineField, ); \ |
| 6460 | OPTIONAL(column, ColumnField, ); |
| 6461 | PARSE_MD_FIELDS(); |
| 6462 | #undef VISIT_MD_FIELDS |
| 6463 | |
| 6464 | Result = GET_OR_DISTINCT( |
| 6465 | DILexicalBlock, (Context, scope.Val, file.Val, line.Val, column.Val)); |
| 6466 | return false; |
| 6467 | } |
| 6468 | |
| 6469 | /// parseDILexicalBlockFile: |
| 6470 | /// ::= !DILexicalBlockFile(scope: !0, file: !2, discriminator: 9) |
| 6471 | bool LLParser::parseDILexicalBlockFile(MDNode *&Result, bool IsDistinct) { |
| 6472 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6473 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
| 6474 | OPTIONAL(file, MDField, ); \ |
| 6475 | REQUIRED(discriminator, MDUnsignedField, (0, UINT32_MAX)); |
| 6476 | PARSE_MD_FIELDS(); |
| 6477 | #undef VISIT_MD_FIELDS |
| 6478 | |
| 6479 | Result = GET_OR_DISTINCT(DILexicalBlockFile, |
| 6480 | (Context, scope.Val, file.Val, discriminator.Val)); |
| 6481 | return false; |
| 6482 | } |
| 6483 | |
| 6484 | /// parseDICommonBlock: |
| 6485 | /// ::= !DICommonBlock(scope: !0, file: !2, name: "COMMON name", line: 9) |
| 6486 | bool LLParser::parseDICommonBlock(MDNode *&Result, bool IsDistinct) { |
| 6487 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6488 | REQUIRED(scope, MDField, ); \ |
| 6489 | OPTIONAL(declaration, MDField, ); \ |
| 6490 | OPTIONAL(name, MDStringField, ); \ |
| 6491 | OPTIONAL(file, MDField, ); \ |
| 6492 | OPTIONAL(line, LineField, ); |
| 6493 | PARSE_MD_FIELDS(); |
| 6494 | #undef VISIT_MD_FIELDS |
| 6495 | |
| 6496 | Result = GET_OR_DISTINCT(DICommonBlock, |
| 6497 | (Context, scope.Val, declaration.Val, name.Val, |
| 6498 | file.Val, line.Val)); |
| 6499 | return false; |
| 6500 | } |
| 6501 | |
| 6502 | /// parseDINamespace: |
| 6503 | /// ::= !DINamespace(scope: !0, file: !2, name: "SomeNamespace", line: 9) |
| 6504 | bool LLParser::parseDINamespace(MDNode *&Result, bool IsDistinct) { |
| 6505 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6506 | REQUIRED(scope, MDField, ); \ |
| 6507 | OPTIONAL(name, MDStringField, ); \ |
| 6508 | OPTIONAL(exportSymbols, MDBoolField, ); |
| 6509 | PARSE_MD_FIELDS(); |
| 6510 | #undef VISIT_MD_FIELDS |
| 6511 | |
| 6512 | Result = GET_OR_DISTINCT(DINamespace, |
| 6513 | (Context, scope.Val, name.Val, exportSymbols.Val)); |
| 6514 | return false; |
| 6515 | } |
| 6516 | |
| 6517 | /// parseDIMacro: |
| 6518 | /// ::= !DIMacro(macinfo: type, line: 9, name: "SomeMacro", value: |
| 6519 | /// "SomeValue") |
| 6520 | bool LLParser::parseDIMacro(MDNode *&Result, bool IsDistinct) { |
| 6521 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6522 | REQUIRED(type, DwarfMacinfoTypeField, ); \ |
| 6523 | OPTIONAL(line, LineField, ); \ |
| 6524 | REQUIRED(name, MDStringField, ); \ |
| 6525 | OPTIONAL(value, MDStringField, ); |
| 6526 | PARSE_MD_FIELDS(); |
| 6527 | #undef VISIT_MD_FIELDS |
| 6528 | |
| 6529 | Result = GET_OR_DISTINCT(DIMacro, |
| 6530 | (Context, type.Val, line.Val, name.Val, value.Val)); |
| 6531 | return false; |
| 6532 | } |
| 6533 | |
| 6534 | /// parseDIMacroFile: |
| 6535 | /// ::= !DIMacroFile(line: 9, file: !2, nodes: !3) |
| 6536 | bool LLParser::parseDIMacroFile(MDNode *&Result, bool IsDistinct) { |
| 6537 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6538 | OPTIONAL(type, DwarfMacinfoTypeField, (dwarf::DW_MACINFO_start_file)); \ |
| 6539 | OPTIONAL(line, LineField, ); \ |
| 6540 | REQUIRED(file, MDField, ); \ |
| 6541 | OPTIONAL(nodes, MDField, ); |
| 6542 | PARSE_MD_FIELDS(); |
| 6543 | #undef VISIT_MD_FIELDS |
| 6544 | |
| 6545 | Result = GET_OR_DISTINCT(DIMacroFile, |
| 6546 | (Context, type.Val, line.Val, file.Val, nodes.Val)); |
| 6547 | return false; |
| 6548 | } |
| 6549 | |
| 6550 | /// parseDIModule: |
| 6551 | /// ::= !DIModule(scope: !0, name: "SomeModule", configMacros: |
| 6552 | /// "-DNDEBUG", includePath: "/usr/include", apinotes: "module.apinotes", |
| 6553 | /// file: !1, line: 4, isDecl: false) |
| 6554 | bool LLParser::parseDIModule(MDNode *&Result, bool IsDistinct) { |
| 6555 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6556 | REQUIRED(scope, MDField, ); \ |
| 6557 | REQUIRED(name, MDStringField, ); \ |
| 6558 | OPTIONAL(configMacros, MDStringField, ); \ |
| 6559 | OPTIONAL(includePath, MDStringField, ); \ |
| 6560 | OPTIONAL(apinotes, MDStringField, ); \ |
| 6561 | OPTIONAL(file, MDField, ); \ |
| 6562 | OPTIONAL(line, LineField, ); \ |
| 6563 | OPTIONAL(isDecl, MDBoolField, ); |
| 6564 | PARSE_MD_FIELDS(); |
| 6565 | #undef VISIT_MD_FIELDS |
| 6566 | |
| 6567 | Result = GET_OR_DISTINCT(DIModule, (Context, file.Val, scope.Val, name.Val, |
| 6568 | configMacros.Val, includePath.Val, |
| 6569 | apinotes.Val, line.Val, isDecl.Val)); |
| 6570 | return false; |
| 6571 | } |
| 6572 | |
| 6573 | /// parseDITemplateTypeParameter: |
| 6574 | /// ::= !DITemplateTypeParameter(name: "Ty", type: !1, defaulted: false) |
| 6575 | bool LLParser::parseDITemplateTypeParameter(MDNode *&Result, bool IsDistinct) { |
| 6576 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6577 | OPTIONAL(name, MDStringField, ); \ |
| 6578 | REQUIRED(type, MDField, ); \ |
| 6579 | OPTIONAL(defaulted, MDBoolField, ); |
| 6580 | PARSE_MD_FIELDS(); |
| 6581 | #undef VISIT_MD_FIELDS |
| 6582 | |
| 6583 | Result = GET_OR_DISTINCT(DITemplateTypeParameter, |
| 6584 | (Context, name.Val, type.Val, defaulted.Val)); |
| 6585 | return false; |
| 6586 | } |
| 6587 | |
| 6588 | /// parseDITemplateValueParameter: |
| 6589 | /// ::= !DITemplateValueParameter(tag: DW_TAG_template_value_parameter, |
| 6590 | /// name: "V", type: !1, defaulted: false, |
| 6591 | /// value: i32 7) |
| 6592 | bool LLParser::parseDITemplateValueParameter(MDNode *&Result, bool IsDistinct) { |
| 6593 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6594 | OPTIONAL(tag, DwarfTagField, (dwarf::DW_TAG_template_value_parameter)); \ |
| 6595 | OPTIONAL(name, MDStringField, ); \ |
| 6596 | OPTIONAL(type, MDField, ); \ |
| 6597 | OPTIONAL(defaulted, MDBoolField, ); \ |
| 6598 | REQUIRED(value, MDField, ); |
| 6599 | |
| 6600 | PARSE_MD_FIELDS(); |
| 6601 | #undef VISIT_MD_FIELDS |
| 6602 | |
| 6603 | Result = GET_OR_DISTINCT( |
| 6604 | DITemplateValueParameter, |
| 6605 | (Context, tag.Val, name.Val, type.Val, defaulted.Val, value.Val)); |
| 6606 | return false; |
| 6607 | } |
| 6608 | |
| 6609 | /// parseDIGlobalVariable: |
| 6610 | /// ::= !DIGlobalVariable(scope: !0, name: "foo", linkageName: "foo", |
| 6611 | /// file: !1, line: 7, type: !2, isLocal: false, |
| 6612 | /// isDefinition: true, templateParams: !3, |
| 6613 | /// declaration: !4, align: 8) |
| 6614 | bool LLParser::parseDIGlobalVariable(MDNode *&Result, bool IsDistinct) { |
| 6615 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6616 | OPTIONAL(name, MDStringField, (MDStringField::EmptyIs::Error)); \ |
| 6617 | OPTIONAL(scope, MDField, ); \ |
| 6618 | OPTIONAL(linkageName, MDStringField, ); \ |
| 6619 | OPTIONAL(file, MDField, ); \ |
| 6620 | OPTIONAL(line, LineField, ); \ |
| 6621 | OPTIONAL(type, MDField, ); \ |
| 6622 | OPTIONAL(isLocal, MDBoolField, ); \ |
| 6623 | OPTIONAL(isDefinition, MDBoolField, (true)); \ |
| 6624 | OPTIONAL(templateParams, MDField, ); \ |
| 6625 | OPTIONAL(declaration, MDField, ); \ |
| 6626 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6627 | OPTIONAL(annotations, MDField, ); |
| 6628 | PARSE_MD_FIELDS(); |
| 6629 | #undef VISIT_MD_FIELDS |
| 6630 | |
| 6631 | Result = |
| 6632 | GET_OR_DISTINCT(DIGlobalVariable, |
| 6633 | (Context, scope.Val, name.Val, linkageName.Val, file.Val, |
| 6634 | line.Val, type.Val, isLocal.Val, isDefinition.Val, |
| 6635 | declaration.Val, templateParams.Val, align.Val, |
| 6636 | annotations.Val)); |
| 6637 | return false; |
| 6638 | } |
| 6639 | |
| 6640 | /// parseDILocalVariable: |
| 6641 | /// ::= !DILocalVariable(arg: 7, scope: !0, name: "foo", |
| 6642 | /// file: !1, line: 7, type: !2, arg: 2, flags: 7, |
| 6643 | /// align: 8) |
| 6644 | /// ::= !DILocalVariable(scope: !0, name: "foo", |
| 6645 | /// file: !1, line: 7, type: !2, arg: 2, flags: 7, |
| 6646 | /// align: 8) |
| 6647 | bool LLParser::parseDILocalVariable(MDNode *&Result, bool IsDistinct) { |
| 6648 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6649 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
| 6650 | OPTIONAL(name, MDStringField, ); \ |
| 6651 | OPTIONAL(arg, MDUnsignedField, (0, UINT16_MAX)); \ |
| 6652 | OPTIONAL(file, MDField, ); \ |
| 6653 | OPTIONAL(line, LineField, ); \ |
| 6654 | OPTIONAL(type, MDField, ); \ |
| 6655 | OPTIONAL(flags, DIFlagField, ); \ |
| 6656 | OPTIONAL(align, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6657 | OPTIONAL(annotations, MDField, ); |
| 6658 | PARSE_MD_FIELDS(); |
| 6659 | #undef VISIT_MD_FIELDS |
| 6660 | |
| 6661 | Result = GET_OR_DISTINCT(DILocalVariable, |
| 6662 | (Context, scope.Val, name.Val, file.Val, line.Val, |
| 6663 | type.Val, arg.Val, flags.Val, align.Val, |
| 6664 | annotations.Val)); |
| 6665 | return false; |
| 6666 | } |
| 6667 | |
| 6668 | /// parseDILabel: |
| 6669 | /// ::= !DILabel(scope: !0, name: "foo", file: !1, line: 7, column: 4) |
| 6670 | bool LLParser::parseDILabel(MDNode *&Result, bool IsDistinct) { |
| 6671 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6672 | REQUIRED(scope, MDField, (/* AllowNull */ false)); \ |
| 6673 | REQUIRED(name, MDStringField, ); \ |
| 6674 | REQUIRED(file, MDField, ); \ |
| 6675 | REQUIRED(line, LineField, ); \ |
| 6676 | OPTIONAL(column, ColumnField, ); \ |
| 6677 | OPTIONAL(isArtificial, MDBoolField, ); \ |
| 6678 | OPTIONAL(coroSuspendIdx, MDUnsignedField, ); |
| 6679 | PARSE_MD_FIELDS(); |
| 6680 | #undef VISIT_MD_FIELDS |
| 6681 | |
| 6682 | std::optional<unsigned> CoroSuspendIdx = |
| 6683 | coroSuspendIdx.Seen ? std::optional<unsigned>(coroSuspendIdx.Val) |
| 6684 | : std::nullopt; |
| 6685 | |
| 6686 | Result = GET_OR_DISTINCT(DILabel, |
| 6687 | (Context, scope.Val, name.Val, file.Val, line.Val, |
| 6688 | column.Val, isArtificial.Val, CoroSuspendIdx)); |
| 6689 | return false; |
| 6690 | } |
| 6691 | |
| 6692 | /// parseDIExpressionBody: |
| 6693 | /// ::= (0, 7, -1) |
| 6694 | bool LLParser::parseDIExpressionBody(MDNode *&Result, bool IsDistinct) { |
| 6695 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 6696 | return true; |
| 6697 | |
| 6698 | SmallVector<uint64_t, 8> Elements; |
| 6699 | if (Lex.getKind() != lltok::rparen) |
| 6700 | do { |
| 6701 | if (Lex.getKind() == lltok::DwarfOp) { |
| 6702 | if (unsigned Op = dwarf::getOperationEncoding(OperationEncodingString: Lex.getStrVal())) { |
| 6703 | Lex.Lex(); |
| 6704 | Elements.push_back(Elt: Op); |
| 6705 | continue; |
| 6706 | } |
| 6707 | return tokError(Msg: Twine("invalid DWARF op '" ) + Lex.getStrVal() + "'" ); |
| 6708 | } |
| 6709 | |
| 6710 | if (Lex.getKind() == lltok::DwarfAttEncoding) { |
| 6711 | if (unsigned Op = dwarf::getAttributeEncoding(EncodingString: Lex.getStrVal())) { |
| 6712 | Lex.Lex(); |
| 6713 | Elements.push_back(Elt: Op); |
| 6714 | continue; |
| 6715 | } |
| 6716 | return tokError(Msg: Twine("invalid DWARF attribute encoding '" ) + |
| 6717 | Lex.getStrVal() + "'" ); |
| 6718 | } |
| 6719 | |
| 6720 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 6721 | return tokError(Msg: "expected unsigned integer" ); |
| 6722 | |
| 6723 | auto &U = Lex.getAPSIntVal(); |
| 6724 | if (U.ugt(UINT64_MAX)) |
| 6725 | return tokError(Msg: "element too large, limit is " + Twine(UINT64_MAX)); |
| 6726 | Elements.push_back(Elt: U.getZExtValue()); |
| 6727 | Lex.Lex(); |
| 6728 | } while (EatIfPresent(T: lltok::comma)); |
| 6729 | |
| 6730 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 6731 | return true; |
| 6732 | |
| 6733 | Result = GET_OR_DISTINCT(DIExpression, (Context, Elements)); |
| 6734 | return false; |
| 6735 | } |
| 6736 | |
| 6737 | /// parseDIExpression: |
| 6738 | /// ::= !DIExpression(0, 7, -1) |
| 6739 | bool LLParser::parseDIExpression(MDNode *&Result, bool IsDistinct) { |
| 6740 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
| 6741 | assert(Lex.getStrVal() == "DIExpression" && "Expected '!DIExpression'" ); |
| 6742 | Lex.Lex(); |
| 6743 | |
| 6744 | return parseDIExpressionBody(Result, IsDistinct); |
| 6745 | } |
| 6746 | |
| 6747 | /// ParseDIArgList: |
| 6748 | /// ::= !DIArgList(i32 7, i64 %0) |
| 6749 | bool LLParser::parseDIArgList(Metadata *&MD, PerFunctionState *PFS) { |
| 6750 | assert(PFS && "Expected valid function state" ); |
| 6751 | assert(Lex.getKind() == lltok::MetadataVar && "Expected metadata type name" ); |
| 6752 | Lex.Lex(); |
| 6753 | |
| 6754 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 6755 | return true; |
| 6756 | |
| 6757 | SmallVector<ValueAsMetadata *, 4> Args; |
| 6758 | if (Lex.getKind() != lltok::rparen) |
| 6759 | do { |
| 6760 | Metadata *MD; |
| 6761 | if (parseValueAsMetadata(MD, TypeMsg: "expected value-as-metadata operand" , PFS)) |
| 6762 | return true; |
| 6763 | Args.push_back(Elt: dyn_cast<ValueAsMetadata>(Val: MD)); |
| 6764 | } while (EatIfPresent(T: lltok::comma)); |
| 6765 | |
| 6766 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 6767 | return true; |
| 6768 | |
| 6769 | MD = DIArgList::get(Context, Args); |
| 6770 | return false; |
| 6771 | } |
| 6772 | |
| 6773 | /// parseDIGlobalVariableExpression: |
| 6774 | /// ::= !DIGlobalVariableExpression(var: !0, expr: !1) |
| 6775 | bool LLParser::parseDIGlobalVariableExpression(MDNode *&Result, |
| 6776 | bool IsDistinct) { |
| 6777 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6778 | REQUIRED(var, MDField, ); \ |
| 6779 | REQUIRED(expr, MDField, ); |
| 6780 | PARSE_MD_FIELDS(); |
| 6781 | #undef VISIT_MD_FIELDS |
| 6782 | |
| 6783 | Result = |
| 6784 | GET_OR_DISTINCT(DIGlobalVariableExpression, (Context, var.Val, expr.Val)); |
| 6785 | return false; |
| 6786 | } |
| 6787 | |
| 6788 | /// parseDIObjCProperty: |
| 6789 | /// ::= !DIObjCProperty(name: "foo", file: !1, line: 7, setter: "setFoo", |
| 6790 | /// getter: "getFoo", attributes: 7, type: !2) |
| 6791 | bool LLParser::parseDIObjCProperty(MDNode *&Result, bool IsDistinct) { |
| 6792 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6793 | OPTIONAL(name, MDStringField, ); \ |
| 6794 | OPTIONAL(file, MDField, ); \ |
| 6795 | OPTIONAL(line, LineField, ); \ |
| 6796 | OPTIONAL(setter, MDStringField, ); \ |
| 6797 | OPTIONAL(getter, MDStringField, ); \ |
| 6798 | OPTIONAL(attributes, MDUnsignedField, (0, UINT32_MAX)); \ |
| 6799 | OPTIONAL(type, MDField, ); |
| 6800 | PARSE_MD_FIELDS(); |
| 6801 | #undef VISIT_MD_FIELDS |
| 6802 | |
| 6803 | Result = GET_OR_DISTINCT(DIObjCProperty, |
| 6804 | (Context, name.Val, file.Val, line.Val, getter.Val, |
| 6805 | setter.Val, attributes.Val, type.Val)); |
| 6806 | return false; |
| 6807 | } |
| 6808 | |
| 6809 | /// parseDIProperty: |
| 6810 | /// ::= !DIProperty(name: "x", file: !1, line: 7, type: !2, |
| 6811 | /// backing_storage: !3) |
| 6812 | bool LLParser::parseDIProperty(MDNode *&Result, bool IsDistinct) { |
| 6813 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6814 | OPTIONAL(name, MDStringField, ); \ |
| 6815 | OPTIONAL(file, MDField, ); \ |
| 6816 | OPTIONAL(line, LineField, ); \ |
| 6817 | OPTIONAL(type, MDField, ); \ |
| 6818 | OPTIONAL(backing_storage, MDField, ); |
| 6819 | PARSE_MD_FIELDS(); |
| 6820 | #undef VISIT_MD_FIELDS |
| 6821 | |
| 6822 | Result = GET_OR_DISTINCT(DIProperty, (Context, name.Val, file.Val, line.Val, |
| 6823 | type.Val, backing_storage.Val)); |
| 6824 | return false; |
| 6825 | } |
| 6826 | |
| 6827 | /// parseDIImportedEntity: |
| 6828 | /// ::= !DIImportedEntity(tag: DW_TAG_imported_module, scope: !0, entity: !1, |
| 6829 | /// line: 7, name: "foo", elements: !2) |
| 6830 | bool LLParser::parseDIImportedEntity(MDNode *&Result, bool IsDistinct) { |
| 6831 | #define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \ |
| 6832 | REQUIRED(tag, DwarfTagField, ); \ |
| 6833 | REQUIRED(scope, MDField, ); \ |
| 6834 | OPTIONAL(entity, MDField, ); \ |
| 6835 | OPTIONAL(file, MDField, ); \ |
| 6836 | OPTIONAL(line, LineField, ); \ |
| 6837 | OPTIONAL(name, MDStringField, ); \ |
| 6838 | OPTIONAL(elements, MDField, ); |
| 6839 | PARSE_MD_FIELDS(); |
| 6840 | #undef VISIT_MD_FIELDS |
| 6841 | |
| 6842 | Result = GET_OR_DISTINCT(DIImportedEntity, |
| 6843 | (Context, tag.Val, scope.Val, entity.Val, file.Val, |
| 6844 | line.Val, name.Val, elements.Val)); |
| 6845 | return false; |
| 6846 | } |
| 6847 | |
| 6848 | #undef PARSE_MD_FIELD |
| 6849 | #undef NOP_FIELD |
| 6850 | #undef REQUIRE_FIELD |
| 6851 | #undef DECLARE_FIELD |
| 6852 | |
| 6853 | /// parseMetadataAsValue |
| 6854 | /// ::= metadata i32 %local |
| 6855 | /// ::= metadata i32 @global |
| 6856 | /// ::= metadata i32 7 |
| 6857 | /// ::= metadata !0 |
| 6858 | /// ::= metadata !{...} |
| 6859 | /// ::= metadata !"string" |
| 6860 | bool LLParser::parseMetadataAsValue(Value *&V, PerFunctionState &PFS) { |
| 6861 | // Note: the type 'metadata' has already been parsed. |
| 6862 | Metadata *MD; |
| 6863 | if (parseMetadata(MD, PFS: &PFS)) |
| 6864 | return true; |
| 6865 | |
| 6866 | V = MetadataAsValue::get(Context, MD); |
| 6867 | return false; |
| 6868 | } |
| 6869 | |
| 6870 | /// parseValueAsMetadata |
| 6871 | /// ::= i32 %local |
| 6872 | /// ::= i32 @global |
| 6873 | /// ::= i32 7 |
| 6874 | bool LLParser::parseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg, |
| 6875 | PerFunctionState *PFS) { |
| 6876 | Type *Ty; |
| 6877 | LocTy Loc; |
| 6878 | if (parseType(Result&: Ty, Msg: TypeMsg, Loc)) |
| 6879 | return true; |
| 6880 | if (Ty->isMetadataTy()) |
| 6881 | return error(L: Loc, Msg: "invalid metadata-value-metadata roundtrip" ); |
| 6882 | |
| 6883 | Value *V; |
| 6884 | if (parseValue(Ty, V, PFS)) |
| 6885 | return true; |
| 6886 | |
| 6887 | MD = ValueAsMetadata::get(V); |
| 6888 | return false; |
| 6889 | } |
| 6890 | |
| 6891 | /// parseMetadata |
| 6892 | /// ::= i32 %local |
| 6893 | /// ::= i32 @global |
| 6894 | /// ::= i32 7 |
| 6895 | /// ::= !42 |
| 6896 | /// ::= !{...} |
| 6897 | /// ::= !"string" |
| 6898 | /// ::= !DILocation(...) |
| 6899 | bool LLParser::parseMetadata(Metadata *&MD, PerFunctionState *PFS) { |
| 6900 | if (Lex.getKind() == lltok::MetadataVar) { |
| 6901 | // DIArgLists are a special case, as they are a list of ValueAsMetadata and |
| 6902 | // so parsing this requires a Function State. |
| 6903 | if (Lex.getStrVal() == "DIArgList" ) { |
| 6904 | Metadata *AL; |
| 6905 | if (parseDIArgList(MD&: AL, PFS)) |
| 6906 | return true; |
| 6907 | MD = AL; |
| 6908 | return false; |
| 6909 | } |
| 6910 | MDNode *N; |
| 6911 | if (parseSpecializedMDNode(N)) { |
| 6912 | return true; |
| 6913 | } |
| 6914 | MD = N; |
| 6915 | return false; |
| 6916 | } |
| 6917 | |
| 6918 | // ValueAsMetadata: |
| 6919 | // <type> <value> |
| 6920 | if (Lex.getKind() != lltok::exclaim) |
| 6921 | return parseValueAsMetadata(MD, TypeMsg: "expected metadata operand" , PFS); |
| 6922 | |
| 6923 | // '!'. |
| 6924 | assert(Lex.getKind() == lltok::exclaim && "Expected '!' here" ); |
| 6925 | Lex.Lex(); |
| 6926 | |
| 6927 | // MDString: |
| 6928 | // ::= '!' STRINGCONSTANT |
| 6929 | if (Lex.getKind() == lltok::StringConstant) { |
| 6930 | MDString *S; |
| 6931 | if (parseMDString(Result&: S)) |
| 6932 | return true; |
| 6933 | MD = S; |
| 6934 | return false; |
| 6935 | } |
| 6936 | |
| 6937 | // MDNode: |
| 6938 | // !{ ... } |
| 6939 | // !7 |
| 6940 | MDNode *N; |
| 6941 | if (parseMDNodeTail(N)) |
| 6942 | return true; |
| 6943 | MD = N; |
| 6944 | return false; |
| 6945 | } |
| 6946 | |
| 6947 | //===----------------------------------------------------------------------===// |
| 6948 | // Function Parsing. |
| 6949 | //===----------------------------------------------------------------------===// |
| 6950 | |
| 6951 | bool LLParser::convertValIDToValue(Type *Ty, ValID &ID, Value *&V, |
| 6952 | PerFunctionState *PFS) { |
| 6953 | if (Ty->isFunctionTy()) |
| 6954 | return error(L: ID.Loc, Msg: "functions are not values, refer to them as pointers" ); |
| 6955 | |
| 6956 | switch (ID.Kind) { |
| 6957 | case ValID::t_LocalID: |
| 6958 | if (!PFS) |
| 6959 | return error(L: ID.Loc, Msg: "invalid use of function-local name" ); |
| 6960 | V = PFS->getVal(ID: ID.UIntVal, Ty, Loc: ID.Loc); |
| 6961 | return V == nullptr; |
| 6962 | case ValID::t_LocalName: |
| 6963 | if (!PFS) |
| 6964 | return error(L: ID.Loc, Msg: "invalid use of function-local name" ); |
| 6965 | V = PFS->getVal(Name: ID.StrVal, Ty, Loc: ID.Loc); |
| 6966 | return V == nullptr; |
| 6967 | case ValID::t_InlineAsm: { |
| 6968 | if (!ID.FTy) |
| 6969 | return error(L: ID.Loc, Msg: "invalid type for inline asm constraint string" ); |
| 6970 | if (Error Err = InlineAsm::verify(Ty: ID.FTy, Constraints: ID.StrVal2)) |
| 6971 | return error(L: ID.Loc, Msg: toString(E: std::move(Err))); |
| 6972 | V = InlineAsm::get( |
| 6973 | Ty: ID.FTy, AsmString: ID.StrVal, Constraints: ID.StrVal2, hasSideEffects: ID.UIntVal & 1, isAlignStack: (ID.UIntVal >> 1) & 1, |
| 6974 | asmDialect: InlineAsm::AsmDialect((ID.UIntVal >> 2) & 1), canThrow: (ID.UIntVal >> 3) & 1); |
| 6975 | return false; |
| 6976 | } |
| 6977 | case ValID::t_GlobalName: |
| 6978 | V = getGlobalVal(Name: ID.StrVal, Ty, Loc: ID.Loc); |
| 6979 | if (V && ID.NoCFI) |
| 6980 | V = NoCFIValue::get(GV: cast<GlobalValue>(Val: V)); |
| 6981 | return V == nullptr; |
| 6982 | case ValID::t_GlobalID: |
| 6983 | V = getGlobalVal(ID: ID.UIntVal, Ty, Loc: ID.Loc); |
| 6984 | if (V && ID.NoCFI) |
| 6985 | V = NoCFIValue::get(GV: cast<GlobalValue>(Val: V)); |
| 6986 | return V == nullptr; |
| 6987 | case ValID::t_APSInt: |
| 6988 | if (!Ty->isIntegerTy() && !Ty->isByteTy()) |
| 6989 | return error(L: ID.Loc, Msg: "integer/byte constant must have integer/byte type" ); |
| 6990 | ID.APSIntVal = ID.APSIntVal.extOrTrunc(width: Ty->getPrimitiveSizeInBits()); |
| 6991 | Ty->isIntegerTy() ? V = ConstantInt::get(Context, V: ID.APSIntVal) |
| 6992 | : V = ConstantByte::get(Context, V: ID.APSIntVal); |
| 6993 | return false; |
| 6994 | case ValID::t_APFloat: |
| 6995 | if (!Ty->isFloatingPointTy() || |
| 6996 | !ConstantFP::isValueValidForType(Ty, V: ID.APFloatVal)) |
| 6997 | return error(L: ID.Loc, Msg: "floating point constant invalid for type" ); |
| 6998 | |
| 6999 | // The lexer has no type info, so builds all half, bfloat, float, and double |
| 7000 | // FP constants as double. Fix this here. Long double does not need this. |
| 7001 | if (&ID.APFloatVal.getSemantics() == &APFloat::IEEEdouble()) { |
| 7002 | // Check for signaling before potentially converting and losing that info. |
| 7003 | bool IsSNAN = ID.APFloatVal.isSignaling(); |
| 7004 | bool Ignored; |
| 7005 | if (Ty->isHalfTy()) |
| 7006 | ID.APFloatVal.convert(ToSemantics: APFloat::IEEEhalf(), RM: APFloat::rmNearestTiesToEven, |
| 7007 | losesInfo: &Ignored); |
| 7008 | else if (Ty->isBFloatTy()) |
| 7009 | ID.APFloatVal.convert(ToSemantics: APFloat::BFloat(), RM: APFloat::rmNearestTiesToEven, |
| 7010 | losesInfo: &Ignored); |
| 7011 | else if (Ty->isFloatTy()) |
| 7012 | ID.APFloatVal.convert(ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, |
| 7013 | losesInfo: &Ignored); |
| 7014 | if (IsSNAN) { |
| 7015 | // The convert call above may quiet an SNaN, so manufacture another |
| 7016 | // SNaN. The bitcast works because the payload (significand) parameter |
| 7017 | // is truncated to fit. |
| 7018 | APInt Payload = ID.APFloatVal.bitcastToAPInt(); |
| 7019 | ID.APFloatVal = APFloat::getSNaN(Sem: ID.APFloatVal.getSemantics(), |
| 7020 | Negative: ID.APFloatVal.isNegative(), payload: &Payload); |
| 7021 | } |
| 7022 | } |
| 7023 | V = ConstantFP::get(Context, V: ID.APFloatVal); |
| 7024 | |
| 7025 | if (V->getType() != Ty) |
| 7026 | return error(L: ID.Loc, Msg: "floating point constant does not have type '" + |
| 7027 | getTypeString(T: Ty) + "'" ); |
| 7028 | |
| 7029 | return false; |
| 7030 | case ValID::t_Null: |
| 7031 | if (!Ty->isPointerTy()) |
| 7032 | return error(L: ID.Loc, Msg: "null must be a pointer type" ); |
| 7033 | V = ConstantPointerNull::get(T: cast<PointerType>(Val: Ty)); |
| 7034 | return false; |
| 7035 | case ValID::t_Undef: |
| 7036 | // FIXME: LabelTy should not be a first-class type. |
| 7037 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
| 7038 | return error(L: ID.Loc, Msg: "invalid type for undef constant" ); |
| 7039 | V = UndefValue::get(T: Ty); |
| 7040 | return false; |
| 7041 | case ValID::t_EmptyArray: |
| 7042 | if (!Ty->isArrayTy() || cast<ArrayType>(Val: Ty)->getNumElements() != 0) |
| 7043 | return error(L: ID.Loc, Msg: "invalid empty array initializer" ); |
| 7044 | V = PoisonValue::get(T: Ty); |
| 7045 | return false; |
| 7046 | case ValID::t_Zero: |
| 7047 | // FIXME: LabelTy should not be a first-class type. |
| 7048 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
| 7049 | return error(L: ID.Loc, Msg: "invalid type for null constant" ); |
| 7050 | if (auto *TETy = dyn_cast<TargetExtType>(Val: Ty)) |
| 7051 | if (!TETy->hasProperty(Prop: TargetExtType::HasZeroInit)) |
| 7052 | return error(L: ID.Loc, Msg: "invalid type for null constant" ); |
| 7053 | V = Constant::getNullValue(Ty); |
| 7054 | return false; |
| 7055 | case ValID::t_None: |
| 7056 | if (!Ty->isTokenTy()) |
| 7057 | return error(L: ID.Loc, Msg: "invalid type for none constant" ); |
| 7058 | V = Constant::getNullValue(Ty); |
| 7059 | return false; |
| 7060 | case ValID::t_Poison: |
| 7061 | // FIXME: LabelTy should not be a first-class type. |
| 7062 | if (!Ty->isFirstClassType() || Ty->isLabelTy()) |
| 7063 | return error(L: ID.Loc, Msg: "invalid type for poison constant" ); |
| 7064 | V = PoisonValue::get(T: Ty); |
| 7065 | return false; |
| 7066 | case ValID::t_Constant: |
| 7067 | if (ID.ConstantVal->getType() != Ty) |
| 7068 | return error(L: ID.Loc, Msg: "constant expression type mismatch: got type '" + |
| 7069 | getTypeString(T: ID.ConstantVal->getType()) + |
| 7070 | "' but expected '" + getTypeString(T: Ty) + "'" ); |
| 7071 | V = ID.ConstantVal; |
| 7072 | return false; |
| 7073 | case ValID::t_ConstantSplat: |
| 7074 | if (!Ty->isVectorTy()) |
| 7075 | return error(L: ID.Loc, Msg: "vector constant must have vector type" ); |
| 7076 | if (ID.ConstantVal->getType() != Ty->getScalarType()) |
| 7077 | return error(L: ID.Loc, Msg: "constant expression type mismatch: got type '" + |
| 7078 | getTypeString(T: ID.ConstantVal->getType()) + |
| 7079 | "' but expected '" + |
| 7080 | getTypeString(T: Ty->getScalarType()) + "'" ); |
| 7081 | V = ConstantVector::getSplat(EC: cast<VectorType>(Val: Ty)->getElementCount(), |
| 7082 | Elt: ID.ConstantVal); |
| 7083 | return false; |
| 7084 | case ValID::t_ConstantStruct: |
| 7085 | case ValID::t_PackedConstantStruct: |
| 7086 | if (StructType *ST = dyn_cast<StructType>(Val: Ty)) { |
| 7087 | if (ST->getNumElements() != ID.UIntVal) |
| 7088 | return error(L: ID.Loc, |
| 7089 | Msg: "initializer with struct type has wrong # elements" ); |
| 7090 | if (ST->isPacked() != (ID.Kind == ValID::t_PackedConstantStruct)) |
| 7091 | return error(L: ID.Loc, Msg: "packed'ness of initializer and type don't match" ); |
| 7092 | |
| 7093 | // Verify that the elements are compatible with the structtype. |
| 7094 | for (unsigned i = 0, e = ID.UIntVal; i != e; ++i) |
| 7095 | if (ID.ConstantStructElts[i]->getType() != ST->getElementType(N: i)) |
| 7096 | return error( |
| 7097 | L: ID.Loc, |
| 7098 | Msg: "element " + Twine(i) + |
| 7099 | " of struct initializer doesn't match struct element type" ); |
| 7100 | |
| 7101 | V = ConstantStruct::get( |
| 7102 | T: ST, V: ArrayRef(ID.ConstantStructElts.get(), ID.UIntVal)); |
| 7103 | } else |
| 7104 | return error(L: ID.Loc, Msg: "constant expression type mismatch" ); |
| 7105 | return false; |
| 7106 | } |
| 7107 | llvm_unreachable("Invalid ValID" ); |
| 7108 | } |
| 7109 | |
| 7110 | bool LLParser::parseConstantValue(Type *Ty, Constant *&C) { |
| 7111 | C = nullptr; |
| 7112 | ValID ID; |
| 7113 | auto Loc = Lex.getLoc(); |
| 7114 | if (parseValID(ID, /*PFS=*/nullptr, /*ExpectedTy=*/Ty)) |
| 7115 | return true; |
| 7116 | switch (ID.Kind) { |
| 7117 | case ValID::t_APSInt: |
| 7118 | case ValID::t_APFloat: |
| 7119 | case ValID::t_Undef: |
| 7120 | case ValID::t_Poison: |
| 7121 | case ValID::t_Zero: |
| 7122 | case ValID::t_Constant: |
| 7123 | case ValID::t_ConstantSplat: |
| 7124 | case ValID::t_ConstantStruct: |
| 7125 | case ValID::t_PackedConstantStruct: { |
| 7126 | Value *V; |
| 7127 | if (convertValIDToValue(Ty, ID, V, /*PFS=*/nullptr)) |
| 7128 | return true; |
| 7129 | assert(isa<Constant>(V) && "Expected a constant value" ); |
| 7130 | C = cast<Constant>(Val: V); |
| 7131 | return false; |
| 7132 | } |
| 7133 | case ValID::t_Null: |
| 7134 | C = Constant::getNullValue(Ty); |
| 7135 | return false; |
| 7136 | default: |
| 7137 | return error(L: Loc, Msg: "expected a constant value" ); |
| 7138 | } |
| 7139 | } |
| 7140 | |
| 7141 | bool LLParser::parseValue(Type *Ty, Value *&V, PerFunctionState *PFS) { |
| 7142 | V = nullptr; |
| 7143 | ValID ID; |
| 7144 | |
| 7145 | FileLoc Start = getTokLineColumnPos(); |
| 7146 | bool Ret = parseValID(ID, PFS, ExpectedTy: Ty) || convertValIDToValue(Ty, ID, V, PFS); |
| 7147 | if (!Ret && ParserContext) { |
| 7148 | FileLoc End = getPrevTokEndLineColumnPos(); |
| 7149 | ParserContext->addValueReferenceAtLocation(V, FileLocRange(Start, End)); |
| 7150 | } |
| 7151 | return Ret; |
| 7152 | } |
| 7153 | |
| 7154 | bool LLParser::parseTypeAndValue(Value *&V, PerFunctionState *PFS) { |
| 7155 | Type *Ty = nullptr; |
| 7156 | return parseType(Result&: Ty) || parseValue(Ty, V, PFS); |
| 7157 | } |
| 7158 | |
| 7159 | bool LLParser::parseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, |
| 7160 | PerFunctionState &PFS) { |
| 7161 | Value *V; |
| 7162 | Loc = Lex.getLoc(); |
| 7163 | if (parseTypeAndValue(V, PFS)) |
| 7164 | return true; |
| 7165 | if (!isa<BasicBlock>(Val: V)) |
| 7166 | return error(L: Loc, Msg: "expected a basic block" ); |
| 7167 | BB = cast<BasicBlock>(Val: V); |
| 7168 | return false; |
| 7169 | } |
| 7170 | |
| 7171 | bool isOldDbgFormatIntrinsic(StringRef Name) { |
| 7172 | // Exit early for the common (non-debug-intrinsic) case. |
| 7173 | // We can make this the only check when we begin supporting all "llvm.dbg" |
| 7174 | // intrinsics in the new debug info format. |
| 7175 | if (!Name.starts_with(Prefix: "llvm.dbg." )) |
| 7176 | return false; |
| 7177 | Intrinsic::ID FnID = Intrinsic::lookupIntrinsicID(Name); |
| 7178 | return FnID == Intrinsic::dbg_declare || FnID == Intrinsic::dbg_value || |
| 7179 | FnID == Intrinsic::dbg_assign; |
| 7180 | } |
| 7181 | |
| 7182 | /// FunctionHeader |
| 7183 | /// ::= OptionalLinkage OptionalPreemptionSpecifier OptionalVisibility |
| 7184 | /// OptionalCallingConv OptRetAttrs OptUnnamedAddr Type GlobalName |
| 7185 | /// '(' ArgList ')' OptAddrSpace OptFuncAttrs OptSection OptionalAlign |
| 7186 | /// OptGC OptionalPrefix OptionalPrologue OptPersonalityFn |
| 7187 | bool LLParser::(Function *&Fn, bool IsDefine, |
| 7188 | unsigned &FunctionNumber, |
| 7189 | SmallVectorImpl<unsigned> &UnnamedArgNums) { |
| 7190 | // parse the linkage. |
| 7191 | LocTy LinkageLoc = Lex.getLoc(); |
| 7192 | unsigned Linkage; |
| 7193 | unsigned Visibility; |
| 7194 | unsigned DLLStorageClass; |
| 7195 | bool DSOLocal; |
| 7196 | AttrBuilder RetAttrs(M->getContext()); |
| 7197 | unsigned CC; |
| 7198 | bool HasLinkage; |
| 7199 | Type *RetType = nullptr; |
| 7200 | LocTy RetTypeLoc = Lex.getLoc(); |
| 7201 | if (parseOptionalLinkage(Res&: Linkage, HasLinkage, Visibility, DLLStorageClass, |
| 7202 | DSOLocal) || |
| 7203 | parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
| 7204 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/)) |
| 7205 | return true; |
| 7206 | |
| 7207 | // Verify that the linkage is ok. |
| 7208 | switch ((GlobalValue::LinkageTypes)Linkage) { |
| 7209 | case GlobalValue::ExternalLinkage: |
| 7210 | break; // always ok. |
| 7211 | case GlobalValue::ExternalWeakLinkage: |
| 7212 | if (IsDefine) |
| 7213 | return error(L: LinkageLoc, Msg: "invalid linkage for function definition" ); |
| 7214 | break; |
| 7215 | case GlobalValue::PrivateLinkage: |
| 7216 | case GlobalValue::InternalLinkage: |
| 7217 | case GlobalValue::AvailableExternallyLinkage: |
| 7218 | case GlobalValue::LinkOnceAnyLinkage: |
| 7219 | case GlobalValue::LinkOnceODRLinkage: |
| 7220 | case GlobalValue::WeakAnyLinkage: |
| 7221 | case GlobalValue::WeakODRLinkage: |
| 7222 | if (!IsDefine) |
| 7223 | return error(L: LinkageLoc, Msg: "invalid linkage for function declaration" ); |
| 7224 | break; |
| 7225 | case GlobalValue::AppendingLinkage: |
| 7226 | case GlobalValue::CommonLinkage: |
| 7227 | return error(L: LinkageLoc, Msg: "invalid function linkage type" ); |
| 7228 | } |
| 7229 | |
| 7230 | if (!isValidVisibilityForLinkage(V: Visibility, L: Linkage)) |
| 7231 | return error(L: LinkageLoc, |
| 7232 | Msg: "symbol with local linkage must have default visibility" ); |
| 7233 | |
| 7234 | if (!isValidDLLStorageClassForLinkage(S: DLLStorageClass, L: Linkage)) |
| 7235 | return error(L: LinkageLoc, |
| 7236 | Msg: "symbol with local linkage cannot have a DLL storage class" ); |
| 7237 | |
| 7238 | if (!FunctionType::isValidReturnType(RetTy: RetType)) |
| 7239 | return error(L: RetTypeLoc, Msg: "invalid function return type" ); |
| 7240 | |
| 7241 | LocTy NameLoc = Lex.getLoc(); |
| 7242 | |
| 7243 | std::string FunctionName; |
| 7244 | if (Lex.getKind() == lltok::GlobalVar) { |
| 7245 | FunctionName = Lex.getStrVal(); |
| 7246 | } else if (Lex.getKind() == lltok::GlobalID) { // @42 is ok. |
| 7247 | FunctionNumber = Lex.getUIntVal(); |
| 7248 | if (checkValueID(Loc: NameLoc, Kind: "function" , Prefix: "@" , NextID: NumberedVals.getNext(), |
| 7249 | ID: FunctionNumber)) |
| 7250 | return true; |
| 7251 | } else { |
| 7252 | return tokError(Msg: "expected function name" ); |
| 7253 | } |
| 7254 | |
| 7255 | Lex.Lex(); |
| 7256 | |
| 7257 | if (Lex.getKind() != lltok::lparen) |
| 7258 | return tokError(Msg: "expected '(' in function argument list" ); |
| 7259 | |
| 7260 | SmallVector<ArgInfo, 8> ArgList; |
| 7261 | bool IsVarArg; |
| 7262 | AttrBuilder FuncAttrs(M->getContext()); |
| 7263 | std::vector<unsigned> FwdRefAttrGrps; |
| 7264 | LocTy BuiltinLoc; |
| 7265 | std::string Section; |
| 7266 | std::string Partition; |
| 7267 | MaybeAlign Alignment, PrefAlignment; |
| 7268 | std::string GC; |
| 7269 | GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None; |
| 7270 | unsigned AddrSpace = 0; |
| 7271 | Constant *Prefix = nullptr; |
| 7272 | Constant *Prologue = nullptr; |
| 7273 | Constant *PersonalityFn = nullptr; |
| 7274 | Comdat *C; |
| 7275 | |
| 7276 | if (parseArgumentList(ArgList, UnnamedArgNums, IsVarArg) || |
| 7277 | parseOptionalUnnamedAddr(UnnamedAddr) || |
| 7278 | parseOptionalProgramAddrSpace(AddrSpace) || |
| 7279 | parseFnAttributeValuePairs(B&: FuncAttrs, FwdRefAttrGrps, InAttrGrp: false, |
| 7280 | BuiltinLoc) || |
| 7281 | (EatIfPresent(T: lltok::kw_section) && parseStringConstant(Result&: Section)) || |
| 7282 | (EatIfPresent(T: lltok::kw_partition) && parseStringConstant(Result&: Partition)) || |
| 7283 | parseOptionalComdat(GlobalName: FunctionName, C) || |
| 7284 | parseOptionalAlignment(Alignment) || |
| 7285 | parseOptionalPrefAlignment(Alignment&: PrefAlignment) || |
| 7286 | (EatIfPresent(T: lltok::kw_gc) && parseStringConstant(Result&: GC)) || |
| 7287 | (EatIfPresent(T: lltok::kw_prefix) && parseGlobalTypeAndValue(V&: Prefix)) || |
| 7288 | (EatIfPresent(T: lltok::kw_prologue) && parseGlobalTypeAndValue(V&: Prologue)) || |
| 7289 | (EatIfPresent(T: lltok::kw_personality) && |
| 7290 | parseGlobalTypeAndValue(V&: PersonalityFn))) |
| 7291 | return true; |
| 7292 | |
| 7293 | if (FuncAttrs.contains(A: Attribute::Builtin)) |
| 7294 | return error(L: BuiltinLoc, Msg: "'builtin' attribute not valid on function" ); |
| 7295 | |
| 7296 | // If the alignment was parsed as an attribute, move to the alignment field. |
| 7297 | if (MaybeAlign A = FuncAttrs.getAlignment()) { |
| 7298 | Alignment = A; |
| 7299 | FuncAttrs.removeAttribute(Val: Attribute::Alignment); |
| 7300 | } |
| 7301 | |
| 7302 | // Okay, if we got here, the function is syntactically valid. Convert types |
| 7303 | // and do semantic checks. |
| 7304 | std::vector<Type*> ParamTypeList; |
| 7305 | SmallVector<AttributeSet, 8> Attrs; |
| 7306 | |
| 7307 | for (const ArgInfo &Arg : ArgList) { |
| 7308 | ParamTypeList.push_back(x: Arg.Ty); |
| 7309 | Attrs.push_back(Elt: Arg.Attrs); |
| 7310 | } |
| 7311 | |
| 7312 | AttributeList PAL = |
| 7313 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FuncAttrs), |
| 7314 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs: Attrs); |
| 7315 | |
| 7316 | if (PAL.hasParamAttr(ArgNo: 0, Kind: Attribute::StructRet) && !RetType->isVoidTy()) |
| 7317 | return error(L: RetTypeLoc, Msg: "functions with 'sret' argument must return void" ); |
| 7318 | |
| 7319 | FunctionType *FT = FunctionType::get(Result: RetType, Params: ParamTypeList, isVarArg: IsVarArg); |
| 7320 | PointerType *PFT = PointerType::get(C&: Context, AddressSpace: AddrSpace); |
| 7321 | |
| 7322 | Fn = nullptr; |
| 7323 | GlobalValue *FwdFn = nullptr; |
| 7324 | if (!FunctionName.empty()) { |
| 7325 | // If this was a definition of a forward reference, remove the definition |
| 7326 | // from the forward reference table and fill in the forward ref. |
| 7327 | auto FRVI = ForwardRefVals.find(x: FunctionName); |
| 7328 | if (FRVI != ForwardRefVals.end()) { |
| 7329 | FwdFn = FRVI->second.first; |
| 7330 | if (FwdFn->getType() != PFT) |
| 7331 | return error(L: FRVI->second.second, |
| 7332 | Msg: "invalid forward reference to " |
| 7333 | "function '" + |
| 7334 | FunctionName + |
| 7335 | "' with wrong type: " |
| 7336 | "expected '" + |
| 7337 | getTypeString(T: PFT) + "' but was '" + |
| 7338 | getTypeString(T: FwdFn->getType()) + "'" ); |
| 7339 | ForwardRefVals.erase(position: FRVI); |
| 7340 | } else if ((Fn = M->getFunction(Name: FunctionName))) { |
| 7341 | // Reject redefinitions. |
| 7342 | return error(L: NameLoc, |
| 7343 | Msg: "invalid redefinition of function '" + FunctionName + "'" ); |
| 7344 | } else if (M->getNamedValue(Name: FunctionName)) { |
| 7345 | return error(L: NameLoc, Msg: "redefinition of function '@" + FunctionName + "'" ); |
| 7346 | } |
| 7347 | |
| 7348 | } else { |
| 7349 | // Handle @"", where a name is syntactically specified, but semantically |
| 7350 | // missing. |
| 7351 | if (FunctionNumber == (unsigned)-1) |
| 7352 | FunctionNumber = NumberedVals.getNext(); |
| 7353 | |
| 7354 | // If this is a definition of a forward referenced function, make sure the |
| 7355 | // types agree. |
| 7356 | auto I = ForwardRefValIDs.find(x: FunctionNumber); |
| 7357 | if (I != ForwardRefValIDs.end()) { |
| 7358 | FwdFn = I->second.first; |
| 7359 | if (FwdFn->getType() != PFT) |
| 7360 | return error(L: NameLoc, Msg: "type of definition and forward reference of '@" + |
| 7361 | Twine(FunctionNumber) + |
| 7362 | "' disagree: " |
| 7363 | "expected '" + |
| 7364 | getTypeString(T: PFT) + "' but was '" + |
| 7365 | getTypeString(T: FwdFn->getType()) + "'" ); |
| 7366 | ForwardRefValIDs.erase(position: I); |
| 7367 | } |
| 7368 | } |
| 7369 | |
| 7370 | Fn = Function::Create(Ty: FT, Linkage: GlobalValue::ExternalLinkage, AddrSpace, |
| 7371 | N: FunctionName, M); |
| 7372 | |
| 7373 | assert(Fn->getAddressSpace() == AddrSpace && "Created function in wrong AS" ); |
| 7374 | |
| 7375 | if (FunctionName.empty()) |
| 7376 | NumberedVals.add(ID: FunctionNumber, V: Fn); |
| 7377 | |
| 7378 | Fn->setLinkage((GlobalValue::LinkageTypes)Linkage); |
| 7379 | maybeSetDSOLocal(DSOLocal, GV&: *Fn); |
| 7380 | Fn->setVisibility((GlobalValue::VisibilityTypes)Visibility); |
| 7381 | Fn->setDLLStorageClass((GlobalValue::DLLStorageClassTypes)DLLStorageClass); |
| 7382 | Fn->setCallingConv(CC); |
| 7383 | Fn->setAttributes(PAL); |
| 7384 | Fn->setUnnamedAddr(UnnamedAddr); |
| 7385 | if (Alignment) |
| 7386 | Fn->setAlignment(*Alignment); |
| 7387 | Fn->setPreferredAlignment(PrefAlignment); |
| 7388 | Fn->setSection(Section); |
| 7389 | Fn->setPartition(Partition); |
| 7390 | Fn->setComdat(C); |
| 7391 | Fn->setPersonalityFn(PersonalityFn); |
| 7392 | if (!GC.empty()) Fn->setGC(GC); |
| 7393 | Fn->setPrefixData(Prefix); |
| 7394 | Fn->setPrologueData(Prologue); |
| 7395 | ForwardRefAttrGroups[Fn] = FwdRefAttrGrps; |
| 7396 | |
| 7397 | // Add all of the arguments we parsed to the function. |
| 7398 | Function::arg_iterator ArgIt = Fn->arg_begin(); |
| 7399 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i, ++ArgIt) { |
| 7400 | if (ParserContext && ArgList[i].IdentLoc) |
| 7401 | ParserContext->addInstructionOrArgumentLocation( |
| 7402 | &*ArgIt, ArgList[i].IdentLoc.value()); |
| 7403 | // If the argument has a name, insert it into the argument symbol table. |
| 7404 | if (ArgList[i].Name.empty()) continue; |
| 7405 | |
| 7406 | // Set the name, if it conflicted, it will be auto-renamed. |
| 7407 | ArgIt->setName(ArgList[i].Name); |
| 7408 | |
| 7409 | if (ArgIt->getName() != ArgList[i].Name) |
| 7410 | return error(L: ArgList[i].Loc, |
| 7411 | Msg: "redefinition of argument '%" + ArgList[i].Name + "'" ); |
| 7412 | } |
| 7413 | |
| 7414 | if (FwdFn) { |
| 7415 | FwdFn->replaceAllUsesWith(V: Fn); |
| 7416 | FwdFn->eraseFromParent(); |
| 7417 | } |
| 7418 | |
| 7419 | if (IsDefine) |
| 7420 | return false; |
| 7421 | |
| 7422 | // Check the declaration has no block address forward references. |
| 7423 | ValID ID; |
| 7424 | if (FunctionName.empty()) { |
| 7425 | ID.Kind = ValID::t_GlobalID; |
| 7426 | ID.UIntVal = FunctionNumber; |
| 7427 | } else { |
| 7428 | ID.Kind = ValID::t_GlobalName; |
| 7429 | ID.StrVal = FunctionName; |
| 7430 | } |
| 7431 | auto Blocks = ForwardRefBlockAddresses.find(x: ID); |
| 7432 | if (Blocks != ForwardRefBlockAddresses.end()) |
| 7433 | return error(L: Blocks->first.Loc, |
| 7434 | Msg: "cannot take blockaddress inside a declaration" ); |
| 7435 | return false; |
| 7436 | } |
| 7437 | |
| 7438 | bool LLParser::PerFunctionState::resolveForwardRefBlockAddresses() { |
| 7439 | ValID ID; |
| 7440 | if (FunctionNumber == -1) { |
| 7441 | ID.Kind = ValID::t_GlobalName; |
| 7442 | ID.StrVal = std::string(F.getName()); |
| 7443 | } else { |
| 7444 | ID.Kind = ValID::t_GlobalID; |
| 7445 | ID.UIntVal = FunctionNumber; |
| 7446 | } |
| 7447 | |
| 7448 | auto Blocks = P.ForwardRefBlockAddresses.find(x: ID); |
| 7449 | if (Blocks == P.ForwardRefBlockAddresses.end()) |
| 7450 | return false; |
| 7451 | |
| 7452 | for (const auto &I : Blocks->second) { |
| 7453 | const ValID &BBID = I.first; |
| 7454 | GlobalValue *GV = I.second; |
| 7455 | |
| 7456 | assert((BBID.Kind == ValID::t_LocalID || BBID.Kind == ValID::t_LocalName) && |
| 7457 | "Expected local id or name" ); |
| 7458 | BasicBlock *BB; |
| 7459 | if (BBID.Kind == ValID::t_LocalName) |
| 7460 | BB = getBB(Name: BBID.StrVal, Loc: BBID.Loc); |
| 7461 | else |
| 7462 | BB = getBB(ID: BBID.UIntVal, Loc: BBID.Loc); |
| 7463 | if (!BB) |
| 7464 | return P.error(L: BBID.Loc, Msg: "referenced value is not a basic block" ); |
| 7465 | |
| 7466 | Value *ResolvedVal = BlockAddress::get(F: &F, BB); |
| 7467 | ResolvedVal = P.checkValidVariableType(Loc: BBID.Loc, Name: BBID.StrVal, Ty: GV->getType(), |
| 7468 | Val: ResolvedVal); |
| 7469 | if (!ResolvedVal) |
| 7470 | return true; |
| 7471 | GV->replaceAllUsesWith(V: ResolvedVal); |
| 7472 | GV->eraseFromParent(); |
| 7473 | } |
| 7474 | |
| 7475 | P.ForwardRefBlockAddresses.erase(position: Blocks); |
| 7476 | return false; |
| 7477 | } |
| 7478 | |
| 7479 | /// parseFunctionBody |
| 7480 | /// ::= '{' BasicBlock+ UseListOrderDirective* '}' |
| 7481 | bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber, |
| 7482 | ArrayRef<unsigned> UnnamedArgNums) { |
| 7483 | if (Lex.getKind() != lltok::lbrace) |
| 7484 | return tokError(Msg: "expected '{' in function body" ); |
| 7485 | Lex.Lex(); // eat the {. |
| 7486 | |
| 7487 | PerFunctionState PFS(*this, Fn, FunctionNumber, UnnamedArgNums); |
| 7488 | |
| 7489 | // Resolve block addresses and allow basic blocks to be forward-declared |
| 7490 | // within this function. |
| 7491 | if (PFS.resolveForwardRefBlockAddresses()) |
| 7492 | return true; |
| 7493 | SaveAndRestore ScopeExit(BlockAddressPFS, &PFS); |
| 7494 | |
| 7495 | // We need at least one basic block. |
| 7496 | if (Lex.getKind() == lltok::rbrace || Lex.getKind() == lltok::kw_uselistorder) |
| 7497 | return tokError(Msg: "function body requires at least one basic block" ); |
| 7498 | |
| 7499 | while (Lex.getKind() != lltok::rbrace && |
| 7500 | Lex.getKind() != lltok::kw_uselistorder) |
| 7501 | if (parseBasicBlock(PFS)) |
| 7502 | return true; |
| 7503 | |
| 7504 | while (Lex.getKind() != lltok::rbrace) |
| 7505 | if (parseUseListOrder(PFS: &PFS)) |
| 7506 | return true; |
| 7507 | |
| 7508 | // Eat the }. |
| 7509 | Lex.Lex(); |
| 7510 | |
| 7511 | // Verify function is ok. |
| 7512 | return PFS.finishFunction(); |
| 7513 | } |
| 7514 | |
| 7515 | /// parseBasicBlock |
| 7516 | /// ::= (LabelStr|LabelID)? Instruction* |
| 7517 | bool LLParser::parseBasicBlock(PerFunctionState &PFS) { |
| 7518 | FileLoc BBStart = getTokLineColumnPos(); |
| 7519 | |
| 7520 | // If this basic block starts out with a name, remember it. |
| 7521 | std::string Name; |
| 7522 | int NameID = -1; |
| 7523 | LocTy NameLoc = Lex.getLoc(); |
| 7524 | if (Lex.getKind() == lltok::LabelStr) { |
| 7525 | Name = Lex.getStrVal(); |
| 7526 | Lex.Lex(); |
| 7527 | } else if (Lex.getKind() == lltok::LabelID) { |
| 7528 | NameID = Lex.getUIntVal(); |
| 7529 | Lex.Lex(); |
| 7530 | } |
| 7531 | |
| 7532 | BasicBlock *BB = PFS.defineBB(Name, NameID, Loc: NameLoc); |
| 7533 | if (!BB) |
| 7534 | return true; |
| 7535 | |
| 7536 | std::string NameStr; |
| 7537 | |
| 7538 | // Parse the instructions and debug values in this block until we get a |
| 7539 | // terminator. |
| 7540 | Instruction *Inst; |
| 7541 | auto DeleteDbgRecord = [](DbgRecord *DR) { DR->deleteRecord(); }; |
| 7542 | using DbgRecordPtr = std::unique_ptr<DbgRecord, decltype(DeleteDbgRecord)>; |
| 7543 | SmallVector<DbgRecordPtr> TrailingDbgRecord; |
| 7544 | do { |
| 7545 | // Handle debug records first - there should always be an instruction |
| 7546 | // following the debug records, i.e. they cannot appear after the block |
| 7547 | // terminator. |
| 7548 | while (Lex.getKind() == lltok::hash) { |
| 7549 | if (SeenOldDbgInfoFormat) |
| 7550 | return error(L: Lex.getLoc(), Msg: "debug record should not appear in a module " |
| 7551 | "containing debug info intrinsics" ); |
| 7552 | SeenNewDbgInfoFormat = true; |
| 7553 | Lex.Lex(); |
| 7554 | |
| 7555 | DbgRecord *DR; |
| 7556 | if (parseDebugRecord(DR, PFS)) |
| 7557 | return true; |
| 7558 | TrailingDbgRecord.emplace_back(Args&: DR, Args&: DeleteDbgRecord); |
| 7559 | } |
| 7560 | |
| 7561 | FileLoc InstStart = getTokLineColumnPos(); |
| 7562 | // This instruction may have three possibilities for a name: a) none |
| 7563 | // specified, b) name specified "%foo =", c) number specified: "%4 =". |
| 7564 | LocTy NameLoc = Lex.getLoc(); |
| 7565 | int NameID = -1; |
| 7566 | NameStr = "" ; |
| 7567 | |
| 7568 | if (Lex.getKind() == lltok::LocalVarID) { |
| 7569 | NameID = Lex.getUIntVal(); |
| 7570 | Lex.Lex(); |
| 7571 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after instruction id" )) |
| 7572 | return true; |
| 7573 | } else if (Lex.getKind() == lltok::LocalVar) { |
| 7574 | NameStr = Lex.getStrVal(); |
| 7575 | Lex.Lex(); |
| 7576 | if (parseToken(T: lltok::equal, ErrMsg: "expected '=' after instruction name" )) |
| 7577 | return true; |
| 7578 | } |
| 7579 | |
| 7580 | switch (parseInstruction(Inst, BB, PFS)) { |
| 7581 | default: |
| 7582 | llvm_unreachable("Unknown parseInstruction result!" ); |
| 7583 | case InstError: return true; |
| 7584 | case InstNormal: |
| 7585 | Inst->insertInto(ParentBB: BB, It: BB->end()); |
| 7586 | |
| 7587 | // With a normal result, we check to see if the instruction is followed by |
| 7588 | // a comma and metadata. |
| 7589 | if (EatIfPresent(T: lltok::comma)) |
| 7590 | if (parseInstructionMetadata(Inst&: *Inst)) |
| 7591 | return true; |
| 7592 | break; |
| 7593 | case InstExtraComma: |
| 7594 | Inst->insertInto(ParentBB: BB, It: BB->end()); |
| 7595 | |
| 7596 | // If the instruction parser ate an extra comma at the end of it, it |
| 7597 | // *must* be followed by metadata. |
| 7598 | if (parseInstructionMetadata(Inst&: *Inst)) |
| 7599 | return true; |
| 7600 | break; |
| 7601 | } |
| 7602 | |
| 7603 | // Set the name on the instruction. |
| 7604 | if (PFS.setInstName(NameID, NameStr, NameLoc, Inst)) |
| 7605 | return true; |
| 7606 | |
| 7607 | // Attach any preceding debug values to this instruction. |
| 7608 | for (DbgRecordPtr &DR : TrailingDbgRecord) |
| 7609 | BB->insertDbgRecordBefore(DR: DR.release(), Here: Inst->getIterator()); |
| 7610 | TrailingDbgRecord.clear(); |
| 7611 | if (ParserContext) { |
| 7612 | ParserContext->addInstructionOrArgumentLocation( |
| 7613 | Inst, FileLocRange(InstStart, getPrevTokEndLineColumnPos())); |
| 7614 | } |
| 7615 | } while (!Inst->isTerminator()); |
| 7616 | |
| 7617 | if (ParserContext) |
| 7618 | ParserContext->addBlockLocation( |
| 7619 | BB, FileLocRange(BBStart, getPrevTokEndLineColumnPos())); |
| 7620 | |
| 7621 | assert(TrailingDbgRecord.empty() && |
| 7622 | "All debug values should have been attached to an instruction." ); |
| 7623 | |
| 7624 | return false; |
| 7625 | } |
| 7626 | |
| 7627 | /// parseDebugRecord |
| 7628 | /// ::= #dbg_label '(' MDNode ')' |
| 7629 | /// ::= #dbg_type '(' Metadata ',' MDNode ',' Metadata ',' |
| 7630 | /// (MDNode ',' Metadata ',' Metadata ',')? MDNode ')' |
| 7631 | bool LLParser::parseDebugRecord(DbgRecord *&DR, PerFunctionState &PFS) { |
| 7632 | using RecordKind = DbgRecord::Kind; |
| 7633 | using LocType = DbgVariableRecord::LocationType; |
| 7634 | LocTy DVRLoc = Lex.getLoc(); |
| 7635 | if (Lex.getKind() != lltok::DbgRecordType) |
| 7636 | return error(L: DVRLoc, Msg: "expected debug record type here" ); |
| 7637 | RecordKind RecordType = StringSwitch<RecordKind>(Lex.getStrVal()) |
| 7638 | .Case(S: "declare" , Value: RecordKind::ValueKind) |
| 7639 | .Case(S: "value" , Value: RecordKind::ValueKind) |
| 7640 | .Case(S: "assign" , Value: RecordKind::ValueKind) |
| 7641 | .Case(S: "label" , Value: RecordKind::LabelKind) |
| 7642 | .Case(S: "declare_value" , Value: RecordKind::ValueKind); |
| 7643 | |
| 7644 | // Parsing labels is trivial; parse here and early exit, otherwise go into the |
| 7645 | // full DbgVariableRecord processing stage. |
| 7646 | if (RecordType == RecordKind::LabelKind) { |
| 7647 | Lex.Lex(); |
| 7648 | if (parseToken(T: lltok::lparen, ErrMsg: "Expected '(' here" )) |
| 7649 | return true; |
| 7650 | MDNode *Label; |
| 7651 | if (parseMDNode(N&: Label)) |
| 7652 | return true; |
| 7653 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7654 | return true; |
| 7655 | MDNode *DbgLoc; |
| 7656 | if (parseMDNode(N&: DbgLoc)) |
| 7657 | return true; |
| 7658 | if (parseToken(T: lltok::rparen, ErrMsg: "Expected ')' here" )) |
| 7659 | return true; |
| 7660 | DR = DbgLabelRecord::createUnresolvedDbgLabelRecord(Label); |
| 7661 | PendingDbgRecords.emplace_back(Args&: DVRLoc, Args&: DR, Args&: DbgLoc); |
| 7662 | return false; |
| 7663 | } |
| 7664 | |
| 7665 | LocType ValueType = StringSwitch<LocType>(Lex.getStrVal()) |
| 7666 | .Case(S: "declare" , Value: LocType::Declare) |
| 7667 | .Case(S: "value" , Value: LocType::Value) |
| 7668 | .Case(S: "assign" , Value: LocType::Assign) |
| 7669 | .Case(S: "declare_value" , Value: LocType::DeclareValue); |
| 7670 | |
| 7671 | Lex.Lex(); |
| 7672 | if (parseToken(T: lltok::lparen, ErrMsg: "Expected '(' here" )) |
| 7673 | return true; |
| 7674 | |
| 7675 | // Parse Value field. |
| 7676 | Metadata *ValLocMD; |
| 7677 | if (parseMetadata(MD&: ValLocMD, PFS: &PFS)) |
| 7678 | return true; |
| 7679 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7680 | return true; |
| 7681 | |
| 7682 | // Parse Variable field. |
| 7683 | MDNode *Variable; |
| 7684 | if (parseMDNode(N&: Variable)) |
| 7685 | return true; |
| 7686 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7687 | return true; |
| 7688 | |
| 7689 | // Parse Expression field. |
| 7690 | MDNode *Expression; |
| 7691 | if (parseMDNode(N&: Expression)) |
| 7692 | return true; |
| 7693 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7694 | return true; |
| 7695 | |
| 7696 | // Parse additional fields for #dbg_assign. |
| 7697 | MDNode *AssignID = nullptr; |
| 7698 | Metadata *AddressLocation = nullptr; |
| 7699 | MDNode *AddressExpression = nullptr; |
| 7700 | if (ValueType == LocType::Assign) { |
| 7701 | // Parse DIAssignID. |
| 7702 | if (parseMDNode(N&: AssignID)) |
| 7703 | return true; |
| 7704 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7705 | return true; |
| 7706 | |
| 7707 | // Parse address ValueAsMetadata. |
| 7708 | if (parseMetadata(MD&: AddressLocation, PFS: &PFS)) |
| 7709 | return true; |
| 7710 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7711 | return true; |
| 7712 | |
| 7713 | // Parse address DIExpression. |
| 7714 | if (parseMDNode(N&: AddressExpression)) |
| 7715 | return true; |
| 7716 | if (parseToken(T: lltok::comma, ErrMsg: "Expected ',' here" )) |
| 7717 | return true; |
| 7718 | } |
| 7719 | |
| 7720 | /// Parse DILocation. |
| 7721 | MDNode *DebugLoc; |
| 7722 | if (parseMDNode(N&: DebugLoc)) |
| 7723 | return true; |
| 7724 | |
| 7725 | if (parseToken(T: lltok::rparen, ErrMsg: "Expected ')' here" )) |
| 7726 | return true; |
| 7727 | DR = DbgVariableRecord::createUnresolvedDbgVariableRecord( |
| 7728 | Type: ValueType, Val: ValLocMD, Variable, Expression, AssignID, Address: AddressLocation, |
| 7729 | AddressExpression); |
| 7730 | PendingDbgRecords.emplace_back(Args&: DVRLoc, Args&: DR, Args&: DebugLoc); |
| 7731 | return false; |
| 7732 | } |
| 7733 | //===----------------------------------------------------------------------===// |
| 7734 | // Instruction Parsing. |
| 7735 | //===----------------------------------------------------------------------===// |
| 7736 | |
| 7737 | /// parseInstruction - parse one of the many different instructions. |
| 7738 | /// |
| 7739 | int LLParser::parseInstruction(Instruction *&Inst, BasicBlock *BB, |
| 7740 | PerFunctionState &PFS) { |
| 7741 | lltok::Kind Token = Lex.getKind(); |
| 7742 | if (Token == lltok::Eof) |
| 7743 | return tokError(Msg: "found end of file when expecting more instructions" ); |
| 7744 | LocTy Loc = Lex.getLoc(); |
| 7745 | unsigned KeywordVal = Lex.getUIntVal(); |
| 7746 | Lex.Lex(); // Eat the keyword. |
| 7747 | |
| 7748 | switch (Token) { |
| 7749 | default: |
| 7750 | return error(L: Loc, Msg: "expected instruction opcode" ); |
| 7751 | // Terminator Instructions. |
| 7752 | case lltok::kw_unreachable: Inst = new UnreachableInst(Context); return false; |
| 7753 | case lltok::kw_ret: |
| 7754 | return parseRet(Inst, BB, PFS); |
| 7755 | case lltok::kw_br: |
| 7756 | return parseBr(Inst, PFS); |
| 7757 | case lltok::kw_switch: |
| 7758 | return parseSwitch(Inst, PFS); |
| 7759 | case lltok::kw_indirectbr: |
| 7760 | return parseIndirectBr(Inst, PFS); |
| 7761 | case lltok::kw_invoke: |
| 7762 | return parseInvoke(Inst, PFS); |
| 7763 | case lltok::kw_resume: |
| 7764 | return parseResume(Inst, PFS); |
| 7765 | case lltok::kw_cleanupret: |
| 7766 | return parseCleanupRet(Inst, PFS); |
| 7767 | case lltok::kw_catchret: |
| 7768 | return parseCatchRet(Inst, PFS); |
| 7769 | case lltok::kw_catchswitch: |
| 7770 | return parseCatchSwitch(Inst, PFS); |
| 7771 | case lltok::kw_catchpad: |
| 7772 | return parseCatchPad(Inst, PFS); |
| 7773 | case lltok::kw_cleanuppad: |
| 7774 | return parseCleanupPad(Inst, PFS); |
| 7775 | case lltok::kw_callbr: |
| 7776 | return parseCallBr(Inst, PFS); |
| 7777 | // Unary Operators. |
| 7778 | case lltok::kw_fneg: { |
| 7779 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7780 | int Res = parseUnaryOp(Inst, PFS, Opc: KeywordVal, /*IsFP*/ true); |
| 7781 | if (Res != 0) |
| 7782 | return Res; |
| 7783 | if (FMF.any()) |
| 7784 | Inst->setFastMathFlags(FMF); |
| 7785 | return false; |
| 7786 | } |
| 7787 | // Binary Operators. |
| 7788 | case lltok::kw_add: |
| 7789 | case lltok::kw_sub: |
| 7790 | case lltok::kw_mul: |
| 7791 | case lltok::kw_shl: { |
| 7792 | bool NUW = EatIfPresent(T: lltok::kw_nuw); |
| 7793 | bool NSW = EatIfPresent(T: lltok::kw_nsw); |
| 7794 | if (!NUW) NUW = EatIfPresent(T: lltok::kw_nuw); |
| 7795 | |
| 7796 | if (parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ false)) |
| 7797 | return true; |
| 7798 | |
| 7799 | if (NUW) cast<BinaryOperator>(Val: Inst)->setHasNoUnsignedWrap(true); |
| 7800 | if (NSW) cast<BinaryOperator>(Val: Inst)->setHasNoSignedWrap(true); |
| 7801 | return false; |
| 7802 | } |
| 7803 | case lltok::kw_fadd: |
| 7804 | case lltok::kw_fsub: |
| 7805 | case lltok::kw_fmul: |
| 7806 | case lltok::kw_fdiv: |
| 7807 | case lltok::kw_frem: { |
| 7808 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7809 | int Res = parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ true); |
| 7810 | if (Res != 0) |
| 7811 | return Res; |
| 7812 | if (FMF.any()) |
| 7813 | Inst->setFastMathFlags(FMF); |
| 7814 | return 0; |
| 7815 | } |
| 7816 | |
| 7817 | case lltok::kw_sdiv: |
| 7818 | case lltok::kw_udiv: |
| 7819 | case lltok::kw_lshr: |
| 7820 | case lltok::kw_ashr: { |
| 7821 | bool Exact = EatIfPresent(T: lltok::kw_exact); |
| 7822 | |
| 7823 | if (parseArithmetic(Inst, PFS, Opc: KeywordVal, /*IsFP*/ false)) |
| 7824 | return true; |
| 7825 | if (Exact) cast<BinaryOperator>(Val: Inst)->setIsExact(true); |
| 7826 | return false; |
| 7827 | } |
| 7828 | |
| 7829 | case lltok::kw_urem: |
| 7830 | case lltok::kw_srem: |
| 7831 | return parseArithmetic(Inst, PFS, Opc: KeywordVal, |
| 7832 | /*IsFP*/ false); |
| 7833 | case lltok::kw_or: { |
| 7834 | bool Disjoint = EatIfPresent(T: lltok::kw_disjoint); |
| 7835 | if (parseLogical(Inst, PFS, Opc: KeywordVal)) |
| 7836 | return true; |
| 7837 | if (Disjoint) |
| 7838 | cast<PossiblyDisjointInst>(Val: Inst)->setIsDisjoint(true); |
| 7839 | return false; |
| 7840 | } |
| 7841 | case lltok::kw_and: |
| 7842 | case lltok::kw_xor: |
| 7843 | return parseLogical(Inst, PFS, Opc: KeywordVal); |
| 7844 | case lltok::kw_icmp: { |
| 7845 | bool SameSign = EatIfPresent(T: lltok::kw_samesign); |
| 7846 | if (parseCompare(Inst, PFS, Opc: KeywordVal)) |
| 7847 | return true; |
| 7848 | if (SameSign) |
| 7849 | cast<ICmpInst>(Val: Inst)->setSameSign(); |
| 7850 | return false; |
| 7851 | } |
| 7852 | case lltok::kw_fcmp: { |
| 7853 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7854 | int Res = parseCompare(Inst, PFS, Opc: KeywordVal); |
| 7855 | if (Res != 0) |
| 7856 | return Res; |
| 7857 | if (FMF.any()) |
| 7858 | Inst->setFastMathFlags(FMF); |
| 7859 | return 0; |
| 7860 | } |
| 7861 | |
| 7862 | // Casts. |
| 7863 | case lltok::kw_uitofp: { |
| 7864 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7865 | bool NonNeg = EatIfPresent(T: lltok::kw_nneg); |
| 7866 | bool Res = parseCast(Inst, PFS, Opc: KeywordVal); |
| 7867 | if (Res != 0) |
| 7868 | return Res; |
| 7869 | if (NonNeg) |
| 7870 | Inst->setNonNeg(); |
| 7871 | Inst->setFastMathFlags(FMF); |
| 7872 | return 0; |
| 7873 | } |
| 7874 | case lltok::kw_zext: { |
| 7875 | bool NonNeg = EatIfPresent(T: lltok::kw_nneg); |
| 7876 | bool Res = parseCast(Inst, PFS, Opc: KeywordVal); |
| 7877 | if (Res != 0) |
| 7878 | return Res; |
| 7879 | if (NonNeg) |
| 7880 | Inst->setNonNeg(); |
| 7881 | return 0; |
| 7882 | } |
| 7883 | case lltok::kw_trunc: { |
| 7884 | bool NUW = EatIfPresent(T: lltok::kw_nuw); |
| 7885 | bool NSW = EatIfPresent(T: lltok::kw_nsw); |
| 7886 | if (!NUW) |
| 7887 | NUW = EatIfPresent(T: lltok::kw_nuw); |
| 7888 | if (parseCast(Inst, PFS, Opc: KeywordVal)) |
| 7889 | return true; |
| 7890 | if (NUW) |
| 7891 | cast<TruncInst>(Val: Inst)->setHasNoUnsignedWrap(true); |
| 7892 | if (NSW) |
| 7893 | cast<TruncInst>(Val: Inst)->setHasNoSignedWrap(true); |
| 7894 | return false; |
| 7895 | } |
| 7896 | case lltok::kw_addrspacecast: { |
| 7897 | bool NonNull = EatIfPresent(T: lltok::kw_nonnull); |
| 7898 | if (parseCast(Inst, PFS, Opc: KeywordVal)) |
| 7899 | return true; |
| 7900 | if (NonNull) |
| 7901 | cast<AddrSpaceCastInst>(Val: Inst)->setNonNull(); |
| 7902 | return false; |
| 7903 | } |
| 7904 | case lltok::kw_sext: |
| 7905 | case lltok::kw_bitcast: |
| 7906 | case lltok::kw_fptoui: |
| 7907 | case lltok::kw_fptosi: |
| 7908 | case lltok::kw_inttoptr: |
| 7909 | case lltok::kw_ptrtoaddr: |
| 7910 | case lltok::kw_ptrtoint: |
| 7911 | return parseCast(Inst, PFS, Opc: KeywordVal); |
| 7912 | case lltok::kw_fptrunc: |
| 7913 | case lltok::kw_fpext: |
| 7914 | case lltok::kw_sitofp: { |
| 7915 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7916 | if (parseCast(Inst, PFS, Opc: KeywordVal)) |
| 7917 | return true; |
| 7918 | if (FMF.any()) |
| 7919 | Inst->setFastMathFlags(FMF); |
| 7920 | return false; |
| 7921 | } |
| 7922 | |
| 7923 | // Other. |
| 7924 | case lltok::kw_select: { |
| 7925 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7926 | int Res = parseSelect(Inst, PFS); |
| 7927 | if (Res != 0) |
| 7928 | return Res; |
| 7929 | if (FMF.any()) { |
| 7930 | if (!isa<FPMathOperator>(Val: Inst)) { |
| 7931 | Inst->deleteValue(); |
| 7932 | return error(L: Loc, Msg: "fast-math-flags specified for select without " |
| 7933 | "floating-point scalar or vector return type" ); |
| 7934 | } |
| 7935 | Inst->setFastMathFlags(FMF); |
| 7936 | } |
| 7937 | return 0; |
| 7938 | } |
| 7939 | case lltok::kw_va_arg: |
| 7940 | return parseVAArg(Inst, PFS); |
| 7941 | case lltok::kw_extractelement: |
| 7942 | return parseExtractElement(Inst, PFS); |
| 7943 | case lltok::kw_insertelement: |
| 7944 | return parseInsertElement(Inst, PFS); |
| 7945 | case lltok::kw_shufflevector: |
| 7946 | return parseShuffleVector(Inst, PFS); |
| 7947 | case lltok::kw_phi: { |
| 7948 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 7949 | int Res = parsePHI(Inst, PFS); |
| 7950 | if (Res != 0) |
| 7951 | return Res; |
| 7952 | if (FMF.any()) { |
| 7953 | if (!isa<FPMathOperator>(Val: Inst)) { |
| 7954 | Inst->deleteValue(); |
| 7955 | return error(L: Loc, Msg: "fast-math-flags specified for phi without " |
| 7956 | "floating-point scalar or vector return type" ); |
| 7957 | } |
| 7958 | Inst->setFastMathFlags(FMF); |
| 7959 | } |
| 7960 | return 0; |
| 7961 | } |
| 7962 | case lltok::kw_landingpad: |
| 7963 | return parseLandingPad(Inst, PFS); |
| 7964 | case lltok::kw_freeze: |
| 7965 | return parseFreeze(I&: Inst, PFS); |
| 7966 | case lltok::kw_bitinsert: |
| 7967 | return parseBitInsert(Inst, PFS); |
| 7968 | case lltok::kw_bitextract: |
| 7969 | return parseBitExtract(Inst, PFS); |
| 7970 | // Call. |
| 7971 | case lltok::kw_call: |
| 7972 | return parseCall(Inst, PFS, TCK: CallInst::TCK_None); |
| 7973 | case lltok::kw_tail: |
| 7974 | return parseCall(Inst, PFS, TCK: CallInst::TCK_Tail); |
| 7975 | case lltok::kw_musttail: |
| 7976 | return parseCall(Inst, PFS, TCK: CallInst::TCK_MustTail); |
| 7977 | case lltok::kw_notail: |
| 7978 | return parseCall(Inst, PFS, TCK: CallInst::TCK_NoTail); |
| 7979 | // Memory. |
| 7980 | case lltok::kw_alloca: |
| 7981 | return parseAlloc(Inst, PFS); |
| 7982 | case lltok::kw_load: |
| 7983 | return parseLoad(Inst, PFS); |
| 7984 | case lltok::kw_store: |
| 7985 | return parseStore(Inst, PFS); |
| 7986 | case lltok::kw_cmpxchg: |
| 7987 | return parseCmpXchg(Inst, PFS); |
| 7988 | case lltok::kw_atomicrmw: |
| 7989 | return parseAtomicRMW(Inst, PFS); |
| 7990 | case lltok::kw_fence: |
| 7991 | return parseFence(Inst, PFS); |
| 7992 | case lltok::kw_getelementptr: |
| 7993 | return parseGetElementPtr(Inst, PFS); |
| 7994 | case lltok::kw_extractvalue: |
| 7995 | return parseExtractValue(Inst, PFS); |
| 7996 | case lltok::kw_insertvalue: |
| 7997 | return parseInsertValue(Inst, PFS); |
| 7998 | } |
| 7999 | } |
| 8000 | |
| 8001 | /// parseCmpPredicate - parse an integer or fp predicate, based on Kind. |
| 8002 | bool LLParser::parseCmpPredicate(unsigned &P, unsigned Opc) { |
| 8003 | if (Opc == Instruction::FCmp) { |
| 8004 | switch (Lex.getKind()) { |
| 8005 | default: |
| 8006 | return tokError(Msg: "expected fcmp predicate (e.g. 'oeq')" ); |
| 8007 | case lltok::kw_oeq: P = CmpInst::FCMP_OEQ; break; |
| 8008 | case lltok::kw_one: P = CmpInst::FCMP_ONE; break; |
| 8009 | case lltok::kw_olt: P = CmpInst::FCMP_OLT; break; |
| 8010 | case lltok::kw_ogt: P = CmpInst::FCMP_OGT; break; |
| 8011 | case lltok::kw_ole: P = CmpInst::FCMP_OLE; break; |
| 8012 | case lltok::kw_oge: P = CmpInst::FCMP_OGE; break; |
| 8013 | case lltok::kw_ord: P = CmpInst::FCMP_ORD; break; |
| 8014 | case lltok::kw_uno: P = CmpInst::FCMP_UNO; break; |
| 8015 | case lltok::kw_ueq: P = CmpInst::FCMP_UEQ; break; |
| 8016 | case lltok::kw_une: P = CmpInst::FCMP_UNE; break; |
| 8017 | case lltok::kw_ult: P = CmpInst::FCMP_ULT; break; |
| 8018 | case lltok::kw_ugt: P = CmpInst::FCMP_UGT; break; |
| 8019 | case lltok::kw_ule: P = CmpInst::FCMP_ULE; break; |
| 8020 | case lltok::kw_uge: P = CmpInst::FCMP_UGE; break; |
| 8021 | case lltok::kw_true: P = CmpInst::FCMP_TRUE; break; |
| 8022 | case lltok::kw_false: P = CmpInst::FCMP_FALSE; break; |
| 8023 | } |
| 8024 | } else { |
| 8025 | switch (Lex.getKind()) { |
| 8026 | default: |
| 8027 | return tokError(Msg: "expected icmp predicate (e.g. 'eq')" ); |
| 8028 | case lltok::kw_eq: P = CmpInst::ICMP_EQ; break; |
| 8029 | case lltok::kw_ne: P = CmpInst::ICMP_NE; break; |
| 8030 | case lltok::kw_slt: P = CmpInst::ICMP_SLT; break; |
| 8031 | case lltok::kw_sgt: P = CmpInst::ICMP_SGT; break; |
| 8032 | case lltok::kw_sle: P = CmpInst::ICMP_SLE; break; |
| 8033 | case lltok::kw_sge: P = CmpInst::ICMP_SGE; break; |
| 8034 | case lltok::kw_ult: P = CmpInst::ICMP_ULT; break; |
| 8035 | case lltok::kw_ugt: P = CmpInst::ICMP_UGT; break; |
| 8036 | case lltok::kw_ule: P = CmpInst::ICMP_ULE; break; |
| 8037 | case lltok::kw_uge: P = CmpInst::ICMP_UGE; break; |
| 8038 | } |
| 8039 | } |
| 8040 | Lex.Lex(); |
| 8041 | return false; |
| 8042 | } |
| 8043 | |
| 8044 | //===----------------------------------------------------------------------===// |
| 8045 | // Terminator Instructions. |
| 8046 | //===----------------------------------------------------------------------===// |
| 8047 | |
| 8048 | /// parseRet - parse a return instruction. |
| 8049 | /// ::= 'ret' void (',' !dbg, !1)* |
| 8050 | /// ::= 'ret' TypeAndValue (',' !dbg, !1)* |
| 8051 | bool LLParser::parseRet(Instruction *&Inst, BasicBlock *BB, |
| 8052 | PerFunctionState &PFS) { |
| 8053 | SMLoc TypeLoc = Lex.getLoc(); |
| 8054 | Type *Ty = nullptr; |
| 8055 | if (parseType(Result&: Ty, AllowVoid: true /*void allowed*/)) |
| 8056 | return true; |
| 8057 | |
| 8058 | Type *ResType = PFS.getFunction().getReturnType(); |
| 8059 | |
| 8060 | if (Ty->isVoidTy()) { |
| 8061 | if (!ResType->isVoidTy()) |
| 8062 | return error(L: TypeLoc, Msg: "value doesn't match function result type '" + |
| 8063 | getTypeString(T: ResType) + "'" ); |
| 8064 | |
| 8065 | Inst = ReturnInst::Create(C&: Context); |
| 8066 | return false; |
| 8067 | } |
| 8068 | |
| 8069 | Value *RV; |
| 8070 | if (parseValue(Ty, V&: RV, PFS)) |
| 8071 | return true; |
| 8072 | |
| 8073 | if (ResType != RV->getType()) |
| 8074 | return error(L: TypeLoc, Msg: "value doesn't match function result type '" + |
| 8075 | getTypeString(T: ResType) + "'" ); |
| 8076 | |
| 8077 | Inst = ReturnInst::Create(C&: Context, retVal: RV); |
| 8078 | return false; |
| 8079 | } |
| 8080 | |
| 8081 | /// parseBr |
| 8082 | /// ::= 'br' TypeAndValue |
| 8083 | /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 8084 | bool LLParser::parseBr(Instruction *&Inst, PerFunctionState &PFS) { |
| 8085 | LocTy Loc, Loc2; |
| 8086 | Value *Op0; |
| 8087 | BasicBlock *Op1, *Op2; |
| 8088 | if (parseTypeAndValue(V&: Op0, Loc, PFS)) |
| 8089 | return true; |
| 8090 | |
| 8091 | if (BasicBlock *BB = dyn_cast<BasicBlock>(Val: Op0)) { |
| 8092 | Inst = UncondBrInst::Create(Target: BB); |
| 8093 | return false; |
| 8094 | } |
| 8095 | |
| 8096 | if (Op0->getType() != Type::getInt1Ty(C&: Context)) |
| 8097 | return error(L: Loc, Msg: "branch condition must have 'i1' type" ); |
| 8098 | |
| 8099 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' after branch condition" ) || |
| 8100 | parseTypeAndBasicBlock(BB&: Op1, Loc, PFS) || |
| 8101 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after true destination" ) || |
| 8102 | parseTypeAndBasicBlock(BB&: Op2, Loc&: Loc2, PFS)) |
| 8103 | return true; |
| 8104 | |
| 8105 | Inst = CondBrInst::Create(Cond: Op0, IfTrue: Op1, IfFalse: Op2); |
| 8106 | return false; |
| 8107 | } |
| 8108 | |
| 8109 | /// parseSwitch |
| 8110 | /// Instruction |
| 8111 | /// ::= 'switch' TypeAndValue ',' TypeAndValue '[' JumpTable ']' |
| 8112 | /// JumpTable |
| 8113 | /// ::= (TypeAndValue ',' TypeAndValue)* |
| 8114 | bool LLParser::parseSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
| 8115 | LocTy CondLoc, BBLoc; |
| 8116 | Value *Cond; |
| 8117 | BasicBlock *DefaultBB; |
| 8118 | if (parseTypeAndValue(V&: Cond, Loc&: CondLoc, PFS) || |
| 8119 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after switch condition" ) || |
| 8120 | parseTypeAndBasicBlock(BB&: DefaultBB, Loc&: BBLoc, PFS) || |
| 8121 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with switch table" )) |
| 8122 | return true; |
| 8123 | |
| 8124 | if (!Cond->getType()->isIntegerTy()) |
| 8125 | return error(L: CondLoc, Msg: "switch condition must have integer type" ); |
| 8126 | |
| 8127 | // parse the jump table pairs. |
| 8128 | SmallPtrSet<Value*, 32> SeenCases; |
| 8129 | SmallVector<std::pair<ConstantInt*, BasicBlock*>, 32> Table; |
| 8130 | while (Lex.getKind() != lltok::rsquare) { |
| 8131 | Value *Constant; |
| 8132 | BasicBlock *DestBB; |
| 8133 | |
| 8134 | if (parseTypeAndValue(V&: Constant, Loc&: CondLoc, PFS) || |
| 8135 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after case value" ) || |
| 8136 | parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
| 8137 | return true; |
| 8138 | |
| 8139 | if (!SeenCases.insert(Ptr: Constant).second) |
| 8140 | return error(L: CondLoc, Msg: "duplicate case value in switch" ); |
| 8141 | if (!isa<ConstantInt>(Val: Constant)) |
| 8142 | return error(L: CondLoc, Msg: "case value is not a constant integer" ); |
| 8143 | |
| 8144 | Table.push_back(Elt: std::make_pair(x: cast<ConstantInt>(Val: Constant), y&: DestBB)); |
| 8145 | } |
| 8146 | |
| 8147 | Lex.Lex(); // Eat the ']'. |
| 8148 | |
| 8149 | SwitchInst *SI = SwitchInst::Create(Value: Cond, Default: DefaultBB, NumCases: Table.size()); |
| 8150 | for (const auto &[OnVal, Dest] : Table) |
| 8151 | SI->addCase(OnVal, Dest); |
| 8152 | Inst = SI; |
| 8153 | return false; |
| 8154 | } |
| 8155 | |
| 8156 | /// parseIndirectBr |
| 8157 | /// Instruction |
| 8158 | /// ::= 'indirectbr' TypeAndValue ',' '[' LabelList ']' |
| 8159 | bool LLParser::parseIndirectBr(Instruction *&Inst, PerFunctionState &PFS) { |
| 8160 | LocTy AddrLoc; |
| 8161 | Value *Address; |
| 8162 | if (parseTypeAndValue(V&: Address, Loc&: AddrLoc, PFS) || |
| 8163 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after indirectbr address" ) || |
| 8164 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with indirectbr" )) |
| 8165 | return true; |
| 8166 | |
| 8167 | if (!Address->getType()->isPointerTy()) |
| 8168 | return error(L: AddrLoc, Msg: "indirectbr address must have pointer type" ); |
| 8169 | |
| 8170 | // parse the destination list. |
| 8171 | SmallVector<BasicBlock*, 16> DestList; |
| 8172 | |
| 8173 | if (Lex.getKind() != lltok::rsquare) { |
| 8174 | BasicBlock *DestBB; |
| 8175 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
| 8176 | return true; |
| 8177 | DestList.push_back(Elt: DestBB); |
| 8178 | |
| 8179 | while (EatIfPresent(T: lltok::comma)) { |
| 8180 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
| 8181 | return true; |
| 8182 | DestList.push_back(Elt: DestBB); |
| 8183 | } |
| 8184 | } |
| 8185 | |
| 8186 | if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' at end of block list" )) |
| 8187 | return true; |
| 8188 | |
| 8189 | IndirectBrInst *IBI = IndirectBrInst::Create(Address, NumDests: DestList.size()); |
| 8190 | for (BasicBlock *Dest : DestList) |
| 8191 | IBI->addDestination(Dest); |
| 8192 | Inst = IBI; |
| 8193 | return false; |
| 8194 | } |
| 8195 | |
| 8196 | // If RetType is a non-function pointer type, then this is the short syntax |
| 8197 | // for the call, which means that RetType is just the return type. Infer the |
| 8198 | // rest of the function argument types from the arguments that are present. |
| 8199 | bool LLParser::resolveFunctionType(Type *RetType, ArrayRef<ParamInfo> ArgList, |
| 8200 | FunctionType *&FuncTy) { |
| 8201 | FuncTy = dyn_cast<FunctionType>(Val: RetType); |
| 8202 | if (!FuncTy) { |
| 8203 | // Pull out the types of all of the arguments... |
| 8204 | SmallVector<Type *, 8> ParamTypes; |
| 8205 | ParamTypes.reserve(N: ArgList.size()); |
| 8206 | for (const ParamInfo &Arg : ArgList) |
| 8207 | ParamTypes.push_back(Elt: Arg.V->getType()); |
| 8208 | |
| 8209 | if (!FunctionType::isValidReturnType(RetTy: RetType)) |
| 8210 | return true; |
| 8211 | |
| 8212 | FuncTy = FunctionType::get(Result: RetType, Params: ParamTypes, isVarArg: false); |
| 8213 | } |
| 8214 | return false; |
| 8215 | } |
| 8216 | |
| 8217 | /// parseInvoke |
| 8218 | /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList |
| 8219 | /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue |
| 8220 | bool LLParser::parseInvoke(Instruction *&Inst, PerFunctionState &PFS) { |
| 8221 | LocTy CallLoc = Lex.getLoc(); |
| 8222 | AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext()); |
| 8223 | std::vector<unsigned> FwdRefAttrGrps; |
| 8224 | LocTy NoBuiltinLoc; |
| 8225 | unsigned CC; |
| 8226 | unsigned InvokeAddrSpace; |
| 8227 | Type *RetType = nullptr; |
| 8228 | LocTy RetTypeLoc; |
| 8229 | ValID CalleeID; |
| 8230 | SmallVector<ParamInfo, 16> ArgList; |
| 8231 | SmallVector<OperandBundleDef, 2> BundleList; |
| 8232 | |
| 8233 | BasicBlock *NormalBB, *UnwindBB; |
| 8234 | if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
| 8235 | parseOptionalProgramAddrSpace(AddrSpace&: InvokeAddrSpace) || |
| 8236 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) || |
| 8237 | parseValID(ID&: CalleeID, PFS: &PFS) || parseParameterList(ArgList, PFS) || |
| 8238 | parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, |
| 8239 | BuiltinLoc&: NoBuiltinLoc) || |
| 8240 | parseOptionalOperandBundles(BundleList, PFS) || |
| 8241 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in invoke" ) || |
| 8242 | parseTypeAndBasicBlock(BB&: NormalBB, PFS) || |
| 8243 | parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' in invoke" ) || |
| 8244 | parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) |
| 8245 | return true; |
| 8246 | |
| 8247 | // If RetType is a non-function pointer type, then this is the short syntax |
| 8248 | // for the call, which means that RetType is just the return type. Infer the |
| 8249 | // rest of the function argument types from the arguments that are present. |
| 8250 | FunctionType *Ty; |
| 8251 | if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty)) |
| 8252 | return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function" ); |
| 8253 | |
| 8254 | CalleeID.FTy = Ty; |
| 8255 | |
| 8256 | // Look up the callee. |
| 8257 | Value *Callee; |
| 8258 | if (convertValIDToValue(Ty: PointerType::get(C&: Context, AddressSpace: InvokeAddrSpace), ID&: CalleeID, |
| 8259 | V&: Callee, PFS: &PFS)) |
| 8260 | return true; |
| 8261 | |
| 8262 | // Set up the Attribute for the function. |
| 8263 | SmallVector<Value *, 8> Args; |
| 8264 | SmallVector<AttributeSet, 8> ArgAttrs; |
| 8265 | |
| 8266 | // Loop through FunctionType's arguments and ensure they are specified |
| 8267 | // correctly. Also, gather any parameter attributes. |
| 8268 | FunctionType::param_iterator I = Ty->param_begin(); |
| 8269 | FunctionType::param_iterator E = Ty->param_end(); |
| 8270 | for (const ParamInfo &Arg : ArgList) { |
| 8271 | Type *ExpectedTy = nullptr; |
| 8272 | if (I != E) { |
| 8273 | ExpectedTy = *I++; |
| 8274 | } else if (!Ty->isVarArg()) { |
| 8275 | return error(L: Arg.Loc, Msg: "too many arguments specified" ); |
| 8276 | } |
| 8277 | |
| 8278 | if (ExpectedTy && ExpectedTy != Arg.V->getType()) |
| 8279 | return error(L: Arg.Loc, Msg: "argument is not of expected type '" + |
| 8280 | getTypeString(T: ExpectedTy) + "'" ); |
| 8281 | Args.push_back(Elt: Arg.V); |
| 8282 | ArgAttrs.push_back(Elt: Arg.Attrs); |
| 8283 | } |
| 8284 | |
| 8285 | if (I != E) |
| 8286 | return error(L: CallLoc, Msg: "not enough parameters specified for call" ); |
| 8287 | |
| 8288 | // Finish off the Attribute and check them |
| 8289 | AttributeList PAL = |
| 8290 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs), |
| 8291 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs); |
| 8292 | |
| 8293 | InvokeInst *II = |
| 8294 | InvokeInst::Create(Ty, Func: Callee, IfNormal: NormalBB, IfException: UnwindBB, Args, Bundles: BundleList); |
| 8295 | II->setCallingConv(CC); |
| 8296 | II->setAttributes(PAL); |
| 8297 | ForwardRefAttrGroups[II] = FwdRefAttrGrps; |
| 8298 | Inst = II; |
| 8299 | return false; |
| 8300 | } |
| 8301 | |
| 8302 | /// parseResume |
| 8303 | /// ::= 'resume' TypeAndValue |
| 8304 | bool LLParser::parseResume(Instruction *&Inst, PerFunctionState &PFS) { |
| 8305 | Value *Exn; LocTy ExnLoc; |
| 8306 | if (parseTypeAndValue(V&: Exn, Loc&: ExnLoc, PFS)) |
| 8307 | return true; |
| 8308 | |
| 8309 | ResumeInst *RI = ResumeInst::Create(Exn); |
| 8310 | Inst = RI; |
| 8311 | return false; |
| 8312 | } |
| 8313 | |
| 8314 | bool LLParser::parseExceptionArgs(SmallVectorImpl<Value *> &Args, |
| 8315 | PerFunctionState &PFS) { |
| 8316 | if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in catchpad/cleanuppad" )) |
| 8317 | return true; |
| 8318 | |
| 8319 | while (Lex.getKind() != lltok::rsquare) { |
| 8320 | // If this isn't the first argument, we need a comma. |
| 8321 | if (!Args.empty() && |
| 8322 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in argument list" )) |
| 8323 | return true; |
| 8324 | |
| 8325 | // parse the argument. |
| 8326 | LocTy ArgLoc; |
| 8327 | Type *ArgTy = nullptr; |
| 8328 | if (parseType(Result&: ArgTy, Loc&: ArgLoc)) |
| 8329 | return true; |
| 8330 | |
| 8331 | Value *V; |
| 8332 | if (ArgTy->isMetadataTy()) { |
| 8333 | if (parseMetadataAsValue(V, PFS)) |
| 8334 | return true; |
| 8335 | } else { |
| 8336 | if (parseValue(Ty: ArgTy, V, PFS)) |
| 8337 | return true; |
| 8338 | } |
| 8339 | Args.push_back(Elt: V); |
| 8340 | } |
| 8341 | |
| 8342 | Lex.Lex(); // Lex the ']'. |
| 8343 | return false; |
| 8344 | } |
| 8345 | |
| 8346 | /// parseCleanupRet |
| 8347 | /// ::= 'cleanupret' from Value unwind ('to' 'caller' | TypeAndValue) |
| 8348 | bool LLParser::parseCleanupRet(Instruction *&Inst, PerFunctionState &PFS) { |
| 8349 | Value *CleanupPad = nullptr; |
| 8350 | |
| 8351 | if (parseToken(T: lltok::kw_from, ErrMsg: "expected 'from' after cleanupret" )) |
| 8352 | return true; |
| 8353 | |
| 8354 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CleanupPad, PFS)) |
| 8355 | return true; |
| 8356 | |
| 8357 | if (parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' in cleanupret" )) |
| 8358 | return true; |
| 8359 | |
| 8360 | BasicBlock *UnwindBB = nullptr; |
| 8361 | if (Lex.getKind() == lltok::kw_to) { |
| 8362 | Lex.Lex(); |
| 8363 | if (parseToken(T: lltok::kw_caller, ErrMsg: "expected 'caller' in cleanupret" )) |
| 8364 | return true; |
| 8365 | } else { |
| 8366 | if (parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) { |
| 8367 | return true; |
| 8368 | } |
| 8369 | } |
| 8370 | |
| 8371 | Inst = CleanupReturnInst::Create(CleanupPad, UnwindBB); |
| 8372 | return false; |
| 8373 | } |
| 8374 | |
| 8375 | /// parseCatchRet |
| 8376 | /// ::= 'catchret' from Parent Value 'to' TypeAndValue |
| 8377 | bool LLParser::parseCatchRet(Instruction *&Inst, PerFunctionState &PFS) { |
| 8378 | Value *CatchPad = nullptr; |
| 8379 | |
| 8380 | if (parseToken(T: lltok::kw_from, ErrMsg: "expected 'from' after catchret" )) |
| 8381 | return true; |
| 8382 | |
| 8383 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CatchPad, PFS)) |
| 8384 | return true; |
| 8385 | |
| 8386 | BasicBlock *BB; |
| 8387 | if (parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in catchret" ) || |
| 8388 | parseTypeAndBasicBlock(BB, PFS)) |
| 8389 | return true; |
| 8390 | |
| 8391 | Inst = CatchReturnInst::Create(CatchPad, BB); |
| 8392 | return false; |
| 8393 | } |
| 8394 | |
| 8395 | /// parseCatchSwitch |
| 8396 | /// ::= 'catchswitch' within Parent |
| 8397 | bool LLParser::parseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS) { |
| 8398 | Value *ParentPad; |
| 8399 | |
| 8400 | if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after catchswitch" )) |
| 8401 | return true; |
| 8402 | |
| 8403 | if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar && |
| 8404 | Lex.getKind() != lltok::LocalVarID) |
| 8405 | return tokError(Msg: "expected scope value for catchswitch" ); |
| 8406 | |
| 8407 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: ParentPad, PFS)) |
| 8408 | return true; |
| 8409 | |
| 8410 | if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' with catchswitch labels" )) |
| 8411 | return true; |
| 8412 | |
| 8413 | SmallVector<BasicBlock *, 32> Table; |
| 8414 | do { |
| 8415 | BasicBlock *DestBB; |
| 8416 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
| 8417 | return true; |
| 8418 | Table.push_back(Elt: DestBB); |
| 8419 | } while (EatIfPresent(T: lltok::comma)); |
| 8420 | |
| 8421 | if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' after catchswitch labels" )) |
| 8422 | return true; |
| 8423 | |
| 8424 | if (parseToken(T: lltok::kw_unwind, ErrMsg: "expected 'unwind' after catchswitch scope" )) |
| 8425 | return true; |
| 8426 | |
| 8427 | BasicBlock *UnwindBB = nullptr; |
| 8428 | if (EatIfPresent(T: lltok::kw_to)) { |
| 8429 | if (parseToken(T: lltok::kw_caller, ErrMsg: "expected 'caller' in catchswitch" )) |
| 8430 | return true; |
| 8431 | } else { |
| 8432 | if (parseTypeAndBasicBlock(BB&: UnwindBB, PFS)) |
| 8433 | return true; |
| 8434 | } |
| 8435 | |
| 8436 | auto *CatchSwitch = |
| 8437 | CatchSwitchInst::Create(ParentPad, UnwindDest: UnwindBB, NumHandlers: Table.size()); |
| 8438 | for (BasicBlock *DestBB : Table) |
| 8439 | CatchSwitch->addHandler(Dest: DestBB); |
| 8440 | Inst = CatchSwitch; |
| 8441 | return false; |
| 8442 | } |
| 8443 | |
| 8444 | /// parseCatchPad |
| 8445 | /// ::= 'catchpad' ParamList 'to' TypeAndValue 'unwind' TypeAndValue |
| 8446 | bool LLParser::parseCatchPad(Instruction *&Inst, PerFunctionState &PFS) { |
| 8447 | Value *CatchSwitch = nullptr; |
| 8448 | |
| 8449 | if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after catchpad" )) |
| 8450 | return true; |
| 8451 | |
| 8452 | if (Lex.getKind() != lltok::LocalVar && Lex.getKind() != lltok::LocalVarID) |
| 8453 | return tokError(Msg: "expected scope value for catchpad" ); |
| 8454 | |
| 8455 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: CatchSwitch, PFS)) |
| 8456 | return true; |
| 8457 | |
| 8458 | SmallVector<Value *, 8> Args; |
| 8459 | if (parseExceptionArgs(Args, PFS)) |
| 8460 | return true; |
| 8461 | |
| 8462 | Inst = CatchPadInst::Create(CatchSwitch, Args); |
| 8463 | return false; |
| 8464 | } |
| 8465 | |
| 8466 | /// parseCleanupPad |
| 8467 | /// ::= 'cleanuppad' within Parent ParamList |
| 8468 | bool LLParser::parseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) { |
| 8469 | Value *ParentPad = nullptr; |
| 8470 | |
| 8471 | if (parseToken(T: lltok::kw_within, ErrMsg: "expected 'within' after cleanuppad" )) |
| 8472 | return true; |
| 8473 | |
| 8474 | if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar && |
| 8475 | Lex.getKind() != lltok::LocalVarID) |
| 8476 | return tokError(Msg: "expected scope value for cleanuppad" ); |
| 8477 | |
| 8478 | if (parseValue(Ty: Type::getTokenTy(C&: Context), V&: ParentPad, PFS)) |
| 8479 | return true; |
| 8480 | |
| 8481 | SmallVector<Value *, 8> Args; |
| 8482 | if (parseExceptionArgs(Args, PFS)) |
| 8483 | return true; |
| 8484 | |
| 8485 | Inst = CleanupPadInst::Create(ParentPad, Args); |
| 8486 | return false; |
| 8487 | } |
| 8488 | |
| 8489 | //===----------------------------------------------------------------------===// |
| 8490 | // Unary Operators. |
| 8491 | //===----------------------------------------------------------------------===// |
| 8492 | |
| 8493 | /// parseUnaryOp |
| 8494 | /// ::= UnaryOp TypeAndValue ',' Value |
| 8495 | /// |
| 8496 | /// If IsFP is false, then any integer operand is allowed, if it is true, any fp |
| 8497 | /// operand is allowed. |
| 8498 | bool LLParser::parseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, |
| 8499 | unsigned Opc, bool IsFP) { |
| 8500 | LocTy Loc; Value *LHS; |
| 8501 | if (parseTypeAndValue(V&: LHS, Loc, PFS)) |
| 8502 | return true; |
| 8503 | |
| 8504 | bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy() |
| 8505 | : LHS->getType()->isIntOrIntVectorTy(); |
| 8506 | |
| 8507 | if (!Valid) |
| 8508 | return error(L: Loc, Msg: "invalid operand type for instruction" ); |
| 8509 | |
| 8510 | Inst = UnaryOperator::Create(Op: (Instruction::UnaryOps)Opc, S: LHS); |
| 8511 | return false; |
| 8512 | } |
| 8513 | |
| 8514 | /// parseCallBr |
| 8515 | /// ::= 'callbr' OptionalCallingConv OptionalAttrs Type Value ParamList |
| 8516 | /// OptionalAttrs OptionalOperandBundles 'to' TypeAndValue |
| 8517 | /// '[' LabelList ']' |
| 8518 | bool LLParser::parseCallBr(Instruction *&Inst, PerFunctionState &PFS) { |
| 8519 | LocTy CallLoc = Lex.getLoc(); |
| 8520 | AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext()); |
| 8521 | std::vector<unsigned> FwdRefAttrGrps; |
| 8522 | LocTy NoBuiltinLoc; |
| 8523 | unsigned CC; |
| 8524 | Type *RetType = nullptr; |
| 8525 | LocTy RetTypeLoc; |
| 8526 | ValID CalleeID; |
| 8527 | SmallVector<ParamInfo, 16> ArgList; |
| 8528 | SmallVector<OperandBundleDef, 2> BundleList; |
| 8529 | |
| 8530 | BasicBlock *DefaultDest; |
| 8531 | if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
| 8532 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) || |
| 8533 | parseValID(ID&: CalleeID, PFS: &PFS) || parseParameterList(ArgList, PFS) || |
| 8534 | parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, |
| 8535 | BuiltinLoc&: NoBuiltinLoc) || |
| 8536 | parseOptionalOperandBundles(BundleList, PFS) || |
| 8537 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' in callbr" ) || |
| 8538 | parseTypeAndBasicBlock(BB&: DefaultDest, PFS) || |
| 8539 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in callbr" )) |
| 8540 | return true; |
| 8541 | |
| 8542 | // parse the destination list. |
| 8543 | SmallVector<BasicBlock *, 16> IndirectDests; |
| 8544 | |
| 8545 | if (Lex.getKind() != lltok::rsquare) { |
| 8546 | BasicBlock *DestBB; |
| 8547 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
| 8548 | return true; |
| 8549 | IndirectDests.push_back(Elt: DestBB); |
| 8550 | |
| 8551 | while (EatIfPresent(T: lltok::comma)) { |
| 8552 | if (parseTypeAndBasicBlock(BB&: DestBB, PFS)) |
| 8553 | return true; |
| 8554 | IndirectDests.push_back(Elt: DestBB); |
| 8555 | } |
| 8556 | } |
| 8557 | |
| 8558 | if (parseToken(T: lltok::rsquare, ErrMsg: "expected ']' at end of block list" )) |
| 8559 | return true; |
| 8560 | |
| 8561 | // If RetType is a non-function pointer type, then this is the short syntax |
| 8562 | // for the call, which means that RetType is just the return type. Infer the |
| 8563 | // rest of the function argument types from the arguments that are present. |
| 8564 | FunctionType *Ty; |
| 8565 | if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty)) |
| 8566 | return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function" ); |
| 8567 | |
| 8568 | CalleeID.FTy = Ty; |
| 8569 | |
| 8570 | // Look up the callee. |
| 8571 | Value *Callee; |
| 8572 | if (convertValIDToValue(Ty: PointerType::getUnqual(C&: Context), ID&: CalleeID, V&: Callee, |
| 8573 | PFS: &PFS)) |
| 8574 | return true; |
| 8575 | |
| 8576 | // Set up the Attribute for the function. |
| 8577 | SmallVector<Value *, 8> Args; |
| 8578 | SmallVector<AttributeSet, 8> ArgAttrs; |
| 8579 | |
| 8580 | // Loop through FunctionType's arguments and ensure they are specified |
| 8581 | // correctly. Also, gather any parameter attributes. |
| 8582 | FunctionType::param_iterator I = Ty->param_begin(); |
| 8583 | FunctionType::param_iterator E = Ty->param_end(); |
| 8584 | for (const ParamInfo &Arg : ArgList) { |
| 8585 | Type *ExpectedTy = nullptr; |
| 8586 | if (I != E) { |
| 8587 | ExpectedTy = *I++; |
| 8588 | } else if (!Ty->isVarArg()) { |
| 8589 | return error(L: Arg.Loc, Msg: "too many arguments specified" ); |
| 8590 | } |
| 8591 | |
| 8592 | if (ExpectedTy && ExpectedTy != Arg.V->getType()) |
| 8593 | return error(L: Arg.Loc, Msg: "argument is not of expected type '" + |
| 8594 | getTypeString(T: ExpectedTy) + "'" ); |
| 8595 | Args.push_back(Elt: Arg.V); |
| 8596 | ArgAttrs.push_back(Elt: Arg.Attrs); |
| 8597 | } |
| 8598 | |
| 8599 | if (I != E) |
| 8600 | return error(L: CallLoc, Msg: "not enough parameters specified for call" ); |
| 8601 | |
| 8602 | // Finish off the Attribute and check them |
| 8603 | AttributeList PAL = |
| 8604 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs), |
| 8605 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs); |
| 8606 | |
| 8607 | CallBrInst *CBI = |
| 8608 | CallBrInst::Create(Ty, Func: Callee, DefaultDest, IndirectDests, Args, |
| 8609 | Bundles: BundleList); |
| 8610 | CBI->setCallingConv(CC); |
| 8611 | CBI->setAttributes(PAL); |
| 8612 | ForwardRefAttrGroups[CBI] = FwdRefAttrGrps; |
| 8613 | Inst = CBI; |
| 8614 | return false; |
| 8615 | } |
| 8616 | |
| 8617 | //===----------------------------------------------------------------------===// |
| 8618 | // Binary Operators. |
| 8619 | //===----------------------------------------------------------------------===// |
| 8620 | |
| 8621 | /// parseArithmetic |
| 8622 | /// ::= ArithmeticOps TypeAndValue ',' Value |
| 8623 | /// |
| 8624 | /// If IsFP is false, then any integer operand is allowed, if it is true, any fp |
| 8625 | /// operand is allowed. |
| 8626 | bool LLParser::parseArithmetic(Instruction *&Inst, PerFunctionState &PFS, |
| 8627 | unsigned Opc, bool IsFP) { |
| 8628 | LocTy Loc; Value *LHS, *RHS; |
| 8629 | if (parseTypeAndValue(V&: LHS, Loc, PFS) || |
| 8630 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in arithmetic operation" ) || |
| 8631 | parseValue(Ty: LHS->getType(), V&: RHS, PFS)) |
| 8632 | return true; |
| 8633 | |
| 8634 | bool Valid = IsFP ? LHS->getType()->isFPOrFPVectorTy() |
| 8635 | : LHS->getType()->isIntOrIntVectorTy(); |
| 8636 | |
| 8637 | if (!Valid) |
| 8638 | return error(L: Loc, Msg: "invalid operand type for instruction" ); |
| 8639 | |
| 8640 | Inst = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS); |
| 8641 | return false; |
| 8642 | } |
| 8643 | |
| 8644 | /// parseLogical |
| 8645 | /// ::= ArithmeticOps TypeAndValue ',' Value { |
| 8646 | bool LLParser::parseLogical(Instruction *&Inst, PerFunctionState &PFS, |
| 8647 | unsigned Opc) { |
| 8648 | LocTy Loc; Value *LHS, *RHS; |
| 8649 | if (parseTypeAndValue(V&: LHS, Loc, PFS) || |
| 8650 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in logical operation" ) || |
| 8651 | parseValue(Ty: LHS->getType(), V&: RHS, PFS)) |
| 8652 | return true; |
| 8653 | |
| 8654 | if (!LHS->getType()->isIntOrIntVectorTy()) |
| 8655 | return error(L: Loc, |
| 8656 | Msg: "instruction requires integer or integer vector operands" ); |
| 8657 | |
| 8658 | Inst = BinaryOperator::Create(Op: (Instruction::BinaryOps)Opc, S1: LHS, S2: RHS); |
| 8659 | return false; |
| 8660 | } |
| 8661 | |
| 8662 | /// parseCompare |
| 8663 | /// ::= 'icmp' IPredicates TypeAndValue ',' Value |
| 8664 | /// ::= 'fcmp' FPredicates TypeAndValue ',' Value |
| 8665 | bool LLParser::parseCompare(Instruction *&Inst, PerFunctionState &PFS, |
| 8666 | unsigned Opc) { |
| 8667 | // parse the integer/fp comparison predicate. |
| 8668 | LocTy Loc; |
| 8669 | unsigned Pred; |
| 8670 | Value *LHS, *RHS; |
| 8671 | if (parseCmpPredicate(P&: Pred, Opc) || parseTypeAndValue(V&: LHS, Loc, PFS) || |
| 8672 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after compare value" ) || |
| 8673 | parseValue(Ty: LHS->getType(), V&: RHS, PFS)) |
| 8674 | return true; |
| 8675 | |
| 8676 | if (Opc == Instruction::FCmp) { |
| 8677 | if (!LHS->getType()->isFPOrFPVectorTy()) |
| 8678 | return error(L: Loc, Msg: "fcmp requires floating point operands" ); |
| 8679 | Inst = new FCmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
| 8680 | } else { |
| 8681 | assert(Opc == Instruction::ICmp && "Unknown opcode for CmpInst!" ); |
| 8682 | if (!LHS->getType()->isIntOrIntVectorTy() && |
| 8683 | !LHS->getType()->isPtrOrPtrVectorTy()) |
| 8684 | return error(L: Loc, Msg: "icmp requires integer operands" ); |
| 8685 | Inst = new ICmpInst(CmpInst::Predicate(Pred), LHS, RHS); |
| 8686 | } |
| 8687 | return false; |
| 8688 | } |
| 8689 | |
| 8690 | //===----------------------------------------------------------------------===// |
| 8691 | // Other Instructions. |
| 8692 | //===----------------------------------------------------------------------===// |
| 8693 | |
| 8694 | /// parseCast |
| 8695 | /// ::= CastOpc TypeAndValue 'to' Type |
| 8696 | bool LLParser::parseCast(Instruction *&Inst, PerFunctionState &PFS, |
| 8697 | unsigned Opc) { |
| 8698 | LocTy Loc; |
| 8699 | Value *Op; |
| 8700 | Type *DestTy = nullptr; |
| 8701 | if (parseTypeAndValue(V&: Op, Loc, PFS) || |
| 8702 | parseToken(T: lltok::kw_to, ErrMsg: "expected 'to' after cast value" ) || |
| 8703 | parseType(Result&: DestTy)) |
| 8704 | return true; |
| 8705 | |
| 8706 | if (!CastInst::castIsValid(op: (Instruction::CastOps)Opc, S: Op, DstTy: DestTy)) |
| 8707 | return error(L: Loc, Msg: "invalid cast opcode for cast from '" + |
| 8708 | getTypeString(T: Op->getType()) + "' to '" + |
| 8709 | getTypeString(T: DestTy) + "'" ); |
| 8710 | Inst = CastInst::Create((Instruction::CastOps)Opc, S: Op, Ty: DestTy); |
| 8711 | return false; |
| 8712 | } |
| 8713 | |
| 8714 | /// parseSelect |
| 8715 | /// ::= 'select' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 8716 | bool LLParser::parseSelect(Instruction *&Inst, PerFunctionState &PFS) { |
| 8717 | LocTy Loc; |
| 8718 | Value *Op0, *Op1, *Op2; |
| 8719 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
| 8720 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after select condition" ) || |
| 8721 | parseTypeAndValue(V&: Op1, PFS) || |
| 8722 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after select value" ) || |
| 8723 | parseTypeAndValue(V&: Op2, PFS)) |
| 8724 | return true; |
| 8725 | |
| 8726 | if (const char *Reason = SelectInst::areInvalidOperands(Cond: Op0, True: Op1, False: Op2)) |
| 8727 | return error(L: Loc, Msg: Reason); |
| 8728 | |
| 8729 | Inst = SelectInst::Create(C: Op0, S1: Op1, S2: Op2); |
| 8730 | return false; |
| 8731 | } |
| 8732 | |
| 8733 | /// parseVAArg |
| 8734 | /// ::= 'va_arg' TypeAndValue ',' Type |
| 8735 | bool LLParser::parseVAArg(Instruction *&Inst, PerFunctionState &PFS) { |
| 8736 | Value *Op; |
| 8737 | Type *EltTy = nullptr; |
| 8738 | LocTy TypeLoc; |
| 8739 | if (parseTypeAndValue(V&: Op, PFS) || |
| 8740 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after vaarg operand" ) || |
| 8741 | parseType(Result&: EltTy, Loc&: TypeLoc)) |
| 8742 | return true; |
| 8743 | |
| 8744 | if (!EltTy->isFirstClassType()) |
| 8745 | return error(L: TypeLoc, Msg: "va_arg requires operand with first class type" ); |
| 8746 | |
| 8747 | Inst = new VAArgInst(Op, EltTy); |
| 8748 | return false; |
| 8749 | } |
| 8750 | |
| 8751 | /// parseExtractElement |
| 8752 | /// ::= 'extractelement' TypeAndValue ',' TypeAndValue |
| 8753 | bool LLParser::(Instruction *&Inst, PerFunctionState &PFS) { |
| 8754 | LocTy Loc; |
| 8755 | Value *Op0, *Op1; |
| 8756 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
| 8757 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after extract value" ) || |
| 8758 | parseTypeAndValue(V&: Op1, PFS)) |
| 8759 | return true; |
| 8760 | |
| 8761 | if (!ExtractElementInst::isValidOperands(Vec: Op0, Idx: Op1)) |
| 8762 | return error(L: Loc, Msg: "invalid extractelement operands" ); |
| 8763 | |
| 8764 | Inst = ExtractElementInst::Create(Vec: Op0, Idx: Op1); |
| 8765 | return false; |
| 8766 | } |
| 8767 | |
| 8768 | /// parseInsertElement |
| 8769 | /// ::= 'insertelement' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 8770 | bool LLParser::parseInsertElement(Instruction *&Inst, PerFunctionState &PFS) { |
| 8771 | LocTy Loc; |
| 8772 | Value *Op0, *Op1, *Op2; |
| 8773 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
| 8774 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value" ) || |
| 8775 | parseTypeAndValue(V&: Op1, PFS) || |
| 8776 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value" ) || |
| 8777 | parseTypeAndValue(V&: Op2, PFS)) |
| 8778 | return true; |
| 8779 | |
| 8780 | if (!InsertElementInst::isValidOperands(Vec: Op0, NewElt: Op1, Idx: Op2)) |
| 8781 | return error(L: Loc, Msg: "invalid insertelement operands" ); |
| 8782 | |
| 8783 | Inst = InsertElementInst::Create(Vec: Op0, NewElt: Op1, Idx: Op2); |
| 8784 | return false; |
| 8785 | } |
| 8786 | |
| 8787 | // parseBitExtract |
| 8788 | // ::= 'bitextract' Type ',' TypeAndValue ',' TypeAndValue |
| 8789 | bool LLParser::(Instruction *&Inst, PerFunctionState &PFS) { |
| 8790 | LocTy Loc; |
| 8791 | Type *Ty = nullptr; |
| 8792 | Value *Op0, *Op1; |
| 8793 | if (parseType(Result&: Ty, Loc) || |
| 8794 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after bitextract type" ) || |
| 8795 | parseTypeAndValue(V&: Op0, Loc, PFS) || |
| 8796 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after bitextract source value" ) || |
| 8797 | parseTypeAndValue(V&: Op1, PFS)) |
| 8798 | return true; |
| 8799 | |
| 8800 | if (const char *Reason = BitExtractInst::areInvalidOperands(Ty, Val: Op0, Offset: Op1)) |
| 8801 | return error(L: Loc, Msg: Reason); |
| 8802 | |
| 8803 | Inst = BitExtractInst::Create(Ty, Src: Op0, Offset: Op1); |
| 8804 | return false; |
| 8805 | } |
| 8806 | |
| 8807 | // parseBitInsert |
| 8808 | // ::= 'bitinsert' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 8809 | bool LLParser::parseBitInsert(Instruction *&Inst, PerFunctionState &PFS) { |
| 8810 | LocTy Loc; |
| 8811 | Value *Op0, *Op1, *Op2; |
| 8812 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
| 8813 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after bitinsert source value" ) || |
| 8814 | parseTypeAndValue(V&: Op1, PFS) || |
| 8815 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after bitinsert insert value" ) || |
| 8816 | parseTypeAndValue(V&: Op2, PFS)) |
| 8817 | return true; |
| 8818 | |
| 8819 | if (const char *Reason = BitInsertInst::areInvalidOperands(Base: Op0, Val: Op1, Offset: Op2)) |
| 8820 | return error(L: Loc, Msg: Reason); |
| 8821 | |
| 8822 | Inst = BitInsertInst::Create(Base: Op0, Val: Op1, Offset: Op2); |
| 8823 | return false; |
| 8824 | } |
| 8825 | |
| 8826 | /// parseShuffleVector |
| 8827 | /// ::= 'shufflevector' TypeAndValue ',' TypeAndValue ',' TypeAndValue |
| 8828 | bool LLParser::parseShuffleVector(Instruction *&Inst, PerFunctionState &PFS) { |
| 8829 | LocTy Loc; |
| 8830 | Value *Op0, *Op1, *Op2; |
| 8831 | if (parseTypeAndValue(V&: Op0, Loc, PFS) || |
| 8832 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after shuffle mask" ) || |
| 8833 | parseTypeAndValue(V&: Op1, PFS) || |
| 8834 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after shuffle value" ) || |
| 8835 | parseTypeAndValue(V&: Op2, PFS)) |
| 8836 | return true; |
| 8837 | |
| 8838 | if (!ShuffleVectorInst::isValidOperands(V1: Op0, V2: Op1, Mask: Op2)) |
| 8839 | return error(L: Loc, Msg: "invalid shufflevector operands" ); |
| 8840 | |
| 8841 | Inst = new ShuffleVectorInst(Op0, Op1, Op2); |
| 8842 | return false; |
| 8843 | } |
| 8844 | |
| 8845 | /// parsePHI |
| 8846 | /// ::= 'phi' Type '[' Value ',' Value ']' (',' '[' Value ',' Value ']')* |
| 8847 | int LLParser::parsePHI(Instruction *&Inst, PerFunctionState &PFS) { |
| 8848 | Type *Ty = nullptr; LocTy TypeLoc; |
| 8849 | Value *Op0, *Op1; |
| 8850 | |
| 8851 | if (parseType(Result&: Ty, Loc&: TypeLoc)) |
| 8852 | return true; |
| 8853 | |
| 8854 | if (!Ty->isFirstClassType()) |
| 8855 | return error(L: TypeLoc, Msg: "phi node must have first class type" ); |
| 8856 | |
| 8857 | bool First = true; |
| 8858 | bool = false; |
| 8859 | SmallVector<std::pair<Value*, BasicBlock*>, 16> PHIVals; |
| 8860 | |
| 8861 | while (true) { |
| 8862 | if (First) { |
| 8863 | if (Lex.getKind() != lltok::lsquare) |
| 8864 | break; |
| 8865 | First = false; |
| 8866 | } else if (!EatIfPresent(T: lltok::comma)) |
| 8867 | break; |
| 8868 | |
| 8869 | if (Lex.getKind() == lltok::MetadataVar) { |
| 8870 | AteExtraComma = true; |
| 8871 | break; |
| 8872 | } |
| 8873 | |
| 8874 | if (parseToken(T: lltok::lsquare, ErrMsg: "expected '[' in phi value list" ) || |
| 8875 | parseValue(Ty, V&: Op0, PFS) || |
| 8876 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after insertelement value" ) || |
| 8877 | parseValue(Ty: Type::getLabelTy(C&: Context), V&: Op1, PFS) || |
| 8878 | parseToken(T: lltok::rsquare, ErrMsg: "expected ']' in phi value list" )) |
| 8879 | return true; |
| 8880 | |
| 8881 | PHIVals.push_back(Elt: std::make_pair(x&: Op0, y: cast<BasicBlock>(Val: Op1))); |
| 8882 | } |
| 8883 | |
| 8884 | PHINode *PN = PHINode::Create(Ty, NumReservedValues: PHIVals.size()); |
| 8885 | for (const auto &[Val, BB] : PHIVals) |
| 8886 | PN->addIncoming(V: Val, BB); |
| 8887 | Inst = PN; |
| 8888 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 8889 | } |
| 8890 | |
| 8891 | /// parseLandingPad |
| 8892 | /// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+ |
| 8893 | /// Clause |
| 8894 | /// ::= 'catch' TypeAndValue |
| 8895 | /// ::= 'filter' |
| 8896 | /// ::= 'filter' TypeAndValue ( ',' TypeAndValue )* |
| 8897 | bool LLParser::parseLandingPad(Instruction *&Inst, PerFunctionState &PFS) { |
| 8898 | Type *Ty = nullptr; LocTy TyLoc; |
| 8899 | |
| 8900 | if (parseType(Result&: Ty, Loc&: TyLoc)) |
| 8901 | return true; |
| 8902 | |
| 8903 | std::unique_ptr<LandingPadInst> LP(LandingPadInst::Create(RetTy: Ty, NumReservedClauses: 0)); |
| 8904 | LP->setCleanup(EatIfPresent(T: lltok::kw_cleanup)); |
| 8905 | |
| 8906 | while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){ |
| 8907 | LandingPadInst::ClauseType CT; |
| 8908 | if (EatIfPresent(T: lltok::kw_catch)) |
| 8909 | CT = LandingPadInst::Catch; |
| 8910 | else if (EatIfPresent(T: lltok::kw_filter)) |
| 8911 | CT = LandingPadInst::Filter; |
| 8912 | else |
| 8913 | return tokError(Msg: "expected 'catch' or 'filter' clause type" ); |
| 8914 | |
| 8915 | Value *V; |
| 8916 | LocTy VLoc; |
| 8917 | if (parseTypeAndValue(V, Loc&: VLoc, PFS)) |
| 8918 | return true; |
| 8919 | |
| 8920 | // A 'catch' type expects a non-array constant. A filter clause expects an |
| 8921 | // array constant. |
| 8922 | if (CT == LandingPadInst::Catch) { |
| 8923 | if (isa<ArrayType>(Val: V->getType())) |
| 8924 | return error(L: VLoc, Msg: "'catch' clause has an invalid type" ); |
| 8925 | } else { |
| 8926 | if (!isa<ArrayType>(Val: V->getType())) |
| 8927 | return error(L: VLoc, Msg: "'filter' clause has an invalid type" ); |
| 8928 | } |
| 8929 | |
| 8930 | Constant *CV = dyn_cast<Constant>(Val: V); |
| 8931 | if (!CV) |
| 8932 | return error(L: VLoc, Msg: "clause argument must be a constant" ); |
| 8933 | LP->addClause(ClauseVal: CV); |
| 8934 | } |
| 8935 | |
| 8936 | Inst = LP.release(); |
| 8937 | return false; |
| 8938 | } |
| 8939 | |
| 8940 | /// parseFreeze |
| 8941 | /// ::= 'freeze' Type Value |
| 8942 | bool LLParser::parseFreeze(Instruction *&Inst, PerFunctionState &PFS) { |
| 8943 | LocTy Loc; |
| 8944 | Value *Op; |
| 8945 | if (parseTypeAndValue(V&: Op, Loc, PFS)) |
| 8946 | return true; |
| 8947 | |
| 8948 | Inst = new FreezeInst(Op); |
| 8949 | return false; |
| 8950 | } |
| 8951 | |
| 8952 | /// parseCall |
| 8953 | /// ::= 'call' OptionalFastMathFlags OptionalCallingConv |
| 8954 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
| 8955 | /// ::= 'tail' 'call' OptionalFastMathFlags OptionalCallingConv |
| 8956 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
| 8957 | /// ::= 'musttail' 'call' OptionalFastMathFlags OptionalCallingConv |
| 8958 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
| 8959 | /// ::= 'notail' 'call' OptionalFastMathFlags OptionalCallingConv |
| 8960 | /// OptionalAttrs Type Value ParameterList OptionalAttrs |
| 8961 | bool LLParser::parseCall(Instruction *&Inst, PerFunctionState &PFS, |
| 8962 | CallInst::TailCallKind TCK) { |
| 8963 | AttrBuilder RetAttrs(M->getContext()), FnAttrs(M->getContext()); |
| 8964 | std::vector<unsigned> FwdRefAttrGrps; |
| 8965 | LocTy BuiltinLoc; |
| 8966 | unsigned CallAddrSpace; |
| 8967 | unsigned CC; |
| 8968 | Type *RetType = nullptr; |
| 8969 | LocTy RetTypeLoc; |
| 8970 | ValID CalleeID; |
| 8971 | SmallVector<ParamInfo, 16> ArgList; |
| 8972 | SmallVector<OperandBundleDef, 2> BundleList; |
| 8973 | LocTy CallLoc = Lex.getLoc(); |
| 8974 | |
| 8975 | if (TCK != CallInst::TCK_None && |
| 8976 | parseToken(T: lltok::kw_call, |
| 8977 | ErrMsg: "expected 'tail call', 'musttail call', or 'notail call'" )) |
| 8978 | return true; |
| 8979 | |
| 8980 | FastMathFlags FMF = EatFastMathFlagsIfPresent(); |
| 8981 | |
| 8982 | if (parseOptionalCallingConv(CC) || parseOptionalReturnAttrs(B&: RetAttrs) || |
| 8983 | parseOptionalProgramAddrSpace(AddrSpace&: CallAddrSpace) || |
| 8984 | parseType(Result&: RetType, Loc&: RetTypeLoc, AllowVoid: true /*void allowed*/) || |
| 8985 | parseValID(ID&: CalleeID, PFS: &PFS) || |
| 8986 | parseParameterList(ArgList, PFS, IsMustTailCall: TCK == CallInst::TCK_MustTail, |
| 8987 | InVarArgsFunc: PFS.getFunction().isVarArg()) || |
| 8988 | parseFnAttributeValuePairs(B&: FnAttrs, FwdRefAttrGrps, InAttrGrp: false, BuiltinLoc) || |
| 8989 | parseOptionalOperandBundles(BundleList, PFS)) |
| 8990 | return true; |
| 8991 | |
| 8992 | // If RetType is a non-function pointer type, then this is the short syntax |
| 8993 | // for the call, which means that RetType is just the return type. Infer the |
| 8994 | // rest of the function argument types from the arguments that are present. |
| 8995 | FunctionType *Ty; |
| 8996 | if (resolveFunctionType(RetType, ArgList, FuncTy&: Ty)) |
| 8997 | return error(L: RetTypeLoc, Msg: "Invalid result type for LLVM function" ); |
| 8998 | |
| 8999 | CalleeID.FTy = Ty; |
| 9000 | |
| 9001 | // Look up the callee. |
| 9002 | Value *Callee; |
| 9003 | if (convertValIDToValue(Ty: PointerType::get(C&: Context, AddressSpace: CallAddrSpace), ID&: CalleeID, |
| 9004 | V&: Callee, PFS: &PFS)) |
| 9005 | return true; |
| 9006 | |
| 9007 | // Set up the Attribute for the function. |
| 9008 | SmallVector<AttributeSet, 8> Attrs; |
| 9009 | |
| 9010 | SmallVector<Value*, 8> Args; |
| 9011 | |
| 9012 | // Loop through FunctionType's arguments and ensure they are specified |
| 9013 | // correctly. Also, gather any parameter attributes. |
| 9014 | FunctionType::param_iterator I = Ty->param_begin(); |
| 9015 | FunctionType::param_iterator E = Ty->param_end(); |
| 9016 | for (const ParamInfo &Arg : ArgList) { |
| 9017 | Type *ExpectedTy = nullptr; |
| 9018 | if (I != E) { |
| 9019 | ExpectedTy = *I++; |
| 9020 | } else if (!Ty->isVarArg()) { |
| 9021 | return error(L: Arg.Loc, Msg: "too many arguments specified" ); |
| 9022 | } |
| 9023 | |
| 9024 | if (ExpectedTy && ExpectedTy != Arg.V->getType()) |
| 9025 | return error(L: Arg.Loc, Msg: "argument is not of expected type '" + |
| 9026 | getTypeString(T: ExpectedTy) + "'" ); |
| 9027 | Args.push_back(Elt: Arg.V); |
| 9028 | Attrs.push_back(Elt: Arg.Attrs); |
| 9029 | } |
| 9030 | |
| 9031 | if (I != E) |
| 9032 | return error(L: CallLoc, Msg: "not enough parameters specified for call" ); |
| 9033 | |
| 9034 | // Finish off the Attribute and check them |
| 9035 | AttributeList PAL = |
| 9036 | AttributeList::get(C&: Context, FnAttrs: AttributeSet::get(C&: Context, B: FnAttrs), |
| 9037 | RetAttrs: AttributeSet::get(C&: Context, B: RetAttrs), ArgAttrs: Attrs); |
| 9038 | |
| 9039 | CallInst *CI = CallInst::Create(Ty, Func: Callee, Args, Bundles: BundleList); |
| 9040 | CI->setTailCallKind(TCK); |
| 9041 | CI->setCallingConv(CC); |
| 9042 | if (FMF.any()) { |
| 9043 | if (!isa<FPMathOperator>(Val: CI)) { |
| 9044 | CI->deleteValue(); |
| 9045 | return error(L: CallLoc, Msg: "fast-math-flags specified for call without " |
| 9046 | "floating-point scalar or vector return type" ); |
| 9047 | } |
| 9048 | CI->setFastMathFlags(FMF); |
| 9049 | } |
| 9050 | |
| 9051 | if (CalleeID.Kind == ValID::t_GlobalName && |
| 9052 | isOldDbgFormatIntrinsic(Name: CalleeID.StrVal)) { |
| 9053 | if (SeenNewDbgInfoFormat) { |
| 9054 | CI->deleteValue(); |
| 9055 | return error(L: CallLoc, Msg: "llvm.dbg intrinsic should not appear in a module " |
| 9056 | "using non-intrinsic debug info" ); |
| 9057 | } |
| 9058 | SeenOldDbgInfoFormat = true; |
| 9059 | } |
| 9060 | CI->setAttributes(PAL); |
| 9061 | ForwardRefAttrGroups[CI] = FwdRefAttrGrps; |
| 9062 | Inst = CI; |
| 9063 | return false; |
| 9064 | } |
| 9065 | |
| 9066 | //===----------------------------------------------------------------------===// |
| 9067 | // Memory Instructions. |
| 9068 | //===----------------------------------------------------------------------===// |
| 9069 | |
| 9070 | /// parseAlloc |
| 9071 | /// ::= 'alloca' 'inalloca'? 'swifterror'? Type (',' TypeAndValue)? |
| 9072 | /// (',' 'align' i32)? (',', 'addrspace(n))? |
| 9073 | int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) { |
| 9074 | Value *Size = nullptr; |
| 9075 | LocTy SizeLoc, TyLoc, ASLoc; |
| 9076 | MaybeAlign Alignment; |
| 9077 | unsigned AddrSpace = 0; |
| 9078 | Type *Ty = nullptr; |
| 9079 | |
| 9080 | bool IsInAlloca = EatIfPresent(T: lltok::kw_inalloca); |
| 9081 | bool IsSwiftError = EatIfPresent(T: lltok::kw_swifterror); |
| 9082 | |
| 9083 | if (parseType(Result&: Ty, Loc&: TyLoc)) |
| 9084 | return true; |
| 9085 | |
| 9086 | if (Ty->isFunctionTy() || !PointerType::isValidElementType(ElemTy: Ty)) |
| 9087 | return error(L: TyLoc, Msg: "invalid type for alloca" ); |
| 9088 | |
| 9089 | bool = false; |
| 9090 | if (EatIfPresent(T: lltok::comma)) { |
| 9091 | if (Lex.getKind() == lltok::kw_align) { |
| 9092 | if (parseOptionalAlignment(Alignment)) |
| 9093 | return true; |
| 9094 | if (parseOptionalCommaAddrSpace(AddrSpace, Loc&: ASLoc, AteExtraComma)) |
| 9095 | return true; |
| 9096 | } else if (Lex.getKind() == lltok::kw_addrspace) { |
| 9097 | ASLoc = Lex.getLoc(); |
| 9098 | if (parseOptionalAddrSpace(AddrSpace)) |
| 9099 | return true; |
| 9100 | } else if (Lex.getKind() == lltok::MetadataVar) { |
| 9101 | AteExtraComma = true; |
| 9102 | } else { |
| 9103 | if (parseTypeAndValue(V&: Size, Loc&: SizeLoc, PFS)) |
| 9104 | return true; |
| 9105 | if (EatIfPresent(T: lltok::comma)) { |
| 9106 | if (Lex.getKind() == lltok::kw_align) { |
| 9107 | if (parseOptionalAlignment(Alignment)) |
| 9108 | return true; |
| 9109 | if (parseOptionalCommaAddrSpace(AddrSpace, Loc&: ASLoc, AteExtraComma)) |
| 9110 | return true; |
| 9111 | } else if (Lex.getKind() == lltok::kw_addrspace) { |
| 9112 | ASLoc = Lex.getLoc(); |
| 9113 | if (parseOptionalAddrSpace(AddrSpace)) |
| 9114 | return true; |
| 9115 | } else if (Lex.getKind() == lltok::MetadataVar) { |
| 9116 | AteExtraComma = true; |
| 9117 | } |
| 9118 | } |
| 9119 | } |
| 9120 | } |
| 9121 | |
| 9122 | if (Size && !Size->getType()->isIntegerTy()) |
| 9123 | return error(L: SizeLoc, Msg: "element count must have integer type" ); |
| 9124 | |
| 9125 | if (!Alignment && !Ty->isSized()) |
| 9126 | return error(L: TyLoc, Msg: "Cannot allocate unsized type" ); |
| 9127 | if (!Alignment) |
| 9128 | Alignment = M->getDataLayout().getPrefTypeAlign(Ty); |
| 9129 | AllocaInst *AI = new AllocaInst(Ty, AddrSpace, Size, *Alignment); |
| 9130 | AI->setUsedWithInAlloca(IsInAlloca); |
| 9131 | AI->setSwiftError(IsSwiftError); |
| 9132 | Inst = AI; |
| 9133 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9134 | } |
| 9135 | |
| 9136 | /// parseLoad |
| 9137 | /// ::= 'load' 'volatile'? TypeAndValue (',' 'align' i32)? |
| 9138 | /// ::= 'load' 'atomic' 'volatile'? 'elementwise'? TypeAndValue |
| 9139 | /// 'singlethread'? AtomicOrdering (',' 'align' i32)? |
| 9140 | int LLParser::parseLoad(Instruction *&Inst, PerFunctionState &PFS) { |
| 9141 | Value *Val; LocTy Loc; |
| 9142 | MaybeAlign Alignment; |
| 9143 | bool = false; |
| 9144 | bool isAtomic = false; |
| 9145 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
| 9146 | SyncScope::ID SSID = SyncScope::System; |
| 9147 | |
| 9148 | if (Lex.getKind() == lltok::kw_atomic) { |
| 9149 | isAtomic = true; |
| 9150 | Lex.Lex(); |
| 9151 | } |
| 9152 | |
| 9153 | bool isVolatile = false; |
| 9154 | if (Lex.getKind() == lltok::kw_volatile) { |
| 9155 | isVolatile = true; |
| 9156 | Lex.Lex(); |
| 9157 | } |
| 9158 | |
| 9159 | bool IsElementwise = false; |
| 9160 | if (Lex.getKind() == lltok::kw_elementwise) { |
| 9161 | IsElementwise = true; |
| 9162 | Lex.Lex(); |
| 9163 | } |
| 9164 | |
| 9165 | Type *Ty; |
| 9166 | LocTy ExplicitTypeLoc = Lex.getLoc(); |
| 9167 | if (parseType(Result&: Ty) || |
| 9168 | parseToken(T: lltok::comma, ErrMsg: "expected comma after load's type" ) || |
| 9169 | parseTypeAndValue(V&: Val, Loc, PFS) || |
| 9170 | parseScopeAndOrdering(IsAtomic: isAtomic, SSID, Ordering) || |
| 9171 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 9172 | return true; |
| 9173 | |
| 9174 | if (!Val->getType()->isPointerTy() || !Ty->isFirstClassType()) |
| 9175 | return error(L: Loc, Msg: "load operand must be a pointer to a first class type" ); |
| 9176 | |
| 9177 | if (IsElementwise && !isAtomic) |
| 9178 | return error(L: Loc, Msg: "elementwise load must be atomic" ); |
| 9179 | |
| 9180 | if (IsElementwise && !isa<FixedVectorType>(Val: Ty)) |
| 9181 | return error(L: ExplicitTypeLoc, |
| 9182 | Msg: "atomic elementwise load operand must have fixed vector type" ); |
| 9183 | |
| 9184 | if (isAtomic && !Alignment) |
| 9185 | return error(L: Loc, Msg: "atomic load must have explicit non-zero alignment" ); |
| 9186 | |
| 9187 | if (Ordering == AtomicOrdering::Release || |
| 9188 | Ordering == AtomicOrdering::AcquireRelease) |
| 9189 | return error(L: Loc, Msg: "atomic load cannot use Release ordering" ); |
| 9190 | if (IsElementwise && Ordering == AtomicOrdering::SequentiallyConsistent) |
| 9191 | return error(L: Loc, |
| 9192 | Msg: "atomic elementwise load cannot be sequentially consistent" ); |
| 9193 | |
| 9194 | if (!Alignment && !Ty->isSized()) |
| 9195 | return error(L: ExplicitTypeLoc, Msg: "loading unsized types is not allowed" ); |
| 9196 | if (!Alignment) |
| 9197 | Alignment = M->getDataLayout().getABITypeAlign(Ty); |
| 9198 | Inst = new LoadInst(Ty, Val, "" , |
| 9199 | LoadStoreInstProperties{.IsVolatile: isVolatile, .Alignment: *Alignment, .Ordering: Ordering, |
| 9200 | .SSID: SSID, .IsElementwise: IsElementwise}, |
| 9201 | /*InsertBefore=*/nullptr); |
| 9202 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9203 | } |
| 9204 | |
| 9205 | /// parseStore |
| 9206 | |
| 9207 | /// ::= 'store' 'volatile'? TypeAndValue ',' TypeAndValue (',' 'align' i32)? |
| 9208 | /// ::= 'store' 'atomic' 'volatile'? 'elementwise'? TypeAndValue ',' |
| 9209 | /// TypeAndValue 'singlethread'? AtomicOrdering (',' 'align' i32)? |
| 9210 | int LLParser::parseStore(Instruction *&Inst, PerFunctionState &PFS) { |
| 9211 | Value *Val, *Ptr; |
| 9212 | LocTy Loc, PtrLoc; |
| 9213 | MaybeAlign Alignment; |
| 9214 | bool = false; |
| 9215 | bool isAtomic = false; |
| 9216 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
| 9217 | SyncScope::ID SSID = SyncScope::System; |
| 9218 | |
| 9219 | if (Lex.getKind() == lltok::kw_atomic) { |
| 9220 | isAtomic = true; |
| 9221 | Lex.Lex(); |
| 9222 | } |
| 9223 | |
| 9224 | bool isVolatile = false; |
| 9225 | if (Lex.getKind() == lltok::kw_volatile) { |
| 9226 | isVolatile = true; |
| 9227 | Lex.Lex(); |
| 9228 | } |
| 9229 | |
| 9230 | bool IsElementwise = false; |
| 9231 | if (Lex.getKind() == lltok::kw_elementwise) { |
| 9232 | IsElementwise = true; |
| 9233 | Lex.Lex(); |
| 9234 | } |
| 9235 | |
| 9236 | if (parseTypeAndValue(V&: Val, Loc, PFS) || |
| 9237 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after store operand" ) || |
| 9238 | parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) || |
| 9239 | parseScopeAndOrdering(IsAtomic: isAtomic, SSID, Ordering) || |
| 9240 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 9241 | return true; |
| 9242 | |
| 9243 | if (!Ptr->getType()->isPointerTy()) |
| 9244 | return error(L: PtrLoc, Msg: "store operand must be a pointer" ); |
| 9245 | if (!Val->getType()->isFirstClassType()) |
| 9246 | return error(L: Loc, Msg: "store operand must be a first class value" ); |
| 9247 | if (isAtomic && !Alignment) |
| 9248 | return error(L: Loc, Msg: "atomic store must have explicit non-zero alignment" ); |
| 9249 | if (Ordering == AtomicOrdering::Acquire || |
| 9250 | Ordering == AtomicOrdering::AcquireRelease) |
| 9251 | return error(L: Loc, Msg: "atomic store cannot use Acquire ordering" ); |
| 9252 | |
| 9253 | if (IsElementwise && !isAtomic) |
| 9254 | return error(L: Loc, Msg: "elementwise store must be atomic" ); |
| 9255 | |
| 9256 | if (IsElementwise && !isa<FixedVectorType>(Val: Val->getType())) |
| 9257 | return error( |
| 9258 | L: Loc, Msg: "atomic elementwise store operand must have fixed vector type" ); |
| 9259 | |
| 9260 | if (IsElementwise && Ordering == AtomicOrdering::SequentiallyConsistent) |
| 9261 | return error(L: Loc, |
| 9262 | Msg: "atomic elementwise store cannot be sequentially consistent" ); |
| 9263 | |
| 9264 | if (!Alignment && !Val->getType()->isSized()) |
| 9265 | return error(L: Loc, Msg: "storing unsized types is not allowed" ); |
| 9266 | if (!Alignment) |
| 9267 | Alignment = M->getDataLayout().getABITypeAlign(Ty: Val->getType()); |
| 9268 | |
| 9269 | Inst = new StoreInst(Val, Ptr, |
| 9270 | LoadStoreInstProperties{.IsVolatile: isVolatile, .Alignment: *Alignment, .Ordering: Ordering, |
| 9271 | .SSID: SSID, .IsElementwise: IsElementwise}, |
| 9272 | /*InsertBefore=*/nullptr); |
| 9273 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9274 | } |
| 9275 | |
| 9276 | /// parseCmpXchg |
| 9277 | /// ::= 'cmpxchg' 'weak'? 'volatile'? TypeAndValue ',' TypeAndValue ',' |
| 9278 | /// TypeAndValue 'singlethread'? AtomicOrdering AtomicOrdering ',' |
| 9279 | /// 'Align'? |
| 9280 | int LLParser::parseCmpXchg(Instruction *&Inst, PerFunctionState &PFS) { |
| 9281 | Value *Ptr, *Cmp, *New; LocTy PtrLoc, CmpLoc, NewLoc; |
| 9282 | bool = false; |
| 9283 | AtomicOrdering SuccessOrdering = AtomicOrdering::NotAtomic; |
| 9284 | AtomicOrdering FailureOrdering = AtomicOrdering::NotAtomic; |
| 9285 | SyncScope::ID SSID = SyncScope::System; |
| 9286 | bool isVolatile = false; |
| 9287 | bool isWeak = false; |
| 9288 | MaybeAlign Alignment; |
| 9289 | |
| 9290 | if (EatIfPresent(T: lltok::kw_weak)) |
| 9291 | isWeak = true; |
| 9292 | |
| 9293 | if (EatIfPresent(T: lltok::kw_volatile)) |
| 9294 | isVolatile = true; |
| 9295 | |
| 9296 | if (parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) || |
| 9297 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after cmpxchg address" ) || |
| 9298 | parseTypeAndValue(V&: Cmp, Loc&: CmpLoc, PFS) || |
| 9299 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after cmpxchg cmp operand" ) || |
| 9300 | parseTypeAndValue(V&: New, Loc&: NewLoc, PFS) || |
| 9301 | parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering&: SuccessOrdering) || |
| 9302 | parseOrdering(Ordering&: FailureOrdering) || |
| 9303 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 9304 | return true; |
| 9305 | |
| 9306 | if (!AtomicCmpXchgInst::isValidSuccessOrdering(Ordering: SuccessOrdering)) |
| 9307 | return tokError(Msg: "invalid cmpxchg success ordering" ); |
| 9308 | if (!AtomicCmpXchgInst::isValidFailureOrdering(Ordering: FailureOrdering)) |
| 9309 | return tokError(Msg: "invalid cmpxchg failure ordering" ); |
| 9310 | if (!Ptr->getType()->isPointerTy()) |
| 9311 | return error(L: PtrLoc, Msg: "cmpxchg operand must be a pointer" ); |
| 9312 | if (Cmp->getType() != New->getType()) |
| 9313 | return error(L: NewLoc, Msg: "compare value and new value type do not match" ); |
| 9314 | if (!New->getType()->isFirstClassType()) |
| 9315 | return error(L: NewLoc, Msg: "cmpxchg operand must be a first class value" ); |
| 9316 | |
| 9317 | const Align DefaultAlignment( |
| 9318 | PFS.getFunction().getDataLayout().getTypeStoreSize( |
| 9319 | Ty: Cmp->getType())); |
| 9320 | |
| 9321 | AtomicCmpXchgInst *CXI = |
| 9322 | new AtomicCmpXchgInst(Ptr, Cmp, New, Alignment.value_or(u: DefaultAlignment), |
| 9323 | SuccessOrdering, FailureOrdering, SSID); |
| 9324 | CXI->setVolatile(isVolatile); |
| 9325 | CXI->setWeak(isWeak); |
| 9326 | |
| 9327 | Inst = CXI; |
| 9328 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9329 | } |
| 9330 | |
| 9331 | /// parseAtomicRMW |
| 9332 | /// ::= 'atomicrmw' 'volatile'? 'elementwise'? BinOp TypeAndValue ',' |
| 9333 | /// TypeAndValue |
| 9334 | /// 'singlethread'? AtomicOrdering |
| 9335 | int LLParser::parseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS) { |
| 9336 | Value *Ptr, *Val; LocTy PtrLoc, ValLoc; |
| 9337 | bool = false; |
| 9338 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
| 9339 | SyncScope::ID SSID = SyncScope::System; |
| 9340 | bool IsVolatile = false; |
| 9341 | bool IsElementwise = false; |
| 9342 | bool IsFP = false; |
| 9343 | AtomicRMWInst::BinOp Operation; |
| 9344 | MaybeAlign Alignment; |
| 9345 | |
| 9346 | if (EatIfPresent(T: lltok::kw_volatile)) |
| 9347 | IsVolatile = true; |
| 9348 | if (EatIfPresent(T: lltok::kw_elementwise)) |
| 9349 | IsElementwise = true; |
| 9350 | |
| 9351 | switch (Lex.getKind()) { |
| 9352 | default: |
| 9353 | return tokError(Msg: "expected binary operation in atomicrmw" ); |
| 9354 | case lltok::kw_xchg: Operation = AtomicRMWInst::Xchg; break; |
| 9355 | case lltok::kw_add: Operation = AtomicRMWInst::Add; break; |
| 9356 | case lltok::kw_sub: Operation = AtomicRMWInst::Sub; break; |
| 9357 | case lltok::kw_and: Operation = AtomicRMWInst::And; break; |
| 9358 | case lltok::kw_nand: Operation = AtomicRMWInst::Nand; break; |
| 9359 | case lltok::kw_or: Operation = AtomicRMWInst::Or; break; |
| 9360 | case lltok::kw_xor: Operation = AtomicRMWInst::Xor; break; |
| 9361 | case lltok::kw_max: Operation = AtomicRMWInst::Max; break; |
| 9362 | case lltok::kw_min: Operation = AtomicRMWInst::Min; break; |
| 9363 | case lltok::kw_umax: Operation = AtomicRMWInst::UMax; break; |
| 9364 | case lltok::kw_umin: Operation = AtomicRMWInst::UMin; break; |
| 9365 | case lltok::kw_uinc_wrap: |
| 9366 | Operation = AtomicRMWInst::UIncWrap; |
| 9367 | break; |
| 9368 | case lltok::kw_udec_wrap: |
| 9369 | Operation = AtomicRMWInst::UDecWrap; |
| 9370 | break; |
| 9371 | case lltok::kw_usub_cond: |
| 9372 | Operation = AtomicRMWInst::USubCond; |
| 9373 | break; |
| 9374 | case lltok::kw_usub_sat: |
| 9375 | Operation = AtomicRMWInst::USubSat; |
| 9376 | break; |
| 9377 | case lltok::kw_fadd: |
| 9378 | Operation = AtomicRMWInst::FAdd; |
| 9379 | IsFP = true; |
| 9380 | break; |
| 9381 | case lltok::kw_fsub: |
| 9382 | Operation = AtomicRMWInst::FSub; |
| 9383 | IsFP = true; |
| 9384 | break; |
| 9385 | case lltok::kw_fmax: |
| 9386 | Operation = AtomicRMWInst::FMax; |
| 9387 | IsFP = true; |
| 9388 | break; |
| 9389 | case lltok::kw_fmin: |
| 9390 | Operation = AtomicRMWInst::FMin; |
| 9391 | IsFP = true; |
| 9392 | break; |
| 9393 | case lltok::kw_fmaximum: |
| 9394 | Operation = AtomicRMWInst::FMaximum; |
| 9395 | IsFP = true; |
| 9396 | break; |
| 9397 | case lltok::kw_fminimum: |
| 9398 | Operation = AtomicRMWInst::FMinimum; |
| 9399 | IsFP = true; |
| 9400 | break; |
| 9401 | case lltok::kw_fmaximumnum: |
| 9402 | Operation = AtomicRMWInst::FMaximumNum; |
| 9403 | IsFP = true; |
| 9404 | break; |
| 9405 | case lltok::kw_fminimumnum: |
| 9406 | Operation = AtomicRMWInst::FMinimumNum; |
| 9407 | IsFP = true; |
| 9408 | break; |
| 9409 | } |
| 9410 | Lex.Lex(); // Eat the operation. |
| 9411 | |
| 9412 | if (parseTypeAndValue(V&: Ptr, Loc&: PtrLoc, PFS) || |
| 9413 | parseToken(T: lltok::comma, ErrMsg: "expected ',' after atomicrmw address" ) || |
| 9414 | parseTypeAndValue(V&: Val, Loc&: ValLoc, PFS) || |
| 9415 | parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering) || |
| 9416 | parseOptionalCommaAlign(Alignment, AteExtraComma)) |
| 9417 | return true; |
| 9418 | |
| 9419 | if (Ordering == AtomicOrdering::Unordered) |
| 9420 | return tokError(Msg: "atomicrmw cannot be unordered" ); |
| 9421 | if (IsElementwise && Ordering == AtomicOrdering::SequentiallyConsistent) |
| 9422 | return tokError(Msg: "atomicrmw elementwise cannot be sequentially consistent" ); |
| 9423 | if (!Ptr->getType()->isPointerTy()) |
| 9424 | return error(L: PtrLoc, Msg: "atomicrmw operand must be a pointer" ); |
| 9425 | if (Val->getType()->isScalableTy()) |
| 9426 | return error(L: ValLoc, Msg: "atomicrmw operand may not be scalable" ); |
| 9427 | |
| 9428 | Type *ValTy = Val->getType(); |
| 9429 | if (IsElementwise) { |
| 9430 | if (!isa<FixedVectorType>(Val: Val->getType())) |
| 9431 | return error(L: ValLoc, |
| 9432 | Msg: "atomicrmw elementwise operand must be a fixed vector type" ); |
| 9433 | } |
| 9434 | |
| 9435 | if (Operation == AtomicRMWInst::Xchg) { |
| 9436 | if (!ValTy->isIntOrIntVectorTy() && !ValTy->isFPOrFPVectorTy() && |
| 9437 | !ValTy->isPtrOrPtrVectorTy()) { |
| 9438 | return error( |
| 9439 | L: ValLoc, |
| 9440 | Msg: "atomicrmw " + AtomicRMWInst::getOperationName(Op: Operation) + |
| 9441 | " operand must be an integer type, a floating-point type, a " |
| 9442 | "pointer type, or a fixed vector of any of these types" ); |
| 9443 | } |
| 9444 | } else if (IsFP) { |
| 9445 | if (!ValTy->isFPOrFPVectorTy()) { |
| 9446 | return error(L: ValLoc, Msg: "atomicrmw " + |
| 9447 | AtomicRMWInst::getOperationName(Op: Operation) + |
| 9448 | " operand must be a floating point or fixed " |
| 9449 | "vector of floating point type" ); |
| 9450 | } |
| 9451 | } else { |
| 9452 | if (!ValTy->isIntOrIntVectorTy()) { |
| 9453 | return error( |
| 9454 | L: ValLoc, |
| 9455 | Msg: "atomicrmw " + AtomicRMWInst::getOperationName(Op: Operation) + |
| 9456 | " operand must be an integer or fixed vector of integer type" ); |
| 9457 | } |
| 9458 | } |
| 9459 | |
| 9460 | unsigned Size = |
| 9461 | PFS.getFunction().getDataLayout().getTypeStoreSizeInBits(Ty: ValTy); |
| 9462 | if (Size < 8 || (Size & (Size - 1))) |
| 9463 | return error(L: ValLoc, |
| 9464 | Msg: "atomicrmw operand must have a power-of-two byte size" ); |
| 9465 | const Align DefaultAlignment( |
| 9466 | PFS.getFunction().getDataLayout().getTypeStoreSize(Ty: Val->getType())); |
| 9467 | AtomicRMWInst *RMWI = new AtomicRMWInst(Operation, Ptr, Val, |
| 9468 | Alignment.value_or(u: DefaultAlignment), |
| 9469 | Ordering, SSID, IsElementwise); |
| 9470 | RMWI->setVolatile(IsVolatile); |
| 9471 | Inst = RMWI; |
| 9472 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9473 | } |
| 9474 | |
| 9475 | /// parseFence |
| 9476 | /// ::= 'fence' 'singlethread'? AtomicOrdering |
| 9477 | int LLParser::parseFence(Instruction *&Inst, PerFunctionState &PFS) { |
| 9478 | AtomicOrdering Ordering = AtomicOrdering::NotAtomic; |
| 9479 | SyncScope::ID SSID = SyncScope::System; |
| 9480 | if (parseScopeAndOrdering(IsAtomic: true /*Always atomic*/, SSID, Ordering)) |
| 9481 | return true; |
| 9482 | |
| 9483 | if (Ordering == AtomicOrdering::Unordered) |
| 9484 | return tokError(Msg: "fence cannot be unordered" ); |
| 9485 | if (Ordering == AtomicOrdering::Monotonic) |
| 9486 | return tokError(Msg: "fence cannot be monotonic" ); |
| 9487 | |
| 9488 | Inst = new FenceInst(Context, Ordering, SSID); |
| 9489 | return InstNormal; |
| 9490 | } |
| 9491 | |
| 9492 | /// parseGetElementPtr |
| 9493 | /// ::= 'getelementptr' 'inbounds'? TypeAndValue (',' TypeAndValue)* |
| 9494 | int LLParser::parseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS) { |
| 9495 | Value *Ptr = nullptr; |
| 9496 | Value *Val = nullptr; |
| 9497 | LocTy Loc, EltLoc; |
| 9498 | GEPNoWrapFlags NW; |
| 9499 | |
| 9500 | while (true) { |
| 9501 | if (EatIfPresent(T: lltok::kw_inbounds)) |
| 9502 | NW |= GEPNoWrapFlags::inBounds(); |
| 9503 | else if (EatIfPresent(T: lltok::kw_nusw)) |
| 9504 | NW |= GEPNoWrapFlags::noUnsignedSignedWrap(); |
| 9505 | else if (EatIfPresent(T: lltok::kw_nuw)) |
| 9506 | NW |= GEPNoWrapFlags::noUnsignedWrap(); |
| 9507 | else |
| 9508 | break; |
| 9509 | } |
| 9510 | |
| 9511 | Type *Ty = nullptr; |
| 9512 | if (parseType(Result&: Ty) || |
| 9513 | parseToken(T: lltok::comma, ErrMsg: "expected comma after getelementptr's type" ) || |
| 9514 | parseTypeAndValue(V&: Ptr, Loc, PFS)) |
| 9515 | return true; |
| 9516 | |
| 9517 | Type *BaseType = Ptr->getType(); |
| 9518 | PointerType *BasePointerType = dyn_cast<PointerType>(Val: BaseType->getScalarType()); |
| 9519 | if (!BasePointerType) |
| 9520 | return error(L: Loc, Msg: "base of getelementptr must be a pointer" ); |
| 9521 | |
| 9522 | SmallVector<Value*, 16> Indices; |
| 9523 | bool = false; |
| 9524 | // GEP returns a vector of pointers if at least one of parameters is a vector. |
| 9525 | // All vector parameters should have the same vector width. |
| 9526 | ElementCount GEPWidth = BaseType->isVectorTy() |
| 9527 | ? cast<VectorType>(Val: BaseType)->getElementCount() |
| 9528 | : ElementCount::getFixed(MinVal: 0); |
| 9529 | |
| 9530 | while (EatIfPresent(T: lltok::comma)) { |
| 9531 | if (Lex.getKind() == lltok::MetadataVar) { |
| 9532 | AteExtraComma = true; |
| 9533 | break; |
| 9534 | } |
| 9535 | if (parseTypeAndValue(V&: Val, Loc&: EltLoc, PFS)) |
| 9536 | return true; |
| 9537 | if (!Val->getType()->isIntOrIntVectorTy()) |
| 9538 | return error(L: EltLoc, Msg: "getelementptr index must be an integer" ); |
| 9539 | |
| 9540 | if (auto *ValVTy = dyn_cast<VectorType>(Val: Val->getType())) { |
| 9541 | ElementCount ValNumEl = ValVTy->getElementCount(); |
| 9542 | if (GEPWidth != ElementCount::getFixed(MinVal: 0) && GEPWidth != ValNumEl) |
| 9543 | return error( |
| 9544 | L: EltLoc, |
| 9545 | Msg: "getelementptr vector index has a wrong number of elements" ); |
| 9546 | GEPWidth = ValNumEl; |
| 9547 | } |
| 9548 | Indices.push_back(Elt: Val); |
| 9549 | } |
| 9550 | |
| 9551 | if (!Indices.empty() && !Ty->isSized()) |
| 9552 | return error(L: Loc, Msg: "base element of getelementptr must be sized" ); |
| 9553 | |
| 9554 | auto *STy = dyn_cast<StructType>(Val: Ty); |
| 9555 | if (STy && STy->isScalableTy()) |
| 9556 | return error(L: Loc, Msg: "getelementptr cannot target structure that contains " |
| 9557 | "scalable vector type" ); |
| 9558 | |
| 9559 | if (!GetElementPtrInst::getIndexedType(Ty, IdxList: Indices)) |
| 9560 | return error(L: Loc, Msg: "invalid getelementptr indices" ); |
| 9561 | GetElementPtrInst *GEP = GetElementPtrInst::Create(PointeeType: Ty, Ptr, IdxList: Indices); |
| 9562 | Inst = GEP; |
| 9563 | GEP->setNoWrapFlags(NW); |
| 9564 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9565 | } |
| 9566 | |
| 9567 | /// parseExtractValue |
| 9568 | /// ::= 'extractvalue' TypeAndValue (',' uint32)+ |
| 9569 | int LLParser::(Instruction *&Inst, PerFunctionState &PFS) { |
| 9570 | Value *Val; LocTy Loc; |
| 9571 | SmallVector<unsigned, 4> Indices; |
| 9572 | bool ; |
| 9573 | if (parseTypeAndValue(V&: Val, Loc, PFS) || |
| 9574 | parseIndexList(Indices, AteExtraComma)) |
| 9575 | return true; |
| 9576 | |
| 9577 | if (!Val->getType()->isAggregateType()) |
| 9578 | return error(L: Loc, Msg: "extractvalue operand must be aggregate type" ); |
| 9579 | |
| 9580 | if (!ExtractValueInst::getIndexedType(Agg: Val->getType(), Idxs: Indices)) |
| 9581 | return error(L: Loc, Msg: "invalid indices for extractvalue" ); |
| 9582 | Inst = ExtractValueInst::Create(Agg: Val, Idxs: Indices); |
| 9583 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9584 | } |
| 9585 | |
| 9586 | /// parseInsertValue |
| 9587 | /// ::= 'insertvalue' TypeAndValue ',' TypeAndValue (',' uint32)+ |
| 9588 | int LLParser::parseInsertValue(Instruction *&Inst, PerFunctionState &PFS) { |
| 9589 | Value *Val0, *Val1; LocTy Loc0, Loc1; |
| 9590 | SmallVector<unsigned, 4> Indices; |
| 9591 | bool ; |
| 9592 | if (parseTypeAndValue(V&: Val0, Loc&: Loc0, PFS) || |
| 9593 | parseToken(T: lltok::comma, ErrMsg: "expected comma after insertvalue operand" ) || |
| 9594 | parseTypeAndValue(V&: Val1, Loc&: Loc1, PFS) || |
| 9595 | parseIndexList(Indices, AteExtraComma)) |
| 9596 | return true; |
| 9597 | |
| 9598 | if (!Val0->getType()->isAggregateType()) |
| 9599 | return error(L: Loc0, Msg: "insertvalue operand must be aggregate type" ); |
| 9600 | |
| 9601 | Type *IndexedType = ExtractValueInst::getIndexedType(Agg: Val0->getType(), Idxs: Indices); |
| 9602 | if (!IndexedType) |
| 9603 | return error(L: Loc0, Msg: "invalid indices for insertvalue" ); |
| 9604 | if (IndexedType != Val1->getType()) |
| 9605 | return error(L: Loc1, Msg: "insertvalue operand and field disagree in type: '" + |
| 9606 | getTypeString(T: Val1->getType()) + "' instead of '" + |
| 9607 | getTypeString(T: IndexedType) + "'" ); |
| 9608 | Inst = InsertValueInst::Create(Agg: Val0, Val: Val1, Idxs: Indices); |
| 9609 | return AteExtraComma ? InstExtraComma : InstNormal; |
| 9610 | } |
| 9611 | |
| 9612 | //===----------------------------------------------------------------------===// |
| 9613 | // Embedded metadata. |
| 9614 | //===----------------------------------------------------------------------===// |
| 9615 | |
| 9616 | /// parseMDNodeVector |
| 9617 | /// ::= { Element (',' Element)* } |
| 9618 | /// Element |
| 9619 | /// ::= 'null' | Metadata |
| 9620 | bool LLParser::parseMDNodeVector(SmallVectorImpl<Metadata *> &Elts) { |
| 9621 | if (parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here" )) |
| 9622 | return true; |
| 9623 | |
| 9624 | // Check for an empty list. |
| 9625 | if (EatIfPresent(T: lltok::rbrace)) |
| 9626 | return false; |
| 9627 | |
| 9628 | do { |
| 9629 | if (EatIfPresent(T: lltok::kw_null)) { |
| 9630 | Elts.push_back(Elt: nullptr); |
| 9631 | continue; |
| 9632 | } |
| 9633 | |
| 9634 | Metadata *MD; |
| 9635 | if (parseMetadata(MD, PFS: nullptr)) |
| 9636 | return true; |
| 9637 | Elts.push_back(Elt: MD); |
| 9638 | } while (EatIfPresent(T: lltok::comma)); |
| 9639 | |
| 9640 | return parseToken(T: lltok::rbrace, ErrMsg: "expected end of metadata node" ); |
| 9641 | } |
| 9642 | |
| 9643 | //===----------------------------------------------------------------------===// |
| 9644 | // Use-list order directives. |
| 9645 | //===----------------------------------------------------------------------===// |
| 9646 | bool LLParser::sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, |
| 9647 | SMLoc Loc) { |
| 9648 | if (!V->hasUseList()) |
| 9649 | return false; |
| 9650 | if (V->use_empty()) |
| 9651 | return error(L: Loc, Msg: "value has no uses" ); |
| 9652 | |
| 9653 | unsigned NumUses = 0; |
| 9654 | SmallDenseMap<const Use *, unsigned, 16> Order; |
| 9655 | for (const Use &U : V->uses()) { |
| 9656 | if (++NumUses > Indexes.size()) |
| 9657 | break; |
| 9658 | Order[&U] = Indexes[NumUses - 1]; |
| 9659 | } |
| 9660 | if (NumUses < 2) |
| 9661 | return error(L: Loc, Msg: "value only has one use" ); |
| 9662 | if (Order.size() != Indexes.size() || NumUses > Indexes.size()) |
| 9663 | return error(L: Loc, |
| 9664 | Msg: "wrong number of indexes, expected " + Twine(V->getNumUses())); |
| 9665 | |
| 9666 | V->sortUseList(Cmp: [&](const Use &L, const Use &R) { |
| 9667 | return Order.lookup(Val: &L) < Order.lookup(Val: &R); |
| 9668 | }); |
| 9669 | return false; |
| 9670 | } |
| 9671 | |
| 9672 | /// parseUseListOrderIndexes |
| 9673 | /// ::= '{' uint32 (',' uint32)+ '}' |
| 9674 | bool LLParser::parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes) { |
| 9675 | SMLoc Loc = Lex.getLoc(); |
| 9676 | if (parseToken(T: lltok::lbrace, ErrMsg: "expected '{' here" )) |
| 9677 | return true; |
| 9678 | if (Lex.getKind() == lltok::rbrace) |
| 9679 | return tokError(Msg: "expected non-empty list of uselistorder indexes" ); |
| 9680 | |
| 9681 | // Use Offset, Max, and IsOrdered to check consistency of indexes. The |
| 9682 | // indexes should be distinct numbers in the range [0, size-1], and should |
| 9683 | // not be in order. |
| 9684 | unsigned Offset = 0; |
| 9685 | unsigned Max = 0; |
| 9686 | bool IsOrdered = true; |
| 9687 | assert(Indexes.empty() && "Expected empty order vector" ); |
| 9688 | do { |
| 9689 | unsigned Index; |
| 9690 | if (parseUInt32(Val&: Index)) |
| 9691 | return true; |
| 9692 | |
| 9693 | // Update consistency checks. |
| 9694 | Offset += Index - Indexes.size(); |
| 9695 | Max = std::max(a: Max, b: Index); |
| 9696 | IsOrdered &= Index == Indexes.size(); |
| 9697 | |
| 9698 | Indexes.push_back(Elt: Index); |
| 9699 | } while (EatIfPresent(T: lltok::comma)); |
| 9700 | |
| 9701 | if (parseToken(T: lltok::rbrace, ErrMsg: "expected '}' here" )) |
| 9702 | return true; |
| 9703 | |
| 9704 | if (Indexes.size() < 2) |
| 9705 | return error(L: Loc, Msg: "expected >= 2 uselistorder indexes" ); |
| 9706 | if (Offset != 0 || Max >= Indexes.size()) |
| 9707 | return error(L: Loc, |
| 9708 | Msg: "expected distinct uselistorder indexes in range [0, size)" ); |
| 9709 | if (IsOrdered) |
| 9710 | return error(L: Loc, Msg: "expected uselistorder indexes to change the order" ); |
| 9711 | |
| 9712 | return false; |
| 9713 | } |
| 9714 | |
| 9715 | /// parseUseListOrder |
| 9716 | /// ::= 'uselistorder' Type Value ',' UseListOrderIndexes |
| 9717 | bool LLParser::parseUseListOrder(PerFunctionState *PFS) { |
| 9718 | SMLoc Loc = Lex.getLoc(); |
| 9719 | if (parseToken(T: lltok::kw_uselistorder, ErrMsg: "expected uselistorder directive" )) |
| 9720 | return true; |
| 9721 | |
| 9722 | Value *V; |
| 9723 | SmallVector<unsigned, 16> Indexes; |
| 9724 | if (parseTypeAndValue(V, PFS) || |
| 9725 | parseToken(T: lltok::comma, ErrMsg: "expected comma in uselistorder directive" ) || |
| 9726 | parseUseListOrderIndexes(Indexes)) |
| 9727 | return true; |
| 9728 | |
| 9729 | return sortUseListOrder(V, Indexes, Loc); |
| 9730 | } |
| 9731 | |
| 9732 | /// ModuleEntry |
| 9733 | /// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')' |
| 9734 | /// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')' |
| 9735 | bool LLParser::parseModuleEntry(unsigned ID) { |
| 9736 | assert(Lex.getKind() == lltok::kw_module); |
| 9737 | Lex.Lex(); |
| 9738 | |
| 9739 | std::string Path; |
| 9740 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9741 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 9742 | parseToken(T: lltok::kw_path, ErrMsg: "expected 'path' here" ) || |
| 9743 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9744 | parseStringConstant(Result&: Path) || |
| 9745 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9746 | parseToken(T: lltok::kw_hash, ErrMsg: "expected 'hash' here" ) || |
| 9747 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9748 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 9749 | return true; |
| 9750 | |
| 9751 | ModuleHash Hash; |
| 9752 | if (parseUInt32(Val&: Hash[0]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9753 | parseUInt32(Val&: Hash[1]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9754 | parseUInt32(Val&: Hash[2]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9755 | parseUInt32(Val&: Hash[3]) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9756 | parseUInt32(Val&: Hash[4])) |
| 9757 | return true; |
| 9758 | |
| 9759 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ) || |
| 9760 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 9761 | return true; |
| 9762 | |
| 9763 | auto ModuleEntry = Index->addModule(ModPath: Path, Hash); |
| 9764 | ModuleIdMap[ID] = ModuleEntry->first(); |
| 9765 | |
| 9766 | return false; |
| 9767 | } |
| 9768 | |
| 9769 | /// TypeIdEntry |
| 9770 | /// ::= 'typeid' ':' '(' 'name' ':' STRINGCONSTANT ',' TypeIdSummary ')' |
| 9771 | bool LLParser::parseTypeIdEntry(unsigned ID) { |
| 9772 | assert(Lex.getKind() == lltok::kw_typeid); |
| 9773 | Lex.Lex(); |
| 9774 | |
| 9775 | std::string Name; |
| 9776 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9777 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 9778 | parseToken(T: lltok::kw_name, ErrMsg: "expected 'name' here" ) || |
| 9779 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9780 | parseStringConstant(Result&: Name)) |
| 9781 | return true; |
| 9782 | |
| 9783 | TypeIdSummary &TIS = Index->getOrInsertTypeIdSummary(TypeId: Name); |
| 9784 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9785 | parseTypeIdSummary(TIS) || parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 9786 | return true; |
| 9787 | |
| 9788 | // Check if this ID was forward referenced, and if so, update the |
| 9789 | // corresponding GUIDs. |
| 9790 | auto FwdRefTIDs = ForwardRefTypeIds.find(x: ID); |
| 9791 | if (FwdRefTIDs != ForwardRefTypeIds.end()) { |
| 9792 | for (auto TIDRef : FwdRefTIDs->second) { |
| 9793 | assert(!*TIDRef.first && |
| 9794 | "Forward referenced type id GUID expected to be 0" ); |
| 9795 | *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(GlobalName: Name); |
| 9796 | } |
| 9797 | ForwardRefTypeIds.erase(position: FwdRefTIDs); |
| 9798 | } |
| 9799 | |
| 9800 | return false; |
| 9801 | } |
| 9802 | |
| 9803 | /// TypeIdSummary |
| 9804 | /// ::= 'summary' ':' '(' TypeTestResolution [',' OptionalWpdResolutions]? ')' |
| 9805 | bool LLParser::parseTypeIdSummary(TypeIdSummary &TIS) { |
| 9806 | if (parseToken(T: lltok::kw_summary, ErrMsg: "expected 'summary' here" ) || |
| 9807 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9808 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 9809 | parseTypeTestResolution(TTRes&: TIS.TTRes)) |
| 9810 | return true; |
| 9811 | |
| 9812 | if (EatIfPresent(T: lltok::comma)) { |
| 9813 | // Expect optional wpdResolutions field |
| 9814 | if (parseOptionalWpdResolutions(WPDResMap&: TIS.WPDRes)) |
| 9815 | return true; |
| 9816 | } |
| 9817 | |
| 9818 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 9819 | return true; |
| 9820 | |
| 9821 | return false; |
| 9822 | } |
| 9823 | |
| 9824 | static ValueInfo EmptyVI = |
| 9825 | ValueInfo(false, (GlobalValueSummaryMapTy::value_type *)-8); |
| 9826 | |
| 9827 | /// TypeIdCompatibleVtableEntry |
| 9828 | /// ::= 'typeidCompatibleVTable' ':' '(' 'name' ':' STRINGCONSTANT ',' |
| 9829 | /// TypeIdCompatibleVtableInfo |
| 9830 | /// ')' |
| 9831 | bool LLParser::parseTypeIdCompatibleVtableEntry(unsigned ID) { |
| 9832 | assert(Lex.getKind() == lltok::kw_typeidCompatibleVTable); |
| 9833 | Lex.Lex(); |
| 9834 | |
| 9835 | std::string Name; |
| 9836 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9837 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 9838 | parseToken(T: lltok::kw_name, ErrMsg: "expected 'name' here" ) || |
| 9839 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9840 | parseStringConstant(Result&: Name)) |
| 9841 | return true; |
| 9842 | |
| 9843 | TypeIdCompatibleVtableInfo &TI = |
| 9844 | Index->getOrInsertTypeIdCompatibleVtableSummary(TypeId: Name); |
| 9845 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9846 | parseToken(T: lltok::kw_summary, ErrMsg: "expected 'summary' here" ) || |
| 9847 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9848 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 9849 | return true; |
| 9850 | |
| 9851 | IdToIndexMapType IdToIndexMap; |
| 9852 | // parse each call edge |
| 9853 | do { |
| 9854 | uint64_t Offset; |
| 9855 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 9856 | parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
| 9857 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: Offset) || |
| 9858 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" )) |
| 9859 | return true; |
| 9860 | |
| 9861 | LocTy Loc = Lex.getLoc(); |
| 9862 | unsigned GVId; |
| 9863 | ValueInfo VI; |
| 9864 | if (parseGVReference(VI, GVId)) |
| 9865 | return true; |
| 9866 | |
| 9867 | // Keep track of the TypeIdCompatibleVtableInfo array index needing a |
| 9868 | // forward reference. We will save the location of the ValueInfo needing an |
| 9869 | // update, but can only do so once the std::vector is finalized. |
| 9870 | if (VI == EmptyVI) |
| 9871 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: TI.size(), y&: Loc)); |
| 9872 | TI.push_back(x: {Offset, VI}); |
| 9873 | |
| 9874 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in call" )) |
| 9875 | return true; |
| 9876 | } while (EatIfPresent(T: lltok::comma)); |
| 9877 | |
| 9878 | // Now that the TI vector is finalized, it is safe to save the locations |
| 9879 | // of any forward GV references that need updating later. |
| 9880 | for (auto I : IdToIndexMap) { |
| 9881 | auto &Infos = ForwardRefValueInfos[I.first]; |
| 9882 | for (auto P : I.second) { |
| 9883 | assert(TI[P.first].VTableVI == EmptyVI && |
| 9884 | "Forward referenced ValueInfo expected to be empty" ); |
| 9885 | Infos.emplace_back(args: &TI[P.first].VTableVI, args&: P.second); |
| 9886 | } |
| 9887 | } |
| 9888 | |
| 9889 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ) || |
| 9890 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 9891 | return true; |
| 9892 | |
| 9893 | // Check if this ID was forward referenced, and if so, update the |
| 9894 | // corresponding GUIDs. |
| 9895 | auto FwdRefTIDs = ForwardRefTypeIds.find(x: ID); |
| 9896 | if (FwdRefTIDs != ForwardRefTypeIds.end()) { |
| 9897 | for (auto TIDRef : FwdRefTIDs->second) { |
| 9898 | assert(!*TIDRef.first && |
| 9899 | "Forward referenced type id GUID expected to be 0" ); |
| 9900 | *TIDRef.first = GlobalValue::getGUIDAssumingExternalLinkage(GlobalName: Name); |
| 9901 | } |
| 9902 | ForwardRefTypeIds.erase(position: FwdRefTIDs); |
| 9903 | } |
| 9904 | |
| 9905 | return false; |
| 9906 | } |
| 9907 | |
| 9908 | /// TypeTestResolution |
| 9909 | /// ::= 'typeTestRes' ':' '(' 'kind' ':' |
| 9910 | /// ( 'unsat' | 'byteArray' | 'inline' | 'single' | 'allOnes' ) ',' |
| 9911 | /// 'sizeM1BitWidth' ':' SizeM1BitWidth [',' 'alignLog2' ':' UInt64]? |
| 9912 | /// [',' 'sizeM1' ':' UInt64]? [',' 'bitMask' ':' UInt8]? |
| 9913 | /// [',' 'inlinesBits' ':' UInt64]? ')' |
| 9914 | bool LLParser::parseTypeTestResolution(TypeTestResolution &TTRes) { |
| 9915 | if (parseToken(T: lltok::kw_typeTestRes, ErrMsg: "expected 'typeTestRes' here" ) || |
| 9916 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9917 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 9918 | parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here" ) || |
| 9919 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 9920 | return true; |
| 9921 | |
| 9922 | switch (Lex.getKind()) { |
| 9923 | case lltok::kw_unknown: |
| 9924 | TTRes.TheKind = TypeTestResolution::Unknown; |
| 9925 | break; |
| 9926 | case lltok::kw_unsat: |
| 9927 | TTRes.TheKind = TypeTestResolution::Unsat; |
| 9928 | break; |
| 9929 | case lltok::kw_byteArray: |
| 9930 | TTRes.TheKind = TypeTestResolution::ByteArray; |
| 9931 | break; |
| 9932 | case lltok::kw_inline: |
| 9933 | TTRes.TheKind = TypeTestResolution::Inline; |
| 9934 | break; |
| 9935 | case lltok::kw_single: |
| 9936 | TTRes.TheKind = TypeTestResolution::Single; |
| 9937 | break; |
| 9938 | case lltok::kw_allOnes: |
| 9939 | TTRes.TheKind = TypeTestResolution::AllOnes; |
| 9940 | break; |
| 9941 | default: |
| 9942 | return error(L: Lex.getLoc(), Msg: "unexpected TypeTestResolution kind" ); |
| 9943 | } |
| 9944 | Lex.Lex(); |
| 9945 | |
| 9946 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 9947 | parseToken(T: lltok::kw_sizeM1BitWidth, ErrMsg: "expected 'sizeM1BitWidth' here" ) || |
| 9948 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9949 | parseUInt32(Val&: TTRes.SizeM1BitWidth)) |
| 9950 | return true; |
| 9951 | |
| 9952 | // parse optional fields |
| 9953 | while (EatIfPresent(T: lltok::comma)) { |
| 9954 | switch (Lex.getKind()) { |
| 9955 | case lltok::kw_alignLog2: |
| 9956 | Lex.Lex(); |
| 9957 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
| 9958 | parseUInt64(Val&: TTRes.AlignLog2)) |
| 9959 | return true; |
| 9960 | break; |
| 9961 | case lltok::kw_sizeM1: |
| 9962 | Lex.Lex(); |
| 9963 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt64(Val&: TTRes.SizeM1)) |
| 9964 | return true; |
| 9965 | break; |
| 9966 | case lltok::kw_bitMask: { |
| 9967 | unsigned Val; |
| 9968 | Lex.Lex(); |
| 9969 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt32(Val)) |
| 9970 | return true; |
| 9971 | assert(Val <= 0xff); |
| 9972 | TTRes.BitMask = (uint8_t)Val; |
| 9973 | break; |
| 9974 | } |
| 9975 | case lltok::kw_inlineBits: |
| 9976 | Lex.Lex(); |
| 9977 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
| 9978 | parseUInt64(Val&: TTRes.InlineBits)) |
| 9979 | return true; |
| 9980 | break; |
| 9981 | default: |
| 9982 | return error(L: Lex.getLoc(), Msg: "expected optional TypeTestResolution field" ); |
| 9983 | } |
| 9984 | } |
| 9985 | |
| 9986 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 9987 | return true; |
| 9988 | |
| 9989 | return false; |
| 9990 | } |
| 9991 | |
| 9992 | /// OptionalWpdResolutions |
| 9993 | /// ::= 'wpsResolutions' ':' '(' WpdResolution [',' WpdResolution]* ')' |
| 9994 | /// WpdResolution ::= '(' 'offset' ':' UInt64 ',' WpdRes ')' |
| 9995 | bool LLParser::parseOptionalWpdResolutions( |
| 9996 | std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap) { |
| 9997 | if (parseToken(T: lltok::kw_wpdResolutions, ErrMsg: "expected 'wpdResolutions' here" ) || |
| 9998 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 9999 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10000 | return true; |
| 10001 | |
| 10002 | do { |
| 10003 | uint64_t Offset; |
| 10004 | WholeProgramDevirtResolution WPDRes; |
| 10005 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10006 | parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
| 10007 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: Offset) || |
| 10008 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseWpdRes(WPDRes) || |
| 10009 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10010 | return true; |
| 10011 | WPDResMap[Offset] = WPDRes; |
| 10012 | } while (EatIfPresent(T: lltok::comma)); |
| 10013 | |
| 10014 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10015 | return true; |
| 10016 | |
| 10017 | return false; |
| 10018 | } |
| 10019 | |
| 10020 | /// WpdRes |
| 10021 | /// ::= 'wpdRes' ':' '(' 'kind' ':' 'indir' |
| 10022 | /// [',' OptionalResByArg]? ')' |
| 10023 | /// ::= 'wpdRes' ':' '(' 'kind' ':' 'singleImpl' |
| 10024 | /// ',' 'singleImplName' ':' STRINGCONSTANT ',' |
| 10025 | /// [',' OptionalResByArg]? ')' |
| 10026 | /// ::= 'wpdRes' ':' '(' 'kind' ':' 'branchFunnel' |
| 10027 | /// [',' OptionalResByArg]? ')' |
| 10028 | bool LLParser::parseWpdRes(WholeProgramDevirtResolution &WPDRes) { |
| 10029 | if (parseToken(T: lltok::kw_wpdRes, ErrMsg: "expected 'wpdRes' here" ) || |
| 10030 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10031 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10032 | parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here" ) || |
| 10033 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 10034 | return true; |
| 10035 | |
| 10036 | switch (Lex.getKind()) { |
| 10037 | case lltok::kw_indir: |
| 10038 | WPDRes.TheKind = WholeProgramDevirtResolution::Indir; |
| 10039 | break; |
| 10040 | case lltok::kw_singleImpl: |
| 10041 | WPDRes.TheKind = WholeProgramDevirtResolution::SingleImpl; |
| 10042 | break; |
| 10043 | case lltok::kw_branchFunnel: |
| 10044 | WPDRes.TheKind = WholeProgramDevirtResolution::BranchFunnel; |
| 10045 | break; |
| 10046 | default: |
| 10047 | return error(L: Lex.getLoc(), Msg: "unexpected WholeProgramDevirtResolution kind" ); |
| 10048 | } |
| 10049 | Lex.Lex(); |
| 10050 | |
| 10051 | // parse optional fields |
| 10052 | while (EatIfPresent(T: lltok::comma)) { |
| 10053 | switch (Lex.getKind()) { |
| 10054 | case lltok::kw_singleImplName: |
| 10055 | Lex.Lex(); |
| 10056 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10057 | parseStringConstant(Result&: WPDRes.SingleImplName)) |
| 10058 | return true; |
| 10059 | break; |
| 10060 | case lltok::kw_resByArg: |
| 10061 | if (parseOptionalResByArg(ResByArg&: WPDRes.ResByArg)) |
| 10062 | return true; |
| 10063 | break; |
| 10064 | default: |
| 10065 | return error(L: Lex.getLoc(), |
| 10066 | Msg: "expected optional WholeProgramDevirtResolution field" ); |
| 10067 | } |
| 10068 | } |
| 10069 | |
| 10070 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10071 | return true; |
| 10072 | |
| 10073 | return false; |
| 10074 | } |
| 10075 | |
| 10076 | /// OptionalResByArg |
| 10077 | /// ::= 'wpdRes' ':' '(' ResByArg[, ResByArg]* ')' |
| 10078 | /// ResByArg ::= Args ',' 'byArg' ':' '(' 'kind' ':' |
| 10079 | /// ( 'indir' | 'uniformRetVal' | 'UniqueRetVal' | |
| 10080 | /// 'virtualConstProp' ) |
| 10081 | /// [',' 'info' ':' UInt64]? [',' 'byte' ':' UInt32]? |
| 10082 | /// [',' 'bit' ':' UInt32]? ')' |
| 10083 | bool LLParser::parseOptionalResByArg( |
| 10084 | std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> |
| 10085 | &ResByArg) { |
| 10086 | if (parseToken(T: lltok::kw_resByArg, ErrMsg: "expected 'resByArg' here" ) || |
| 10087 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10088 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10089 | return true; |
| 10090 | |
| 10091 | do { |
| 10092 | std::vector<uint64_t> Args; |
| 10093 | if (parseArgs(Args) || parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10094 | parseToken(T: lltok::kw_byArg, ErrMsg: "expected 'byArg here" ) || |
| 10095 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10096 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10097 | parseToken(T: lltok::kw_kind, ErrMsg: "expected 'kind' here" ) || |
| 10098 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 10099 | return true; |
| 10100 | |
| 10101 | WholeProgramDevirtResolution::ByArg ByArg; |
| 10102 | switch (Lex.getKind()) { |
| 10103 | case lltok::kw_indir: |
| 10104 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::Indir; |
| 10105 | break; |
| 10106 | case lltok::kw_uniformRetVal: |
| 10107 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniformRetVal; |
| 10108 | break; |
| 10109 | case lltok::kw_uniqueRetVal: |
| 10110 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::UniqueRetVal; |
| 10111 | break; |
| 10112 | case lltok::kw_virtualConstProp: |
| 10113 | ByArg.TheKind = WholeProgramDevirtResolution::ByArg::VirtualConstProp; |
| 10114 | break; |
| 10115 | default: |
| 10116 | return error(L: Lex.getLoc(), |
| 10117 | Msg: "unexpected WholeProgramDevirtResolution::ByArg kind" ); |
| 10118 | } |
| 10119 | Lex.Lex(); |
| 10120 | |
| 10121 | // parse optional fields |
| 10122 | while (EatIfPresent(T: lltok::comma)) { |
| 10123 | switch (Lex.getKind()) { |
| 10124 | case lltok::kw_info: |
| 10125 | Lex.Lex(); |
| 10126 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10127 | parseUInt64(Val&: ByArg.Info)) |
| 10128 | return true; |
| 10129 | break; |
| 10130 | case lltok::kw_byte: |
| 10131 | Lex.Lex(); |
| 10132 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10133 | parseUInt32(Val&: ByArg.Byte)) |
| 10134 | return true; |
| 10135 | break; |
| 10136 | case lltok::kw_bit: |
| 10137 | Lex.Lex(); |
| 10138 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10139 | parseUInt32(Val&: ByArg.Bit)) |
| 10140 | return true; |
| 10141 | break; |
| 10142 | default: |
| 10143 | return error(L: Lex.getLoc(), |
| 10144 | Msg: "expected optional whole program devirt field" ); |
| 10145 | } |
| 10146 | } |
| 10147 | |
| 10148 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10149 | return true; |
| 10150 | |
| 10151 | ResByArg[Args] = ByArg; |
| 10152 | } while (EatIfPresent(T: lltok::comma)); |
| 10153 | |
| 10154 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10155 | return true; |
| 10156 | |
| 10157 | return false; |
| 10158 | } |
| 10159 | |
| 10160 | /// OptionalResByArg |
| 10161 | /// ::= 'args' ':' '(' UInt64[, UInt64]* ')' |
| 10162 | bool LLParser::parseArgs(std::vector<uint64_t> &Args) { |
| 10163 | if (parseToken(T: lltok::kw_args, ErrMsg: "expected 'args' here" ) || |
| 10164 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10165 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10166 | return true; |
| 10167 | |
| 10168 | do { |
| 10169 | uint64_t Val; |
| 10170 | if (parseUInt64(Val)) |
| 10171 | return true; |
| 10172 | Args.push_back(x: Val); |
| 10173 | } while (EatIfPresent(T: lltok::comma)); |
| 10174 | |
| 10175 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10176 | return true; |
| 10177 | |
| 10178 | return false; |
| 10179 | } |
| 10180 | |
| 10181 | static const auto FwdVIRef = (GlobalValueSummaryMapTy::value_type *)-8; |
| 10182 | |
| 10183 | static void resolveFwdRef(ValueInfo *Fwd, ValueInfo &Resolved) { |
| 10184 | bool ReadOnly = Fwd->isReadOnly(); |
| 10185 | bool WriteOnly = Fwd->isWriteOnly(); |
| 10186 | assert(!(ReadOnly && WriteOnly)); |
| 10187 | *Fwd = Resolved; |
| 10188 | if (ReadOnly) |
| 10189 | Fwd->setReadOnly(); |
| 10190 | if (WriteOnly) |
| 10191 | Fwd->setWriteOnly(); |
| 10192 | } |
| 10193 | |
| 10194 | /// Stores the given Name/GUID and associated summary into the Index. |
| 10195 | /// Also updates any forward references to the associated entry ID. |
| 10196 | bool LLParser::addGlobalValueToIndex( |
| 10197 | std::string Name, GlobalValue::GUID GUID, GlobalValue::LinkageTypes Linkage, |
| 10198 | unsigned ID, std::unique_ptr<GlobalValueSummary> Summary, LocTy Loc) { |
| 10199 | // First create the ValueInfo utilizing the Name or GUID. |
| 10200 | ValueInfo VI; |
| 10201 | if (GUID != 0) { |
| 10202 | assert(Name.empty()); |
| 10203 | VI = Index->getOrInsertValueInfo(GUID); |
| 10204 | } else { |
| 10205 | assert(!Name.empty()); |
| 10206 | if (M) { |
| 10207 | auto *GV = M->getNamedValue(Name); |
| 10208 | if (!GV) |
| 10209 | return error(L: Loc, Msg: "Reference to undefined global \"" + Name + "\"" ); |
| 10210 | |
| 10211 | // Be a little lenient here, to accomodate older files without GUIDs |
| 10212 | // already computed and assigned as metadata. |
| 10213 | GUID = GV->getGUIDOrFallback(); |
| 10214 | |
| 10215 | VI = Index->getOrInsertValueInfo(GV, GUID); |
| 10216 | } else { |
| 10217 | assert( |
| 10218 | (!GlobalValue::isLocalLinkage(Linkage) || !SourceFileName.empty()) && |
| 10219 | "Need a source_filename to compute GUID for local" ); |
| 10220 | GUID = GlobalValue::getGUIDAssumingExternalLinkage( |
| 10221 | GlobalName: GlobalValue::getGlobalIdentifier(Name, Linkage, FileName: SourceFileName)); |
| 10222 | VI = Index->getOrInsertValueInfo(GUID, Name: Index->saveString(String: Name)); |
| 10223 | } |
| 10224 | } |
| 10225 | |
| 10226 | // Resolve forward references from calls/refs |
| 10227 | auto FwdRefVIs = ForwardRefValueInfos.find(x: ID); |
| 10228 | if (FwdRefVIs != ForwardRefValueInfos.end()) { |
| 10229 | for (auto VIRef : FwdRefVIs->second) { |
| 10230 | assert(VIRef.first->getRef() == FwdVIRef && |
| 10231 | "Forward referenced ValueInfo expected to be empty" ); |
| 10232 | resolveFwdRef(Fwd: VIRef.first, Resolved&: VI); |
| 10233 | } |
| 10234 | ForwardRefValueInfos.erase(position: FwdRefVIs); |
| 10235 | } |
| 10236 | |
| 10237 | // Resolve forward references from aliases |
| 10238 | auto FwdRefAliasees = ForwardRefAliasees.find(x: ID); |
| 10239 | if (FwdRefAliasees != ForwardRefAliasees.end()) { |
| 10240 | for (auto AliaseeRef : FwdRefAliasees->second) { |
| 10241 | assert(!AliaseeRef.first->hasAliasee() && |
| 10242 | "Forward referencing alias already has aliasee" ); |
| 10243 | assert(Summary && "Aliasee must be a definition" ); |
| 10244 | AliaseeRef.first->setAliasee(AliaseeVI&: VI, Aliasee: Summary.get()); |
| 10245 | } |
| 10246 | ForwardRefAliasees.erase(position: FwdRefAliasees); |
| 10247 | } |
| 10248 | |
| 10249 | // Add the summary if one was provided. |
| 10250 | if (Summary) |
| 10251 | Index->addGlobalValueSummary(VI, Summary: std::move(Summary)); |
| 10252 | |
| 10253 | // Save the associated ValueInfo for use in later references by ID. |
| 10254 | if (ID == NumberedValueInfos.size()) |
| 10255 | NumberedValueInfos.push_back(x: VI); |
| 10256 | else { |
| 10257 | // Handle non-continuous numbers (to make test simplification easier). |
| 10258 | if (ID > NumberedValueInfos.size()) |
| 10259 | NumberedValueInfos.resize(new_size: ID + 1); |
| 10260 | NumberedValueInfos[ID] = VI; |
| 10261 | } |
| 10262 | |
| 10263 | return false; |
| 10264 | } |
| 10265 | |
| 10266 | /// parseSummaryIndexFlags |
| 10267 | /// ::= 'flags' ':' UInt64 |
| 10268 | bool LLParser::parseSummaryIndexFlags() { |
| 10269 | assert(Lex.getKind() == lltok::kw_flags); |
| 10270 | Lex.Lex(); |
| 10271 | |
| 10272 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 10273 | return true; |
| 10274 | uint64_t Flags; |
| 10275 | if (parseUInt64(Val&: Flags)) |
| 10276 | return true; |
| 10277 | if (Index) |
| 10278 | Index->setFlags(Flags); |
| 10279 | return false; |
| 10280 | } |
| 10281 | |
| 10282 | /// parseBlockCount |
| 10283 | /// ::= 'blockcount' ':' UInt64 |
| 10284 | bool LLParser::parseBlockCount() { |
| 10285 | assert(Lex.getKind() == lltok::kw_blockcount); |
| 10286 | Lex.Lex(); |
| 10287 | |
| 10288 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 10289 | return true; |
| 10290 | uint64_t BlockCount; |
| 10291 | if (parseUInt64(Val&: BlockCount)) |
| 10292 | return true; |
| 10293 | if (Index) |
| 10294 | Index->setBlockCount(BlockCount); |
| 10295 | return false; |
| 10296 | } |
| 10297 | |
| 10298 | /// parseGVEntry |
| 10299 | /// ::= 'gv' ':' '(' ('name' ':' STRINGCONSTANT | 'guid' ':' UInt64) |
| 10300 | /// [',' 'summaries' ':' Summary[',' Summary]* ]? ')' |
| 10301 | /// Summary ::= '(' (FunctionSummary | VariableSummary | AliasSummary) ')' |
| 10302 | bool LLParser::parseGVEntry(unsigned ID) { |
| 10303 | assert(Lex.getKind() == lltok::kw_gv); |
| 10304 | Lex.Lex(); |
| 10305 | |
| 10306 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10307 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10308 | return true; |
| 10309 | |
| 10310 | LocTy Loc = Lex.getLoc(); |
| 10311 | std::string Name; |
| 10312 | GlobalValue::GUID GUID = 0; |
| 10313 | switch (Lex.getKind()) { |
| 10314 | case lltok::kw_name: |
| 10315 | Lex.Lex(); |
| 10316 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10317 | parseStringConstant(Result&: Name)) |
| 10318 | return true; |
| 10319 | // Can't create GUID/ValueInfo until we have the linkage. |
| 10320 | break; |
| 10321 | case lltok::kw_guid: |
| 10322 | Lex.Lex(); |
| 10323 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: GUID)) |
| 10324 | return true; |
| 10325 | break; |
| 10326 | default: |
| 10327 | return error(L: Lex.getLoc(), Msg: "expected name or guid tag" ); |
| 10328 | } |
| 10329 | |
| 10330 | if (!EatIfPresent(T: lltok::comma)) { |
| 10331 | // No summaries. Wrap up. |
| 10332 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10333 | return true; |
| 10334 | // This was created for a call to an external or indirect target. |
| 10335 | // A GUID with no summary came from a VALUE_GUID record, dummy GUID |
| 10336 | // created for indirect calls with VP. A Name with no GUID came from |
| 10337 | // an external definition. We pass ExternalLinkage since that is only |
| 10338 | // used when the GUID must be computed from Name, and in that case |
| 10339 | // the symbol must have external linkage. |
| 10340 | return addGlobalValueToIndex(Name, GUID, Linkage: GlobalValue::ExternalLinkage, ID, |
| 10341 | Summary: nullptr, Loc); |
| 10342 | } |
| 10343 | |
| 10344 | // Have a list of summaries |
| 10345 | if (parseToken(T: lltok::kw_summaries, ErrMsg: "expected 'summaries' here" ) || |
| 10346 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10347 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10348 | return true; |
| 10349 | do { |
| 10350 | switch (Lex.getKind()) { |
| 10351 | case lltok::kw_function: |
| 10352 | if (parseFunctionSummary(Name, GUID, ID)) |
| 10353 | return true; |
| 10354 | break; |
| 10355 | case lltok::kw_variable: |
| 10356 | if (parseVariableSummary(Name, GUID, ID)) |
| 10357 | return true; |
| 10358 | break; |
| 10359 | case lltok::kw_alias: |
| 10360 | if (parseAliasSummary(Name, GUID, ID)) |
| 10361 | return true; |
| 10362 | break; |
| 10363 | default: |
| 10364 | return error(L: Lex.getLoc(), Msg: "expected summary type" ); |
| 10365 | } |
| 10366 | } while (EatIfPresent(T: lltok::comma)); |
| 10367 | |
| 10368 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ) || |
| 10369 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10370 | return true; |
| 10371 | |
| 10372 | return false; |
| 10373 | } |
| 10374 | |
| 10375 | /// FunctionSummary |
| 10376 | /// ::= 'function' ':' '(' 'module' ':' ModuleReference ',' GVFlags |
| 10377 | /// ',' 'insts' ':' UInt32 [',' OptionalFFlags]? [',' OptionalCalls]? |
| 10378 | /// [',' OptionalTypeIdInfo]? [',' OptionalParamAccesses]? |
| 10379 | /// [',' OptionalRefs]? ')' |
| 10380 | bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID, |
| 10381 | unsigned ID) { |
| 10382 | LocTy Loc = Lex.getLoc(); |
| 10383 | assert(Lex.getKind() == lltok::kw_function); |
| 10384 | Lex.Lex(); |
| 10385 | |
| 10386 | StringRef ModulePath; |
| 10387 | GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags( |
| 10388 | GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility, |
| 10389 | /*NotEligibleToImport=*/false, |
| 10390 | /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false, |
| 10391 | GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false); |
| 10392 | unsigned InstCount; |
| 10393 | SmallVector<FunctionSummary::EdgeTy, 0> Calls; |
| 10394 | FunctionSummary::TypeIdInfo TypeIdInfo; |
| 10395 | std::vector<FunctionSummary::ParamAccess> ParamAccesses; |
| 10396 | SmallVector<ValueInfo, 0> Refs; |
| 10397 | std::vector<CallsiteInfo> Callsites; |
| 10398 | std::vector<AllocInfo> Allocs; |
| 10399 | // Default is all-zeros (conservative values). |
| 10400 | FunctionSummary::FFlags FFlags = {}; |
| 10401 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10402 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10403 | parseModuleReference(ModulePath) || |
| 10404 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseGVFlags(GVFlags) || |
| 10405 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10406 | parseToken(T: lltok::kw_insts, ErrMsg: "expected 'insts' here" ) || |
| 10407 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt32(Val&: InstCount)) |
| 10408 | return true; |
| 10409 | |
| 10410 | // parse optional fields |
| 10411 | while (EatIfPresent(T: lltok::comma)) { |
| 10412 | switch (Lex.getKind()) { |
| 10413 | case lltok::kw_funcFlags: |
| 10414 | if (parseOptionalFFlags(FFlags)) |
| 10415 | return true; |
| 10416 | break; |
| 10417 | case lltok::kw_calls: |
| 10418 | if (parseOptionalCalls(Calls)) |
| 10419 | return true; |
| 10420 | break; |
| 10421 | case lltok::kw_typeIdInfo: |
| 10422 | if (parseOptionalTypeIdInfo(TypeIdInfo)) |
| 10423 | return true; |
| 10424 | break; |
| 10425 | case lltok::kw_refs: |
| 10426 | if (parseOptionalRefs(Refs)) |
| 10427 | return true; |
| 10428 | break; |
| 10429 | case lltok::kw_params: |
| 10430 | if (parseOptionalParamAccesses(Params&: ParamAccesses)) |
| 10431 | return true; |
| 10432 | break; |
| 10433 | case lltok::kw_allocs: |
| 10434 | if (parseOptionalAllocs(Allocs)) |
| 10435 | return true; |
| 10436 | break; |
| 10437 | case lltok::kw_callsites: |
| 10438 | if (parseOptionalCallsites(Callsites)) |
| 10439 | return true; |
| 10440 | break; |
| 10441 | default: |
| 10442 | return error(L: Lex.getLoc(), Msg: "expected optional function summary field" ); |
| 10443 | } |
| 10444 | } |
| 10445 | |
| 10446 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10447 | return true; |
| 10448 | |
| 10449 | auto FS = std::make_unique<FunctionSummary>( |
| 10450 | args&: GVFlags, args&: InstCount, args&: FFlags, args: std::move(Refs), args: std::move(Calls), |
| 10451 | args: std::move(TypeIdInfo.TypeTests), |
| 10452 | args: std::move(TypeIdInfo.TypeTestAssumeVCalls), |
| 10453 | args: std::move(TypeIdInfo.TypeCheckedLoadVCalls), |
| 10454 | args: std::move(TypeIdInfo.TypeTestAssumeConstVCalls), |
| 10455 | args: std::move(TypeIdInfo.TypeCheckedLoadConstVCalls), |
| 10456 | args: std::move(ParamAccesses), args: std::move(Callsites), args: std::move(Allocs)); |
| 10457 | |
| 10458 | FS->setModulePath(ModulePath); |
| 10459 | |
| 10460 | return addGlobalValueToIndex(Name, GUID, |
| 10461 | Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, |
| 10462 | Summary: std::move(FS), Loc); |
| 10463 | } |
| 10464 | |
| 10465 | /// VariableSummary |
| 10466 | /// ::= 'variable' ':' '(' 'module' ':' ModuleReference ',' GVFlags |
| 10467 | /// [',' OptionalRefs]? ')' |
| 10468 | bool LLParser::parseVariableSummary(std::string Name, GlobalValue::GUID GUID, |
| 10469 | unsigned ID) { |
| 10470 | LocTy Loc = Lex.getLoc(); |
| 10471 | assert(Lex.getKind() == lltok::kw_variable); |
| 10472 | Lex.Lex(); |
| 10473 | |
| 10474 | StringRef ModulePath; |
| 10475 | GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags( |
| 10476 | GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility, |
| 10477 | /*NotEligibleToImport=*/false, |
| 10478 | /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false, |
| 10479 | GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false); |
| 10480 | GlobalVarSummary::GVarFlags GVarFlags(/*ReadOnly*/ false, |
| 10481 | /* WriteOnly */ false, |
| 10482 | /* Constant */ false, |
| 10483 | GlobalObject::VCallVisibilityPublic); |
| 10484 | SmallVector<ValueInfo, 0> Refs; |
| 10485 | VTableFuncList VTableFuncs; |
| 10486 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10487 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10488 | parseModuleReference(ModulePath) || |
| 10489 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseGVFlags(GVFlags) || |
| 10490 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10491 | parseGVarFlags(GVarFlags)) |
| 10492 | return true; |
| 10493 | |
| 10494 | // parse optional fields |
| 10495 | while (EatIfPresent(T: lltok::comma)) { |
| 10496 | switch (Lex.getKind()) { |
| 10497 | case lltok::kw_vTableFuncs: |
| 10498 | if (parseOptionalVTableFuncs(VTableFuncs)) |
| 10499 | return true; |
| 10500 | break; |
| 10501 | case lltok::kw_refs: |
| 10502 | if (parseOptionalRefs(Refs)) |
| 10503 | return true; |
| 10504 | break; |
| 10505 | default: |
| 10506 | return error(L: Lex.getLoc(), Msg: "expected optional variable summary field" ); |
| 10507 | } |
| 10508 | } |
| 10509 | |
| 10510 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10511 | return true; |
| 10512 | |
| 10513 | auto GS = |
| 10514 | std::make_unique<GlobalVarSummary>(args&: GVFlags, args&: GVarFlags, args: std::move(Refs)); |
| 10515 | |
| 10516 | GS->setModulePath(ModulePath); |
| 10517 | GS->setVTableFuncs(std::move(VTableFuncs)); |
| 10518 | |
| 10519 | return addGlobalValueToIndex(Name, GUID, |
| 10520 | Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, |
| 10521 | Summary: std::move(GS), Loc); |
| 10522 | } |
| 10523 | |
| 10524 | /// AliasSummary |
| 10525 | /// ::= 'alias' ':' '(' 'module' ':' ModuleReference ',' GVFlags ',' |
| 10526 | /// 'aliasee' ':' GVReference ')' |
| 10527 | bool LLParser::parseAliasSummary(std::string Name, GlobalValue::GUID GUID, |
| 10528 | unsigned ID) { |
| 10529 | assert(Lex.getKind() == lltok::kw_alias); |
| 10530 | LocTy Loc = Lex.getLoc(); |
| 10531 | Lex.Lex(); |
| 10532 | |
| 10533 | StringRef ModulePath; |
| 10534 | GlobalValueSummary::GVFlags GVFlags = GlobalValueSummary::GVFlags( |
| 10535 | GlobalValue::ExternalLinkage, GlobalValue::DefaultVisibility, |
| 10536 | /*NotEligibleToImport=*/false, |
| 10537 | /*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false, |
| 10538 | GlobalValueSummary::Definition, /*NoRenameOnPromotion=*/false); |
| 10539 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10540 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10541 | parseModuleReference(ModulePath) || |
| 10542 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || parseGVFlags(GVFlags) || |
| 10543 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10544 | parseToken(T: lltok::kw_aliasee, ErrMsg: "expected 'aliasee' here" ) || |
| 10545 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 10546 | return true; |
| 10547 | |
| 10548 | ValueInfo AliaseeVI; |
| 10549 | unsigned GVId; |
| 10550 | auto AS = std::make_unique<AliasSummary>(args&: GVFlags); |
| 10551 | AS->setModulePath(ModulePath); |
| 10552 | |
| 10553 | if (!EatIfPresent(T: lltok::kw_null)) { |
| 10554 | if (parseGVReference(VI&: AliaseeVI, GVId)) |
| 10555 | return true; |
| 10556 | |
| 10557 | // Record forward reference if the aliasee is not parsed yet. |
| 10558 | if (AliaseeVI.getRef() == FwdVIRef) { |
| 10559 | ForwardRefAliasees[GVId].emplace_back(args: AS.get(), args&: Loc); |
| 10560 | } else { |
| 10561 | auto Summary = Index->findSummaryInModule(VI: AliaseeVI, ModuleId: ModulePath); |
| 10562 | assert(Summary && "Aliasee must be a definition" ); |
| 10563 | AS->setAliasee(AliaseeVI, Aliasee: Summary); |
| 10564 | } |
| 10565 | } |
| 10566 | |
| 10567 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10568 | return true; |
| 10569 | |
| 10570 | return addGlobalValueToIndex(Name, GUID, |
| 10571 | Linkage: (GlobalValue::LinkageTypes)GVFlags.Linkage, ID, |
| 10572 | Summary: std::move(AS), Loc); |
| 10573 | } |
| 10574 | |
| 10575 | /// Flag |
| 10576 | /// ::= [0|1] |
| 10577 | bool LLParser::parseFlag(unsigned &Val) { |
| 10578 | if (Lex.getKind() != lltok::APSInt || Lex.getAPSIntVal().isSigned()) |
| 10579 | return tokError(Msg: "expected integer" ); |
| 10580 | Val = (unsigned)Lex.getAPSIntVal().getBoolValue(); |
| 10581 | Lex.Lex(); |
| 10582 | return false; |
| 10583 | } |
| 10584 | |
| 10585 | /// OptionalFFlags |
| 10586 | /// := 'funcFlags' ':' '(' ['readNone' ':' Flag]? |
| 10587 | /// [',' 'readOnly' ':' Flag]? [',' 'noRecurse' ':' Flag]? |
| 10588 | /// [',' 'returnDoesNotAlias' ':' Flag]? ')' |
| 10589 | /// [',' 'noInline' ':' Flag]? ')' |
| 10590 | /// [',' 'alwaysInline' ':' Flag]? ')' |
| 10591 | /// [',' 'noUnwind' ':' Flag]? ')' |
| 10592 | /// [',' 'mayThrow' ':' Flag]? ')' |
| 10593 | /// [',' 'hasUnknownCall' ':' Flag]? ')' |
| 10594 | /// [',' 'mustBeUnreachable' ':' Flag]? ')' |
| 10595 | |
| 10596 | bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) { |
| 10597 | assert(Lex.getKind() == lltok::kw_funcFlags); |
| 10598 | Lex.Lex(); |
| 10599 | |
| 10600 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in funcFlags" ) || |
| 10601 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in funcFlags" )) |
| 10602 | return true; |
| 10603 | |
| 10604 | do { |
| 10605 | unsigned Val = 0; |
| 10606 | switch (Lex.getKind()) { |
| 10607 | case lltok::kw_readNone: |
| 10608 | Lex.Lex(); |
| 10609 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10610 | return true; |
| 10611 | FFlags.ReadNone = Val; |
| 10612 | break; |
| 10613 | case lltok::kw_readOnly: |
| 10614 | Lex.Lex(); |
| 10615 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10616 | return true; |
| 10617 | FFlags.ReadOnly = Val; |
| 10618 | break; |
| 10619 | case lltok::kw_noRecurse: |
| 10620 | Lex.Lex(); |
| 10621 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10622 | return true; |
| 10623 | FFlags.NoRecurse = Val; |
| 10624 | break; |
| 10625 | case lltok::kw_returnDoesNotAlias: |
| 10626 | Lex.Lex(); |
| 10627 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10628 | return true; |
| 10629 | FFlags.ReturnDoesNotAlias = Val; |
| 10630 | break; |
| 10631 | case lltok::kw_noInline: |
| 10632 | Lex.Lex(); |
| 10633 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10634 | return true; |
| 10635 | FFlags.NoInline = Val; |
| 10636 | break; |
| 10637 | case lltok::kw_alwaysInline: |
| 10638 | Lex.Lex(); |
| 10639 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10640 | return true; |
| 10641 | FFlags.AlwaysInline = Val; |
| 10642 | break; |
| 10643 | case lltok::kw_noUnwind: |
| 10644 | Lex.Lex(); |
| 10645 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10646 | return true; |
| 10647 | FFlags.NoUnwind = Val; |
| 10648 | break; |
| 10649 | case lltok::kw_mayThrow: |
| 10650 | Lex.Lex(); |
| 10651 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10652 | return true; |
| 10653 | FFlags.MayThrow = Val; |
| 10654 | break; |
| 10655 | case lltok::kw_hasUnknownCall: |
| 10656 | Lex.Lex(); |
| 10657 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10658 | return true; |
| 10659 | FFlags.HasUnknownCall = Val; |
| 10660 | break; |
| 10661 | case lltok::kw_mustBeUnreachable: |
| 10662 | Lex.Lex(); |
| 10663 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val)) |
| 10664 | return true; |
| 10665 | FFlags.MustBeUnreachable = Val; |
| 10666 | break; |
| 10667 | default: |
| 10668 | return error(L: Lex.getLoc(), Msg: "expected function flag type" ); |
| 10669 | } |
| 10670 | } while (EatIfPresent(T: lltok::comma)); |
| 10671 | |
| 10672 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in funcFlags" )) |
| 10673 | return true; |
| 10674 | |
| 10675 | return false; |
| 10676 | } |
| 10677 | |
| 10678 | /// OptionalCalls |
| 10679 | /// := 'calls' ':' '(' Call [',' Call]* ')' |
| 10680 | /// Call ::= '(' 'callee' ':' GVReference |
| 10681 | /// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]? |
| 10682 | /// [ ',' 'tail' ]? ')' |
| 10683 | bool LLParser::parseOptionalCalls( |
| 10684 | SmallVectorImpl<FunctionSummary::EdgeTy> &Calls) { |
| 10685 | assert(Lex.getKind() == lltok::kw_calls); |
| 10686 | Lex.Lex(); |
| 10687 | |
| 10688 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in calls" ) || |
| 10689 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in calls" )) |
| 10690 | return true; |
| 10691 | |
| 10692 | IdToIndexMapType IdToIndexMap; |
| 10693 | // parse each call edge |
| 10694 | do { |
| 10695 | ValueInfo VI; |
| 10696 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in call" ) || |
| 10697 | parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' in call" ) || |
| 10698 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 10699 | return true; |
| 10700 | |
| 10701 | LocTy Loc = Lex.getLoc(); |
| 10702 | unsigned GVId; |
| 10703 | if (parseGVReference(VI, GVId)) |
| 10704 | return true; |
| 10705 | |
| 10706 | CalleeInfo::HotnessType Hotness = CalleeInfo::HotnessType::Unknown; |
| 10707 | unsigned RelBF = 0; |
| 10708 | unsigned HasTailCall = false; |
| 10709 | |
| 10710 | // parse optional fields |
| 10711 | while (EatIfPresent(T: lltok::comma)) { |
| 10712 | switch (Lex.getKind()) { |
| 10713 | case lltok::kw_hotness: |
| 10714 | Lex.Lex(); |
| 10715 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseHotness(Hotness)) |
| 10716 | return true; |
| 10717 | break; |
| 10718 | // Deprecated, keep in order to support old files. |
| 10719 | case lltok::kw_relbf: |
| 10720 | Lex.Lex(); |
| 10721 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt32(Val&: RelBF)) |
| 10722 | return true; |
| 10723 | break; |
| 10724 | case lltok::kw_tail: |
| 10725 | Lex.Lex(); |
| 10726 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: HasTailCall)) |
| 10727 | return true; |
| 10728 | break; |
| 10729 | default: |
| 10730 | return error(L: Lex.getLoc(), Msg: "expected hotness, relbf, or tail" ); |
| 10731 | } |
| 10732 | } |
| 10733 | // Keep track of the Call array index needing a forward reference. |
| 10734 | // We will save the location of the ValueInfo needing an update, but |
| 10735 | // can only do so once the std::vector is finalized. |
| 10736 | if (VI.getRef() == FwdVIRef) |
| 10737 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: Calls.size(), y&: Loc)); |
| 10738 | Calls.push_back( |
| 10739 | Elt: FunctionSummary::EdgeTy{VI, CalleeInfo(Hotness, HasTailCall)}); |
| 10740 | |
| 10741 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in call" )) |
| 10742 | return true; |
| 10743 | } while (EatIfPresent(T: lltok::comma)); |
| 10744 | |
| 10745 | // Now that the Calls vector is finalized, it is safe to save the locations |
| 10746 | // of any forward GV references that need updating later. |
| 10747 | for (auto I : IdToIndexMap) { |
| 10748 | auto &Infos = ForwardRefValueInfos[I.first]; |
| 10749 | for (auto P : I.second) { |
| 10750 | assert(Calls[P.first].first.getRef() == FwdVIRef && |
| 10751 | "Forward referenced ValueInfo expected to be empty" ); |
| 10752 | Infos.emplace_back(args: &Calls[P.first].first, args&: P.second); |
| 10753 | } |
| 10754 | } |
| 10755 | |
| 10756 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in calls" )) |
| 10757 | return true; |
| 10758 | |
| 10759 | return false; |
| 10760 | } |
| 10761 | |
| 10762 | /// Hotness |
| 10763 | /// := ('unknown'|'cold'|'none'|'hot'|'critical') |
| 10764 | bool LLParser::parseHotness(CalleeInfo::HotnessType &Hotness) { |
| 10765 | switch (Lex.getKind()) { |
| 10766 | case lltok::kw_unknown: |
| 10767 | Hotness = CalleeInfo::HotnessType::Unknown; |
| 10768 | break; |
| 10769 | case lltok::kw_cold: |
| 10770 | Hotness = CalleeInfo::HotnessType::Cold; |
| 10771 | break; |
| 10772 | case lltok::kw_none: |
| 10773 | Hotness = CalleeInfo::HotnessType::None; |
| 10774 | break; |
| 10775 | case lltok::kw_hot: |
| 10776 | Hotness = CalleeInfo::HotnessType::Hot; |
| 10777 | break; |
| 10778 | case lltok::kw_critical: |
| 10779 | Hotness = CalleeInfo::HotnessType::Critical; |
| 10780 | break; |
| 10781 | default: |
| 10782 | return error(L: Lex.getLoc(), Msg: "invalid call edge hotness" ); |
| 10783 | } |
| 10784 | Lex.Lex(); |
| 10785 | return false; |
| 10786 | } |
| 10787 | |
| 10788 | /// OptionalVTableFuncs |
| 10789 | /// := 'vTableFuncs' ':' '(' VTableFunc [',' VTableFunc]* ')' |
| 10790 | /// VTableFunc ::= '(' 'virtFunc' ':' GVReference ',' 'offset' ':' UInt64 ')' |
| 10791 | bool LLParser::parseOptionalVTableFuncs(VTableFuncList &VTableFuncs) { |
| 10792 | assert(Lex.getKind() == lltok::kw_vTableFuncs); |
| 10793 | Lex.Lex(); |
| 10794 | |
| 10795 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in vTableFuncs" ) || |
| 10796 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in vTableFuncs" )) |
| 10797 | return true; |
| 10798 | |
| 10799 | IdToIndexMapType IdToIndexMap; |
| 10800 | // parse each virtual function pair |
| 10801 | do { |
| 10802 | ValueInfo VI; |
| 10803 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in vTableFunc" ) || |
| 10804 | parseToken(T: lltok::kw_virtFunc, ErrMsg: "expected 'callee' in vTableFunc" ) || |
| 10805 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 10806 | return true; |
| 10807 | |
| 10808 | LocTy Loc = Lex.getLoc(); |
| 10809 | unsigned GVId; |
| 10810 | if (parseGVReference(VI, GVId)) |
| 10811 | return true; |
| 10812 | |
| 10813 | uint64_t Offset; |
| 10814 | if (parseToken(T: lltok::comma, ErrMsg: "expected comma" ) || |
| 10815 | parseToken(T: lltok::kw_offset, ErrMsg: "expected offset" ) || |
| 10816 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseUInt64(Val&: Offset)) |
| 10817 | return true; |
| 10818 | |
| 10819 | // Keep track of the VTableFuncs array index needing a forward reference. |
| 10820 | // We will save the location of the ValueInfo needing an update, but |
| 10821 | // can only do so once the std::vector is finalized. |
| 10822 | if (VI == EmptyVI) |
| 10823 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: VTableFuncs.size(), y&: Loc)); |
| 10824 | VTableFuncs.push_back(x: {VI, Offset}); |
| 10825 | |
| 10826 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in vTableFunc" )) |
| 10827 | return true; |
| 10828 | } while (EatIfPresent(T: lltok::comma)); |
| 10829 | |
| 10830 | // Now that the VTableFuncs vector is finalized, it is safe to save the |
| 10831 | // locations of any forward GV references that need updating later. |
| 10832 | for (auto I : IdToIndexMap) { |
| 10833 | auto &Infos = ForwardRefValueInfos[I.first]; |
| 10834 | for (auto P : I.second) { |
| 10835 | assert(VTableFuncs[P.first].FuncVI == EmptyVI && |
| 10836 | "Forward referenced ValueInfo expected to be empty" ); |
| 10837 | Infos.emplace_back(args: &VTableFuncs[P.first].FuncVI, args&: P.second); |
| 10838 | } |
| 10839 | } |
| 10840 | |
| 10841 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in vTableFuncs" )) |
| 10842 | return true; |
| 10843 | |
| 10844 | return false; |
| 10845 | } |
| 10846 | |
| 10847 | /// ParamNo := 'param' ':' UInt64 |
| 10848 | bool LLParser::parseParamNo(uint64_t &ParamNo) { |
| 10849 | if (parseToken(T: lltok::kw_param, ErrMsg: "expected 'param' here" ) || |
| 10850 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || parseUInt64(Val&: ParamNo)) |
| 10851 | return true; |
| 10852 | return false; |
| 10853 | } |
| 10854 | |
| 10855 | /// ParamAccessOffset := 'offset' ':' '[' APSINTVAL ',' APSINTVAL ']' |
| 10856 | bool LLParser::parseParamAccessOffset(ConstantRange &Range) { |
| 10857 | APSInt Lower; |
| 10858 | APSInt Upper; |
| 10859 | auto ParseAPSInt = [&](APSInt &Val) { |
| 10860 | if (Lex.getKind() != lltok::APSInt) |
| 10861 | return tokError(Msg: "expected integer" ); |
| 10862 | Val = Lex.getAPSIntVal(); |
| 10863 | Val = Val.extOrTrunc(width: FunctionSummary::ParamAccess::RangeWidth); |
| 10864 | Val.setIsSigned(true); |
| 10865 | Lex.Lex(); |
| 10866 | return false; |
| 10867 | }; |
| 10868 | if (parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
| 10869 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10870 | parseToken(T: lltok::lsquare, ErrMsg: "expected '[' here" ) || ParseAPSInt(Lower) || |
| 10871 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || ParseAPSInt(Upper) || |
| 10872 | parseToken(T: lltok::rsquare, ErrMsg: "expected ']' here" )) |
| 10873 | return true; |
| 10874 | |
| 10875 | ++Upper; |
| 10876 | Range = |
| 10877 | (Lower == Upper && !Lower.isMaxValue()) |
| 10878 | ? ConstantRange::getEmpty(BitWidth: FunctionSummary::ParamAccess::RangeWidth) |
| 10879 | : ConstantRange(Lower, Upper); |
| 10880 | |
| 10881 | return false; |
| 10882 | } |
| 10883 | |
| 10884 | /// ParamAccessCall |
| 10885 | /// := '(' 'callee' ':' GVReference ',' ParamNo ',' ParamAccessOffset ')' |
| 10886 | bool LLParser::parseParamAccessCall(FunctionSummary::ParamAccess::Call &Call, |
| 10887 | IdLocListType &IdLocList) { |
| 10888 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10889 | parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' here" ) || |
| 10890 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" )) |
| 10891 | return true; |
| 10892 | |
| 10893 | unsigned GVId; |
| 10894 | ValueInfo VI; |
| 10895 | LocTy Loc = Lex.getLoc(); |
| 10896 | if (parseGVReference(VI, GVId)) |
| 10897 | return true; |
| 10898 | |
| 10899 | Call.Callee = VI; |
| 10900 | IdLocList.emplace_back(args&: GVId, args&: Loc); |
| 10901 | |
| 10902 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10903 | parseParamNo(ParamNo&: Call.ParamNo) || |
| 10904 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10905 | parseParamAccessOffset(Range&: Call.Offsets)) |
| 10906 | return true; |
| 10907 | |
| 10908 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10909 | return true; |
| 10910 | |
| 10911 | return false; |
| 10912 | } |
| 10913 | |
| 10914 | /// ParamAccess |
| 10915 | /// := '(' ParamNo ',' ParamAccessOffset [',' OptionalParamAccessCalls]? ')' |
| 10916 | /// OptionalParamAccessCalls := '(' Call [',' Call]* ')' |
| 10917 | bool LLParser::parseParamAccess(FunctionSummary::ParamAccess &Param, |
| 10918 | IdLocListType &IdLocList) { |
| 10919 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 10920 | parseParamNo(ParamNo&: Param.ParamNo) || |
| 10921 | parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 10922 | parseParamAccessOffset(Range&: Param.Use)) |
| 10923 | return true; |
| 10924 | |
| 10925 | if (EatIfPresent(T: lltok::comma)) { |
| 10926 | if (parseToken(T: lltok::kw_calls, ErrMsg: "expected 'calls' here" ) || |
| 10927 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10928 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10929 | return true; |
| 10930 | do { |
| 10931 | FunctionSummary::ParamAccess::Call Call; |
| 10932 | if (parseParamAccessCall(Call, IdLocList)) |
| 10933 | return true; |
| 10934 | Param.Calls.push_back(x: Call); |
| 10935 | } while (EatIfPresent(T: lltok::comma)); |
| 10936 | |
| 10937 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10938 | return true; |
| 10939 | } |
| 10940 | |
| 10941 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10942 | return true; |
| 10943 | |
| 10944 | return false; |
| 10945 | } |
| 10946 | |
| 10947 | /// OptionalParamAccesses |
| 10948 | /// := 'params' ':' '(' ParamAccess [',' ParamAccess]* ')' |
| 10949 | bool LLParser::parseOptionalParamAccesses( |
| 10950 | std::vector<FunctionSummary::ParamAccess> &Params) { |
| 10951 | assert(Lex.getKind() == lltok::kw_params); |
| 10952 | Lex.Lex(); |
| 10953 | |
| 10954 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 10955 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 10956 | return true; |
| 10957 | |
| 10958 | IdLocListType VContexts; |
| 10959 | size_t CallsNum = 0; |
| 10960 | do { |
| 10961 | FunctionSummary::ParamAccess ParamAccess; |
| 10962 | if (parseParamAccess(Param&: ParamAccess, IdLocList&: VContexts)) |
| 10963 | return true; |
| 10964 | CallsNum += ParamAccess.Calls.size(); |
| 10965 | assert(VContexts.size() == CallsNum); |
| 10966 | (void)CallsNum; |
| 10967 | Params.emplace_back(args: std::move(ParamAccess)); |
| 10968 | } while (EatIfPresent(T: lltok::comma)); |
| 10969 | |
| 10970 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 10971 | return true; |
| 10972 | |
| 10973 | // Now that the Params is finalized, it is safe to save the locations |
| 10974 | // of any forward GV references that need updating later. |
| 10975 | IdLocListType::const_iterator ItContext = VContexts.begin(); |
| 10976 | for (auto &PA : Params) { |
| 10977 | for (auto &C : PA.Calls) { |
| 10978 | if (C.Callee.getRef() == FwdVIRef) |
| 10979 | ForwardRefValueInfos[ItContext->first].emplace_back(args: &C.Callee, |
| 10980 | args: ItContext->second); |
| 10981 | ++ItContext; |
| 10982 | } |
| 10983 | } |
| 10984 | assert(ItContext == VContexts.end()); |
| 10985 | |
| 10986 | return false; |
| 10987 | } |
| 10988 | |
| 10989 | /// OptionalRefs |
| 10990 | /// := 'refs' ':' '(' GVReference [',' GVReference]* ')' |
| 10991 | bool LLParser::parseOptionalRefs(SmallVectorImpl<ValueInfo> &Refs) { |
| 10992 | assert(Lex.getKind() == lltok::kw_refs); |
| 10993 | Lex.Lex(); |
| 10994 | |
| 10995 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in refs" ) || |
| 10996 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in refs" )) |
| 10997 | return true; |
| 10998 | |
| 10999 | struct ValueContext { |
| 11000 | ValueInfo VI; |
| 11001 | unsigned GVId; |
| 11002 | LocTy Loc; |
| 11003 | }; |
| 11004 | std::vector<ValueContext> VContexts; |
| 11005 | // parse each ref edge |
| 11006 | do { |
| 11007 | ValueContext VC; |
| 11008 | VC.Loc = Lex.getLoc(); |
| 11009 | if (parseGVReference(VI&: VC.VI, GVId&: VC.GVId)) |
| 11010 | return true; |
| 11011 | VContexts.push_back(x: VC); |
| 11012 | } while (EatIfPresent(T: lltok::comma)); |
| 11013 | |
| 11014 | // Sort value contexts so that ones with writeonly |
| 11015 | // and readonly ValueInfo are at the end of VContexts vector. |
| 11016 | // See FunctionSummary::specialRefCounts() |
| 11017 | llvm::sort(C&: VContexts, Comp: [](const ValueContext &VC1, const ValueContext &VC2) { |
| 11018 | return VC1.VI.getAccessSpecifier() < VC2.VI.getAccessSpecifier(); |
| 11019 | }); |
| 11020 | |
| 11021 | IdToIndexMapType IdToIndexMap; |
| 11022 | for (auto &VC : VContexts) { |
| 11023 | // Keep track of the Refs array index needing a forward reference. |
| 11024 | // We will save the location of the ValueInfo needing an update, but |
| 11025 | // can only do so once the std::vector is finalized. |
| 11026 | if (VC.VI.getRef() == FwdVIRef) |
| 11027 | IdToIndexMap[VC.GVId].push_back(x: std::make_pair(x: Refs.size(), y&: VC.Loc)); |
| 11028 | Refs.push_back(Elt: VC.VI); |
| 11029 | } |
| 11030 | |
| 11031 | // Now that the Refs vector is finalized, it is safe to save the locations |
| 11032 | // of any forward GV references that need updating later. |
| 11033 | for (auto I : IdToIndexMap) { |
| 11034 | auto &Infos = ForwardRefValueInfos[I.first]; |
| 11035 | for (auto P : I.second) { |
| 11036 | assert(Refs[P.first].getRef() == FwdVIRef && |
| 11037 | "Forward referenced ValueInfo expected to be empty" ); |
| 11038 | Infos.emplace_back(args: &Refs[P.first], args&: P.second); |
| 11039 | } |
| 11040 | } |
| 11041 | |
| 11042 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in refs" )) |
| 11043 | return true; |
| 11044 | |
| 11045 | return false; |
| 11046 | } |
| 11047 | |
| 11048 | /// OptionalTypeIdInfo |
| 11049 | /// := 'typeidinfo' ':' '(' [',' TypeTests]? [',' TypeTestAssumeVCalls]? |
| 11050 | /// [',' TypeCheckedLoadVCalls]? [',' TypeTestAssumeConstVCalls]? |
| 11051 | /// [',' TypeCheckedLoadConstVCalls]? ')' |
| 11052 | bool LLParser::parseOptionalTypeIdInfo( |
| 11053 | FunctionSummary::TypeIdInfo &TypeIdInfo) { |
| 11054 | assert(Lex.getKind() == lltok::kw_typeIdInfo); |
| 11055 | Lex.Lex(); |
| 11056 | |
| 11057 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11058 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in typeIdInfo" )) |
| 11059 | return true; |
| 11060 | |
| 11061 | do { |
| 11062 | switch (Lex.getKind()) { |
| 11063 | case lltok::kw_typeTests: |
| 11064 | if (parseTypeTests(TypeTests&: TypeIdInfo.TypeTests)) |
| 11065 | return true; |
| 11066 | break; |
| 11067 | case lltok::kw_typeTestAssumeVCalls: |
| 11068 | if (parseVFuncIdList(Kind: lltok::kw_typeTestAssumeVCalls, |
| 11069 | VFuncIdList&: TypeIdInfo.TypeTestAssumeVCalls)) |
| 11070 | return true; |
| 11071 | break; |
| 11072 | case lltok::kw_typeCheckedLoadVCalls: |
| 11073 | if (parseVFuncIdList(Kind: lltok::kw_typeCheckedLoadVCalls, |
| 11074 | VFuncIdList&: TypeIdInfo.TypeCheckedLoadVCalls)) |
| 11075 | return true; |
| 11076 | break; |
| 11077 | case lltok::kw_typeTestAssumeConstVCalls: |
| 11078 | if (parseConstVCallList(Kind: lltok::kw_typeTestAssumeConstVCalls, |
| 11079 | ConstVCallList&: TypeIdInfo.TypeTestAssumeConstVCalls)) |
| 11080 | return true; |
| 11081 | break; |
| 11082 | case lltok::kw_typeCheckedLoadConstVCalls: |
| 11083 | if (parseConstVCallList(Kind: lltok::kw_typeCheckedLoadConstVCalls, |
| 11084 | ConstVCallList&: TypeIdInfo.TypeCheckedLoadConstVCalls)) |
| 11085 | return true; |
| 11086 | break; |
| 11087 | default: |
| 11088 | return error(L: Lex.getLoc(), Msg: "invalid typeIdInfo list type" ); |
| 11089 | } |
| 11090 | } while (EatIfPresent(T: lltok::comma)); |
| 11091 | |
| 11092 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in typeIdInfo" )) |
| 11093 | return true; |
| 11094 | |
| 11095 | return false; |
| 11096 | } |
| 11097 | |
| 11098 | /// TypeTests |
| 11099 | /// ::= 'typeTests' ':' '(' (SummaryID | UInt64) |
| 11100 | /// [',' (SummaryID | UInt64)]* ')' |
| 11101 | bool LLParser::parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests) { |
| 11102 | assert(Lex.getKind() == lltok::kw_typeTests); |
| 11103 | Lex.Lex(); |
| 11104 | |
| 11105 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11106 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in typeIdInfo" )) |
| 11107 | return true; |
| 11108 | |
| 11109 | IdToIndexMapType IdToIndexMap; |
| 11110 | do { |
| 11111 | GlobalValue::GUID GUID = 0; |
| 11112 | if (Lex.getKind() == lltok::SummaryID) { |
| 11113 | unsigned ID = Lex.getUIntVal(); |
| 11114 | LocTy Loc = Lex.getLoc(); |
| 11115 | // Keep track of the TypeTests array index needing a forward reference. |
| 11116 | // We will save the location of the GUID needing an update, but |
| 11117 | // can only do so once the std::vector is finalized. |
| 11118 | IdToIndexMap[ID].push_back(x: std::make_pair(x: TypeTests.size(), y&: Loc)); |
| 11119 | Lex.Lex(); |
| 11120 | } else if (parseUInt64(Val&: GUID)) |
| 11121 | return true; |
| 11122 | TypeTests.push_back(x: GUID); |
| 11123 | } while (EatIfPresent(T: lltok::comma)); |
| 11124 | |
| 11125 | // Now that the TypeTests vector is finalized, it is safe to save the |
| 11126 | // locations of any forward GV references that need updating later. |
| 11127 | for (auto I : IdToIndexMap) { |
| 11128 | auto &Ids = ForwardRefTypeIds[I.first]; |
| 11129 | for (auto P : I.second) { |
| 11130 | assert(TypeTests[P.first] == 0 && |
| 11131 | "Forward referenced type id GUID expected to be 0" ); |
| 11132 | Ids.emplace_back(args: &TypeTests[P.first], args&: P.second); |
| 11133 | } |
| 11134 | } |
| 11135 | |
| 11136 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in typeIdInfo" )) |
| 11137 | return true; |
| 11138 | |
| 11139 | return false; |
| 11140 | } |
| 11141 | |
| 11142 | /// VFuncIdList |
| 11143 | /// ::= Kind ':' '(' VFuncId [',' VFuncId]* ')' |
| 11144 | bool LLParser::parseVFuncIdList( |
| 11145 | lltok::Kind Kind, std::vector<FunctionSummary::VFuncId> &VFuncIdList) { |
| 11146 | assert(Lex.getKind() == Kind); |
| 11147 | Lex.Lex(); |
| 11148 | |
| 11149 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11150 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 11151 | return true; |
| 11152 | |
| 11153 | IdToIndexMapType IdToIndexMap; |
| 11154 | do { |
| 11155 | FunctionSummary::VFuncId VFuncId; |
| 11156 | if (parseVFuncId(VFuncId, IdToIndexMap, Index: VFuncIdList.size())) |
| 11157 | return true; |
| 11158 | VFuncIdList.push_back(x: VFuncId); |
| 11159 | } while (EatIfPresent(T: lltok::comma)); |
| 11160 | |
| 11161 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 11162 | return true; |
| 11163 | |
| 11164 | // Now that the VFuncIdList vector is finalized, it is safe to save the |
| 11165 | // locations of any forward GV references that need updating later. |
| 11166 | for (auto I : IdToIndexMap) { |
| 11167 | auto &Ids = ForwardRefTypeIds[I.first]; |
| 11168 | for (auto P : I.second) { |
| 11169 | assert(VFuncIdList[P.first].GUID == 0 && |
| 11170 | "Forward referenced type id GUID expected to be 0" ); |
| 11171 | Ids.emplace_back(args: &VFuncIdList[P.first].GUID, args&: P.second); |
| 11172 | } |
| 11173 | } |
| 11174 | |
| 11175 | return false; |
| 11176 | } |
| 11177 | |
| 11178 | /// ConstVCallList |
| 11179 | /// ::= Kind ':' '(' ConstVCall [',' ConstVCall]* ')' |
| 11180 | bool LLParser::parseConstVCallList( |
| 11181 | lltok::Kind Kind, |
| 11182 | std::vector<FunctionSummary::ConstVCall> &ConstVCallList) { |
| 11183 | assert(Lex.getKind() == Kind); |
| 11184 | Lex.Lex(); |
| 11185 | |
| 11186 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11187 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 11188 | return true; |
| 11189 | |
| 11190 | IdToIndexMapType IdToIndexMap; |
| 11191 | do { |
| 11192 | FunctionSummary::ConstVCall ConstVCall; |
| 11193 | if (parseConstVCall(ConstVCall, IdToIndexMap, Index: ConstVCallList.size())) |
| 11194 | return true; |
| 11195 | ConstVCallList.push_back(x: ConstVCall); |
| 11196 | } while (EatIfPresent(T: lltok::comma)); |
| 11197 | |
| 11198 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 11199 | return true; |
| 11200 | |
| 11201 | // Now that the ConstVCallList vector is finalized, it is safe to save the |
| 11202 | // locations of any forward GV references that need updating later. |
| 11203 | for (auto I : IdToIndexMap) { |
| 11204 | auto &Ids = ForwardRefTypeIds[I.first]; |
| 11205 | for (auto P : I.second) { |
| 11206 | assert(ConstVCallList[P.first].VFunc.GUID == 0 && |
| 11207 | "Forward referenced type id GUID expected to be 0" ); |
| 11208 | Ids.emplace_back(args: &ConstVCallList[P.first].VFunc.GUID, args&: P.second); |
| 11209 | } |
| 11210 | } |
| 11211 | |
| 11212 | return false; |
| 11213 | } |
| 11214 | |
| 11215 | /// ConstVCall |
| 11216 | /// ::= '(' VFuncId ',' Args ')' |
| 11217 | bool LLParser::parseConstVCall(FunctionSummary::ConstVCall &ConstVCall, |
| 11218 | IdToIndexMapType &IdToIndexMap, unsigned Index) { |
| 11219 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" ) || |
| 11220 | parseVFuncId(VFuncId&: ConstVCall.VFunc, IdToIndexMap, Index)) |
| 11221 | return true; |
| 11222 | |
| 11223 | if (EatIfPresent(T: lltok::comma)) |
| 11224 | if (parseArgs(Args&: ConstVCall.Args)) |
| 11225 | return true; |
| 11226 | |
| 11227 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 11228 | return true; |
| 11229 | |
| 11230 | return false; |
| 11231 | } |
| 11232 | |
| 11233 | /// VFuncId |
| 11234 | /// ::= 'vFuncId' ':' '(' (SummaryID | 'guid' ':' UInt64) ',' |
| 11235 | /// 'offset' ':' UInt64 ')' |
| 11236 | bool LLParser::parseVFuncId(FunctionSummary::VFuncId &VFuncId, |
| 11237 | IdToIndexMapType &IdToIndexMap, unsigned Index) { |
| 11238 | assert(Lex.getKind() == lltok::kw_vFuncId); |
| 11239 | Lex.Lex(); |
| 11240 | |
| 11241 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11242 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 11243 | return true; |
| 11244 | |
| 11245 | if (Lex.getKind() == lltok::SummaryID) { |
| 11246 | VFuncId.GUID = 0; |
| 11247 | unsigned ID = Lex.getUIntVal(); |
| 11248 | LocTy Loc = Lex.getLoc(); |
| 11249 | // Keep track of the array index needing a forward reference. |
| 11250 | // We will save the location of the GUID needing an update, but |
| 11251 | // can only do so once the caller's std::vector is finalized. |
| 11252 | IdToIndexMap[ID].push_back(x: std::make_pair(x&: Index, y&: Loc)); |
| 11253 | Lex.Lex(); |
| 11254 | } else if (parseToken(T: lltok::kw_guid, ErrMsg: "expected 'guid' here" ) || |
| 11255 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11256 | parseUInt64(Val&: VFuncId.GUID)) |
| 11257 | return true; |
| 11258 | |
| 11259 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' here" ) || |
| 11260 | parseToken(T: lltok::kw_offset, ErrMsg: "expected 'offset' here" ) || |
| 11261 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11262 | parseUInt64(Val&: VFuncId.Offset) || |
| 11263 | parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 11264 | return true; |
| 11265 | |
| 11266 | return false; |
| 11267 | } |
| 11268 | |
| 11269 | /// GVFlags |
| 11270 | /// ::= 'flags' ':' '(' 'linkage' ':' OptionalLinkageAux ',' |
| 11271 | /// 'visibility' ':' Flag 'notEligibleToImport' ':' Flag ',' |
| 11272 | /// 'live' ':' Flag ',' 'dsoLocal' ':' Flag ',' |
| 11273 | /// 'canAutoHide' ':' Flag ',' ')' |
| 11274 | bool LLParser::parseGVFlags(GlobalValueSummary::GVFlags &GVFlags) { |
| 11275 | assert(Lex.getKind() == lltok::kw_flags); |
| 11276 | Lex.Lex(); |
| 11277 | |
| 11278 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11279 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 11280 | return true; |
| 11281 | |
| 11282 | do { |
| 11283 | unsigned Flag = 0; |
| 11284 | switch (Lex.getKind()) { |
| 11285 | case lltok::kw_linkage: |
| 11286 | Lex.Lex(); |
| 11287 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 11288 | return true; |
| 11289 | bool HasLinkage; |
| 11290 | GVFlags.Linkage = parseOptionalLinkageAux(Kind: Lex.getKind(), HasLinkage); |
| 11291 | assert(HasLinkage && "Linkage not optional in summary entry" ); |
| 11292 | Lex.Lex(); |
| 11293 | break; |
| 11294 | case lltok::kw_visibility: |
| 11295 | Lex.Lex(); |
| 11296 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 11297 | return true; |
| 11298 | parseOptionalVisibility(Res&: Flag); |
| 11299 | GVFlags.Visibility = Flag; |
| 11300 | break; |
| 11301 | case lltok::kw_notEligibleToImport: |
| 11302 | Lex.Lex(); |
| 11303 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
| 11304 | return true; |
| 11305 | GVFlags.NotEligibleToImport = Flag; |
| 11306 | break; |
| 11307 | case lltok::kw_live: |
| 11308 | Lex.Lex(); |
| 11309 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
| 11310 | return true; |
| 11311 | GVFlags.Live = Flag; |
| 11312 | break; |
| 11313 | case lltok::kw_dsoLocal: |
| 11314 | Lex.Lex(); |
| 11315 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
| 11316 | return true; |
| 11317 | GVFlags.DSOLocal = Flag; |
| 11318 | break; |
| 11319 | case lltok::kw_canAutoHide: |
| 11320 | Lex.Lex(); |
| 11321 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
| 11322 | return true; |
| 11323 | GVFlags.CanAutoHide = Flag; |
| 11324 | break; |
| 11325 | case lltok::kw_importType: |
| 11326 | Lex.Lex(); |
| 11327 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 11328 | return true; |
| 11329 | GlobalValueSummary::ImportKind IK; |
| 11330 | if (parseOptionalImportType(Kind: Lex.getKind(), Res&: IK)) |
| 11331 | return true; |
| 11332 | GVFlags.ImportType = static_cast<unsigned>(IK); |
| 11333 | Lex.Lex(); |
| 11334 | break; |
| 11335 | case lltok::kw_noRenameOnPromotion: |
| 11336 | Lex.Lex(); |
| 11337 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || parseFlag(Val&: Flag)) |
| 11338 | return true; |
| 11339 | GVFlags.NoRenameOnPromotion = Flag; |
| 11340 | break; |
| 11341 | default: |
| 11342 | return error(L: Lex.getLoc(), Msg: "expected gv flag type" ); |
| 11343 | } |
| 11344 | } while (EatIfPresent(T: lltok::comma)); |
| 11345 | |
| 11346 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" )) |
| 11347 | return true; |
| 11348 | |
| 11349 | return false; |
| 11350 | } |
| 11351 | |
| 11352 | /// GVarFlags |
| 11353 | /// ::= 'varFlags' ':' '(' 'readonly' ':' Flag |
| 11354 | /// ',' 'writeonly' ':' Flag |
| 11355 | /// ',' 'constant' ':' Flag ')' |
| 11356 | bool LLParser::parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags) { |
| 11357 | assert(Lex.getKind() == lltok::kw_varFlags); |
| 11358 | Lex.Lex(); |
| 11359 | |
| 11360 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11361 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' here" )) |
| 11362 | return true; |
| 11363 | |
| 11364 | auto ParseRest = [this](unsigned int &Val) { |
| 11365 | Lex.Lex(); |
| 11366 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 11367 | return true; |
| 11368 | return parseFlag(Val); |
| 11369 | }; |
| 11370 | |
| 11371 | do { |
| 11372 | unsigned Flag = 0; |
| 11373 | switch (Lex.getKind()) { |
| 11374 | case lltok::kw_readonly: |
| 11375 | if (ParseRest(Flag)) |
| 11376 | return true; |
| 11377 | GVarFlags.MaybeReadOnly = Flag; |
| 11378 | break; |
| 11379 | case lltok::kw_writeonly: |
| 11380 | if (ParseRest(Flag)) |
| 11381 | return true; |
| 11382 | GVarFlags.MaybeWriteOnly = Flag; |
| 11383 | break; |
| 11384 | case lltok::kw_constant: |
| 11385 | if (ParseRest(Flag)) |
| 11386 | return true; |
| 11387 | GVarFlags.Constant = Flag; |
| 11388 | break; |
| 11389 | case lltok::kw_vcall_visibility: |
| 11390 | if (ParseRest(Flag)) |
| 11391 | return true; |
| 11392 | GVarFlags.VCallVisibility = Flag; |
| 11393 | break; |
| 11394 | default: |
| 11395 | return error(L: Lex.getLoc(), Msg: "expected gvar flag type" ); |
| 11396 | } |
| 11397 | } while (EatIfPresent(T: lltok::comma)); |
| 11398 | return parseToken(T: lltok::rparen, ErrMsg: "expected ')' here" ); |
| 11399 | } |
| 11400 | |
| 11401 | /// ModuleReference |
| 11402 | /// ::= 'module' ':' UInt |
| 11403 | bool LLParser::parseModuleReference(StringRef &ModulePath) { |
| 11404 | // parse module id. |
| 11405 | if (parseToken(T: lltok::kw_module, ErrMsg: "expected 'module' here" ) || |
| 11406 | parseToken(T: lltok::colon, ErrMsg: "expected ':' here" ) || |
| 11407 | parseToken(T: lltok::SummaryID, ErrMsg: "expected module ID" )) |
| 11408 | return true; |
| 11409 | |
| 11410 | unsigned ModuleID = Lex.getUIntVal(); |
| 11411 | auto I = ModuleIdMap.find(x: ModuleID); |
| 11412 | // We should have already parsed all module IDs |
| 11413 | assert(I != ModuleIdMap.end()); |
| 11414 | ModulePath = I->second; |
| 11415 | return false; |
| 11416 | } |
| 11417 | |
| 11418 | /// GVReference |
| 11419 | /// ::= SummaryID |
| 11420 | bool LLParser::parseGVReference(ValueInfo &VI, unsigned &GVId) { |
| 11421 | bool WriteOnly = false, ReadOnly = EatIfPresent(T: lltok::kw_readonly); |
| 11422 | if (!ReadOnly) |
| 11423 | WriteOnly = EatIfPresent(T: lltok::kw_writeonly); |
| 11424 | if (parseToken(T: lltok::SummaryID, ErrMsg: "expected GV ID" )) |
| 11425 | return true; |
| 11426 | |
| 11427 | GVId = Lex.getUIntVal(); |
| 11428 | // Check if we already have a VI for this GV |
| 11429 | if (GVId < NumberedValueInfos.size() && NumberedValueInfos[GVId]) { |
| 11430 | assert(NumberedValueInfos[GVId].getRef() != FwdVIRef); |
| 11431 | VI = NumberedValueInfos[GVId]; |
| 11432 | } else |
| 11433 | // We will create a forward reference to the stored location. |
| 11434 | VI = ValueInfo(false, FwdVIRef); |
| 11435 | |
| 11436 | if (ReadOnly) |
| 11437 | VI.setReadOnly(); |
| 11438 | if (WriteOnly) |
| 11439 | VI.setWriteOnly(); |
| 11440 | return false; |
| 11441 | } |
| 11442 | |
| 11443 | /// OptionalAllocs |
| 11444 | /// := 'allocs' ':' '(' Alloc [',' Alloc]* ')' |
| 11445 | /// Alloc ::= '(' 'versions' ':' '(' Version [',' Version]* ')' |
| 11446 | /// ',' MemProfs ')' |
| 11447 | /// Version ::= UInt32 |
| 11448 | bool LLParser::parseOptionalAllocs(std::vector<AllocInfo> &Allocs) { |
| 11449 | assert(Lex.getKind() == lltok::kw_allocs); |
| 11450 | Lex.Lex(); |
| 11451 | |
| 11452 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in allocs" ) || |
| 11453 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in allocs" )) |
| 11454 | return true; |
| 11455 | |
| 11456 | // parse each alloc |
| 11457 | do { |
| 11458 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in alloc" ) || |
| 11459 | parseToken(T: lltok::kw_versions, ErrMsg: "expected 'versions' in alloc" ) || |
| 11460 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
| 11461 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in versions" )) |
| 11462 | return true; |
| 11463 | |
| 11464 | SmallVector<uint8_t> Versions; |
| 11465 | do { |
| 11466 | uint8_t V = 0; |
| 11467 | if (parseAllocType(AllocType&: V)) |
| 11468 | return true; |
| 11469 | Versions.push_back(Elt: V); |
| 11470 | } while (EatIfPresent(T: lltok::comma)); |
| 11471 | |
| 11472 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in versions" ) || |
| 11473 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in alloc" )) |
| 11474 | return true; |
| 11475 | |
| 11476 | std::vector<MIBInfo> MIBs; |
| 11477 | if (parseMemProfs(MIBs)) |
| 11478 | return true; |
| 11479 | |
| 11480 | Allocs.push_back(x: {Versions, MIBs}); |
| 11481 | |
| 11482 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in alloc" )) |
| 11483 | return true; |
| 11484 | } while (EatIfPresent(T: lltok::comma)); |
| 11485 | |
| 11486 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in allocs" )) |
| 11487 | return true; |
| 11488 | |
| 11489 | return false; |
| 11490 | } |
| 11491 | |
| 11492 | /// MemProfs |
| 11493 | /// := 'memProf' ':' '(' MemProf [',' MemProf]* ')' |
| 11494 | /// MemProf ::= '(' 'type' ':' AllocType |
| 11495 | /// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')' |
| 11496 | /// StackId ::= UInt64 |
| 11497 | bool LLParser::parseMemProfs(std::vector<MIBInfo> &MIBs) { |
| 11498 | assert(Lex.getKind() == lltok::kw_memProf); |
| 11499 | Lex.Lex(); |
| 11500 | |
| 11501 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in memprof" ) || |
| 11502 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in memprof" )) |
| 11503 | return true; |
| 11504 | |
| 11505 | // parse each MIB |
| 11506 | do { |
| 11507 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in memprof" ) || |
| 11508 | parseToken(T: lltok::kw_type, ErrMsg: "expected 'type' in memprof" ) || |
| 11509 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 11510 | return true; |
| 11511 | |
| 11512 | uint8_t AllocType; |
| 11513 | if (parseAllocType(AllocType)) |
| 11514 | return true; |
| 11515 | |
| 11516 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' in memprof" ) || |
| 11517 | parseToken(T: lltok::kw_stackIds, ErrMsg: "expected 'stackIds' in memprof" ) || |
| 11518 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
| 11519 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in stackIds" )) |
| 11520 | return true; |
| 11521 | |
| 11522 | SmallVector<unsigned> StackIdIndices; |
| 11523 | // Combined index alloc records may not have a stack id list. |
| 11524 | if (Lex.getKind() != lltok::rparen) { |
| 11525 | do { |
| 11526 | uint64_t StackId = 0; |
| 11527 | if (parseUInt64(Val&: StackId)) |
| 11528 | return true; |
| 11529 | StackIdIndices.push_back(Elt: Index->addOrGetStackIdIndex(StackId)); |
| 11530 | } while (EatIfPresent(T: lltok::comma)); |
| 11531 | } |
| 11532 | |
| 11533 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in stackIds" )) |
| 11534 | return true; |
| 11535 | |
| 11536 | MIBs.push_back(x: {(AllocationType)AllocType, StackIdIndices}); |
| 11537 | |
| 11538 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in memprof" )) |
| 11539 | return true; |
| 11540 | } while (EatIfPresent(T: lltok::comma)); |
| 11541 | |
| 11542 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in memprof" )) |
| 11543 | return true; |
| 11544 | |
| 11545 | return false; |
| 11546 | } |
| 11547 | |
| 11548 | /// AllocType |
| 11549 | /// := ('none'|'notcold'|'cold'|'hot') |
| 11550 | bool LLParser::parseAllocType(uint8_t &AllocType) { |
| 11551 | switch (Lex.getKind()) { |
| 11552 | case lltok::kw_none: |
| 11553 | AllocType = (uint8_t)AllocationType::None; |
| 11554 | break; |
| 11555 | case lltok::kw_notcold: |
| 11556 | AllocType = (uint8_t)AllocationType::NotCold; |
| 11557 | break; |
| 11558 | case lltok::kw_cold: |
| 11559 | AllocType = (uint8_t)AllocationType::Cold; |
| 11560 | break; |
| 11561 | case lltok::kw_hot: |
| 11562 | AllocType = (uint8_t)AllocationType::Hot; |
| 11563 | break; |
| 11564 | default: |
| 11565 | return error(L: Lex.getLoc(), Msg: "invalid alloc type" ); |
| 11566 | } |
| 11567 | Lex.Lex(); |
| 11568 | return false; |
| 11569 | } |
| 11570 | |
| 11571 | /// OptionalCallsites |
| 11572 | /// := 'callsites' ':' '(' Callsite [',' Callsite]* ')' |
| 11573 | /// Callsite ::= '(' 'callee' ':' GVReference |
| 11574 | /// ',' 'clones' ':' '(' Version [',' Version]* ')' |
| 11575 | /// ',' 'stackIds' ':' '(' StackId [',' StackId]* ')' ')' |
| 11576 | /// Version ::= UInt32 |
| 11577 | /// StackId ::= UInt64 |
| 11578 | bool LLParser::parseOptionalCallsites(std::vector<CallsiteInfo> &Callsites) { |
| 11579 | assert(Lex.getKind() == lltok::kw_callsites); |
| 11580 | Lex.Lex(); |
| 11581 | |
| 11582 | if (parseToken(T: lltok::colon, ErrMsg: "expected ':' in callsites" ) || |
| 11583 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in callsites" )) |
| 11584 | return true; |
| 11585 | |
| 11586 | IdToIndexMapType IdToIndexMap; |
| 11587 | // parse each callsite |
| 11588 | do { |
| 11589 | if (parseToken(T: lltok::lparen, ErrMsg: "expected '(' in callsite" ) || |
| 11590 | parseToken(T: lltok::kw_callee, ErrMsg: "expected 'callee' in callsite" ) || |
| 11591 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" )) |
| 11592 | return true; |
| 11593 | |
| 11594 | ValueInfo VI; |
| 11595 | unsigned GVId = 0; |
| 11596 | LocTy Loc = Lex.getLoc(); |
| 11597 | if (!EatIfPresent(T: lltok::kw_null)) { |
| 11598 | if (parseGVReference(VI, GVId)) |
| 11599 | return true; |
| 11600 | } |
| 11601 | |
| 11602 | if (parseToken(T: lltok::comma, ErrMsg: "expected ',' in callsite" ) || |
| 11603 | parseToken(T: lltok::kw_clones, ErrMsg: "expected 'clones' in callsite" ) || |
| 11604 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
| 11605 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in clones" )) |
| 11606 | return true; |
| 11607 | |
| 11608 | SmallVector<unsigned> Clones; |
| 11609 | do { |
| 11610 | unsigned V = 0; |
| 11611 | if (parseUInt32(Val&: V)) |
| 11612 | return true; |
| 11613 | Clones.push_back(Elt: V); |
| 11614 | } while (EatIfPresent(T: lltok::comma)); |
| 11615 | |
| 11616 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in clones" ) || |
| 11617 | parseToken(T: lltok::comma, ErrMsg: "expected ',' in callsite" ) || |
| 11618 | parseToken(T: lltok::kw_stackIds, ErrMsg: "expected 'stackIds' in callsite" ) || |
| 11619 | parseToken(T: lltok::colon, ErrMsg: "expected ':'" ) || |
| 11620 | parseToken(T: lltok::lparen, ErrMsg: "expected '(' in stackIds" )) |
| 11621 | return true; |
| 11622 | |
| 11623 | SmallVector<unsigned> StackIdIndices; |
| 11624 | // Synthesized callsite records will not have a stack id list. |
| 11625 | if (Lex.getKind() != lltok::rparen) { |
| 11626 | do { |
| 11627 | uint64_t StackId = 0; |
| 11628 | if (parseUInt64(Val&: StackId)) |
| 11629 | return true; |
| 11630 | StackIdIndices.push_back(Elt: Index->addOrGetStackIdIndex(StackId)); |
| 11631 | } while (EatIfPresent(T: lltok::comma)); |
| 11632 | } |
| 11633 | |
| 11634 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in stackIds" )) |
| 11635 | return true; |
| 11636 | |
| 11637 | // Keep track of the Callsites array index needing a forward reference. |
| 11638 | // We will save the location of the ValueInfo needing an update, but |
| 11639 | // can only do so once the SmallVector is finalized. |
| 11640 | if (VI.getRef() == FwdVIRef) |
| 11641 | IdToIndexMap[GVId].push_back(x: std::make_pair(x: Callsites.size(), y&: Loc)); |
| 11642 | Callsites.push_back(x: {VI, Clones, StackIdIndices}); |
| 11643 | |
| 11644 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in callsite" )) |
| 11645 | return true; |
| 11646 | } while (EatIfPresent(T: lltok::comma)); |
| 11647 | |
| 11648 | // Now that the Callsites vector is finalized, it is safe to save the |
| 11649 | // locations of any forward GV references that need updating later. |
| 11650 | for (auto I : IdToIndexMap) { |
| 11651 | auto &Infos = ForwardRefValueInfos[I.first]; |
| 11652 | for (auto P : I.second) { |
| 11653 | assert(Callsites[P.first].Callee.getRef() == FwdVIRef && |
| 11654 | "Forward referenced ValueInfo expected to be empty" ); |
| 11655 | Infos.emplace_back(args: &Callsites[P.first].Callee, args&: P.second); |
| 11656 | } |
| 11657 | } |
| 11658 | |
| 11659 | if (parseToken(T: lltok::rparen, ErrMsg: "expected ')' in callsites" )) |
| 11660 | return true; |
| 11661 | |
| 11662 | return false; |
| 11663 | } |
| 11664 | |