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/Module.h"
19 #include "llvm/Type.h"
35 /// ValID - Represents a reference of a definition of some sort with no type.
36 /// There are several cases where we have to parse the value but where the
37 /// type can depend on later context. This may either be a numeric reference
38 /// or a symbolic (%var) reference. This is just a discriminated union.
41 t_LocalID, t_GlobalID, // ID in UIntVal.
42 t_LocalName, t_GlobalName, // Name in StrVal.
43 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
44 t_Null, t_Undef, t_Zero, // No value.
45 t_EmptyArray, // No value: []
46 t_Constant, // Value in ConstantVal.
47 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
48 t_Metadata // Value in MetadataVal.
53 std::string StrVal, StrVal2;
56 Constant *ConstantVal;
57 MetadataBase *MetadataVal;
58 ValID() : APFloatVal(0.0) {}
60 bool operator<(const ValID &RHS) const {
61 if (Kind == t_LocalID || Kind == t_GlobalID)
62 return UIntVal < RHS.UIntVal;
63 assert((Kind == t_LocalName || Kind == t_GlobalName) &&
64 "Ordering not defined for this ValID kind yet");
65 return StrVal < RHS.StrVal;
71 typedef LLLexer::LocTy LocTy;
77 // Type resolution handling data structures.
78 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
79 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
80 std::vector<PATypeHolder> NumberedTypes;
81 /// MetadataCache - This map keeps track of parsed metadata constants.
82 std::map<unsigned, WeakVH> MetadataCache;
83 std::map<unsigned, std::pair<WeakVH, LocTy> > ForwardRefMDNodes;
84 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
86 /// Loc - This is the location of the upref.
89 /// NestingLevel - The number of nesting levels that need to be popped
90 /// before this type is resolved.
91 unsigned NestingLevel;
93 /// LastContainedTy - This is the type at the current binding level for
94 /// the type. Every time we reduce the nesting level, this gets updated.
95 const Type *LastContainedTy;
97 /// UpRefTy - This is the actual opaque type that the upreference is
101 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
102 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
105 std::vector<UpRefRecord> UpRefs;
107 // Global Value reference information.
108 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
109 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
110 std::vector<GlobalValue*> NumberedVals;
112 // References to blockaddress. The key is the function ValID, the value is
113 // a list of references to blocks in that function.
114 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
115 ForwardRefBlockAddresses;
119 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
120 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
121 M(m), MallocF(NULL) {}
124 LLVMContext& getContext() { return Context; }
128 bool Error(LocTy L, const std::string &Msg) const {
129 return Lex.Error(L, Msg);
131 bool TokError(const std::string &Msg) const {
132 return Error(Lex.getLoc(), Msg);
135 /// GetGlobalVal - Get a value with the specified name or ID, creating a
136 /// forward reference record if needed. This can return null if the value
137 /// exists but does not have the right type.
138 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
139 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
142 bool ParseToken(lltok::Kind T, const char *ErrMsg);
143 bool EatIfPresent(lltok::Kind T) {
144 if (Lex.getKind() != T) return false;
148 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
149 if (Lex.getKind() != T) {
157 bool ParseStringConstant(std::string &Result);
158 bool ParseUInt32(unsigned &Val);
159 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
161 return ParseUInt32(Val);
163 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
164 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
165 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
166 bool ParseOptionalLinkage(unsigned &Linkage) {
167 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
169 bool ParseOptionalVisibility(unsigned &Visibility);
170 bool ParseOptionalCallingConv(CallingConv::ID &CC);
171 bool ParseOptionalAlignment(unsigned &Alignment);
172 bool ParseOptionalCustomMetadata();
173 bool ParseOptionalInfo(unsigned &Alignment);
174 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
176 // Top-Level Entities
177 bool ParseTopLevelEntities();
178 bool ValidateEndOfModule();
179 bool ParseTargetDefinition();
181 bool ParseModuleAsm();
182 bool ParseUnnamedType();
183 bool ParseNamedType();
187 bool ParseGlobalType(bool &IsConstant);
188 bool ParseUnnamedGlobal();
189 bool ParseNamedGlobal();
190 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
191 bool HasLinkage, unsigned Visibility);
192 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
193 bool ParseStandaloneMetadata();
194 bool ParseNamedMetadata();
195 bool ParseMDString(MetadataBase *&S);
196 bool ParseMDNode(MetadataBase *&N);
199 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
200 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
202 return ParseType(Result, AllowVoid);
204 bool ParseTypeRec(PATypeHolder &H);
205 bool ParseStructType(PATypeHolder &H, bool Packed);
206 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
207 bool ParseFunctionType(PATypeHolder &Result);
208 PATypeHolder HandleUpRefs(const Type *Ty);
211 bool ParseValID(ValID &ID);
212 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
213 bool ParseGlobalValue(const Type *Ty, Constant *&V);
214 bool ParseGlobalTypeAndValue(Constant *&V);
215 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
216 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
219 // Function Semantic Analysis.
220 class PerFunctionState {
223 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
224 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
225 std::vector<Value*> NumberedVals;
227 /// FunctionNumber - If this is an unnamed function, this is the slot
228 /// number of it, otherwise it is -1.
231 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
234 Function &getFunction() const { return F; }
236 bool FinishFunction();
238 /// GetVal - Get a value with the specified name or ID, creating a
239 /// forward reference record if needed. This can return null if the value
240 /// exists but does not have the right type.
241 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
242 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
244 /// SetInstName - After an instruction is parsed and inserted into its
245 /// basic block, this installs its name.
246 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
249 /// GetBB - Get a basic block with the specified name or ID, creating a
250 /// forward reference record if needed. This can return null if the value
251 /// is not a BasicBlock.
252 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
253 BasicBlock *GetBB(unsigned ID, LocTy Loc);
255 /// DefineBB - Define the specified basic block, which is either named or
256 /// unnamed. If there is an error, this returns null otherwise it returns
257 /// the block being defined.
258 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
261 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
262 PerFunctionState &PFS);
264 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
265 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
266 PerFunctionState &PFS) {
268 return ParseValue(Ty, V, PFS);
271 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
272 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
274 return ParseTypeAndValue(V, PFS);
276 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
277 PerFunctionState &PFS);
278 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
280 return ParseTypeAndBasicBlock(BB, Loc, PFS);
283 bool ParseInlineMetadata(Value *&V, PerFunctionState &PFS);
289 ParamInfo(LocTy loc, Value *v, unsigned attrs)
290 : Loc(loc), V(v), Attrs(attrs) {}
292 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
293 PerFunctionState &PFS);
301 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
302 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
304 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
305 bool &isVarArg, bool inType);
306 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
307 bool ParseFunctionBody(Function &Fn);
308 bool ParseBasicBlock(PerFunctionState &PFS);
310 // Instruction Parsing.
311 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
312 PerFunctionState &PFS);
313 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
315 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
316 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
317 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
318 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
319 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
321 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
322 unsigned OperandType);
323 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
324 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
325 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
326 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
327 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
328 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
329 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
330 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
331 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
332 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
333 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
334 BasicBlock *BB = 0, bool isAlloca = true);
335 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
336 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
337 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
338 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
339 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
340 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
341 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
343 bool ResolveForwardRefBlockAddresses(Function *TheFn,
344 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
345 PerFunctionState *PFS);
347 } // End llvm namespace