LLParser.h revision 198396
1//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the parser class for .ll files. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_ASMPARSER_LLPARSER_H 15#define LLVM_ASMPARSER_LLPARSER_H 16 17#include "LLLexer.h" 18#include "llvm/Module.h" 19#include "llvm/Type.h" 20#include <map> 21 22namespace llvm { 23 class Module; 24 class OpaqueType; 25 class Function; 26 class Value; 27 class BasicBlock; 28 class Instruction; 29 class Constant; 30 class GlobalValue; 31 class MetadataBase; 32 class MDString; 33 class MDNode; 34 struct ValID; 35 36 class LLParser { 37 public: 38 typedef LLLexer::LocTy LocTy; 39 private: 40 LLVMContext& Context; 41 LLLexer Lex; 42 Module *M; 43 44 // Type resolution handling data structures. 45 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes; 46 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs; 47 std::vector<PATypeHolder> NumberedTypes; 48 /// MetadataCache - This map keeps track of parsed metadata constants. 49 std::map<unsigned, MetadataBase *> MetadataCache; 50 std::map<unsigned, std::pair<MetadataBase *, LocTy> > ForwardRefMDNodes; 51 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst; 52 struct UpRefRecord { 53 /// Loc - This is the location of the upref. 54 LocTy Loc; 55 56 /// NestingLevel - The number of nesting levels that need to be popped 57 /// before this type is resolved. 58 unsigned NestingLevel; 59 60 /// LastContainedTy - This is the type at the current binding level for 61 /// the type. Every time we reduce the nesting level, this gets updated. 62 const Type *LastContainedTy; 63 64 /// UpRefTy - This is the actual opaque type that the upreference is 65 /// represented with. 66 OpaqueType *UpRefTy; 67 68 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy) 69 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy), 70 UpRefTy(URTy) {} 71 }; 72 std::vector<UpRefRecord> UpRefs; 73 74 // Global Value reference information. 75 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 76 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 77 std::vector<GlobalValue*> NumberedVals; 78 Function* MallocF; 79 public: 80 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : 81 Context(m->getContext()), Lex(F, SM, Err, m->getContext()), 82 M(m), MallocF(NULL) {} 83 bool Run(); 84 85 LLVMContext& getContext() { return Context; } 86 87 private: 88 89 bool Error(LocTy L, const std::string &Msg) const { 90 return Lex.Error(L, Msg); 91 } 92 bool TokError(const std::string &Msg) const { 93 return Error(Lex.getLoc(), Msg); 94 } 95 96 /// GetGlobalVal - Get a value with the specified name or ID, creating a 97 /// forward reference record if needed. This can return null if the value 98 /// exists but does not have the right type. 99 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc); 100 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc); 101 102 // Helper Routines. 103 bool ParseToken(lltok::Kind T, const char *ErrMsg); 104 bool EatIfPresent(lltok::Kind T) { 105 if (Lex.getKind() != T) return false; 106 Lex.Lex(); 107 return true; 108 } 109 bool ParseOptionalToken(lltok::Kind T, bool &Present) { 110 if (Lex.getKind() != T) { 111 Present = false; 112 } else { 113 Lex.Lex(); 114 Present = true; 115 } 116 return false; 117 } 118 bool ParseStringConstant(std::string &Result); 119 bool ParseUInt32(unsigned &Val); 120 bool ParseUInt32(unsigned &Val, LocTy &Loc) { 121 Loc = Lex.getLoc(); 122 return ParseUInt32(Val); 123 } 124 bool ParseOptionalAddrSpace(unsigned &AddrSpace); 125 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind); 126 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); 127 bool ParseOptionalLinkage(unsigned &Linkage) { 128 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); 129 } 130 bool ParseOptionalVisibility(unsigned &Visibility); 131 bool ParseOptionalCallingConv(CallingConv::ID &CC); 132 bool ParseOptionalAlignment(unsigned &Alignment); 133 bool ParseOptionalCustomMetadata(); 134 bool ParseOptionalInfo(unsigned &Alignment); 135 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices); 136 137 // Top-Level Entities 138 bool ParseTopLevelEntities(); 139 bool ValidateEndOfModule(); 140 bool ParseTargetDefinition(); 141 bool ParseDepLibs(); 142 bool ParseModuleAsm(); 143 bool ParseUnnamedType(); 144 bool ParseNamedType(); 145 bool ParseDeclare(); 146 bool ParseDefine(); 147 148 bool ParseGlobalType(bool &IsConstant); 149 bool ParseUnnamedGlobal(); 150 bool ParseNamedGlobal(); 151 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, 152 bool HasLinkage, unsigned Visibility); 153 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); 154 bool ParseStandaloneMetadata(); 155 bool ParseNamedMetadata(); 156 bool ParseMDString(MetadataBase *&S); 157 bool ParseMDNode(MetadataBase *&N); 158 159 // Type Parsing. 160 bool ParseType(PATypeHolder &Result, bool AllowVoid = false); 161 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) { 162 Loc = Lex.getLoc(); 163 return ParseType(Result, AllowVoid); 164 } 165 bool ParseTypeRec(PATypeHolder &H); 166 bool ParseStructType(PATypeHolder &H, bool Packed); 167 bool ParseArrayVectorType(PATypeHolder &H, bool isVector); 168 bool ParseFunctionType(PATypeHolder &Result); 169 PATypeHolder HandleUpRefs(const Type *Ty); 170 171 // Constants. 172 bool ParseValID(ValID &ID); 173 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V); 174 bool ParseGlobalValue(const Type *Ty, Constant *&V); 175 bool ParseGlobalTypeAndValue(Constant *&V); 176 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts); 177 bool ParseMDNodeVector(SmallVectorImpl<Value*> &); 178 179 180 // Function Semantic Analysis. 181 class PerFunctionState { 182 LLParser &P; 183 Function &F; 184 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 185 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 186 std::vector<Value*> NumberedVals; 187 public: 188 PerFunctionState(LLParser &p, Function &f); 189 ~PerFunctionState(); 190 191 Function &getFunction() const { return F; } 192 193 bool VerifyFunctionComplete(); 194 195 /// GetVal - Get a value with the specified name or ID, creating a 196 /// forward reference record if needed. This can return null if the value 197 /// exists but does not have the right type. 198 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc); 199 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc); 200 201 /// SetInstName - After an instruction is parsed and inserted into its 202 /// basic block, this installs its name. 203 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 204 Instruction *Inst); 205 206 /// GetBB - Get a basic block with the specified name or ID, creating a 207 /// forward reference record if needed. This can return null if the value 208 /// is not a BasicBlock. 209 BasicBlock *GetBB(const std::string &Name, LocTy Loc); 210 BasicBlock *GetBB(unsigned ID, LocTy Loc); 211 212 /// DefineBB - Define the specified basic block, which is either named or 213 /// unnamed. If there is an error, this returns null otherwise it returns 214 /// the block being defined. 215 BasicBlock *DefineBB(const std::string &Name, LocTy Loc); 216 }; 217 218 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V, 219 PerFunctionState &PFS); 220 221 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS); 222 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc, 223 PerFunctionState &PFS) { 224 Loc = Lex.getLoc(); 225 return ParseValue(Ty, V, PFS); 226 } 227 228 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS); 229 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 230 Loc = Lex.getLoc(); 231 return ParseTypeAndValue(V, PFS); 232 } 233 234 struct ParamInfo { 235 LocTy Loc; 236 Value *V; 237 unsigned Attrs; 238 ParamInfo(LocTy loc, Value *v, unsigned attrs) 239 : Loc(loc), V(v), Attrs(attrs) {} 240 }; 241 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 242 PerFunctionState &PFS); 243 244 // Function Parsing. 245 struct ArgInfo { 246 LocTy Loc; 247 PATypeHolder Type; 248 unsigned Attrs; 249 std::string Name; 250 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N) 251 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {} 252 }; 253 bool ParseArgumentList(std::vector<ArgInfo> &ArgList, 254 bool &isVarArg, bool inType); 255 bool ParseFunctionHeader(Function *&Fn, bool isDefine); 256 bool ParseFunctionBody(Function &Fn); 257 bool ParseBasicBlock(PerFunctionState &PFS); 258 259 // Instruction Parsing. 260 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB, 261 PerFunctionState &PFS); 262 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); 263 264 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 265 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 266 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 267 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 268 269 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, 270 unsigned OperandType); 271 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 272 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 273 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 274 bool ParseSelect(Instruction *&I, PerFunctionState &PFS); 275 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); 276 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); 277 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); 278 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); 279 bool ParsePHI(Instruction *&I, PerFunctionState &PFS); 280 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail); 281 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, 282 BasicBlock *BB = 0, bool isAlloca = true); 283 bool ParseFree(Instruction *&I, PerFunctionState &PFS); 284 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 285 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 286 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS); 287 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); 288 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS); 289 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS); 290 }; 291} // End llvm namespace 292 293#endif 294