1193323Sed//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// 2193323Sed// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6193323Sed// 7193323Sed//===----------------------------------------------------------------------===// 8193323Sed// 9193323Sed// This file defines the parser class for .ll files. 10193323Sed// 11193323Sed//===----------------------------------------------------------------------===// 12193323Sed 13280031Sdim#ifndef LLVM_LIB_ASMPARSER_LLPARSER_H 14280031Sdim#define LLVM_LIB_ASMPARSER_LLPARSER_H 15193323Sed 16193323Sed#include "LLLexer.h" 17309124Sdim#include "llvm/ADT/Optional.h" 18224145Sdim#include "llvm/ADT/StringMap.h" 19249423Sdim#include "llvm/IR/Attributes.h" 20249423Sdim#include "llvm/IR/Instructions.h" 21249423Sdim#include "llvm/IR/Module.h" 22341825Sdim#include "llvm/IR/ModuleSummaryIndex.h" 23249423Sdim#include "llvm/IR/Operator.h" 24249423Sdim#include "llvm/IR/Type.h" 25276479Sdim#include "llvm/IR/ValueHandle.h" 26193323Sed#include <map> 27193323Sed 28193323Sednamespace llvm { 29193323Sed class Module; 30193323Sed class OpaqueType; 31193323Sed class Function; 32193323Sed class Value; 33193323Sed class BasicBlock; 34193323Sed class Instruction; 35193323Sed class Constant; 36193323Sed class GlobalValue; 37276479Sdim class Comdat; 38193323Sed class MDString; 39193323Sed class MDNode; 40288943Sdim struct SlotMapping; 41224145Sdim class StructType; 42193323Sed 43198892Srdivacky /// ValID - Represents a reference of a definition of some sort with no type. 44198892Srdivacky /// There are several cases where we have to parse the value but where the 45198892Srdivacky /// type can depend on later context. This may either be a numeric reference 46198892Srdivacky /// or a symbolic (%var) reference. This is just a discriminated union. 47198892Srdivacky struct ValID { 48198892Srdivacky enum { 49296417Sdim t_LocalID, t_GlobalID, // ID in UIntVal. 50296417Sdim t_LocalName, t_GlobalName, // Name in StrVal. 51296417Sdim t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 52296417Sdim t_Null, t_Undef, t_Zero, t_None, // No value. 53296417Sdim t_EmptyArray, // No value: [] 54296417Sdim t_Constant, // Value in ConstantVal. 55296417Sdim t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal. 56296417Sdim t_ConstantStruct, // Value in ConstantStructElts. 57296417Sdim t_PackedConstantStruct // Value in ConstantStructElts. 58296417Sdim } Kind = t_LocalID; 59249423Sdim 60198892Srdivacky LLLexer::LocTy Loc; 61198892Srdivacky unsigned UIntVal; 62296417Sdim FunctionType *FTy = nullptr; 63198892Srdivacky std::string StrVal, StrVal2; 64198892Srdivacky APSInt APSIntVal; 65296417Sdim APFloat APFloatVal{0.0}; 66198892Srdivacky Constant *ConstantVal; 67296417Sdim std::unique_ptr<Constant *[]> ConstantStructElts; 68249423Sdim 69296417Sdim ValID() = default; 70296417Sdim ValID(const ValID &RHS) 71296417Sdim : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy), 72296417Sdim StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal), 73296417Sdim APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal) { 74296417Sdim assert(!RHS.ConstantStructElts); 75224145Sdim } 76249423Sdim 77198892Srdivacky bool operator<(const ValID &RHS) const { 78198892Srdivacky if (Kind == t_LocalID || Kind == t_GlobalID) 79198892Srdivacky return UIntVal < RHS.UIntVal; 80224145Sdim assert((Kind == t_LocalName || Kind == t_GlobalName || 81249423Sdim Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && 82198892Srdivacky "Ordering not defined for this ValID kind yet"); 83198892Srdivacky return StrVal < RHS.StrVal; 84198892Srdivacky } 85198892Srdivacky }; 86249423Sdim 87193323Sed class LLParser { 88193323Sed public: 89193323Sed typedef LLLexer::LocTy LocTy; 90193323Sed private: 91207618Srdivacky LLVMContext &Context; 92193323Sed LLLexer Lex; 93341825Sdim // Module being parsed, null if we are only parsing summary index. 94193323Sed Module *M; 95341825Sdim // Summary index being parsed, null if we are only parsing Module. 96341825Sdim ModuleSummaryIndex *Index; 97288943Sdim SlotMapping *Slots; 98249423Sdim 99206083Srdivacky // Instruction metadata resolution. Each instruction can have a list of 100206083Srdivacky // MDRef info associated with them. 101212904Sdim // 102212904Sdim // The simpler approach of just creating temporary MDNodes and then calling 103212904Sdim // RAUW on them when the definition is processed doesn't work because some 104212904Sdim // instruction metadata kinds, such as dbg, get stored in the IR in an 105212904Sdim // "optimized" format which doesn't participate in the normal value use 106212904Sdim // lists. This means that RAUW doesn't work, even on temporary MDNodes 107212904Sdim // which otherwise support RAUW. Instead, we defer resolving MDNode 108212904Sdim // references until the definitions have been processed. 109206083Srdivacky struct MDRef { 110206083Srdivacky SMLoc Loc; 111206083Srdivacky unsigned MDKind, MDSlot; 112206083Srdivacky }; 113193323Sed 114261991Sdim SmallVector<Instruction*, 64> InstsWithTBAATag; 115261991Sdim 116224145Sdim // Type resolution handling data structures. The location is set when we 117224145Sdim // have processed a use of the type but not a definition yet. 118224145Sdim StringMap<std::pair<Type*, LocTy> > NamedTypes; 119288943Sdim std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes; 120249423Sdim 121288943Sdim std::map<unsigned, TrackingMDNodeRef> NumberedMetadata; 122288943Sdim std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes; 123193323Sed 124193323Sed // Global Value reference information. 125193323Sed std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 126193323Sed std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 127193323Sed std::vector<GlobalValue*> NumberedVals; 128249423Sdim 129276479Sdim // Comdat forward reference information. 130276479Sdim std::map<std::string, LocTy> ForwardRefComdats; 131276479Sdim 132198892Srdivacky // References to blockaddress. The key is the function ValID, the value is 133198892Srdivacky // a list of references to blocks in that function. 134280031Sdim std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses; 135280031Sdim class PerFunctionState; 136280031Sdim /// Reference to per-function state to allow basic blocks to be 137280031Sdim /// forward-referenced by blockaddress instructions within the same 138280031Sdim /// function. 139280031Sdim PerFunctionState *BlockAddressPFS; 140249423Sdim 141249423Sdim // Attribute builder reference information. 142249423Sdim std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups; 143249423Sdim std::map<unsigned, AttrBuilder> NumberedAttrBuilders; 144249423Sdim 145341825Sdim // Summary global value reference information. 146341825Sdim std::map<unsigned, std::vector<std::pair<ValueInfo *, LocTy>>> 147341825Sdim ForwardRefValueInfos; 148341825Sdim std::map<unsigned, std::vector<std::pair<AliasSummary *, LocTy>>> 149341825Sdim ForwardRefAliasees; 150341825Sdim std::vector<ValueInfo> NumberedValueInfos; 151341825Sdim 152341825Sdim // Summary type id reference information. 153341825Sdim std::map<unsigned, std::vector<std::pair<GlobalValue::GUID *, LocTy>>> 154341825Sdim ForwardRefTypeIds; 155341825Sdim 156341825Sdim // Map of module ID to path. 157341825Sdim std::map<unsigned, StringRef> ModuleIdMap; 158341825Sdim 159327952Sdim /// Only the llvm-as tool may set this to false to bypass 160327952Sdim /// UpgradeDebuginfo so it can generate broken bitcode. 161327952Sdim bool UpgradeDebugInfo; 162327952Sdim 163341825Sdim /// DataLayout string to override that in LLVM assembly. 164341825Sdim StringRef DataLayoutStr; 165341825Sdim 166341825Sdim std::string SourceFileName; 167341825Sdim 168193323Sed public: 169288943Sdim LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M, 170341825Sdim ModuleSummaryIndex *Index, LLVMContext &Context, 171341825Sdim SlotMapping *Slots = nullptr, bool UpgradeDebugInfo = true, 172341825Sdim StringRef DataLayoutString = "") 173341825Sdim : Context(Context), Lex(F, SM, Err, Context), M(M), Index(Index), 174327952Sdim Slots(Slots), BlockAddressPFS(nullptr), 175341825Sdim UpgradeDebugInfo(UpgradeDebugInfo), DataLayoutStr(DataLayoutString) { 176341825Sdim if (!DataLayoutStr.empty()) 177341825Sdim M->setDataLayout(DataLayoutStr); 178341825Sdim } 179193323Sed bool Run(); 180193323Sed 181296417Sdim bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots); 182296417Sdim 183309124Sdim bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, 184309124Sdim const SlotMapping *Slots); 185309124Sdim 186224145Sdim LLVMContext &getContext() { return Context; } 187195340Sed 188193323Sed private: 189193323Sed 190218893Sdim bool Error(LocTy L, const Twine &Msg) const { 191193323Sed return Lex.Error(L, Msg); 192193323Sed } 193218893Sdim bool TokError(const Twine &Msg) const { 194193323Sed return Error(Lex.getLoc(), Msg); 195193323Sed } 196193323Sed 197296417Sdim /// Restore the internal name and slot mappings using the mappings that 198296417Sdim /// were created at an earlier parsing stage. 199296417Sdim void restoreParsingState(const SlotMapping *Slots); 200296417Sdim 201193323Sed /// GetGlobalVal - Get a value with the specified name or ID, creating a 202193323Sed /// forward reference record if needed. This can return null if the value 203193323Sed /// exists but does not have the right type. 204344779Sdim GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc, 205344779Sdim bool IsCall); 206344779Sdim GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc, bool IsCall); 207193323Sed 208276479Sdim /// Get a Comdat with the specified name, creating a forward reference 209276479Sdim /// record if needed. 210341825Sdim Comdat *getComdat(const std::string &Name, LocTy Loc); 211276479Sdim 212193323Sed // Helper Routines. 213193323Sed bool ParseToken(lltok::Kind T, const char *ErrMsg); 214193323Sed bool EatIfPresent(lltok::Kind T) { 215193323Sed if (Lex.getKind() != T) return false; 216193323Sed Lex.Lex(); 217193323Sed return true; 218193323Sed } 219249423Sdim 220249423Sdim FastMathFlags EatFastMathFlagsIfPresent() { 221249423Sdim FastMathFlags FMF; 222249423Sdim while (true) 223249423Sdim switch (Lex.getKind()) { 224327952Sdim case lltok::kw_fast: FMF.setFast(); Lex.Lex(); continue; 225249423Sdim case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue; 226249423Sdim case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue; 227249423Sdim case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue; 228249423Sdim case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue; 229321369Sdim case lltok::kw_contract: 230321369Sdim FMF.setAllowContract(true); 231321369Sdim Lex.Lex(); 232321369Sdim continue; 233327952Sdim case lltok::kw_reassoc: FMF.setAllowReassoc(); Lex.Lex(); continue; 234327952Sdim case lltok::kw_afn: FMF.setApproxFunc(); Lex.Lex(); continue; 235249423Sdim default: return FMF; 236249423Sdim } 237249423Sdim return FMF; 238249423Sdim } 239249423Sdim 240276479Sdim bool ParseOptionalToken(lltok::Kind T, bool &Present, 241276479Sdim LocTy *Loc = nullptr) { 242193323Sed if (Lex.getKind() != T) { 243193323Sed Present = false; 244193323Sed } else { 245218893Sdim if (Loc) 246218893Sdim *Loc = Lex.getLoc(); 247193323Sed Lex.Lex(); 248193323Sed Present = true; 249193323Sed } 250193323Sed return false; 251193323Sed } 252193323Sed bool ParseStringConstant(std::string &Result); 253193323Sed bool ParseUInt32(unsigned &Val); 254193323Sed bool ParseUInt32(unsigned &Val, LocTy &Loc) { 255193323Sed Loc = Lex.getLoc(); 256193323Sed return ParseUInt32(Val); 257193323Sed } 258276479Sdim bool ParseUInt64(uint64_t &Val); 259276479Sdim bool ParseUInt64(uint64_t &Val, LocTy &Loc) { 260276479Sdim Loc = Lex.getLoc(); 261276479Sdim return ParseUInt64(Val); 262276479Sdim } 263341825Sdim bool ParseFlag(unsigned &Val); 264239462Sdim 265296417Sdim bool ParseStringAttribute(AttrBuilder &B); 266296417Sdim 267239462Sdim bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM); 268239462Sdim bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); 269309124Sdim bool ParseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr); 270344779Sdim bool ParseOptionalAddrSpace(unsigned &AddrSpace, unsigned DefaultAS = 0); 271344779Sdim bool ParseOptionalProgramAddrSpace(unsigned &AddrSpace) { 272344779Sdim return ParseOptionalAddrSpace( 273344779Sdim AddrSpace, M->getDataLayout().getProgramAddressSpace()); 274344779Sdim }; 275249423Sdim bool ParseOptionalParamAttrs(AttrBuilder &B); 276249423Sdim bool ParseOptionalReturnAttrs(AttrBuilder &B); 277341825Sdim bool ParseOptionalLinkage(unsigned &Res, bool &HasLinkage, 278327952Sdim unsigned &Visibility, unsigned &DLLStorageClass, 279327952Sdim bool &DSOLocal); 280327952Sdim void ParseOptionalDSOLocal(bool &DSOLocal); 281341825Sdim void ParseOptionalVisibility(unsigned &Res); 282341825Sdim void ParseOptionalDLLStorageClass(unsigned &Res); 283280031Sdim bool ParseOptionalCallingConv(unsigned &CC); 284360784Sdim bool ParseOptionalAlignment(MaybeAlign &Alignment); 285288943Sdim bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes); 286321369Sdim bool ParseScopeAndOrdering(bool isAtomic, SyncScope::ID &SSID, 287226633Sdim AtomicOrdering &Ordering); 288321369Sdim bool ParseScope(SyncScope::ID &SSID); 289276479Sdim bool ParseOrdering(AtomicOrdering &Ordering); 290203954Srdivacky bool ParseOptionalStackAlignment(unsigned &Alignment); 291360784Sdim bool ParseOptionalCommaAlign(MaybeAlign &Alignment, bool &AteExtraComma); 292321369Sdim bool ParseOptionalCommaAddrSpace(unsigned &AddrSpace, LocTy &Loc, 293321369Sdim bool &AteExtraComma); 294276479Sdim bool ParseOptionalCommaInAlloca(bool &IsInAlloca); 295341825Sdim bool parseAllocSizeArguments(unsigned &BaseSizeArg, 296309124Sdim Optional<unsigned> &HowManyArg); 297309124Sdim bool ParseIndexList(SmallVectorImpl<unsigned> &Indices, 298309124Sdim bool &AteExtraComma); 299201360Srdivacky bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 300201360Srdivacky bool AteExtraComma; 301201360Srdivacky if (ParseIndexList(Indices, AteExtraComma)) return true; 302201360Srdivacky if (AteExtraComma) 303201360Srdivacky return TokError("expected index"); 304201360Srdivacky return false; 305201360Srdivacky } 306193323Sed 307193323Sed // Top-Level Entities 308193323Sed bool ParseTopLevelEntities(); 309193323Sed bool ValidateEndOfModule(); 310341825Sdim bool ValidateEndOfIndex(); 311193323Sed bool ParseTargetDefinition(); 312193323Sed bool ParseModuleAsm(); 313309124Sdim bool ParseSourceFileName(); 314249423Sdim bool ParseDepLibs(); // FIXME: Remove in 4.0. 315193323Sed bool ParseUnnamedType(); 316193323Sed bool ParseNamedType(); 317193323Sed bool ParseDeclare(); 318193323Sed bool ParseDefine(); 319193323Sed 320193323Sed bool ParseGlobalType(bool &IsConstant); 321198090Srdivacky bool ParseUnnamedGlobal(); 322193323Sed bool ParseNamedGlobal(); 323341825Sdim bool ParseGlobal(const std::string &Name, LocTy NameLoc, unsigned Linkage, 324276479Sdim bool HasLinkage, unsigned Visibility, 325327952Sdim unsigned DLLStorageClass, bool DSOLocal, 326309124Sdim GlobalVariable::ThreadLocalMode TLM, 327309124Sdim GlobalVariable::UnnamedAddr UnnamedAddr); 328341825Sdim bool parseIndirectSymbol(const std::string &Name, LocTy NameLoc, 329341825Sdim unsigned L, unsigned Visibility, 330327952Sdim unsigned DLLStorageClass, bool DSOLocal, 331309124Sdim GlobalVariable::ThreadLocalMode TLM, 332309124Sdim GlobalVariable::UnnamedAddr UnnamedAddr); 333276479Sdim bool parseComdat(); 334195340Sed bool ParseStandaloneMetadata(); 335198090Srdivacky bool ParseNamedMetadata(); 336201360Srdivacky bool ParseMDString(MDString *&Result); 337201360Srdivacky bool ParseMDNodeID(MDNode *&Result); 338249423Sdim bool ParseUnnamedAttrGrp(); 339249423Sdim bool ParseFnAttributeValuePairs(AttrBuilder &B, 340249423Sdim std::vector<unsigned> &FwdRefAttrGrps, 341261991Sdim bool inAttrGrp, LocTy &BuiltinLoc); 342353358Sdim bool ParseByValWithOptionalType(Type *&Result); 343193323Sed 344341825Sdim // Module Summary Index Parsing. 345341825Sdim bool SkipModuleSummaryEntry(); 346341825Sdim bool ParseSummaryEntry(); 347341825Sdim bool ParseModuleEntry(unsigned ID); 348341825Sdim bool ParseModuleReference(StringRef &ModulePath); 349341825Sdim bool ParseGVReference(ValueInfo &VI, unsigned &GVId); 350341825Sdim bool ParseGVEntry(unsigned ID); 351341825Sdim bool ParseFunctionSummary(std::string Name, GlobalValue::GUID, unsigned ID); 352341825Sdim bool ParseVariableSummary(std::string Name, GlobalValue::GUID, unsigned ID); 353341825Sdim bool ParseAliasSummary(std::string Name, GlobalValue::GUID, unsigned ID); 354341825Sdim bool ParseGVFlags(GlobalValueSummary::GVFlags &GVFlags); 355344779Sdim bool ParseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags); 356341825Sdim bool ParseOptionalFFlags(FunctionSummary::FFlags &FFlags); 357341825Sdim bool ParseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls); 358341825Sdim bool ParseHotness(CalleeInfo::HotnessType &Hotness); 359341825Sdim bool ParseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo); 360341825Sdim bool ParseTypeTests(std::vector<GlobalValue::GUID> &TypeTests); 361341825Sdim bool ParseVFuncIdList(lltok::Kind Kind, 362341825Sdim std::vector<FunctionSummary::VFuncId> &VFuncIdList); 363341825Sdim bool ParseConstVCallList( 364341825Sdim lltok::Kind Kind, 365341825Sdim std::vector<FunctionSummary::ConstVCall> &ConstVCallList); 366341825Sdim using IdToIndexMapType = 367341825Sdim std::map<unsigned, std::vector<std::pair<unsigned, LocTy>>>; 368341825Sdim bool ParseConstVCall(FunctionSummary::ConstVCall &ConstVCall, 369341825Sdim IdToIndexMapType &IdToIndexMap, unsigned Index); 370341825Sdim bool ParseVFuncId(FunctionSummary::VFuncId &VFuncId, 371341825Sdim IdToIndexMapType &IdToIndexMap, unsigned Index); 372353358Sdim bool ParseOptionalVTableFuncs(VTableFuncList &VTableFuncs); 373341825Sdim bool ParseOptionalRefs(std::vector<ValueInfo> &Refs); 374341825Sdim bool ParseTypeIdEntry(unsigned ID); 375341825Sdim bool ParseTypeIdSummary(TypeIdSummary &TIS); 376353358Sdim bool ParseTypeIdCompatibleVtableEntry(unsigned ID); 377341825Sdim bool ParseTypeTestResolution(TypeTestResolution &TTRes); 378341825Sdim bool ParseOptionalWpdResolutions( 379341825Sdim std::map<uint64_t, WholeProgramDevirtResolution> &WPDResMap); 380341825Sdim bool ParseWpdRes(WholeProgramDevirtResolution &WPDRes); 381341825Sdim bool ParseOptionalResByArg( 382341825Sdim std::map<std::vector<uint64_t>, WholeProgramDevirtResolution::ByArg> 383341825Sdim &ResByArg); 384341825Sdim bool ParseArgs(std::vector<uint64_t> &Args); 385341825Sdim void AddGlobalValueToIndex(std::string Name, GlobalValue::GUID, 386341825Sdim GlobalValue::LinkageTypes Linkage, unsigned ID, 387341825Sdim std::unique_ptr<GlobalValueSummary> Summary); 388341825Sdim 389193323Sed // Type Parsing. 390280031Sdim bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false); 391280031Sdim bool ParseType(Type *&Result, bool AllowVoid = false) { 392280031Sdim return ParseType(Result, "expected type", AllowVoid); 393280031Sdim } 394280031Sdim bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc, 395280031Sdim bool AllowVoid = false) { 396280031Sdim Loc = Lex.getLoc(); 397280031Sdim return ParseType(Result, Msg, AllowVoid); 398280031Sdim } 399224145Sdim bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { 400193323Sed Loc = Lex.getLoc(); 401193323Sed return ParseType(Result, AllowVoid); 402193323Sed } 403224145Sdim bool ParseAnonStructType(Type *&Result, bool Packed); 404224145Sdim bool ParseStructBody(SmallVectorImpl<Type*> &Body); 405224145Sdim bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name, 406224145Sdim std::pair<Type*, LocTy> &Entry, 407224145Sdim Type *&ResultTy); 408193323Sed 409224145Sdim bool ParseArrayVectorType(Type *&Result, bool isVector); 410224145Sdim bool ParseFunctionType(Type *&Result); 411224145Sdim 412193323Sed // Function Semantic Analysis. 413193323Sed class PerFunctionState { 414193323Sed LLParser &P; 415193323Sed Function &F; 416193323Sed std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 417193323Sed std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 418193323Sed std::vector<Value*> NumberedVals; 419249423Sdim 420198892Srdivacky /// FunctionNumber - If this is an unnamed function, this is the slot 421198892Srdivacky /// number of it, otherwise it is -1. 422198892Srdivacky int FunctionNumber; 423193323Sed public: 424341825Sdim PerFunctionState(LLParser &p, Function &f, int functionNumber); 425193323Sed ~PerFunctionState(); 426193323Sed 427193323Sed Function &getFunction() const { return F; } 428193323Sed 429198892Srdivacky bool FinishFunction(); 430193323Sed 431193323Sed /// GetVal - Get a value with the specified name or ID, creating a 432193323Sed /// forward reference record if needed. This can return null if the value 433193323Sed /// exists but does not have the right type. 434341825Sdim Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc, bool IsCall); 435341825Sdim Value *GetVal(unsigned ID, Type *Ty, LocTy Loc, bool IsCall); 436193323Sed 437193323Sed /// SetInstName - After an instruction is parsed and inserted into its 438193323Sed /// basic block, this installs its name. 439193323Sed bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 440193323Sed Instruction *Inst); 441193323Sed 442193323Sed /// GetBB - Get a basic block with the specified name or ID, creating a 443193323Sed /// forward reference record if needed. This can return null if the value 444193323Sed /// is not a BasicBlock. 445193323Sed BasicBlock *GetBB(const std::string &Name, LocTy Loc); 446193323Sed BasicBlock *GetBB(unsigned ID, LocTy Loc); 447193323Sed 448193323Sed /// DefineBB - Define the specified basic block, which is either named or 449193323Sed /// unnamed. If there is an error, this returns null otherwise it returns 450193323Sed /// the block being defined. 451353358Sdim BasicBlock *DefineBB(const std::string &Name, int NameID, LocTy Loc); 452280031Sdim 453280031Sdim bool resolveForwardRefBlockAddresses(); 454193323Sed }; 455193323Sed 456226633Sdim bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, 457341825Sdim PerFunctionState *PFS, bool IsCall); 458193323Sed 459344779Sdim Value *checkValidVariableType(LocTy Loc, const Twine &Name, Type *Ty, 460344779Sdim Value *Val, bool IsCall); 461344779Sdim 462296417Sdim bool parseConstantValue(Type *Ty, Constant *&C); 463226633Sdim bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS); 464226633Sdim bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { 465224145Sdim return ParseValue(Ty, V, &PFS); 466224145Sdim } 467296417Sdim 468226633Sdim bool ParseValue(Type *Ty, Value *&V, LocTy &Loc, 469193323Sed PerFunctionState &PFS) { 470193323Sed Loc = Lex.getLoc(); 471224145Sdim return ParseValue(Ty, V, &PFS); 472193323Sed } 473193323Sed 474224145Sdim bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS); 475224145Sdim bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 476224145Sdim return ParseTypeAndValue(V, &PFS); 477224145Sdim } 478193323Sed bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 479193323Sed Loc = Lex.getLoc(); 480193323Sed return ParseTypeAndValue(V, PFS); 481193323Sed } 482198892Srdivacky bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 483198892Srdivacky PerFunctionState &PFS); 484198892Srdivacky bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { 485198892Srdivacky LocTy Loc; 486198892Srdivacky return ParseTypeAndBasicBlock(BB, Loc, PFS); 487198892Srdivacky } 488200581Srdivacky 489203954Srdivacky 490193323Sed struct ParamInfo { 491193323Sed LocTy Loc; 492193323Sed Value *V; 493249423Sdim AttributeSet Attrs; 494249423Sdim ParamInfo(LocTy loc, Value *v, AttributeSet attrs) 495321369Sdim : Loc(loc), V(v), Attrs(attrs) {} 496193323Sed }; 497193323Sed bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 498280031Sdim PerFunctionState &PFS, 499280031Sdim bool IsMustTailCall = false, 500280031Sdim bool InVarArgsFunc = false); 501193323Sed 502296417Sdim bool 503296417Sdim ParseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList, 504296417Sdim PerFunctionState &PFS); 505296417Sdim 506296417Sdim bool ParseExceptionArgs(SmallVectorImpl<Value *> &Args, 507296417Sdim PerFunctionState &PFS); 508296417Sdim 509202375Srdivacky // Constant Parsing. 510276479Sdim bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr); 511341825Sdim bool ParseGlobalValue(Type *Ty, Constant *&C); 512202375Srdivacky bool ParseGlobalTypeAndValue(Constant *&V); 513314564Sdim bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts, 514314564Sdim Optional<unsigned> *InRangeOp = nullptr); 515280031Sdim bool parseOptionalComdat(StringRef GlobalName, Comdat *&C); 516280031Sdim bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS); 517288943Sdim bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg, 518288943Sdim PerFunctionState *PFS); 519280031Sdim bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS); 520280031Sdim bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false); 521341825Sdim bool ParseMDNode(MDNode *&N); 522341825Sdim bool ParseMDNodeTail(MDNode *&N); 523341825Sdim bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &Elts); 524288943Sdim bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD); 525288943Sdim bool ParseInstructionMetadata(Instruction &Inst); 526309124Sdim bool ParseGlobalObjectMetadataAttachment(GlobalObject &GO); 527288943Sdim bool ParseOptionalFunctionMetadata(Function &F); 528202375Srdivacky 529288943Sdim template <class FieldTy> 530288943Sdim bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result); 531288943Sdim template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result); 532288943Sdim template <class ParserTy> 533288943Sdim bool ParseMDFieldsImplBody(ParserTy parseField); 534288943Sdim template <class ParserTy> 535288943Sdim bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc); 536280031Sdim bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false); 537280031Sdim 538288943Sdim#define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ 539288943Sdim bool Parse##CLASS(MDNode *&Result, bool IsDistinct); 540288943Sdim#include "llvm/IR/Metadata.def" 541288943Sdim 542193323Sed // Function Parsing. 543193323Sed struct ArgInfo { 544193323Sed LocTy Loc; 545224145Sdim Type *Ty; 546249423Sdim AttributeSet Attrs; 547193323Sed std::string Name; 548249423Sdim ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N) 549321369Sdim : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} 550193323Sed }; 551224145Sdim bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg); 552193323Sed bool ParseFunctionHeader(Function *&Fn, bool isDefine); 553193323Sed bool ParseFunctionBody(Function &Fn); 554193323Sed bool ParseBasicBlock(PerFunctionState &PFS); 555193323Sed 556276479Sdim enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail }; 557276479Sdim 558201360Srdivacky // Instruction Parsing. Each instruction parsing routine can return with a 559201360Srdivacky // normal result, an error result, or return having eaten an extra comma. 560201360Srdivacky enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; 561201360Srdivacky int ParseInstruction(Instruction *&Inst, BasicBlock *BB, 562201360Srdivacky PerFunctionState &PFS); 563341825Sdim bool ParseCmpPredicate(unsigned &P, unsigned Opc); 564193323Sed 565224145Sdim bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 566193323Sed bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 567193323Sed bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 568198892Srdivacky bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); 569193323Sed bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 570226633Sdim bool ParseResume(Instruction *&Inst, PerFunctionState &PFS); 571296417Sdim bool ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS); 572296417Sdim bool ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS); 573296417Sdim bool ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS); 574296417Sdim bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS); 575296417Sdim bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS); 576353358Sdim bool ParseCallBr(Instruction *&Inst, PerFunctionState &PFS); 577193323Sed 578344779Sdim bool ParseUnaryOp(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc, 579353358Sdim bool IsFP); 580341825Sdim bool ParseArithmetic(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc, 581353358Sdim bool IsFP); 582341825Sdim bool ParseLogical(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc); 583341825Sdim bool ParseCompare(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc); 584341825Sdim bool ParseCast(Instruction *&Inst, PerFunctionState &PFS, unsigned Opc); 585341825Sdim bool ParseSelect(Instruction *&Inst, PerFunctionState &PFS); 586341825Sdim bool ParseVA_Arg(Instruction *&Inst, PerFunctionState &PFS); 587341825Sdim bool ParseExtractElement(Instruction *&Inst, PerFunctionState &PFS); 588341825Sdim bool ParseInsertElement(Instruction *&Inst, PerFunctionState &PFS); 589341825Sdim bool ParseShuffleVector(Instruction *&Inst, PerFunctionState &PFS); 590341825Sdim int ParsePHI(Instruction *&Inst, PerFunctionState &PFS); 591341825Sdim bool ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS); 592341825Sdim bool ParseCall(Instruction *&Inst, PerFunctionState &PFS, 593341825Sdim CallInst::TailCallKind TCK); 594341825Sdim int ParseAlloc(Instruction *&Inst, PerFunctionState &PFS); 595341825Sdim int ParseLoad(Instruction *&Inst, PerFunctionState &PFS); 596341825Sdim int ParseStore(Instruction *&Inst, PerFunctionState &PFS); 597341825Sdim int ParseCmpXchg(Instruction *&Inst, PerFunctionState &PFS); 598341825Sdim int ParseAtomicRMW(Instruction *&Inst, PerFunctionState &PFS); 599341825Sdim int ParseFence(Instruction *&Inst, PerFunctionState &PFS); 600341825Sdim int ParseGetElementPtr(Instruction *&Inst, PerFunctionState &PFS); 601341825Sdim int ParseExtractValue(Instruction *&Inst, PerFunctionState &PFS); 602341825Sdim int ParseInsertValue(Instruction *&Inst, PerFunctionState &PFS); 603360784Sdim bool ParseFreeze(Instruction *&I, PerFunctionState &PFS); 604249423Sdim 605280031Sdim // Use-list order directives. 606280031Sdim bool ParseUseListOrder(PerFunctionState *PFS = nullptr); 607280031Sdim bool ParseUseListOrderBB(); 608280031Sdim bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes); 609280031Sdim bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc); 610193323Sed }; 611193323Sed} // End llvm namespace 612193323Sed 613193323Sed#endif 614