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"
20 #include "llvm/Support/ValueHandle.h"
36 /// ValID - Represents a reference of a definition of some sort with no type.
37 /// There are several cases where we have to parse the value but where the
38 /// type can depend on later context. This may either be a numeric reference
39 /// or a symbolic (%var) reference. This is just a discriminated union.
42 t_LocalID, t_GlobalID, // ID in UIntVal.
43 t_LocalName, t_GlobalName, // Name in StrVal.
44 t_APSInt, t_APFloat, // Value in APSIntVal/APFloatVal.
45 t_Null, t_Undef, t_Zero, // No value.
46 t_EmptyArray, // No value: []
47 t_Constant, // Value in ConstantVal.
48 t_InlineAsm, // Value in StrVal/StrVal2/UIntVal.
49 t_MDNode, // Value in MDNodeVal.
50 t_MDString // Value in MDStringVal.
55 std::string StrVal, StrVal2;
58 Constant *ConstantVal;
60 MDString *MDStringVal;
61 ValID() : APFloatVal(0.0) {}
63 bool operator<(const ValID &RHS) const {
64 if (Kind == t_LocalID || Kind == t_GlobalID)
65 return UIntVal < RHS.UIntVal;
66 assert((Kind == t_LocalName || Kind == t_GlobalName) &&
67 "Ordering not defined for this ValID kind yet");
68 return StrVal < RHS.StrVal;
74 typedef LLLexer::LocTy LocTy;
80 // Type resolution handling data structures.
81 std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
82 std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
83 std::vector<PATypeHolder> NumberedTypes;
84 /// MetadataCache - This map keeps track of parsed metadata constants.
85 std::map<unsigned, TrackingVH<MDNode> > MetadataCache;
86 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
87 SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
89 /// Loc - This is the location of the upref.
92 /// NestingLevel - The number of nesting levels that need to be popped
93 /// before this type is resolved.
94 unsigned NestingLevel;
96 /// LastContainedTy - This is the type at the current binding level for
97 /// the type. Every time we reduce the nesting level, this gets updated.
98 const Type *LastContainedTy;
100 /// UpRefTy - This is the actual opaque type that the upreference is
101 /// represented with.
104 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
105 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
108 std::vector<UpRefRecord> UpRefs;
110 // Global Value reference information.
111 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
112 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
113 std::vector<GlobalValue*> NumberedVals;
115 // References to blockaddress. The key is the function ValID, the value is
116 // a list of references to blocks in that function.
117 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
118 ForwardRefBlockAddresses;
122 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
123 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
124 M(m), MallocF(NULL) {}
127 LLVMContext& getContext() { return Context; }
131 bool Error(LocTy L, const std::string &Msg) const {
132 return Lex.Error(L, Msg);
134 bool TokError(const std::string &Msg) const {
135 return Error(Lex.getLoc(), Msg);
138 /// GetGlobalVal - Get a value with the specified name or ID, creating a
139 /// forward reference record if needed. This can return null if the value
140 /// exists but does not have the right type.
141 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
142 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
145 bool ParseToken(lltok::Kind T, const char *ErrMsg);
146 bool EatIfPresent(lltok::Kind T) {
147 if (Lex.getKind() != T) return false;
151 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
152 if (Lex.getKind() != T) {
160 bool ParseStringConstant(std::string &Result);
161 bool ParseUInt32(unsigned &Val);
162 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
164 return ParseUInt32(Val);
166 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
167 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
168 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
169 bool ParseOptionalLinkage(unsigned &Linkage) {
170 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
172 bool ParseOptionalVisibility(unsigned &Visibility);
173 bool ParseOptionalCallingConv(CallingConv::ID &CC);
174 bool ParseOptionalAlignment(unsigned &Alignment);
175 bool ParseOptionalCustomMetadata();
176 bool ParseOptionalInfo(unsigned &Alignment);
177 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
179 // Top-Level Entities
180 bool ParseTopLevelEntities();
181 bool ValidateEndOfModule();
182 bool ParseTargetDefinition();
184 bool ParseModuleAsm();
185 bool ParseUnnamedType();
186 bool ParseNamedType();
190 bool ParseGlobalType(bool &IsConstant);
191 bool ParseUnnamedGlobal();
192 bool ParseNamedGlobal();
193 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
194 bool HasLinkage, unsigned Visibility);
195 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
196 bool ParseStandaloneMetadata();
197 bool ParseNamedMetadata();
198 bool ParseMDString(MDString *&Result);
199 bool ParseMDNodeID(MDNode *&Result);
202 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
203 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
205 return ParseType(Result, AllowVoid);
207 bool ParseTypeRec(PATypeHolder &H);
208 bool ParseStructType(PATypeHolder &H, bool Packed);
209 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
210 bool ParseFunctionType(PATypeHolder &Result);
211 PATypeHolder HandleUpRefs(const Type *Ty);
214 bool ParseValID(ValID &ID);
215 bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
216 bool ConvertGlobalOrMetadataValIDToValue(const Type *Ty, ValID &ID,
218 bool ParseGlobalValue(const Type *Ty, Constant *&V);
219 bool ParseGlobalTypeAndValue(Constant *&V);
220 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
221 bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
224 // Function Semantic Analysis.
225 class PerFunctionState {
228 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
229 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
230 std::vector<Value*> NumberedVals;
232 /// FunctionNumber - If this is an unnamed function, this is the slot
233 /// number of it, otherwise it is -1.
236 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
239 Function &getFunction() const { return F; }
241 bool FinishFunction();
243 /// GetVal - Get a value with the specified name or ID, creating a
244 /// forward reference record if needed. This can return null if the value
245 /// exists but does not have the right type.
246 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
247 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
249 /// SetInstName - After an instruction is parsed and inserted into its
250 /// basic block, this installs its name.
251 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
254 /// GetBB - Get a basic block with the specified name or ID, creating a
255 /// forward reference record if needed. This can return null if the value
256 /// is not a BasicBlock.
257 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
258 BasicBlock *GetBB(unsigned ID, LocTy Loc);
260 /// DefineBB - Define the specified basic block, which is either named or
261 /// unnamed. If there is an error, this returns null otherwise it returns
262 /// the block being defined.
263 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
266 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
267 PerFunctionState &PFS);
269 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
270 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
271 PerFunctionState &PFS) {
273 return ParseValue(Ty, V, PFS);
276 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
277 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
279 return ParseTypeAndValue(V, PFS);
281 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
282 PerFunctionState &PFS);
283 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
285 return ParseTypeAndBasicBlock(BB, Loc, PFS);
292 ParamInfo(LocTy loc, Value *v, unsigned attrs)
293 : Loc(loc), V(v), Attrs(attrs) {}
295 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
296 PerFunctionState &PFS);
304 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
305 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
307 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
308 bool &isVarArg, bool inType);
309 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
310 bool ParseFunctionBody(Function &Fn);
311 bool ParseBasicBlock(PerFunctionState &PFS);
313 // Instruction Parsing.
314 bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
315 PerFunctionState &PFS);
316 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
318 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
319 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
320 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
321 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
322 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
324 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
325 unsigned OperandType);
326 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
327 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
328 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
329 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
330 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
331 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
332 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
333 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
334 bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
335 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
336 bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
337 BasicBlock *BB = 0, bool isAlloca = true);
338 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
339 bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
340 bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
341 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
342 bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
343 bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
344 bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
346 bool ResolveForwardRefBlockAddresses(Function *TheFn,
347 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
348 PerFunctionState *PFS);
350 } // End llvm namespace