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