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