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 "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/Support/ValueHandle.h" 23 #include <map> 24 25 namespace llvm { 26 class Module; 27 class OpaqueType; 28 class Function; 29 class Value; 30 class BasicBlock; 31 class Instruction; 32 class Constant; 33 class GlobalValue; 34 class MDString; 35 class MDNode; 36 class StructType; 37 38 /// ValID - Represents a reference of a definition of some sort with no type. 39 /// There are several cases where we have to parse the value but where the 40 /// type can depend on later context. This may either be a numeric reference 41 /// or a symbolic (%var) reference. This is just a discriminated union. 42 struct ValID { 43 enum { 44 t_LocalID, t_GlobalID, // ID in UIntVal. 45 t_LocalName, t_GlobalName, // Name in StrVal. 46 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal. 47 t_Null, t_Undef, t_Zero, // No value. 48 t_EmptyArray, // No value: [] 49 t_Constant, // Value in ConstantVal. 50 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal. 51 t_MDNode, // Value in MDNodeVal. 52 t_MDString, // Value in MDStringVal. 53 t_ConstantStruct, // Value in ConstantStructElts. 54 t_PackedConstantStruct // Value in ConstantStructElts. 55 } Kind; 56 57 LLLexer::LocTy Loc; 58 unsigned UIntVal; 59 std::string StrVal, StrVal2; 60 APSInt APSIntVal; 61 APFloat APFloatVal; 62 Constant *ConstantVal; 63 MDNode *MDNodeVal; 64 MDString *MDStringVal; 65 Constant **ConstantStructElts; 66 ValIDValID67 ValID() : Kind(t_LocalID), APFloatVal(0.0) {} ~ValIDValID68 ~ValID() { 69 if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) 70 delete [] ConstantStructElts; 71 } 72 73 bool operator<(const ValID &RHS) const { 74 if (Kind == t_LocalID || Kind == t_GlobalID) 75 return UIntVal < RHS.UIntVal; 76 assert((Kind == t_LocalName || Kind == t_GlobalName || 77 Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) && 78 "Ordering not defined for this ValID kind yet"); 79 return StrVal < RHS.StrVal; 80 } 81 }; 82 83 class LLParser { 84 public: 85 typedef LLLexer::LocTy LocTy; 86 private: 87 LLVMContext &Context; 88 LLLexer Lex; 89 Module *M; 90 91 // Instruction metadata resolution. Each instruction can have a list of 92 // MDRef info associated with them. 93 // 94 // The simpler approach of just creating temporary MDNodes and then calling 95 // RAUW on them when the definition is processed doesn't work because some 96 // instruction metadata kinds, such as dbg, get stored in the IR in an 97 // "optimized" format which doesn't participate in the normal value use 98 // lists. This means that RAUW doesn't work, even on temporary MDNodes 99 // which otherwise support RAUW. Instead, we defer resolving MDNode 100 // references until the definitions have been processed. 101 struct MDRef { 102 SMLoc Loc; 103 unsigned MDKind, MDSlot; 104 }; 105 DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata; 106 107 // Type resolution handling data structures. The location is set when we 108 // have processed a use of the type but not a definition yet. 109 StringMap<std::pair<Type*, LocTy> > NamedTypes; 110 std::vector<std::pair<Type*, LocTy> > NumberedTypes; 111 112 std::vector<TrackingVH<MDNode> > NumberedMetadata; 113 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes; 114 115 // Global Value reference information. 116 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals; 117 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs; 118 std::vector<GlobalValue*> NumberedVals; 119 120 // References to blockaddress. The key is the function ValID, the value is 121 // a list of references to blocks in that function. 122 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > > 123 ForwardRefBlockAddresses; 124 125 public: LLParser(MemoryBuffer * F,SourceMgr & SM,SMDiagnostic & Err,Module * m)126 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : 127 Context(m->getContext()), Lex(F, SM, Err, m->getContext()), 128 M(m) {} 129 bool Run(); 130 getContext()131 LLVMContext &getContext() { return Context; } 132 133 private: 134 Error(LocTy L,const Twine & Msg)135 bool Error(LocTy L, const Twine &Msg) const { 136 return Lex.Error(L, Msg); 137 } TokError(const Twine & Msg)138 bool TokError(const Twine &Msg) const { 139 return Error(Lex.getLoc(), Msg); 140 } 141 142 /// GetGlobalVal - Get a value with the specified name or ID, creating a 143 /// forward reference record if needed. This can return null if the value 144 /// exists but does not have the right type. 145 GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc); 146 GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc); 147 148 // Helper Routines. 149 bool ParseToken(lltok::Kind T, const char *ErrMsg); EatIfPresent(lltok::Kind T)150 bool EatIfPresent(lltok::Kind T) { 151 if (Lex.getKind() != T) return false; 152 Lex.Lex(); 153 return true; 154 } 155 bool ParseOptionalToken(lltok::Kind T, bool &Present, LocTy *Loc = 0) { 156 if (Lex.getKind() != T) { 157 Present = false; 158 } else { 159 if (Loc) 160 *Loc = Lex.getLoc(); 161 Lex.Lex(); 162 Present = true; 163 } 164 return false; 165 } 166 bool ParseStringConstant(std::string &Result); 167 bool ParseUInt32(unsigned &Val); ParseUInt32(unsigned & Val,LocTy & Loc)168 bool ParseUInt32(unsigned &Val, LocTy &Loc) { 169 Loc = Lex.getLoc(); 170 return ParseUInt32(Val); 171 } 172 bool ParseOptionalAddrSpace(unsigned &AddrSpace); 173 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind); 174 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage); ParseOptionalLinkage(unsigned & Linkage)175 bool ParseOptionalLinkage(unsigned &Linkage) { 176 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage); 177 } 178 bool ParseOptionalVisibility(unsigned &Visibility); 179 bool ParseOptionalCallingConv(CallingConv::ID &CC); 180 bool ParseOptionalAlignment(unsigned &Alignment); 181 bool ParseOptionalStackAlignment(unsigned &Alignment); 182 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma); 183 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma); ParseIndexList(SmallVectorImpl<unsigned> & Indices)184 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) { 185 bool AteExtraComma; 186 if (ParseIndexList(Indices, AteExtraComma)) return true; 187 if (AteExtraComma) 188 return TokError("expected index"); 189 return false; 190 } 191 192 // Top-Level Entities 193 bool ParseTopLevelEntities(); 194 bool ValidateEndOfModule(); 195 bool ParseTargetDefinition(); 196 bool ParseDepLibs(); 197 bool ParseModuleAsm(); 198 bool ParseUnnamedType(); 199 bool ParseNamedType(); 200 bool ParseDeclare(); 201 bool ParseDefine(); 202 203 bool ParseGlobalType(bool &IsConstant); 204 bool ParseUnnamedGlobal(); 205 bool ParseNamedGlobal(); 206 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage, 207 bool HasLinkage, unsigned Visibility); 208 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); 209 bool ParseStandaloneMetadata(); 210 bool ParseNamedMetadata(); 211 bool ParseMDString(MDString *&Result); 212 bool ParseMDNodeID(MDNode *&Result); 213 bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo); 214 215 // Type Parsing. 216 bool ParseType(Type *&Result, bool AllowVoid = false); 217 bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) { 218 Loc = Lex.getLoc(); 219 return ParseType(Result, AllowVoid); 220 } 221 bool ParseAnonStructType(Type *&Result, bool Packed); 222 bool ParseStructBody(SmallVectorImpl<Type*> &Body); 223 bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name, 224 std::pair<Type*, LocTy> &Entry, 225 Type *&ResultTy); 226 227 bool ParseArrayVectorType(Type *&Result, bool isVector); 228 bool ParseFunctionType(Type *&Result); 229 230 // Function Semantic Analysis. 231 class PerFunctionState { 232 LLParser &P; 233 Function &F; 234 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals; 235 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs; 236 std::vector<Value*> NumberedVals; 237 238 /// FunctionNumber - If this is an unnamed function, this is the slot 239 /// number of it, otherwise it is -1. 240 int FunctionNumber; 241 public: 242 PerFunctionState(LLParser &p, Function &f, int FunctionNumber); 243 ~PerFunctionState(); 244 getFunction()245 Function &getFunction() const { return F; } 246 247 bool FinishFunction(); 248 249 /// GetVal - Get a value with the specified name or ID, creating a 250 /// forward reference record if needed. This can return null if the value 251 /// exists but does not have the right type. 252 Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc); 253 Value *GetVal(unsigned ID, Type *Ty, LocTy Loc); 254 255 /// SetInstName - After an instruction is parsed and inserted into its 256 /// basic block, this installs its name. 257 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc, 258 Instruction *Inst); 259 260 /// GetBB - Get a basic block with the specified name or ID, creating a 261 /// forward reference record if needed. This can return null if the value 262 /// is not a BasicBlock. 263 BasicBlock *GetBB(const std::string &Name, LocTy Loc); 264 BasicBlock *GetBB(unsigned ID, LocTy Loc); 265 266 /// DefineBB - Define the specified basic block, which is either named or 267 /// unnamed. If there is an error, this returns null otherwise it returns 268 /// the block being defined. 269 BasicBlock *DefineBB(const std::string &Name, LocTy Loc); 270 }; 271 272 bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V, 273 PerFunctionState *PFS); 274 275 bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS); ParseValue(Type * Ty,Value * & V,PerFunctionState & PFS)276 bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) { 277 return ParseValue(Ty, V, &PFS); 278 } ParseValue(Type * Ty,Value * & V,LocTy & Loc,PerFunctionState & PFS)279 bool ParseValue(Type *Ty, Value *&V, LocTy &Loc, 280 PerFunctionState &PFS) { 281 Loc = Lex.getLoc(); 282 return ParseValue(Ty, V, &PFS); 283 } 284 285 bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS); ParseTypeAndValue(Value * & V,PerFunctionState & PFS)286 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) { 287 return ParseTypeAndValue(V, &PFS); 288 } ParseTypeAndValue(Value * & V,LocTy & Loc,PerFunctionState & PFS)289 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) { 290 Loc = Lex.getLoc(); 291 return ParseTypeAndValue(V, PFS); 292 } 293 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc, 294 PerFunctionState &PFS); ParseTypeAndBasicBlock(BasicBlock * & BB,PerFunctionState & PFS)295 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) { 296 LocTy Loc; 297 return ParseTypeAndBasicBlock(BB, Loc, PFS); 298 } 299 300 301 struct ParamInfo { 302 LocTy Loc; 303 Value *V; 304 unsigned Attrs; ParamInfoParamInfo305 ParamInfo(LocTy loc, Value *v, unsigned attrs) 306 : Loc(loc), V(v), Attrs(attrs) {} 307 }; 308 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList, 309 PerFunctionState &PFS); 310 311 // Constant Parsing. 312 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL); 313 bool ParseGlobalValue(Type *Ty, Constant *&V); 314 bool ParseGlobalTypeAndValue(Constant *&V); 315 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts); 316 bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS); 317 bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS); 318 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS); 319 bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS); 320 321 // Function Parsing. 322 struct ArgInfo { 323 LocTy Loc; 324 Type *Ty; 325 unsigned Attrs; 326 std::string Name; ArgInfoArgInfo327 ArgInfo(LocTy L, Type *ty, unsigned Attr, const std::string &N) 328 : Loc(L), Ty(ty), Attrs(Attr), Name(N) {} 329 }; 330 bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg); 331 bool ParseFunctionHeader(Function *&Fn, bool isDefine); 332 bool ParseFunctionBody(Function &Fn); 333 bool ParseBasicBlock(PerFunctionState &PFS); 334 335 // Instruction Parsing. Each instruction parsing routine can return with a 336 // normal result, an error result, or return having eaten an extra comma. 337 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 }; 338 int ParseInstruction(Instruction *&Inst, BasicBlock *BB, 339 PerFunctionState &PFS); 340 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc); 341 342 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS); 343 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS); 344 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS); 345 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS); 346 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS); 347 348 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc, 349 unsigned OperandType); 350 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 351 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 352 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc); 353 bool ParseSelect(Instruction *&I, PerFunctionState &PFS); 354 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS); 355 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS); 356 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS); 357 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS); 358 int ParsePHI(Instruction *&I, PerFunctionState &PFS); 359 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail); 360 int ParseAlloc(Instruction *&I, PerFunctionState &PFS); 361 int ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 362 int ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile); 363 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS); 364 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS); 365 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS); 366 367 bool ResolveForwardRefBlockAddresses(Function *TheFn, 368 std::vector<std::pair<ValID, GlobalValue*> > &Refs, 369 PerFunctionState *PFS); 370 }; 371 } // End llvm namespace 372 373 #endif 374