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 std::vector<TrackingVH<MDNode> > NumberedMetadata;
85 std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
87 /// Loc - This is the location of the upref.
90 /// NestingLevel - The number of nesting levels that need to be popped
91 /// before this type is resolved.
92 unsigned NestingLevel;
94 /// LastContainedTy - This is the type at the current binding level for
95 /// the type. Every time we reduce the nesting level, this gets updated.
96 const Type *LastContainedTy;
98 /// UpRefTy - This is the actual opaque type that the upreference is
102 UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
103 : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
106 std::vector<UpRefRecord> UpRefs;
108 // Global Value reference information.
109 std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
110 std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
111 std::vector<GlobalValue*> NumberedVals;
113 // References to blockaddress. The key is the function ValID, the value is
114 // a list of references to blocks in that function.
115 std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
116 ForwardRefBlockAddresses;
120 LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
121 Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
122 M(m), MallocF(NULL) {}
125 LLVMContext& getContext() { return Context; }
129 bool Error(LocTy L, const std::string &Msg) const {
130 return Lex.Error(L, Msg);
132 bool TokError(const std::string &Msg) const {
133 return Error(Lex.getLoc(), Msg);
136 /// GetGlobalVal - Get a value with the specified name or ID, creating a
137 /// forward reference record if needed. This can return null if the value
138 /// exists but does not have the right type.
139 GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
140 GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
143 bool ParseToken(lltok::Kind T, const char *ErrMsg);
144 bool EatIfPresent(lltok::Kind T) {
145 if (Lex.getKind() != T) return false;
149 bool ParseOptionalToken(lltok::Kind T, bool &Present) {
150 if (Lex.getKind() != T) {
158 bool ParseStringConstant(std::string &Result);
159 bool ParseUInt32(unsigned &Val);
160 bool ParseUInt32(unsigned &Val, LocTy &Loc) {
162 return ParseUInt32(Val);
164 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
165 bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
166 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
167 bool ParseOptionalLinkage(unsigned &Linkage) {
168 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
170 bool ParseOptionalVisibility(unsigned &Visibility);
171 bool ParseOptionalCallingConv(CallingConv::ID &CC);
172 bool ParseOptionalAlignment(unsigned &Alignment);
173 bool ParseOptionalStackAlignment(unsigned &Alignment);
174 bool ParseInstructionMetadata(SmallVectorImpl<std::pair<unsigned,
176 bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
177 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
178 bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
180 if (ParseIndexList(Indices, AteExtraComma)) return true;
182 return TokError("expected index");
186 // Top-Level Entities
187 bool ParseTopLevelEntities();
188 bool ValidateEndOfModule();
189 bool ParseTargetDefinition();
191 bool ParseModuleAsm();
192 bool ParseUnnamedType();
193 bool ParseNamedType();
197 bool ParseGlobalType(bool &IsConstant);
198 bool ParseUnnamedGlobal();
199 bool ParseNamedGlobal();
200 bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
201 bool HasLinkage, unsigned Visibility);
202 bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
203 bool ParseStandaloneMetadata();
204 bool ParseNamedMetadata();
205 bool ParseMDString(MDString *&Result);
206 bool ParseMDNodeID(MDNode *&Result);
209 bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
210 bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
212 return ParseType(Result, AllowVoid);
214 bool ParseTypeRec(PATypeHolder &H);
215 bool ParseStructType(PATypeHolder &H, bool Packed);
216 bool ParseUnionType(PATypeHolder &H);
217 bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
218 bool ParseFunctionType(PATypeHolder &Result);
219 PATypeHolder HandleUpRefs(const Type *Ty);
221 // Function Semantic Analysis.
222 class PerFunctionState {
225 std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
226 std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
227 std::vector<Value*> NumberedVals;
229 /// FunctionNumber - If this is an unnamed function, this is the slot
230 /// number of it, otherwise it is -1.
233 PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
236 Function &getFunction() const { return F; }
238 bool FinishFunction();
240 /// GetVal - Get a value with the specified name or ID, creating a
241 /// forward reference record if needed. This can return null if the value
242 /// exists but does not have the right type.
243 Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
244 Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
246 /// SetInstName - After an instruction is parsed and inserted into its
247 /// basic block, this installs its name.
248 bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
251 /// GetBB - Get a basic block with the specified name or ID, creating a
252 /// forward reference record if needed. This can return null if the value
253 /// is not a BasicBlock.
254 BasicBlock *GetBB(const std::string &Name, LocTy Loc);
255 BasicBlock *GetBB(unsigned ID, LocTy Loc);
257 /// DefineBB - Define the specified basic block, which is either named or
258 /// unnamed. If there is an error, this returns null otherwise it returns
259 /// the block being defined.
260 BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
263 bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
264 PerFunctionState *PFS);
266 bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
267 bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
268 PerFunctionState &PFS) {
270 return ParseValue(Ty, V, PFS);
273 bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
274 bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
276 return ParseTypeAndValue(V, PFS);
278 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
279 PerFunctionState &PFS);
280 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
282 return ParseTypeAndBasicBlock(BB, Loc, PFS);
285 bool ParseUnionValue(const UnionType* utype, ValID &ID, Value *&V);
291 ParamInfo(LocTy loc, Value *v, unsigned attrs)
292 : Loc(loc), V(v), Attrs(attrs) {}
294 bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
295 PerFunctionState &PFS);
298 bool ParseValID(ValID &ID, PerFunctionState *PFS = NULL);
299 bool ParseGlobalValue(const Type *Ty, Constant *&V);
300 bool ParseGlobalTypeAndValue(Constant *&V);
301 bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
302 bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
310 ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
311 : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
313 bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
314 bool &isVarArg, bool inType);
315 bool ParseFunctionHeader(Function *&Fn, bool isDefine);
316 bool ParseFunctionBody(Function &Fn);
317 bool ParseBasicBlock(PerFunctionState &PFS);
319 // Instruction Parsing. Each instruction parsing routine can return with a
320 // normal result, an error result, or return having eaten an extra comma.
321 enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
322 int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
323 PerFunctionState &PFS);
324 bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
326 int ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
327 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
328 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
329 bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
330 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
332 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
333 unsigned OperandType);
334 bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
335 bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
336 bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
337 bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
338 bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
339 bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
340 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
341 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
342 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
343 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
344 int ParseAlloc(Instruction *&I, PerFunctionState &PFS,
345 BasicBlock *BB = 0, bool isAlloca = true);
346 bool ParseFree(Instruction *&I, PerFunctionState &PFS, BasicBlock *BB);
347 int ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
348 int ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
349 bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
350 int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
351 int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
352 int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
354 bool ResolveForwardRefBlockAddresses(Function *TheFn,
355 std::vector<std::pair<ValID, GlobalValue*> > &Refs,
356 PerFunctionState *PFS);
358 } // End llvm namespace