LLParser.h revision 207618
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" 18195340Sed#include "llvm/Module.h" 19193323Sed#include "llvm/Type.h" 20206083Srdivacky#include "llvm/ADT/DenseMap.h" 21201360Srdivacky#include "llvm/Support/ValueHandle.h" 22193323Sed#include <map> 23193323Sed 24193323Sednamespace llvm { 25193323Sed class Module; 26193323Sed class OpaqueType; 27193323Sed class Function; 28193323Sed class Value; 29193323Sed class BasicBlock; 30193323Sed class Instruction; 31193323Sed class Constant; 32193323Sed class GlobalValue; 33193323Sed class MDString; 34193323Sed class MDNode; 35203954Srdivacky class UnionType; 36193323Sed 37198892Srdivacky /// ValID - Represents a reference of a definition of some sort with no type. 38198892Srdivacky /// There are several cases where we have to parse the value but where the 39198892Srdivacky /// type can depend on later context. This may either be a numeric reference 40198892Srdivacky /// or a symbolic (%var) reference. This is just a discriminated union. 41198892Srdivacky struct ValID { 42198892Srdivacky enum { 43198892Srdivacky t_LocalID, t_GlobalID, // ID in UIntVal. 44198892Srdivacky t_LocalName, t_GlobalName, // Name in StrVal. 45198892Srdivacky t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 46198892Srdivacky t_Null, t_Undef, t_Zero, // No value. 47198892Srdivacky t_EmptyArray, // No value: [] 48198892Srdivacky t_Constant, // Value in ConstantVal. 49198892Srdivacky t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. 50201360Srdivacky t_MDNode, // Value in MDNodeVal. 51201360Srdivacky t_MDString // Value in MDStringVal. 52198892Srdivacky } Kind; 53198892Srdivacky 54198892Srdivacky LLLexer::LocTy Loc; 55198892Srdivacky unsigned UIntVal; 56198892Srdivacky std::string StrVal, StrVal2; 57198892Srdivacky APSInt APSIntVal; 58198892Srdivacky APFloat APFloatVal; 59198892Srdivacky Constant *ConstantVal; 60201360Srdivacky MDNode *MDNodeVal; 61201360Srdivacky MDString *MDStringVal; 62198892Srdivacky ValID() : APFloatVal(0.0) {} 63198892Srdivacky 64198892Srdivacky bool operator<(const ValID &RHS) const { 65198892Srdivacky if (Kind == t_LocalID || Kind == t_GlobalID) 66198892Srdivacky return UIntVal < RHS.UIntVal; 67198892Srdivacky assert((Kind == t_LocalName || Kind == t_GlobalName) && 68198892Srdivacky "Ordering not defined for this ValID kind yet"); 69198892Srdivacky return StrVal < RHS.StrVal; 70198892Srdivacky } 71198892Srdivacky }; 72198892Srdivacky 73193323Sed class LLParser { 74193323Sed public: 75193323Sed typedef LLLexer::LocTy LocTy; 76193323Sed private: 77207618Srdivacky LLVMContext &Context; 78193323Sed LLLexer Lex; 79193323Sed Module *M; 80206083Srdivacky 81206083Srdivacky // Instruction metadata resolution. Each instruction can have a list of 82206083Srdivacky // MDRef info associated with them. 83206083Srdivacky struct MDRef { 84206083Srdivacky SMLoc Loc; 85206083Srdivacky unsigned MDKind, MDSlot; 86206083Srdivacky }; 87206083Srdivacky DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata; 88193323Sed 89193323Sed // Type resolution handling data structures. 90193323Sed std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes; 91193323Sed std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs; 92193323Sed std::vector<PATypeHolder> NumberedTypes; 93201360Srdivacky std::vector<TrackingVH<MDNode> > NumberedMetadata; 94201360Srdivacky std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes; 95193323Sed struct UpRefRecord { 96193323Sed /// Loc - This is the location of the upref. 97193323Sed LocTy Loc; 98193323Sed 99193323Sed /// NestingLevel - The number of nesting levels that need to be popped 100193323Sed /// before this type is resolved. 101193323Sed unsigned NestingLevel; 102193323Sed 103193323Sed /// LastContainedTy - This is the type at the current binding level for 104193323Sed /// the type. Every time we reduce the nesting level, this gets updated. 105193323Sed const Type *LastContainedTy; 106193323Sed 107193323Sed /// UpRefTy - This is the actual opaque type that the upreference is 108193323Sed /// represented with. 109193323Sed OpaqueType *UpRefTy; 110193323Sed 111193323Sed UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy) 112193323Sed : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy), 113193323Sed UpRefTy(URTy) {} 114193323Sed }; 115193323Sed std::vector<UpRefRecord> UpRefs; 116193323Sed 117193323Sed // Global Value reference information. 118193323Sed std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 119193323Sed std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 120193323Sed std::vector<GlobalValue*> NumberedVals; 121198892Srdivacky 122198892Srdivacky // References to blockaddress. The key is the function ValID, the value is 123198892Srdivacky // a list of references to blocks in that function. 124198892Srdivacky std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > > 125198892Srdivacky ForwardRefBlockAddresses; 126198892Srdivacky 127198892Srdivacky Function *MallocF; 128193323Sed public: 129195340Sed LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : 130198396Srdivacky Context(m->getContext()), Lex(F, SM, Err, m->getContext()), 131198396Srdivacky M(m), MallocF(NULL) {} 132193323Sed bool Run(); 133193323Sed 134195340Sed LLVMContext& getContext() { return Context; } 135195340Sed 136193323Sed private: 137193323Sed 138193323Sed bool Error(LocTy L, const std::string &Msg) const { 139193323Sed return Lex.Error(L, Msg); 140193323Sed } 141193323Sed bool TokError(const std::string &Msg) const { 142193323Sed return Error(Lex.getLoc(), Msg); 143193323Sed } 144193323Sed 145193323Sed /// GetGlobalVal - Get a value with the specified name or ID, creating a 146193323Sed /// forward reference record if needed. This can return null if the value 147193323Sed /// exists but does not have the right type. 148193323Sed GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc); 149193323Sed GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc); 150193323Sed 151193323Sed // Helper Routines. 152193323Sed bool ParseToken(lltok::Kind T, const char *ErrMsg); 153193323Sed bool EatIfPresent(lltok::Kind T) { 154193323Sed if (Lex.getKind() != T) return false; 155193323Sed Lex.Lex(); 156193323Sed return true; 157193323Sed } 158193323Sed bool ParseOptionalToken(lltok::Kind T, bool &Present) { 159193323Sed if (Lex.getKind() != T) { 160193323Sed Present = false; 161193323Sed } else { 162193323Sed Lex.Lex(); 163193323Sed Present = true; 164193323Sed } 165193323Sed return false; 166193323Sed } 167193323Sed bool ParseStringConstant(std::string &Result); 168193323Sed bool ParseUInt32(unsigned &Val); 169193323Sed bool ParseUInt32(unsigned &Val, LocTy &Loc) { 170193323Sed Loc = Lex.getLoc(); 171193323Sed return ParseUInt32(Val); 172193323Sed } 173193323Sed bool ParseOptionalAddrSpace(unsigned &AddrSpace); 174193323Sed bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind); 175193323Sed bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); 176193323Sed bool ParseOptionalLinkage(unsigned &Linkage) { 177193323Sed bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); 178193323Sed } 179193323Sed bool ParseOptionalVisibility(unsigned &Visibility); 180198090Srdivacky bool ParseOptionalCallingConv(CallingConv::ID &CC); 181193323Sed bool ParseOptionalAlignment(unsigned &Alignment); 182203954Srdivacky bool ParseOptionalStackAlignment(unsigned &Alignment); 183206083Srdivacky bool ParseInstructionMetadata(Instruction *Inst); 184201360Srdivacky bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma); 185201360Srdivacky bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma); 186201360Srdivacky bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 187201360Srdivacky bool AteExtraComma; 188201360Srdivacky if (ParseIndexList(Indices, AteExtraComma)) return true; 189201360Srdivacky if (AteExtraComma) 190201360Srdivacky return TokError("expected index"); 191201360Srdivacky return false; 192201360Srdivacky } 193193323Sed 194193323Sed // Top-Level Entities 195193323Sed bool ParseTopLevelEntities(); 196193323Sed bool ValidateEndOfModule(); 197193323Sed bool ParseTargetDefinition(); 198193323Sed bool ParseDepLibs(); 199193323Sed bool ParseModuleAsm(); 200193323Sed bool ParseUnnamedType(); 201193323Sed bool ParseNamedType(); 202193323Sed bool ParseDeclare(); 203193323Sed bool ParseDefine(); 204193323Sed 205193323Sed bool ParseGlobalType(bool &IsConstant); 206198090Srdivacky bool ParseUnnamedGlobal(); 207193323Sed bool ParseNamedGlobal(); 208193323Sed bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, 209193323Sed bool HasLinkage, unsigned Visibility); 210193323Sed bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); 211195340Sed bool ParseStandaloneMetadata(); 212198090Srdivacky bool ParseNamedMetadata(); 213201360Srdivacky bool ParseMDString(MDString *&Result); 214201360Srdivacky bool ParseMDNodeID(MDNode *&Result); 215206083Srdivacky bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo); 216193323Sed 217193323Sed // Type Parsing. 218193323Sed bool ParseType(PATypeHolder &Result, bool AllowVoid = false); 219193323Sed bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) { 220193323Sed Loc = Lex.getLoc(); 221193323Sed return ParseType(Result, AllowVoid); 222193323Sed } 223193323Sed bool ParseTypeRec(PATypeHolder &H); 224193323Sed bool ParseStructType(PATypeHolder &H, bool Packed); 225203954Srdivacky bool ParseUnionType(PATypeHolder &H); 226193323Sed bool ParseArrayVectorType(PATypeHolder &H, bool isVector); 227193323Sed bool ParseFunctionType(PATypeHolder &Result); 228193323Sed PATypeHolder HandleUpRefs(const Type *Ty); 229193323Sed 230193323Sed // Function Semantic Analysis. 231193323Sed class PerFunctionState { 232193323Sed LLParser &P; 233193323Sed Function &F; 234193323Sed std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 235193323Sed std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 236193323Sed std::vector<Value*> NumberedVals; 237198892Srdivacky 238198892Srdivacky /// FunctionNumber - If this is an unnamed function, this is the slot 239198892Srdivacky /// number of it, otherwise it is -1. 240198892Srdivacky int FunctionNumber; 241193323Sed public: 242198892Srdivacky PerFunctionState(LLParser &p, Function &f, int FunctionNumber); 243193323Sed ~PerFunctionState(); 244193323Sed 245193323Sed Function &getFunction() const { return F; } 246193323Sed 247198892Srdivacky bool FinishFunction(); 248193323Sed 249193323Sed /// GetVal - Get a value with the specified name or ID, creating a 250193323Sed /// forward reference record if needed. This can return null if the value 251193323Sed /// exists but does not have the right type. 252193323Sed Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc); 253193323Sed Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc); 254193323Sed 255193323Sed /// SetInstName - After an instruction is parsed and inserted into its 256193323Sed /// basic block, this installs its name. 257193323Sed bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 258193323Sed Instruction *Inst); 259193323Sed 260193323Sed /// GetBB - Get a basic block with the specified name or ID, creating a 261193323Sed /// forward reference record if needed. This can return null if the value 262193323Sed /// is not a BasicBlock. 263193323Sed BasicBlock *GetBB(const std::string &Name, LocTy Loc); 264193323Sed BasicBlock *GetBB(unsigned ID, LocTy Loc); 265193323Sed 266193323Sed /// DefineBB - Define the specified basic block, which is either named or 267193323Sed /// unnamed. If there is an error, this returns null otherwise it returns 268193323Sed /// the block being defined. 269193323Sed BasicBlock *DefineBB(const std::string &Name, LocTy Loc); 270193323Sed }; 271193323Sed 272193323Sed bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, 273202375Srdivacky PerFunctionState *PFS); 274193323Sed 275193323Sed bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS); 276193323Sed bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc, 277193323Sed PerFunctionState &PFS) { 278193323Sed Loc = Lex.getLoc(); 279193323Sed return ParseValue(Ty, V, PFS); 280193323Sed } 281193323Sed 282193323Sed bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS); 283193323Sed bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 284193323Sed Loc = Lex.getLoc(); 285193323Sed return ParseTypeAndValue(V, PFS); 286193323Sed } 287198892Srdivacky bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 288198892Srdivacky PerFunctionState &PFS); 289198892Srdivacky bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { 290198892Srdivacky LocTy Loc; 291198892Srdivacky return ParseTypeAndBasicBlock(BB, Loc, PFS); 292198892Srdivacky } 293200581Srdivacky 294203954Srdivacky bool ParseUnionValue(const UnionType* utype, ValID &ID, Value *&V); 295203954Srdivacky 296193323Sed struct ParamInfo { 297193323Sed LocTy Loc; 298193323Sed Value *V; 299193323Sed unsigned Attrs; 300193323Sed ParamInfo(LocTy loc, Value *v, unsigned attrs) 301193323Sed : Loc(loc), V(v), Attrs(attrs) {} 302193323Sed }; 303193323Sed bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 304193323Sed PerFunctionState &PFS); 305193323Sed 306202375Srdivacky // Constant Parsing. 307202375Srdivacky bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL); 308202375Srdivacky bool ParseGlobalValue(const Type *Ty, Constant *&V); 309202375Srdivacky bool ParseGlobalTypeAndValue(Constant *&V); 310202375Srdivacky bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts); 311202375Srdivacky bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS); 312202375Srdivacky 313193323Sed // Function Parsing. 314193323Sed struct ArgInfo { 315193323Sed LocTy Loc; 316193323Sed PATypeHolder Type; 317193323Sed unsigned Attrs; 318193323Sed std::string Name; 319193323Sed ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N) 320193323Sed : Loc(L), Type(Ty), Attrs(Attr), Name(N) {} 321193323Sed }; 322193323Sed bool ParseArgumentList(std::vector<ArgInfo> &ArgList, 323193323Sed bool &isVarArg, bool inType); 324193323Sed bool ParseFunctionHeader(Function *&Fn, bool isDefine); 325193323Sed bool ParseFunctionBody(Function &Fn); 326193323Sed bool ParseBasicBlock(PerFunctionState &PFS); 327193323Sed 328201360Srdivacky // Instruction Parsing. Each instruction parsing routine can return with a 329201360Srdivacky // normal result, an error result, or return having eaten an extra comma. 330201360Srdivacky enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; 331201360Srdivacky int ParseInstruction(Instruction *&Inst, BasicBlock *BB, 332201360Srdivacky PerFunctionState &PFS); 333193323Sed bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); 334193323Sed 335201360Srdivacky int ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 336193323Sed bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 337193323Sed bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 338198892Srdivacky bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); 339193323Sed bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 340193323Sed 341193323Sed bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, 342193323Sed unsigned OperandType); 343193323Sed bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 344193323Sed bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 345193323Sed bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 346193323Sed bool ParseSelect(Instruction *&I, PerFunctionState &PFS); 347193323Sed bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); 348193323Sed bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); 349193323Sed bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); 350193323Sed bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); 351201360Srdivacky int ParsePHI(Instruction *&I, PerFunctionState &PFS); 352193323Sed bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail); 353201360Srdivacky int ParseAlloc(Instruction *&I, PerFunctionState &PFS, 354198396Srdivacky BasicBlock *BB = 0, bool isAlloca = true); 355198892Srdivacky bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB); 356201360Srdivacky int ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 357201360Srdivacky int ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 358193323Sed bool ParseGetResult(Instruction *&I, PerFunctionState &PFS); 359201360Srdivacky int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); 360201360Srdivacky int ParseExtractValue(Instruction *&I, PerFunctionState &PFS); 361201360Srdivacky int ParseInsertValue(Instruction *&I, PerFunctionState &PFS); 362198892Srdivacky 363198892Srdivacky bool ResolveForwardRefBlockAddresses(Function *TheFn, 364198892Srdivacky std::vector<std::pair<ValID, GlobalValue*> > &Refs, 365198892Srdivacky PerFunctionState *PFS); 366193323Sed }; 367193323Sed} // End llvm namespace 368193323Sed 369193323Sed#endif 370