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