LLVM API Documentation
00001 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the parser class for .ll files. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_ASMPARSER_LLPARSER_H 00015 #define LLVM_ASMPARSER_LLPARSER_H 00016 00017 #include "LLLexer.h" 00018 #include "llvm/Type.h" 00019 #include <map> 00020 00021 namespace llvm { 00022 class Module; 00023 class OpaqueType; 00024 class Function; 00025 class Value; 00026 class BasicBlock; 00027 class Instruction; 00028 class Constant; 00029 class GlobalValue; 00030 struct ValID; 00031 00032 class LLParser { 00033 public: 00034 typedef LLLexer::LocTy LocTy; 00035 private: 00036 00037 LLLexer Lex; 00038 Module *M; 00039 00040 // Type resolution handling data structures. 00041 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes; 00042 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs; 00043 std::vector<PATypeHolder> NumberedTypes; 00044 00045 struct UpRefRecord { 00046 /// Loc - This is the location of the upref. 00047 LocTy Loc; 00048 00049 /// NestingLevel - The number of nesting levels that need to be popped 00050 /// before this type is resolved. 00051 unsigned NestingLevel; 00052 00053 /// LastContainedTy - This is the type at the current binding level for 00054 /// the type. Every time we reduce the nesting level, this gets updated. 00055 const Type *LastContainedTy; 00056 00057 /// UpRefTy - This is the actual opaque type that the upreference is 00058 /// represented with. 00059 OpaqueType *UpRefTy; 00060 00061 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy) 00062 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy), 00063 UpRefTy(URTy) {} 00064 }; 00065 std::vector<UpRefRecord> UpRefs; 00066 00067 // Global Value reference information. 00068 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 00069 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 00070 std::vector<GlobalValue*> NumberedVals; 00071 public: 00072 LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {} 00073 bool Run(); 00074 00075 private: 00076 00077 bool Error(LocTy L, const std::string &Msg) const { 00078 return Lex.Error(L, Msg); 00079 } 00080 bool TokError(const std::string &Msg) const { 00081 return Error(Lex.getLoc(), Msg); 00082 } 00083 00084 /// GetGlobalVal - Get a value with the specified name or ID, creating a 00085 /// forward reference record if needed. This can return null if the value 00086 /// exists but does not have the right type. 00087 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc); 00088 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc); 00089 00090 // Helper Routines. 00091 bool ParseToken(lltok::Kind T, const char *ErrMsg); 00092 bool EatIfPresent(lltok::Kind T) { 00093 if (Lex.getKind() != T) return false; 00094 Lex.Lex(); 00095 return true; 00096 } 00097 bool ParseOptionalToken(lltok::Kind T, bool &Present) { 00098 if (Lex.getKind() != T) { 00099 Present = false; 00100 } else { 00101 Lex.Lex(); 00102 Present = true; 00103 } 00104 return false; 00105 } 00106 bool ParseStringConstant(std::string &Result); 00107 bool ParseUInt32(unsigned &Val); 00108 bool ParseUInt32(unsigned &Val, LocTy &Loc) { 00109 Loc = Lex.getLoc(); 00110 return ParseUInt32(Val); 00111 } 00112 bool ParseOptionalAddrSpace(unsigned &AddrSpace); 00113 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind); 00114 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); 00115 bool ParseOptionalLinkage(unsigned &Linkage) { 00116 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); 00117 } 00118 bool ParseOptionalVisibility(unsigned &Visibility); 00119 bool ParseOptionalCallingConv(unsigned &CC); 00120 bool ParseOptionalAlignment(unsigned &Alignment); 00121 bool ParseOptionalCommaAlignment(unsigned &Alignment); 00122 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices); 00123 00124 // Top-Level Entities 00125 bool ParseTopLevelEntities(); 00126 bool ValidateEndOfModule(); 00127 bool ParseTargetDefinition(); 00128 bool ParseDepLibs(); 00129 bool ParseModuleAsm(); 00130 bool ParseUnnamedType(); 00131 bool ParseNamedType(); 00132 bool ParseDeclare(); 00133 bool ParseDefine(); 00134 00135 bool ParseGlobalType(bool &IsConstant); 00136 bool ParseNamedGlobal(); 00137 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, 00138 bool HasLinkage, unsigned Visibility); 00139 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); 00140 00141 // Type Parsing. 00142 bool ParseType(PATypeHolder &Result); 00143 bool ParseType(PATypeHolder &Result, LocTy &Loc) { 00144 Loc = Lex.getLoc(); 00145 return ParseType(Result); 00146 } 00147 bool ParseTypeRec(PATypeHolder &H); 00148 bool ParseStructType(PATypeHolder &H, bool Packed); 00149 bool ParseArrayVectorType(PATypeHolder &H, bool isVector); 00150 bool ParseFunctionType(PATypeHolder &Result); 00151 PATypeHolder HandleUpRefs(const Type *Ty); 00152 00153 // Constants. 00154 bool ParseValID(ValID &ID); 00155 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V); 00156 bool ParseGlobalValue(const Type *Ty, Constant *&V); 00157 bool ParseGlobalTypeAndValue(Constant *&V); 00158 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts); 00159 00160 00161 // Function Semantic Analysis. 00162 class PerFunctionState { 00163 LLParser &P; 00164 Function &F; 00165 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 00166 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 00167 std::vector<Value*> NumberedVals; 00168 public: 00169 PerFunctionState(LLParser &p, Function &f); 00170 ~PerFunctionState(); 00171 00172 Function &getFunction() const { return F; } 00173 00174 bool VerifyFunctionComplete(); 00175 00176 /// GetVal - Get a value with the specified name or ID, creating a 00177 /// forward reference record if needed. This can return null if the value 00178 /// exists but does not have the right type. 00179 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc); 00180 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc); 00181 00182 /// SetInstName - After an instruction is parsed and inserted into its 00183 /// basic block, this installs its name. 00184 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 00185 Instruction *Inst); 00186 00187 /// GetBB - Get a basic block with the specified name or ID, creating a 00188 /// forward reference record if needed. This can return null if the value 00189 /// is not a BasicBlock. 00190 BasicBlock *GetBB(const std::string &Name, LocTy Loc); 00191 BasicBlock *GetBB(unsigned ID, LocTy Loc); 00192 00193 /// DefineBB - Define the specified basic block, which is either named or 00194 /// unnamed. If there is an error, this returns null otherwise it returns 00195 /// the block being defined. 00196 BasicBlock *DefineBB(const std::string &Name, LocTy Loc); 00197 }; 00198 00199 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, 00200 PerFunctionState &PFS); 00201 00202 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS); 00203 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc, 00204 PerFunctionState &PFS) { 00205 Loc = Lex.getLoc(); 00206 return ParseValue(Ty, V, PFS); 00207 } 00208 00209 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS); 00210 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 00211 Loc = Lex.getLoc(); 00212 return ParseTypeAndValue(V, PFS); 00213 } 00214 00215 struct ParamInfo { 00216 LocTy Loc; 00217 Value *V; 00218 unsigned Attrs; 00219 ParamInfo(LocTy loc, Value *v, unsigned attrs) 00220 : Loc(loc), V(v), Attrs(attrs) {} 00221 }; 00222 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 00223 PerFunctionState &PFS); 00224 00225 // Function Parsing. 00226 struct ArgInfo { 00227 LocTy Loc; 00228 PATypeHolder Type; 00229 unsigned Attrs; 00230 std::string Name; 00231 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N) 00232 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {} 00233 }; 00234 bool ParseArgumentList(std::vector<ArgInfo> &ArgList, 00235 bool &isVarArg, bool inType); 00236 bool ParseFunctionHeader(Function *&Fn, bool isDefine); 00237 bool ParseFunctionBody(Function &Fn); 00238 bool ParseBasicBlock(PerFunctionState &PFS); 00239 00240 // Instruction Parsing. 00241 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB, 00242 PerFunctionState &PFS); 00243 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); 00244 00245 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 00246 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 00247 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 00248 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 00249 00250 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, 00251 unsigned OperandType); 00252 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 00253 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 00254 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 00255 bool ParseSelect(Instruction *&I, PerFunctionState &PFS); 00256 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); 00257 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); 00258 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); 00259 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); 00260 bool ParsePHI(Instruction *&I, PerFunctionState &PFS); 00261 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail); 00262 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 00263 bool ParseFree(Instruction *&I, PerFunctionState &PFS); 00264 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 00265 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 00266 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS); 00267 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); 00268 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS); 00269 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS); 00270 }; 00271 } // End llvm namespace 00272 00273 #endif