1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the parser class for .ll files.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ASMPARSER_LLPARSER_H
15 #define LLVM_ASMPARSER_LLPARSER_H
18 #include "llvm/Type.h"
36 typedef LLLexer::LocTy LocTy;
42 // Type resolution handling data structures.
43 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
44 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
45 std::vector<PATypeHolder> NumberedTypes;
46 /// MetadataCache - This map keeps track of parsed metadata constants.
47 std::map<unsigned, Constant *> MetadataCache;
49 /// Loc - This is the location of the upref.
52 /// NestingLevel - The number of nesting levels that need to be popped
53 /// before this type is resolved.
54 unsigned NestingLevel;
56 /// LastContainedTy - This is the type at the current binding level for
57 /// the type. Every time we reduce the nesting level, this gets updated.
58 const Type *LastContainedTy;
60 /// UpRefTy - This is the actual opaque type that the upreference is
64 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
65 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
68 std::vector<UpRefRecord> UpRefs;
70 // Global Value reference information.
71 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
72 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
73 std::vector<GlobalValue*> NumberedVals;
75 LLParser(MemoryBuffer *F, ParseError &Err, Module *m) : Lex(F, Err), M(m) {}
80 bool Error(LocTy L, const std::string &Msg) const {
81 return Lex.Error(L, Msg);
83 bool TokError(const std::string &Msg) const {
84 return Error(Lex.getLoc(), Msg);
87 /// GetGlobalVal - Get a value with the specified name or ID, creating a
88 /// forward reference record if needed. This can return null if the value
89 /// exists but does not have the right type.
90 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
91 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
94 bool ParseToken(lltok::Kind T, const char *ErrMsg);
95 bool EatIfPresent(lltok::Kind T) {
96 if (Lex.getKind() != T) return false;
100 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
101 if (Lex.getKind() != T) {
109 bool ParseStringConstant(std::string &Result);
110 bool ParseUInt32(unsigned &Val);
111 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
113 return ParseUInt32(Val);
115 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
116 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
117 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
118 bool ParseOptionalLinkage(unsigned &Linkage) {
119 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
121 bool ParseOptionalVisibility(unsigned &Visibility);
122 bool ParseOptionalCallingConv(unsigned &CC);
123 bool ParseOptionalAlignment(unsigned &Alignment);
124 bool ParseOptionalCommaAlignment(unsigned &Alignment);
125 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
127 // Top-Level Entities
128 bool ParseTopLevelEntities();
129 bool ValidateEndOfModule();
130 bool ParseTargetDefinition();
132 bool ParseModuleAsm();
133 bool ParseUnnamedType();
134 bool ParseNamedType();
138 bool ParseGlobalType(bool &IsConstant);
139 bool ParseNamedGlobal();
140 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
141 bool HasLinkage, unsigned Visibility);
142 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
143 bool ParseStandaloneMetadata();
146 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
147 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
149 return ParseType(Result, AllowVoid);
151 bool ParseTypeRec(PATypeHolder &H);
152 bool ParseStructType(PATypeHolder &H, bool Packed);
153 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
154 bool ParseFunctionType(PATypeHolder &Result);
155 PATypeHolder HandleUpRefs(const Type *Ty);
158 bool ParseValID(ValID &ID);
159 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
160 bool ParseGlobalValue(const Type *Ty, Constant *&V);
161 bool ParseGlobalTypeAndValue(Constant *&V);
162 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
163 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
166 // Function Semantic Analysis.
167 class PerFunctionState {
170 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
171 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
172 std::vector<Value*> NumberedVals;
174 PerFunctionState(LLParser &p, Function &f);
177 Function &getFunction() const { return F; }
179 bool VerifyFunctionComplete();
181 /// GetVal - Get a value with the specified name or ID, creating a
182 /// forward reference record if needed. This can return null if the value
183 /// exists but does not have the right type.
184 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
185 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
187 /// SetInstName - After an instruction is parsed and inserted into its
188 /// basic block, this installs its name.
189 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
192 /// GetBB - Get a basic block with the specified name or ID, creating a
193 /// forward reference record if needed. This can return null if the value
194 /// is not a BasicBlock.
195 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
196 BasicBlock *GetBB(unsigned ID, LocTy Loc);
198 /// DefineBB - Define the specified basic block, which is either named or
199 /// unnamed. If there is an error, this returns null otherwise it returns
200 /// the block being defined.
201 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
204 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
205 PerFunctionState &PFS);
207 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
208 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
209 PerFunctionState &PFS) {
211 return ParseValue(Ty, V, PFS);
214 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
215 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
217 return ParseTypeAndValue(V, PFS);
224 ParamInfo(LocTy loc, Value *v, unsigned attrs)
225 : Loc(loc), V(v), Attrs(attrs) {}
227 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
228 PerFunctionState &PFS);
236 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
237 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
239 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
240 bool &isVarArg, bool inType);
241 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
242 bool ParseFunctionBody(Function &Fn);
243 bool ParseBasicBlock(PerFunctionState &PFS);
245 // Instruction Parsing.
246 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
247 PerFunctionState &PFS);
248 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
250 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
251 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
252 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
253 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
255 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
256 unsigned OperandType);
257 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
258 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
259 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
260 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
261 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
262 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
263 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
264 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
265 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
266 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
267 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
268 bool ParseFree(Instruction *&I, PerFunctionState &PFS);
269 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
270 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
271 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
272 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
273 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
274 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
276 } // End llvm namespace