LLParser.h revision 198396
1193323Sed//===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
2193323Sed//
3193323Sed//                     The LLVM Compiler Infrastructure
4193323Sed//
5193323Sed// This file is distributed under the University of Illinois Open Source
6193323Sed// License. See LICENSE.TXT for details.
7193323Sed//
8193323Sed//===----------------------------------------------------------------------===//
9193323Sed//
10193323Sed//  This file defines the parser class for .ll files.
11193323Sed//
12193323Sed//===----------------------------------------------------------------------===//
13193323Sed
14193323Sed#ifndef LLVM_ASMPARSER_LLPARSER_H
15193323Sed#define LLVM_ASMPARSER_LLPARSER_H
16193323Sed
17193323Sed#include "LLLexer.h"
18195340Sed#include "llvm/Module.h"
19193323Sed#include "llvm/Type.h"
20193323Sed#include <map>
21193323Sed
22193323Sednamespace llvm {
23193323Sed  class Module;
24193323Sed  class OpaqueType;
25193323Sed  class Function;
26193323Sed  class Value;
27193323Sed  class BasicBlock;
28193323Sed  class Instruction;
29193323Sed  class Constant;
30193323Sed  class GlobalValue;
31198090Srdivacky  class MetadataBase;
32193323Sed  class MDString;
33193323Sed  class MDNode;
34193323Sed  struct ValID;
35193323Sed
36193323Sed  class LLParser {
37193323Sed  public:
38193323Sed    typedef LLLexer::LocTy LocTy;
39193323Sed  private:
40195340Sed    LLVMContext& Context;
41193323Sed    LLLexer Lex;
42193323Sed    Module *M;
43193323Sed
44193323Sed    // Type resolution handling data structures.
45193323Sed    std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
46193323Sed    std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
47193323Sed    std::vector<PATypeHolder> NumberedTypes;
48195340Sed    /// MetadataCache - This map keeps track of parsed metadata constants.
49198090Srdivacky    std::map<unsigned, MetadataBase *> MetadataCache;
50198090Srdivacky    std::map<unsigned, std::pair<MetadataBase *, LocTy> > ForwardRefMDNodes;
51198090Srdivacky    SmallVector<std::pair<unsigned, MDNode *>, 2> MDsOnInst;
52193323Sed    struct UpRefRecord {
53193323Sed      /// Loc - This is the location of the upref.
54193323Sed      LocTy Loc;
55193323Sed
56193323Sed      /// NestingLevel - The number of nesting levels that need to be popped
57193323Sed      /// before this type is resolved.
58193323Sed      unsigned NestingLevel;
59193323Sed
60193323Sed      /// LastContainedTy - This is the type at the current binding level for
61193323Sed      /// the type.  Every time we reduce the nesting level, this gets updated.
62193323Sed      const Type *LastContainedTy;
63193323Sed
64193323Sed      /// UpRefTy - This is the actual opaque type that the upreference is
65193323Sed      /// represented with.
66193323Sed      OpaqueType *UpRefTy;
67193323Sed
68193323Sed      UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
69193323Sed        : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
70193323Sed          UpRefTy(URTy) {}
71193323Sed    };
72193323Sed    std::vector<UpRefRecord> UpRefs;
73193323Sed
74193323Sed    // Global Value reference information.
75193323Sed    std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
76193323Sed    std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
77193323Sed    std::vector<GlobalValue*> NumberedVals;
78198396Srdivacky    Function* MallocF;
79193323Sed  public:
80195340Sed    LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
81198396Srdivacky      Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
82198396Srdivacky      M(m), MallocF(NULL) {}
83193323Sed    bool Run();
84193323Sed
85195340Sed    LLVMContext& getContext() { return Context; }
86195340Sed
87193323Sed  private:
88193323Sed
89193323Sed    bool Error(LocTy L, const std::string &Msg) const {
90193323Sed      return Lex.Error(L, Msg);
91193323Sed    }
92193323Sed    bool TokError(const std::string &Msg) const {
93193323Sed      return Error(Lex.getLoc(), Msg);
94193323Sed    }
95193323Sed
96193323Sed    /// GetGlobalVal - Get a value with the specified name or ID, creating a
97193323Sed    /// forward reference record if needed.  This can return null if the value
98193323Sed    /// exists but does not have the right type.
99193323Sed    GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
100193323Sed    GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
101193323Sed
102193323Sed    // Helper Routines.
103193323Sed    bool ParseToken(lltok::Kind T, const char *ErrMsg);
104193323Sed    bool EatIfPresent(lltok::Kind T) {
105193323Sed      if (Lex.getKind() != T) return false;
106193323Sed      Lex.Lex();
107193323Sed      return true;
108193323Sed    }
109193323Sed    bool ParseOptionalToken(lltok::Kind T, bool &Present) {
110193323Sed      if (Lex.getKind() != T) {
111193323Sed        Present = false;
112193323Sed      } else {
113193323Sed        Lex.Lex();
114193323Sed        Present = true;
115193323Sed      }
116193323Sed      return false;
117193323Sed    }
118193323Sed    bool ParseStringConstant(std::string &Result);
119193323Sed    bool ParseUInt32(unsigned &Val);
120193323Sed    bool ParseUInt32(unsigned &Val, LocTy &Loc) {
121193323Sed      Loc = Lex.getLoc();
122193323Sed      return ParseUInt32(Val);
123193323Sed    }
124193323Sed    bool ParseOptionalAddrSpace(unsigned &AddrSpace);
125193323Sed    bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
126193323Sed    bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
127193323Sed    bool ParseOptionalLinkage(unsigned &Linkage) {
128193323Sed      bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
129193323Sed    }
130193323Sed    bool ParseOptionalVisibility(unsigned &Visibility);
131198090Srdivacky    bool ParseOptionalCallingConv(CallingConv::ID &CC);
132193323Sed    bool ParseOptionalAlignment(unsigned &Alignment);
133198090Srdivacky    bool ParseOptionalCustomMetadata();
134198090Srdivacky    bool ParseOptionalInfo(unsigned &Alignment);
135193323Sed    bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
136193323Sed
137193323Sed    // Top-Level Entities
138193323Sed    bool ParseTopLevelEntities();
139193323Sed    bool ValidateEndOfModule();
140193323Sed    bool ParseTargetDefinition();
141193323Sed    bool ParseDepLibs();
142193323Sed    bool ParseModuleAsm();
143193323Sed    bool ParseUnnamedType();
144193323Sed    bool ParseNamedType();
145193323Sed    bool ParseDeclare();
146193323Sed    bool ParseDefine();
147193323Sed
148193323Sed    bool ParseGlobalType(bool &IsConstant);
149198090Srdivacky    bool ParseUnnamedGlobal();
150193323Sed    bool ParseNamedGlobal();
151193323Sed    bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
152193323Sed                     bool HasLinkage, unsigned Visibility);
153193323Sed    bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
154195340Sed    bool ParseStandaloneMetadata();
155198090Srdivacky    bool ParseNamedMetadata();
156198090Srdivacky    bool ParseMDString(MetadataBase *&S);
157198090Srdivacky    bool ParseMDNode(MetadataBase *&N);
158193323Sed
159193323Sed    // Type Parsing.
160193323Sed    bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
161193323Sed    bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
162193323Sed      Loc = Lex.getLoc();
163193323Sed      return ParseType(Result, AllowVoid);
164193323Sed    }
165193323Sed    bool ParseTypeRec(PATypeHolder &H);
166193323Sed    bool ParseStructType(PATypeHolder &H, bool Packed);
167193323Sed    bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
168193323Sed    bool ParseFunctionType(PATypeHolder &Result);
169193323Sed    PATypeHolder HandleUpRefs(const Type *Ty);
170193323Sed
171193323Sed    // Constants.
172193323Sed    bool ParseValID(ValID &ID);
173193323Sed    bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
174193323Sed    bool ParseGlobalValue(const Type *Ty, Constant *&V);
175193323Sed    bool ParseGlobalTypeAndValue(Constant *&V);
176193323Sed    bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
177193323Sed    bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
178193323Sed
179193323Sed
180193323Sed    // Function Semantic Analysis.
181193323Sed    class PerFunctionState {
182193323Sed      LLParser &P;
183193323Sed      Function &F;
184193323Sed      std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
185193323Sed      std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
186193323Sed      std::vector<Value*> NumberedVals;
187193323Sed    public:
188193323Sed      PerFunctionState(LLParser &p, Function &f);
189193323Sed      ~PerFunctionState();
190193323Sed
191193323Sed      Function &getFunction() const { return F; }
192193323Sed
193193323Sed      bool VerifyFunctionComplete();
194193323Sed
195193323Sed      /// GetVal - Get a value with the specified name or ID, creating a
196193323Sed      /// forward reference record if needed.  This can return null if the value
197193323Sed      /// exists but does not have the right type.
198193323Sed      Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
199193323Sed      Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
200193323Sed
201193323Sed      /// SetInstName - After an instruction is parsed and inserted into its
202193323Sed      /// basic block, this installs its name.
203193323Sed      bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
204193323Sed                       Instruction *Inst);
205193323Sed
206193323Sed      /// GetBB - Get a basic block with the specified name or ID, creating a
207193323Sed      /// forward reference record if needed.  This can return null if the value
208193323Sed      /// is not a BasicBlock.
209193323Sed      BasicBlock *GetBB(const std::string &Name, LocTy Loc);
210193323Sed      BasicBlock *GetBB(unsigned ID, LocTy Loc);
211193323Sed
212193323Sed      /// DefineBB - Define the specified basic block, which is either named or
213193323Sed      /// unnamed.  If there is an error, this returns null otherwise it returns
214193323Sed      /// the block being defined.
215193323Sed      BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
216193323Sed    };
217193323Sed
218193323Sed    bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
219193323Sed                             PerFunctionState &PFS);
220193323Sed
221193323Sed    bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
222193323Sed    bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
223193323Sed                    PerFunctionState &PFS) {
224193323Sed      Loc = Lex.getLoc();
225193323Sed      return ParseValue(Ty, V, PFS);
226193323Sed    }
227193323Sed
228193323Sed    bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
229193323Sed    bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
230193323Sed      Loc = Lex.getLoc();
231193323Sed      return ParseTypeAndValue(V, PFS);
232193323Sed    }
233193323Sed
234193323Sed    struct ParamInfo {
235193323Sed      LocTy Loc;
236193323Sed      Value *V;
237193323Sed      unsigned Attrs;
238193323Sed      ParamInfo(LocTy loc, Value *v, unsigned attrs)
239193323Sed        : Loc(loc), V(v), Attrs(attrs) {}
240193323Sed    };
241193323Sed    bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
242193323Sed                            PerFunctionState &PFS);
243193323Sed
244193323Sed    // Function Parsing.
245193323Sed    struct ArgInfo {
246193323Sed      LocTy Loc;
247193323Sed      PATypeHolder Type;
248193323Sed      unsigned Attrs;
249193323Sed      std::string Name;
250193323Sed      ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
251193323Sed        : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
252193323Sed    };
253193323Sed    bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
254193323Sed                           bool &isVarArg, bool inType);
255193323Sed    bool ParseFunctionHeader(Function *&Fn, bool isDefine);
256193323Sed    bool ParseFunctionBody(Function &Fn);
257193323Sed    bool ParseBasicBlock(PerFunctionState &PFS);
258193323Sed
259193323Sed    // Instruction Parsing.
260193323Sed    bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
261193323Sed                          PerFunctionState &PFS);
262193323Sed    bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
263193323Sed
264193323Sed    bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
265193323Sed    bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
266193323Sed    bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
267193323Sed    bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
268193323Sed
269193323Sed    bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
270193323Sed                         unsigned OperandType);
271193323Sed    bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
272193323Sed    bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
273193323Sed    bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
274193323Sed    bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
275193323Sed    bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
276193323Sed    bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
277193323Sed    bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
278193323Sed    bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
279193323Sed    bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
280193323Sed    bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
281198396Srdivacky    bool ParseAlloc(Instruction *&I, PerFunctionState &PFS,
282198396Srdivacky                    BasicBlock *BB = 0, bool isAlloca = true);
283193323Sed    bool ParseFree(Instruction *&I, PerFunctionState &PFS);
284193323Sed    bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
285193323Sed    bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
286193323Sed    bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
287193323Sed    bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
288193323Sed    bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
289193323Sed    bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
290193323Sed  };
291193323Sed} // End llvm namespace
292193323Sed
293193323Sed#endif
294