1*9880d681SAndroid Build Coastguard Worker //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // This file defines the parser class for .ll files. 11*9880d681SAndroid Build Coastguard Worker // 12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 13*9880d681SAndroid Build Coastguard Worker 14*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_LIB_ASMPARSER_LLPARSER_H 15*9880d681SAndroid Build Coastguard Worker #define LLVM_LIB_ASMPARSER_LLPARSER_H 16*9880d681SAndroid Build Coastguard Worker 17*9880d681SAndroid Build Coastguard Worker #include "LLLexer.h" 18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Optional.h" 19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringMap.h" 20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Attributes.h" 21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h" 22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h" 23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Operator.h" 24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h" 25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ValueHandle.h" 26*9880d681SAndroid Build Coastguard Worker #include <map> 27*9880d681SAndroid Build Coastguard Worker 28*9880d681SAndroid Build Coastguard Worker namespace llvm { 29*9880d681SAndroid Build Coastguard Worker class Module; 30*9880d681SAndroid Build Coastguard Worker class OpaqueType; 31*9880d681SAndroid Build Coastguard Worker class Function; 32*9880d681SAndroid Build Coastguard Worker class Value; 33*9880d681SAndroid Build Coastguard Worker class BasicBlock; 34*9880d681SAndroid Build Coastguard Worker class Instruction; 35*9880d681SAndroid Build Coastguard Worker class Constant; 36*9880d681SAndroid Build Coastguard Worker class GlobalValue; 37*9880d681SAndroid Build Coastguard Worker class Comdat; 38*9880d681SAndroid Build Coastguard Worker class MDString; 39*9880d681SAndroid Build Coastguard Worker class MDNode; 40*9880d681SAndroid Build Coastguard Worker struct SlotMapping; 41*9880d681SAndroid Build Coastguard Worker class StructType; 42*9880d681SAndroid Build Coastguard Worker 43*9880d681SAndroid Build Coastguard Worker /// ValID - Represents a reference of a definition of some sort with no type. 44*9880d681SAndroid Build Coastguard Worker /// There are several cases where we have to parse the value but where the 45*9880d681SAndroid Build Coastguard Worker /// type can depend on later context. This may either be a numeric reference 46*9880d681SAndroid Build Coastguard Worker /// or a symbolic (%var) reference. This is just a discriminated union. 47*9880d681SAndroid Build Coastguard Worker struct ValID { 48*9880d681SAndroid Build Coastguard Worker enum { 49*9880d681SAndroid Build Coastguard Worker t_LocalID, t_GlobalID, // ID in UIntVal. 50*9880d681SAndroid Build Coastguard Worker t_LocalName, t_GlobalName, // Name in StrVal. 51*9880d681SAndroid Build Coastguard Worker t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 52*9880d681SAndroid Build Coastguard Worker t_Null, t_Undef, t_Zero, t_None, // No value. 53*9880d681SAndroid Build Coastguard Worker t_EmptyArray, // No value: [] 54*9880d681SAndroid Build Coastguard Worker t_Constant, // Value in ConstantVal. 55*9880d681SAndroid Build Coastguard Worker t_InlineAsm, // Value in FTy/StrVal/StrVal2/UIntVal. 56*9880d681SAndroid Build Coastguard Worker t_ConstantStruct, // Value in ConstantStructElts. 57*9880d681SAndroid Build Coastguard Worker t_PackedConstantStruct // Value in ConstantStructElts. 58*9880d681SAndroid Build Coastguard Worker } Kind = t_LocalID; 59*9880d681SAndroid Build Coastguard Worker 60*9880d681SAndroid Build Coastguard Worker LLLexer::LocTy Loc; 61*9880d681SAndroid Build Coastguard Worker unsigned UIntVal; 62*9880d681SAndroid Build Coastguard Worker FunctionType *FTy = nullptr; 63*9880d681SAndroid Build Coastguard Worker std::string StrVal, StrVal2; 64*9880d681SAndroid Build Coastguard Worker APSInt APSIntVal; 65*9880d681SAndroid Build Coastguard Worker APFloat APFloatVal{0.0}; 66*9880d681SAndroid Build Coastguard Worker Constant *ConstantVal; 67*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Constant *[]> ConstantStructElts; 68*9880d681SAndroid Build Coastguard Worker 69*9880d681SAndroid Build Coastguard Worker ValID() = default; ValIDValID70*9880d681SAndroid Build Coastguard Worker ValID(const ValID &RHS) 71*9880d681SAndroid Build Coastguard Worker : Kind(RHS.Kind), Loc(RHS.Loc), UIntVal(RHS.UIntVal), FTy(RHS.FTy), 72*9880d681SAndroid Build Coastguard Worker StrVal(RHS.StrVal), StrVal2(RHS.StrVal2), APSIntVal(RHS.APSIntVal), 73*9880d681SAndroid Build Coastguard Worker APFloatVal(RHS.APFloatVal), ConstantVal(RHS.ConstantVal) { 74*9880d681SAndroid Build Coastguard Worker assert(!RHS.ConstantStructElts); 75*9880d681SAndroid Build Coastguard Worker } 76*9880d681SAndroid Build Coastguard Worker 77*9880d681SAndroid Build Coastguard Worker bool operator<(const ValID &RHS) const { 78*9880d681SAndroid Build Coastguard Worker if (Kind == t_LocalID || Kind == t_GlobalID) 79*9880d681SAndroid Build Coastguard Worker return UIntVal < RHS.UIntVal; 80*9880d681SAndroid Build Coastguard Worker assert((Kind == t_LocalName || Kind == t_GlobalName || 81*9880d681SAndroid Build Coastguard Worker Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && 82*9880d681SAndroid Build Coastguard Worker "Ordering not defined for this ValID kind yet"); 83*9880d681SAndroid Build Coastguard Worker return StrVal < RHS.StrVal; 84*9880d681SAndroid Build Coastguard Worker } 85*9880d681SAndroid Build Coastguard Worker }; 86*9880d681SAndroid Build Coastguard Worker 87*9880d681SAndroid Build Coastguard Worker class LLParser { 88*9880d681SAndroid Build Coastguard Worker public: 89*9880d681SAndroid Build Coastguard Worker typedef LLLexer::LocTy LocTy; 90*9880d681SAndroid Build Coastguard Worker private: 91*9880d681SAndroid Build Coastguard Worker LLVMContext &Context; 92*9880d681SAndroid Build Coastguard Worker LLLexer Lex; 93*9880d681SAndroid Build Coastguard Worker Module *M; 94*9880d681SAndroid Build Coastguard Worker SlotMapping *Slots; 95*9880d681SAndroid Build Coastguard Worker 96*9880d681SAndroid Build Coastguard Worker // Instruction metadata resolution. Each instruction can have a list of 97*9880d681SAndroid Build Coastguard Worker // MDRef info associated with them. 98*9880d681SAndroid Build Coastguard Worker // 99*9880d681SAndroid Build Coastguard Worker // The simpler approach of just creating temporary MDNodes and then calling 100*9880d681SAndroid Build Coastguard Worker // RAUW on them when the definition is processed doesn't work because some 101*9880d681SAndroid Build Coastguard Worker // instruction metadata kinds, such as dbg, get stored in the IR in an 102*9880d681SAndroid Build Coastguard Worker // "optimized" format which doesn't participate in the normal value use 103*9880d681SAndroid Build Coastguard Worker // lists. This means that RAUW doesn't work, even on temporary MDNodes 104*9880d681SAndroid Build Coastguard Worker // which otherwise support RAUW. Instead, we defer resolving MDNode 105*9880d681SAndroid Build Coastguard Worker // references until the definitions have been processed. 106*9880d681SAndroid Build Coastguard Worker struct MDRef { 107*9880d681SAndroid Build Coastguard Worker SMLoc Loc; 108*9880d681SAndroid Build Coastguard Worker unsigned MDKind, MDSlot; 109*9880d681SAndroid Build Coastguard Worker }; 110*9880d681SAndroid Build Coastguard Worker 111*9880d681SAndroid Build Coastguard Worker SmallVector<Instruction*, 64> InstsWithTBAATag; 112*9880d681SAndroid Build Coastguard Worker 113*9880d681SAndroid Build Coastguard Worker // Type resolution handling data structures. The location is set when we 114*9880d681SAndroid Build Coastguard Worker // have processed a use of the type but not a definition yet. 115*9880d681SAndroid Build Coastguard Worker StringMap<std::pair<Type*, LocTy> > NamedTypes; 116*9880d681SAndroid Build Coastguard Worker std::map<unsigned, std::pair<Type*, LocTy> > NumberedTypes; 117*9880d681SAndroid Build Coastguard Worker 118*9880d681SAndroid Build Coastguard Worker std::map<unsigned, TrackingMDNodeRef> NumberedMetadata; 119*9880d681SAndroid Build Coastguard Worker std::map<unsigned, std::pair<TempMDTuple, LocTy>> ForwardRefMDNodes; 120*9880d681SAndroid Build Coastguard Worker 121*9880d681SAndroid Build Coastguard Worker // Global Value reference information. 122*9880d681SAndroid Build Coastguard Worker std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 123*9880d681SAndroid Build Coastguard Worker std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 124*9880d681SAndroid Build Coastguard Worker std::vector<GlobalValue*> NumberedVals; 125*9880d681SAndroid Build Coastguard Worker 126*9880d681SAndroid Build Coastguard Worker // Comdat forward reference information. 127*9880d681SAndroid Build Coastguard Worker std::map<std::string, LocTy> ForwardRefComdats; 128*9880d681SAndroid Build Coastguard Worker 129*9880d681SAndroid Build Coastguard Worker // References to blockaddress. The key is the function ValID, the value is 130*9880d681SAndroid Build Coastguard Worker // a list of references to blocks in that function. 131*9880d681SAndroid Build Coastguard Worker std::map<ValID, std::map<ValID, GlobalValue *>> ForwardRefBlockAddresses; 132*9880d681SAndroid Build Coastguard Worker class PerFunctionState; 133*9880d681SAndroid Build Coastguard Worker /// Reference to per-function state to allow basic blocks to be 134*9880d681SAndroid Build Coastguard Worker /// forward-referenced by blockaddress instructions within the same 135*9880d681SAndroid Build Coastguard Worker /// function. 136*9880d681SAndroid Build Coastguard Worker PerFunctionState *BlockAddressPFS; 137*9880d681SAndroid Build Coastguard Worker 138*9880d681SAndroid Build Coastguard Worker // Attribute builder reference information. 139*9880d681SAndroid Build Coastguard Worker std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups; 140*9880d681SAndroid Build Coastguard Worker std::map<unsigned, AttrBuilder> NumberedAttrBuilders; 141*9880d681SAndroid Build Coastguard Worker 142*9880d681SAndroid Build Coastguard Worker public: 143*9880d681SAndroid Build Coastguard Worker LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M, 144*9880d681SAndroid Build Coastguard Worker SlotMapping *Slots = nullptr) 145*9880d681SAndroid Build Coastguard Worker : Context(M->getContext()), Lex(F, SM, Err, M->getContext()), M(M), 146*9880d681SAndroid Build Coastguard Worker Slots(Slots), BlockAddressPFS(nullptr) {} 147*9880d681SAndroid Build Coastguard Worker bool Run(); 148*9880d681SAndroid Build Coastguard Worker 149*9880d681SAndroid Build Coastguard Worker bool parseStandaloneConstantValue(Constant *&C, const SlotMapping *Slots); 150*9880d681SAndroid Build Coastguard Worker 151*9880d681SAndroid Build Coastguard Worker bool parseTypeAtBeginning(Type *&Ty, unsigned &Read, 152*9880d681SAndroid Build Coastguard Worker const SlotMapping *Slots); 153*9880d681SAndroid Build Coastguard Worker getContext()154*9880d681SAndroid Build Coastguard Worker LLVMContext &getContext() { return Context; } 155*9880d681SAndroid Build Coastguard Worker 156*9880d681SAndroid Build Coastguard Worker private: 157*9880d681SAndroid Build Coastguard Worker Error(LocTy L,const Twine & Msg)158*9880d681SAndroid Build Coastguard Worker bool Error(LocTy L, const Twine &Msg) const { 159*9880d681SAndroid Build Coastguard Worker return Lex.Error(L, Msg); 160*9880d681SAndroid Build Coastguard Worker } TokError(const Twine & Msg)161*9880d681SAndroid Build Coastguard Worker bool TokError(const Twine &Msg) const { 162*9880d681SAndroid Build Coastguard Worker return Error(Lex.getLoc(), Msg); 163*9880d681SAndroid Build Coastguard Worker } 164*9880d681SAndroid Build Coastguard Worker 165*9880d681SAndroid Build Coastguard Worker /// Restore the internal name and slot mappings using the mappings that 166*9880d681SAndroid Build Coastguard Worker /// were created at an earlier parsing stage. 167*9880d681SAndroid Build Coastguard Worker void restoreParsingState(const SlotMapping *Slots); 168*9880d681SAndroid Build Coastguard Worker 169*9880d681SAndroid Build Coastguard Worker /// GetGlobalVal - Get a value with the specified name or ID, creating a 170*9880d681SAndroid Build Coastguard Worker /// forward reference record if needed. This can return null if the value 171*9880d681SAndroid Build Coastguard Worker /// exists but does not have the right type. 172*9880d681SAndroid Build Coastguard Worker GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc); 173*9880d681SAndroid Build Coastguard Worker GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc); 174*9880d681SAndroid Build Coastguard Worker 175*9880d681SAndroid Build Coastguard Worker /// Get a Comdat with the specified name, creating a forward reference 176*9880d681SAndroid Build Coastguard Worker /// record if needed. 177*9880d681SAndroid Build Coastguard Worker Comdat *getComdat(const std::string &N, LocTy Loc); 178*9880d681SAndroid Build Coastguard Worker 179*9880d681SAndroid Build Coastguard Worker // Helper Routines. 180*9880d681SAndroid Build Coastguard Worker bool ParseToken(lltok::Kind T, const char *ErrMsg); EatIfPresent(lltok::Kind T)181*9880d681SAndroid Build Coastguard Worker bool EatIfPresent(lltok::Kind T) { 182*9880d681SAndroid Build Coastguard Worker if (Lex.getKind() != T) return false; 183*9880d681SAndroid Build Coastguard Worker Lex.Lex(); 184*9880d681SAndroid Build Coastguard Worker return true; 185*9880d681SAndroid Build Coastguard Worker } 186*9880d681SAndroid Build Coastguard Worker EatFastMathFlagsIfPresent()187*9880d681SAndroid Build Coastguard Worker FastMathFlags EatFastMathFlagsIfPresent() { 188*9880d681SAndroid Build Coastguard Worker FastMathFlags FMF; 189*9880d681SAndroid Build Coastguard Worker while (true) 190*9880d681SAndroid Build Coastguard Worker switch (Lex.getKind()) { 191*9880d681SAndroid Build Coastguard Worker case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue; 192*9880d681SAndroid Build Coastguard Worker case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue; 193*9880d681SAndroid Build Coastguard Worker case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue; 194*9880d681SAndroid Build Coastguard Worker case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue; 195*9880d681SAndroid Build Coastguard Worker case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue; 196*9880d681SAndroid Build Coastguard Worker default: return FMF; 197*9880d681SAndroid Build Coastguard Worker } 198*9880d681SAndroid Build Coastguard Worker return FMF; 199*9880d681SAndroid Build Coastguard Worker } 200*9880d681SAndroid Build Coastguard Worker 201*9880d681SAndroid Build Coastguard Worker bool ParseOptionalToken(lltok::Kind T, bool &Present, 202*9880d681SAndroid Build Coastguard Worker LocTy *Loc = nullptr) { 203*9880d681SAndroid Build Coastguard Worker if (Lex.getKind() != T) { 204*9880d681SAndroid Build Coastguard Worker Present = false; 205*9880d681SAndroid Build Coastguard Worker } else { 206*9880d681SAndroid Build Coastguard Worker if (Loc) 207*9880d681SAndroid Build Coastguard Worker *Loc = Lex.getLoc(); 208*9880d681SAndroid Build Coastguard Worker Lex.Lex(); 209*9880d681SAndroid Build Coastguard Worker Present = true; 210*9880d681SAndroid Build Coastguard Worker } 211*9880d681SAndroid Build Coastguard Worker return false; 212*9880d681SAndroid Build Coastguard Worker } 213*9880d681SAndroid Build Coastguard Worker bool ParseStringConstant(std::string &Result); 214*9880d681SAndroid Build Coastguard Worker bool ParseUInt32(unsigned &Val); ParseUInt32(unsigned & Val,LocTy & Loc)215*9880d681SAndroid Build Coastguard Worker bool ParseUInt32(unsigned &Val, LocTy &Loc) { 216*9880d681SAndroid Build Coastguard Worker Loc = Lex.getLoc(); 217*9880d681SAndroid Build Coastguard Worker return ParseUInt32(Val); 218*9880d681SAndroid Build Coastguard Worker } 219*9880d681SAndroid Build Coastguard Worker bool ParseUInt64(uint64_t &Val); ParseUInt64(uint64_t & Val,LocTy & Loc)220*9880d681SAndroid Build Coastguard Worker bool ParseUInt64(uint64_t &Val, LocTy &Loc) { 221*9880d681SAndroid Build Coastguard Worker Loc = Lex.getLoc(); 222*9880d681SAndroid Build Coastguard Worker return ParseUInt64(Val); 223*9880d681SAndroid Build Coastguard Worker } 224*9880d681SAndroid Build Coastguard Worker 225*9880d681SAndroid Build Coastguard Worker bool ParseStringAttribute(AttrBuilder &B); 226*9880d681SAndroid Build Coastguard Worker 227*9880d681SAndroid Build Coastguard Worker bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM); 228*9880d681SAndroid Build Coastguard Worker bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); 229*9880d681SAndroid Build Coastguard Worker bool ParseOptionalUnnamedAddr(GlobalVariable::UnnamedAddr &UnnamedAddr); 230*9880d681SAndroid Build Coastguard Worker bool ParseOptionalAddrSpace(unsigned &AddrSpace); 231*9880d681SAndroid Build Coastguard Worker bool ParseOptionalParamAttrs(AttrBuilder &B); 232*9880d681SAndroid Build Coastguard Worker bool ParseOptionalReturnAttrs(AttrBuilder &B); 233*9880d681SAndroid Build Coastguard Worker bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage, 234*9880d681SAndroid Build Coastguard Worker unsigned &Visibility, unsigned &DLLStorageClass); 235*9880d681SAndroid Build Coastguard Worker void ParseOptionalVisibility(unsigned &Visibility); 236*9880d681SAndroid Build Coastguard Worker void ParseOptionalDLLStorageClass(unsigned &DLLStorageClass); 237*9880d681SAndroid Build Coastguard Worker bool ParseOptionalCallingConv(unsigned &CC); 238*9880d681SAndroid Build Coastguard Worker bool ParseOptionalAlignment(unsigned &Alignment); 239*9880d681SAndroid Build Coastguard Worker bool ParseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes); 240*9880d681SAndroid Build Coastguard Worker bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, 241*9880d681SAndroid Build Coastguard Worker AtomicOrdering &Ordering); 242*9880d681SAndroid Build Coastguard Worker bool ParseOrdering(AtomicOrdering &Ordering); 243*9880d681SAndroid Build Coastguard Worker bool ParseOptionalStackAlignment(unsigned &Alignment); 244*9880d681SAndroid Build Coastguard Worker bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma); 245*9880d681SAndroid Build Coastguard Worker bool ParseOptionalCommaInAlloca(bool &IsInAlloca); 246*9880d681SAndroid Build Coastguard Worker bool parseAllocSizeArguments(unsigned &ElemSizeArg, 247*9880d681SAndroid Build Coastguard Worker Optional<unsigned> &HowManyArg); 248*9880d681SAndroid Build Coastguard Worker bool ParseIndexList(SmallVectorImpl<unsigned> &Indices, 249*9880d681SAndroid Build Coastguard Worker bool &AteExtraComma); ParseIndexList(SmallVectorImpl<unsigned> & Indices)250*9880d681SAndroid Build Coastguard Worker bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 251*9880d681SAndroid Build Coastguard Worker bool AteExtraComma; 252*9880d681SAndroid Build Coastguard Worker if (ParseIndexList(Indices, AteExtraComma)) return true; 253*9880d681SAndroid Build Coastguard Worker if (AteExtraComma) 254*9880d681SAndroid Build Coastguard Worker return TokError("expected index"); 255*9880d681SAndroid Build Coastguard Worker return false; 256*9880d681SAndroid Build Coastguard Worker } 257*9880d681SAndroid Build Coastguard Worker 258*9880d681SAndroid Build Coastguard Worker // Top-Level Entities 259*9880d681SAndroid Build Coastguard Worker bool ParseTopLevelEntities(); 260*9880d681SAndroid Build Coastguard Worker bool ValidateEndOfModule(); 261*9880d681SAndroid Build Coastguard Worker bool ParseTargetDefinition(); 262*9880d681SAndroid Build Coastguard Worker bool ParseModuleAsm(); 263*9880d681SAndroid Build Coastguard Worker bool ParseSourceFileName(); 264*9880d681SAndroid Build Coastguard Worker bool ParseDepLibs(); // FIXME: Remove in 4.0. 265*9880d681SAndroid Build Coastguard Worker bool ParseUnnamedType(); 266*9880d681SAndroid Build Coastguard Worker bool ParseNamedType(); 267*9880d681SAndroid Build Coastguard Worker bool ParseDeclare(); 268*9880d681SAndroid Build Coastguard Worker bool ParseDefine(); 269*9880d681SAndroid Build Coastguard Worker 270*9880d681SAndroid Build Coastguard Worker bool ParseGlobalType(bool &IsConstant); 271*9880d681SAndroid Build Coastguard Worker bool ParseUnnamedGlobal(); 272*9880d681SAndroid Build Coastguard Worker bool ParseNamedGlobal(); 273*9880d681SAndroid Build Coastguard Worker bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, 274*9880d681SAndroid Build Coastguard Worker bool HasLinkage, unsigned Visibility, 275*9880d681SAndroid Build Coastguard Worker unsigned DLLStorageClass, 276*9880d681SAndroid Build Coastguard Worker GlobalVariable::ThreadLocalMode TLM, 277*9880d681SAndroid Build Coastguard Worker GlobalVariable::UnnamedAddr UnnamedAddr); 278*9880d681SAndroid Build Coastguard Worker bool parseIndirectSymbol(const std::string &Name, LocTy Loc, 279*9880d681SAndroid Build Coastguard Worker unsigned Linkage, unsigned Visibility, 280*9880d681SAndroid Build Coastguard Worker unsigned DLLStorageClass, 281*9880d681SAndroid Build Coastguard Worker GlobalVariable::ThreadLocalMode TLM, 282*9880d681SAndroid Build Coastguard Worker GlobalVariable::UnnamedAddr UnnamedAddr); 283*9880d681SAndroid Build Coastguard Worker bool parseComdat(); 284*9880d681SAndroid Build Coastguard Worker bool ParseStandaloneMetadata(); 285*9880d681SAndroid Build Coastguard Worker bool ParseNamedMetadata(); 286*9880d681SAndroid Build Coastguard Worker bool ParseMDString(MDString *&Result); 287*9880d681SAndroid Build Coastguard Worker bool ParseMDNodeID(MDNode *&Result); 288*9880d681SAndroid Build Coastguard Worker bool ParseUnnamedAttrGrp(); 289*9880d681SAndroid Build Coastguard Worker bool ParseFnAttributeValuePairs(AttrBuilder &B, 290*9880d681SAndroid Build Coastguard Worker std::vector<unsigned> &FwdRefAttrGrps, 291*9880d681SAndroid Build Coastguard Worker bool inAttrGrp, LocTy &BuiltinLoc); 292*9880d681SAndroid Build Coastguard Worker 293*9880d681SAndroid Build Coastguard Worker // Type Parsing. 294*9880d681SAndroid Build Coastguard Worker bool ParseType(Type *&Result, const Twine &Msg, bool AllowVoid = false); 295*9880d681SAndroid Build Coastguard Worker bool ParseType(Type *&Result, bool AllowVoid = false) { 296*9880d681SAndroid Build Coastguard Worker return ParseType(Result, "expected type", AllowVoid); 297*9880d681SAndroid Build Coastguard Worker } 298*9880d681SAndroid Build Coastguard Worker bool ParseType(Type *&Result, const Twine &Msg, LocTy &Loc, 299*9880d681SAndroid Build Coastguard Worker bool AllowVoid = false) { 300*9880d681SAndroid Build Coastguard Worker Loc = Lex.getLoc(); 301*9880d681SAndroid Build Coastguard Worker return ParseType(Result, Msg, AllowVoid); 302*9880d681SAndroid Build Coastguard Worker } 303*9880d681SAndroid Build Coastguard Worker bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { 304*9880d681SAndroid Build Coastguard Worker Loc = Lex.getLoc(); 305*9880d681SAndroid Build Coastguard Worker return ParseType(Result, AllowVoid); 306*9880d681SAndroid Build Coastguard Worker } 307*9880d681SAndroid Build Coastguard Worker bool ParseAnonStructType(Type *&Result, bool Packed); 308*9880d681SAndroid Build Coastguard Worker bool ParseStructBody(SmallVectorImpl<Type*> &Body); 309*9880d681SAndroid Build Coastguard Worker bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name, 310*9880d681SAndroid Build Coastguard Worker std::pair<Type*, LocTy> &Entry, 311*9880d681SAndroid Build Coastguard Worker Type *&ResultTy); 312*9880d681SAndroid Build Coastguard Worker 313*9880d681SAndroid Build Coastguard Worker bool ParseArrayVectorType(Type *&Result, bool isVector); 314*9880d681SAndroid Build Coastguard Worker bool ParseFunctionType(Type *&Result); 315*9880d681SAndroid Build Coastguard Worker 316*9880d681SAndroid Build Coastguard Worker // Function Semantic Analysis. 317*9880d681SAndroid Build Coastguard Worker class PerFunctionState { 318*9880d681SAndroid Build Coastguard Worker LLParser &P; 319*9880d681SAndroid Build Coastguard Worker Function &F; 320*9880d681SAndroid Build Coastguard Worker std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 321*9880d681SAndroid Build Coastguard Worker std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 322*9880d681SAndroid Build Coastguard Worker std::vector<Value*> NumberedVals; 323*9880d681SAndroid Build Coastguard Worker 324*9880d681SAndroid Build Coastguard Worker /// FunctionNumber - If this is an unnamed function, this is the slot 325*9880d681SAndroid Build Coastguard Worker /// number of it, otherwise it is -1. 326*9880d681SAndroid Build Coastguard Worker int FunctionNumber; 327*9880d681SAndroid Build Coastguard Worker public: 328*9880d681SAndroid Build Coastguard Worker PerFunctionState(LLParser &p, Function &f, int FunctionNumber); 329*9880d681SAndroid Build Coastguard Worker ~PerFunctionState(); 330*9880d681SAndroid Build Coastguard Worker getFunction()331*9880d681SAndroid Build Coastguard Worker Function &getFunction() const { return F; } 332*9880d681SAndroid Build Coastguard Worker 333*9880d681SAndroid Build Coastguard Worker bool FinishFunction(); 334*9880d681SAndroid Build Coastguard Worker 335*9880d681SAndroid Build Coastguard Worker /// GetVal - Get a value with the specified name or ID, creating a 336*9880d681SAndroid Build Coastguard Worker /// forward reference record if needed. This can return null if the value 337*9880d681SAndroid Build Coastguard Worker /// exists but does not have the right type. 338*9880d681SAndroid Build Coastguard Worker Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc); 339*9880d681SAndroid Build Coastguard Worker Value *GetVal(unsigned ID, Type *Ty, LocTy Loc); 340*9880d681SAndroid Build Coastguard Worker 341*9880d681SAndroid Build Coastguard Worker /// SetInstName - After an instruction is parsed and inserted into its 342*9880d681SAndroid Build Coastguard Worker /// basic block, this installs its name. 343*9880d681SAndroid Build Coastguard Worker bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 344*9880d681SAndroid Build Coastguard Worker Instruction *Inst); 345*9880d681SAndroid Build Coastguard Worker 346*9880d681SAndroid Build Coastguard Worker /// GetBB - Get a basic block with the specified name or ID, creating a 347*9880d681SAndroid Build Coastguard Worker /// forward reference record if needed. This can return null if the value 348*9880d681SAndroid Build Coastguard Worker /// is not a BasicBlock. 349*9880d681SAndroid Build Coastguard Worker BasicBlock *GetBB(const std::string &Name, LocTy Loc); 350*9880d681SAndroid Build Coastguard Worker BasicBlock *GetBB(unsigned ID, LocTy Loc); 351*9880d681SAndroid Build Coastguard Worker 352*9880d681SAndroid Build Coastguard Worker /// DefineBB - Define the specified basic block, which is either named or 353*9880d681SAndroid Build Coastguard Worker /// unnamed. If there is an error, this returns null otherwise it returns 354*9880d681SAndroid Build Coastguard Worker /// the block being defined. 355*9880d681SAndroid Build Coastguard Worker BasicBlock *DefineBB(const std::string &Name, LocTy Loc); 356*9880d681SAndroid Build Coastguard Worker 357*9880d681SAndroid Build Coastguard Worker bool resolveForwardRefBlockAddresses(); 358*9880d681SAndroid Build Coastguard Worker }; 359*9880d681SAndroid Build Coastguard Worker 360*9880d681SAndroid Build Coastguard Worker bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, 361*9880d681SAndroid Build Coastguard Worker PerFunctionState *PFS); 362*9880d681SAndroid Build Coastguard Worker 363*9880d681SAndroid Build Coastguard Worker bool parseConstantValue(Type *Ty, Constant *&C); 364*9880d681SAndroid Build Coastguard Worker bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS); ParseValue(Type * Ty,Value * & V,PerFunctionState & PFS)365*9880d681SAndroid Build Coastguard Worker bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { 366*9880d681SAndroid Build Coastguard Worker return ParseValue(Ty, V, &PFS); 367*9880d681SAndroid Build Coastguard Worker } 368*9880d681SAndroid Build Coastguard Worker ParseValue(Type * Ty,Value * & V,LocTy & Loc,PerFunctionState & PFS)369*9880d681SAndroid Build Coastguard Worker bool ParseValue(Type *Ty, Value *&V, LocTy &Loc, 370*9880d681SAndroid Build Coastguard Worker PerFunctionState &PFS) { 371*9880d681SAndroid Build Coastguard Worker Loc = Lex.getLoc(); 372*9880d681SAndroid Build Coastguard Worker return ParseValue(Ty, V, &PFS); 373*9880d681SAndroid Build Coastguard Worker } 374*9880d681SAndroid Build Coastguard Worker 375*9880d681SAndroid Build Coastguard Worker bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS); ParseTypeAndValue(Value * & V,PerFunctionState & PFS)376*9880d681SAndroid Build Coastguard Worker bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 377*9880d681SAndroid Build Coastguard Worker return ParseTypeAndValue(V, &PFS); 378*9880d681SAndroid Build Coastguard Worker } ParseTypeAndValue(Value * & V,LocTy & Loc,PerFunctionState & PFS)379*9880d681SAndroid Build Coastguard Worker bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 380*9880d681SAndroid Build Coastguard Worker Loc = Lex.getLoc(); 381*9880d681SAndroid Build Coastguard Worker return ParseTypeAndValue(V, PFS); 382*9880d681SAndroid Build Coastguard Worker } 383*9880d681SAndroid Build Coastguard Worker bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 384*9880d681SAndroid Build Coastguard Worker PerFunctionState &PFS); ParseTypeAndBasicBlock(BasicBlock * & BB,PerFunctionState & PFS)385*9880d681SAndroid Build Coastguard Worker bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { 386*9880d681SAndroid Build Coastguard Worker LocTy Loc; 387*9880d681SAndroid Build Coastguard Worker return ParseTypeAndBasicBlock(BB, Loc, PFS); 388*9880d681SAndroid Build Coastguard Worker } 389*9880d681SAndroid Build Coastguard Worker 390*9880d681SAndroid Build Coastguard Worker 391*9880d681SAndroid Build Coastguard Worker struct ParamInfo { 392*9880d681SAndroid Build Coastguard Worker LocTy Loc; 393*9880d681SAndroid Build Coastguard Worker Value *V; 394*9880d681SAndroid Build Coastguard Worker AttributeSet Attrs; ParamInfoParamInfo395*9880d681SAndroid Build Coastguard Worker ParamInfo(LocTy loc, Value *v, AttributeSet attrs) 396*9880d681SAndroid Build Coastguard Worker : Loc(loc), V(v), Attrs(attrs) {} 397*9880d681SAndroid Build Coastguard Worker }; 398*9880d681SAndroid Build Coastguard Worker bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 399*9880d681SAndroid Build Coastguard Worker PerFunctionState &PFS, 400*9880d681SAndroid Build Coastguard Worker bool IsMustTailCall = false, 401*9880d681SAndroid Build Coastguard Worker bool InVarArgsFunc = false); 402*9880d681SAndroid Build Coastguard Worker 403*9880d681SAndroid Build Coastguard Worker bool 404*9880d681SAndroid Build Coastguard Worker ParseOptionalOperandBundles(SmallVectorImpl<OperandBundleDef> &BundleList, 405*9880d681SAndroid Build Coastguard Worker PerFunctionState &PFS); 406*9880d681SAndroid Build Coastguard Worker 407*9880d681SAndroid Build Coastguard Worker bool ParseExceptionArgs(SmallVectorImpl<Value *> &Args, 408*9880d681SAndroid Build Coastguard Worker PerFunctionState &PFS); 409*9880d681SAndroid Build Coastguard Worker 410*9880d681SAndroid Build Coastguard Worker // Constant Parsing. 411*9880d681SAndroid Build Coastguard Worker bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr); 412*9880d681SAndroid Build Coastguard Worker bool ParseGlobalValue(Type *Ty, Constant *&V); 413*9880d681SAndroid Build Coastguard Worker bool ParseGlobalTypeAndValue(Constant *&V); 414*9880d681SAndroid Build Coastguard Worker bool ParseGlobalValueVector(SmallVectorImpl<Constant *> &Elts); 415*9880d681SAndroid Build Coastguard Worker bool parseOptionalComdat(StringRef GlobalName, Comdat *&C); 416*9880d681SAndroid Build Coastguard Worker bool ParseMetadataAsValue(Value *&V, PerFunctionState &PFS); 417*9880d681SAndroid Build Coastguard Worker bool ParseValueAsMetadata(Metadata *&MD, const Twine &TypeMsg, 418*9880d681SAndroid Build Coastguard Worker PerFunctionState *PFS); 419*9880d681SAndroid Build Coastguard Worker bool ParseMetadata(Metadata *&MD, PerFunctionState *PFS); 420*9880d681SAndroid Build Coastguard Worker bool ParseMDTuple(MDNode *&MD, bool IsDistinct = false); 421*9880d681SAndroid Build Coastguard Worker bool ParseMDNode(MDNode *&MD); 422*9880d681SAndroid Build Coastguard Worker bool ParseMDNodeTail(MDNode *&MD); 423*9880d681SAndroid Build Coastguard Worker bool ParseMDNodeVector(SmallVectorImpl<Metadata *> &MDs); 424*9880d681SAndroid Build Coastguard Worker bool ParseMetadataAttachment(unsigned &Kind, MDNode *&MD); 425*9880d681SAndroid Build Coastguard Worker bool ParseInstructionMetadata(Instruction &Inst); 426*9880d681SAndroid Build Coastguard Worker bool ParseGlobalObjectMetadataAttachment(GlobalObject &GO); 427*9880d681SAndroid Build Coastguard Worker bool ParseOptionalFunctionMetadata(Function &F); 428*9880d681SAndroid Build Coastguard Worker 429*9880d681SAndroid Build Coastguard Worker template <class FieldTy> 430*9880d681SAndroid Build Coastguard Worker bool ParseMDField(LocTy Loc, StringRef Name, FieldTy &Result); 431*9880d681SAndroid Build Coastguard Worker template <class FieldTy> bool ParseMDField(StringRef Name, FieldTy &Result); 432*9880d681SAndroid Build Coastguard Worker template <class ParserTy> 433*9880d681SAndroid Build Coastguard Worker bool ParseMDFieldsImplBody(ParserTy parseField); 434*9880d681SAndroid Build Coastguard Worker template <class ParserTy> 435*9880d681SAndroid Build Coastguard Worker bool ParseMDFieldsImpl(ParserTy parseField, LocTy &ClosingLoc); 436*9880d681SAndroid Build Coastguard Worker bool ParseSpecializedMDNode(MDNode *&N, bool IsDistinct = false); 437*9880d681SAndroid Build Coastguard Worker 438*9880d681SAndroid Build Coastguard Worker #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) \ 439*9880d681SAndroid Build Coastguard Worker bool Parse##CLASS(MDNode *&Result, bool IsDistinct); 440*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Metadata.def" 441*9880d681SAndroid Build Coastguard Worker 442*9880d681SAndroid Build Coastguard Worker // Function Parsing. 443*9880d681SAndroid Build Coastguard Worker struct ArgInfo { 444*9880d681SAndroid Build Coastguard Worker LocTy Loc; 445*9880d681SAndroid Build Coastguard Worker Type *Ty; 446*9880d681SAndroid Build Coastguard Worker AttributeSet Attrs; 447*9880d681SAndroid Build Coastguard Worker std::string Name; ArgInfoArgInfo448*9880d681SAndroid Build Coastguard Worker ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N) 449*9880d681SAndroid Build Coastguard Worker : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} 450*9880d681SAndroid Build Coastguard Worker }; 451*9880d681SAndroid Build Coastguard Worker bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg); 452*9880d681SAndroid Build Coastguard Worker bool ParseFunctionHeader(Function *&Fn, bool isDefine); 453*9880d681SAndroid Build Coastguard Worker bool ParseFunctionBody(Function &Fn); 454*9880d681SAndroid Build Coastguard Worker bool ParseBasicBlock(PerFunctionState &PFS); 455*9880d681SAndroid Build Coastguard Worker 456*9880d681SAndroid Build Coastguard Worker enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail }; 457*9880d681SAndroid Build Coastguard Worker 458*9880d681SAndroid Build Coastguard Worker // Instruction Parsing. Each instruction parsing routine can return with a 459*9880d681SAndroid Build Coastguard Worker // normal result, an error result, or return having eaten an extra comma. 460*9880d681SAndroid Build Coastguard Worker enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; 461*9880d681SAndroid Build Coastguard Worker int ParseInstruction(Instruction *&Inst, BasicBlock *BB, 462*9880d681SAndroid Build Coastguard Worker PerFunctionState &PFS); 463*9880d681SAndroid Build Coastguard Worker bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); 464*9880d681SAndroid Build Coastguard Worker 465*9880d681SAndroid Build Coastguard Worker bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 466*9880d681SAndroid Build Coastguard Worker bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 467*9880d681SAndroid Build Coastguard Worker bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 468*9880d681SAndroid Build Coastguard Worker bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); 469*9880d681SAndroid Build Coastguard Worker bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 470*9880d681SAndroid Build Coastguard Worker bool ParseResume(Instruction *&Inst, PerFunctionState &PFS); 471*9880d681SAndroid Build Coastguard Worker bool ParseCleanupRet(Instruction *&Inst, PerFunctionState &PFS); 472*9880d681SAndroid Build Coastguard Worker bool ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS); 473*9880d681SAndroid Build Coastguard Worker bool ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS); 474*9880d681SAndroid Build Coastguard Worker bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS); 475*9880d681SAndroid Build Coastguard Worker bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS); 476*9880d681SAndroid Build Coastguard Worker 477*9880d681SAndroid Build Coastguard Worker bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, 478*9880d681SAndroid Build Coastguard Worker unsigned OperandType); 479*9880d681SAndroid Build Coastguard Worker bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 480*9880d681SAndroid Build Coastguard Worker bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 481*9880d681SAndroid Build Coastguard Worker bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 482*9880d681SAndroid Build Coastguard Worker bool ParseSelect(Instruction *&I, PerFunctionState &PFS); 483*9880d681SAndroid Build Coastguard Worker bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); 484*9880d681SAndroid Build Coastguard Worker bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); 485*9880d681SAndroid Build Coastguard Worker bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); 486*9880d681SAndroid Build Coastguard Worker bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); 487*9880d681SAndroid Build Coastguard Worker int ParsePHI(Instruction *&I, PerFunctionState &PFS); 488*9880d681SAndroid Build Coastguard Worker bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS); 489*9880d681SAndroid Build Coastguard Worker bool ParseCall(Instruction *&I, PerFunctionState &PFS, 490*9880d681SAndroid Build Coastguard Worker CallInst::TailCallKind IsTail); 491*9880d681SAndroid Build Coastguard Worker int ParseAlloc(Instruction *&I, PerFunctionState &PFS); 492*9880d681SAndroid Build Coastguard Worker int ParseLoad(Instruction *&I, PerFunctionState &PFS); 493*9880d681SAndroid Build Coastguard Worker int ParseStore(Instruction *&I, PerFunctionState &PFS); 494*9880d681SAndroid Build Coastguard Worker int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS); 495*9880d681SAndroid Build Coastguard Worker int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS); 496*9880d681SAndroid Build Coastguard Worker int ParseFence(Instruction *&I, PerFunctionState &PFS); 497*9880d681SAndroid Build Coastguard Worker int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); 498*9880d681SAndroid Build Coastguard Worker int ParseExtractValue(Instruction *&I, PerFunctionState &PFS); 499*9880d681SAndroid Build Coastguard Worker int ParseInsertValue(Instruction *&I, PerFunctionState &PFS); 500*9880d681SAndroid Build Coastguard Worker 501*9880d681SAndroid Build Coastguard Worker // Use-list order directives. 502*9880d681SAndroid Build Coastguard Worker bool ParseUseListOrder(PerFunctionState *PFS = nullptr); 503*9880d681SAndroid Build Coastguard Worker bool ParseUseListOrderBB(); 504*9880d681SAndroid Build Coastguard Worker bool ParseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes); 505*9880d681SAndroid Build Coastguard Worker bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc); 506*9880d681SAndroid Build Coastguard Worker }; 507*9880d681SAndroid Build Coastguard Worker } // End llvm namespace 508*9880d681SAndroid Build Coastguard Worker 509*9880d681SAndroid Build Coastguard Worker #endif 510