1193323Sed//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the parser class for .ll files. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#ifndef LLVM_ASMPARSER_LLPARSER_H 15193323Sed#define LLVM_ASMPARSER_LLPARSER_H 16193323Sed 17193323Sed#include "LLLexer.h" 18206083Srdivacky#include "llvm/ADT/DenseMap.h" 19224145Sdim#include "llvm/ADT/StringMap.h" 20249423Sdim#include "llvm/IR/Attributes.h" 21249423Sdim#include "llvm/IR/Instructions.h" 22249423Sdim#include "llvm/IR/Module.h" 23249423Sdim#include "llvm/IR/Operator.h" 24249423Sdim#include "llvm/IR/Type.h" 25201360Srdivacky#include "llvm/Support/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; 37193323Sed class MDString; 38193323Sed class MDNode; 39224145Sdim class StructType; 40193323Sed 41198892Srdivacky /// ValID - Represents a reference of a definition of some sort with no type. 42198892Srdivacky /// There are several cases where we have to parse the value but where the 43198892Srdivacky /// type can depend on later context. This may either be a numeric reference 44198892Srdivacky /// or a symbolic (%var) reference. This is just a discriminated union. 45198892Srdivacky struct ValID { 46198892Srdivacky enum { 47198892Srdivacky t_LocalID, t_GlobalID, // ID in UIntVal. 48198892Srdivacky t_LocalName, t_GlobalName, // Name in StrVal. 49198892Srdivacky t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 50198892Srdivacky t_Null, t_Undef, t_Zero, // No value. 51198892Srdivacky t_EmptyArray, // No value: [] 52198892Srdivacky t_Constant, // Value in ConstantVal. 53198892Srdivacky t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. 54201360Srdivacky t_MDNode, // Value in MDNodeVal. 55224145Sdim t_MDString, // Value in MDStringVal. 56224145Sdim t_ConstantStruct, // Value in ConstantStructElts. 57224145Sdim t_PackedConstantStruct // Value in ConstantStructElts. 58198892Srdivacky } Kind; 59249423Sdim 60198892Srdivacky LLLexer::LocTy Loc; 61198892Srdivacky unsigned UIntVal; 62198892Srdivacky std::string StrVal, StrVal2; 63198892Srdivacky APSInt APSIntVal; 64198892Srdivacky APFloat APFloatVal; 65198892Srdivacky Constant *ConstantVal; 66201360Srdivacky MDNode *MDNodeVal; 67201360Srdivacky MDString *MDStringVal; 68224145Sdim Constant **ConstantStructElts; 69249423Sdim 70224145Sdim ValID() : Kind(t_LocalID), APFloatVal(0.0) {} 71224145Sdim ~ValID() { 72224145Sdim if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) 73224145Sdim delete [] ConstantStructElts; 74224145Sdim } 75249423Sdim 76198892Srdivacky bool operator<(const ValID &RHS) const { 77198892Srdivacky if (Kind == t_LocalID || Kind == t_GlobalID) 78198892Srdivacky return UIntVal < RHS.UIntVal; 79224145Sdim assert((Kind == t_LocalName || Kind == t_GlobalName || 80249423Sdim Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && 81198892Srdivacky "Ordering not defined for this ValID kind yet"); 82198892Srdivacky return StrVal < RHS.StrVal; 83198892Srdivacky } 84198892Srdivacky }; 85249423Sdim 86193323Sed class LLParser { 87193323Sed public: 88193323Sed typedef LLLexer::LocTy LocTy; 89193323Sed private: 90207618Srdivacky LLVMContext &Context; 91193323Sed LLLexer Lex; 92193323Sed Module *M; 93249423Sdim 94206083Srdivacky // Instruction metadata resolution. Each instruction can have a list of 95206083Srdivacky // MDRef info associated with them. 96212904Sdim // 97212904Sdim // The simpler approach of just creating temporary MDNodes and then calling 98212904Sdim // RAUW on them when the definition is processed doesn't work because some 99212904Sdim // instruction metadata kinds, such as dbg, get stored in the IR in an 100212904Sdim // "optimized" format which doesn't participate in the normal value use 101212904Sdim // lists. This means that RAUW doesn't work, even on temporary MDNodes 102212904Sdim // which otherwise support RAUW. Instead, we defer resolving MDNode 103212904Sdim // references until the definitions have been processed. 104206083Srdivacky struct MDRef { 105206083Srdivacky SMLoc Loc; 106206083Srdivacky unsigned MDKind, MDSlot; 107206083Srdivacky }; 108206083Srdivacky DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata; 109193323Sed 110263508Sdim SmallVector<Instruction*, 64> InstsWithTBAATag; 111263508Sdim 112224145Sdim // Type resolution handling data structures. The location is set when we 113224145Sdim // have processed a use of the type but not a definition yet. 114224145Sdim StringMap<std::pair<Type*, LocTy> > NamedTypes; 115224145Sdim std::vector<std::pair<Type*, LocTy> > NumberedTypes; 116249423Sdim 117201360Srdivacky std::vector<TrackingVH<MDNode> > NumberedMetadata; 118201360Srdivacky std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes; 119193323Sed 120193323Sed // Global Value reference information. 121193323Sed std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 122193323Sed std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 123193323Sed std::vector<GlobalValue*> NumberedVals; 124249423Sdim 125198892Srdivacky // References to blockaddress. The key is the function ValID, the value is 126198892Srdivacky // a list of references to blocks in that function. 127198892Srdivacky std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > > 128198892Srdivacky ForwardRefBlockAddresses; 129249423Sdim 130249423Sdim // Attribute builder reference information. 131249423Sdim std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups; 132249423Sdim std::map<unsigned, AttrBuilder> NumberedAttrBuilders; 133249423Sdim 134193323Sed public: 135249423Sdim LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : 136198396Srdivacky Context(m->getContext()), Lex(F, SM, Err, m->getContext()), 137224145Sdim M(m) {} 138193323Sed bool Run(); 139193323Sed 140224145Sdim LLVMContext &getContext() { return Context; } 141195340Sed 142193323Sed private: 143193323Sed 144218893Sdim bool Error(LocTy L, const Twine &Msg) const { 145193323Sed return Lex.Error(L, Msg); 146193323Sed } 147218893Sdim bool TokError(const Twine &Msg) const { 148193323Sed return Error(Lex.getLoc(), Msg); 149193323Sed } 150193323Sed 151193323Sed /// GetGlobalVal - Get a value with the specified name or ID, creating a 152193323Sed /// forward reference record if needed. This can return null if the value 153193323Sed /// exists but does not have the right type. 154226633Sdim GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc); 155226633Sdim GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc); 156193323Sed 157193323Sed // Helper Routines. 158193323Sed bool ParseToken(lltok::Kind T, const char *ErrMsg); 159193323Sed bool EatIfPresent(lltok::Kind T) { 160193323Sed if (Lex.getKind() != T) return false; 161193323Sed Lex.Lex(); 162193323Sed return true; 163193323Sed } 164249423Sdim 165249423Sdim FastMathFlags EatFastMathFlagsIfPresent() { 166249423Sdim FastMathFlags FMF; 167249423Sdim while (true) 168249423Sdim switch (Lex.getKind()) { 169249423Sdim case lltok::kw_fast: FMF.setUnsafeAlgebra(); Lex.Lex(); continue; 170249423Sdim case lltok::kw_nnan: FMF.setNoNaNs(); Lex.Lex(); continue; 171249423Sdim case lltok::kw_ninf: FMF.setNoInfs(); Lex.Lex(); continue; 172249423Sdim case lltok::kw_nsz: FMF.setNoSignedZeros(); Lex.Lex(); continue; 173249423Sdim case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue; 174249423Sdim default: return FMF; 175249423Sdim } 176249423Sdim return FMF; 177249423Sdim } 178249423Sdim 179218893Sdim bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) { 180193323Sed if (Lex.getKind() != T) { 181193323Sed Present = false; 182193323Sed } else { 183218893Sdim if (Loc) 184218893Sdim *Loc = Lex.getLoc(); 185193323Sed Lex.Lex(); 186193323Sed Present = true; 187193323Sed } 188193323Sed return false; 189193323Sed } 190193323Sed bool ParseStringConstant(std::string &Result); 191193323Sed bool ParseUInt32(unsigned &Val); 192193323Sed bool ParseUInt32(unsigned &Val, LocTy &Loc) { 193193323Sed Loc = Lex.getLoc(); 194193323Sed return ParseUInt32(Val); 195193323Sed } 196239462Sdim 197239462Sdim bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM); 198239462Sdim bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM); 199193323Sed bool ParseOptionalAddrSpace(unsigned &AddrSpace); 200249423Sdim bool ParseOptionalParamAttrs(AttrBuilder &B); 201249423Sdim bool ParseOptionalReturnAttrs(AttrBuilder &B); 202193323Sed bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); 203193323Sed bool ParseOptionalLinkage(unsigned &Linkage) { 204193323Sed bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); 205193323Sed } 206193323Sed bool ParseOptionalVisibility(unsigned &Visibility); 207198090Srdivacky bool ParseOptionalCallingConv(CallingConv::ID &CC); 208193323Sed bool ParseOptionalAlignment(unsigned &Alignment); 209226633Sdim bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope, 210226633Sdim AtomicOrdering &Ordering); 211203954Srdivacky bool ParseOptionalStackAlignment(unsigned &Alignment); 212201360Srdivacky bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma); 213201360Srdivacky bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma); 214201360Srdivacky bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 215201360Srdivacky bool AteExtraComma; 216201360Srdivacky if (ParseIndexList(Indices, AteExtraComma)) return true; 217201360Srdivacky if (AteExtraComma) 218201360Srdivacky return TokError("expected index"); 219201360Srdivacky return false; 220201360Srdivacky } 221193323Sed 222193323Sed // Top-Level Entities 223193323Sed bool ParseTopLevelEntities(); 224193323Sed bool ValidateEndOfModule(); 225193323Sed bool ParseTargetDefinition(); 226193323Sed bool ParseModuleAsm(); 227249423Sdim bool ParseDepLibs(); // FIXME: Remove in 4.0. 228193323Sed bool ParseUnnamedType(); 229193323Sed bool ParseNamedType(); 230193323Sed bool ParseDeclare(); 231193323Sed bool ParseDefine(); 232193323Sed 233193323Sed bool ParseGlobalType(bool &IsConstant); 234198090Srdivacky bool ParseUnnamedGlobal(); 235193323Sed bool ParseNamedGlobal(); 236193323Sed bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, 237193323Sed bool HasLinkage, unsigned Visibility); 238193323Sed bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); 239195340Sed bool ParseStandaloneMetadata(); 240198090Srdivacky bool ParseNamedMetadata(); 241201360Srdivacky bool ParseMDString(MDString *&Result); 242201360Srdivacky bool ParseMDNodeID(MDNode *&Result); 243206083Srdivacky bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo); 244249423Sdim bool ParseUnnamedAttrGrp(); 245249423Sdim bool ParseFnAttributeValuePairs(AttrBuilder &B, 246249423Sdim std::vector<unsigned> &FwdRefAttrGrps, 247263508Sdim bool inAttrGrp, LocTy &BuiltinLoc); 248193323Sed 249193323Sed // Type Parsing. 250224145Sdim bool ParseType(Type *&Result, bool AllowVoid = false); 251224145Sdim bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { 252193323Sed Loc = Lex.getLoc(); 253193323Sed return ParseType(Result, AllowVoid); 254193323Sed } 255224145Sdim bool ParseAnonStructType(Type *&Result, bool Packed); 256224145Sdim bool ParseStructBody(SmallVectorImpl<Type*> &Body); 257224145Sdim bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name, 258224145Sdim std::pair<Type*, LocTy> &Entry, 259224145Sdim Type *&ResultTy); 260193323Sed 261224145Sdim bool ParseArrayVectorType(Type *&Result, bool isVector); 262224145Sdim bool ParseFunctionType(Type *&Result); 263224145Sdim 264193323Sed // Function Semantic Analysis. 265193323Sed class PerFunctionState { 266193323Sed LLParser &P; 267193323Sed Function &F; 268193323Sed std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 269193323Sed std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 270193323Sed std::vector<Value*> NumberedVals; 271249423Sdim 272198892Srdivacky /// FunctionNumber - If this is an unnamed function, this is the slot 273198892Srdivacky /// number of it, otherwise it is -1. 274198892Srdivacky int FunctionNumber; 275193323Sed public: 276198892Srdivacky PerFunctionState(LLParser &p, Function &f, int FunctionNumber); 277193323Sed ~PerFunctionState(); 278193323Sed 279193323Sed Function &getFunction() const { return F; } 280193323Sed 281198892Srdivacky bool FinishFunction(); 282193323Sed 283193323Sed /// GetVal - Get a value with the specified name or ID, creating a 284193323Sed /// forward reference record if needed. This can return null if the value 285193323Sed /// exists but does not have the right type. 286226633Sdim Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc); 287226633Sdim Value *GetVal(unsigned ID, Type *Ty, LocTy Loc); 288193323Sed 289193323Sed /// SetInstName - After an instruction is parsed and inserted into its 290193323Sed /// basic block, this installs its name. 291193323Sed bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 292193323Sed Instruction *Inst); 293193323Sed 294193323Sed /// GetBB - Get a basic block with the specified name or ID, creating a 295193323Sed /// forward reference record if needed. This can return null if the value 296193323Sed /// is not a BasicBlock. 297193323Sed BasicBlock *GetBB(const std::string &Name, LocTy Loc); 298193323Sed BasicBlock *GetBB(unsigned ID, LocTy Loc); 299193323Sed 300193323Sed /// DefineBB - Define the specified basic block, which is either named or 301193323Sed /// unnamed. If there is an error, this returns null otherwise it returns 302193323Sed /// the block being defined. 303193323Sed BasicBlock *DefineBB(const std::string &Name, LocTy Loc); 304193323Sed }; 305193323Sed 306226633Sdim bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, 307202375Srdivacky PerFunctionState *PFS); 308193323Sed 309226633Sdim bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS); 310226633Sdim bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { 311224145Sdim return ParseValue(Ty, V, &PFS); 312224145Sdim } 313226633Sdim bool ParseValue(Type *Ty, Value *&V, LocTy &Loc, 314193323Sed PerFunctionState &PFS) { 315193323Sed Loc = Lex.getLoc(); 316224145Sdim return ParseValue(Ty, V, &PFS); 317193323Sed } 318193323Sed 319224145Sdim bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS); 320224145Sdim bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 321224145Sdim return ParseTypeAndValue(V, &PFS); 322224145Sdim } 323193323Sed bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 324193323Sed Loc = Lex.getLoc(); 325193323Sed return ParseTypeAndValue(V, PFS); 326193323Sed } 327198892Srdivacky bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 328198892Srdivacky PerFunctionState &PFS); 329198892Srdivacky bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { 330198892Srdivacky LocTy Loc; 331198892Srdivacky return ParseTypeAndBasicBlock(BB, Loc, PFS); 332198892Srdivacky } 333200581Srdivacky 334203954Srdivacky 335193323Sed struct ParamInfo { 336193323Sed LocTy Loc; 337193323Sed Value *V; 338249423Sdim AttributeSet Attrs; 339249423Sdim ParamInfo(LocTy loc, Value *v, AttributeSet attrs) 340193323Sed : Loc(loc), V(v), Attrs(attrs) {} 341193323Sed }; 342193323Sed bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 343193323Sed PerFunctionState &PFS); 344193323Sed 345202375Srdivacky // Constant Parsing. 346202375Srdivacky bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL); 347226633Sdim bool ParseGlobalValue(Type *Ty, Constant *&V); 348202375Srdivacky bool ParseGlobalTypeAndValue(Constant *&V); 349202375Srdivacky bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts); 350212904Sdim bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS); 351210299Sed bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS); 352202375Srdivacky bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS); 353212904Sdim bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); 354202375Srdivacky 355193323Sed // Function Parsing. 356193323Sed struct ArgInfo { 357193323Sed LocTy Loc; 358224145Sdim Type *Ty; 359249423Sdim AttributeSet Attrs; 360193323Sed std::string Name; 361249423Sdim ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N) 362224145Sdim : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} 363193323Sed }; 364224145Sdim bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg); 365193323Sed bool ParseFunctionHeader(Function *&Fn, bool isDefine); 366193323Sed bool ParseFunctionBody(Function &Fn); 367193323Sed bool ParseBasicBlock(PerFunctionState &PFS); 368193323Sed 369201360Srdivacky // Instruction Parsing. Each instruction parsing routine can return with a 370201360Srdivacky // normal result, an error result, or return having eaten an extra comma. 371201360Srdivacky enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; 372201360Srdivacky int ParseInstruction(Instruction *&Inst, BasicBlock *BB, 373201360Srdivacky PerFunctionState &PFS); 374193323Sed bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); 375193323Sed 376224145Sdim bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 377193323Sed bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 378193323Sed bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 379198892Srdivacky bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); 380193323Sed bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 381226633Sdim bool ParseResume(Instruction *&Inst, PerFunctionState &PFS); 382193323Sed 383193323Sed bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, 384193323Sed unsigned OperandType); 385193323Sed bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 386193323Sed bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 387193323Sed bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 388193323Sed bool ParseSelect(Instruction *&I, PerFunctionState &PFS); 389193323Sed bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); 390193323Sed bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); 391193323Sed bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); 392193323Sed bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); 393201360Srdivacky int ParsePHI(Instruction *&I, PerFunctionState &PFS); 394226633Sdim bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS); 395193323Sed bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail); 396224145Sdim int ParseAlloc(Instruction *&I, PerFunctionState &PFS); 397234353Sdim int ParseLoad(Instruction *&I, PerFunctionState &PFS); 398234353Sdim int ParseStore(Instruction *&I, PerFunctionState &PFS); 399226633Sdim int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS); 400226633Sdim int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS); 401226633Sdim int ParseFence(Instruction *&I, PerFunctionState &PFS); 402201360Srdivacky int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); 403201360Srdivacky int ParseExtractValue(Instruction *&I, PerFunctionState &PFS); 404201360Srdivacky int ParseInsertValue(Instruction *&I, PerFunctionState &PFS); 405249423Sdim 406249423Sdim bool ResolveForwardRefBlockAddresses(Function *TheFn, 407198892Srdivacky std::vector<std::pair<ValID, GlobalValue*> > &Refs, 408198892Srdivacky PerFunctionState *PFS); 409193323Sed }; 410193323Sed} // End llvm namespace 411193323Sed 412193323Sed#endif 413