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