LLVM API Documentation

LLParser.h

Go to the documentation of this file.
00001 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the parser class for .ll files.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ASMPARSER_LLPARSER_H
00015 #define LLVM_ASMPARSER_LLPARSER_H
00016 
00017 #include "LLLexer.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/Type.h"
00020 #include <map>
00021 
00022 namespace llvm {
00023   class Module;
00024   class OpaqueType;
00025   class Function;
00026   class Value;
00027   class BasicBlock;
00028   class Instruction;
00029   class Constant;
00030   class GlobalValue;
00031   class MetadataBase;
00032   class MDString;
00033   class MDNode;
00034   struct ValID;
00035 
00036   class LLParser {
00037   public:
00038     typedef LLLexer::LocTy LocTy;
00039   private:
00040     LLVMContext& Context;
00041     LLLexer Lex;
00042     Module *M;
00043 
00044     // Type resolution handling data structures.
00045     std::map<std::string, std::pair<PATypeHolder, LocTy> > ForwardRefTypes;
00046     std::map<unsigned, std::pair<PATypeHolder, LocTy> > ForwardRefTypeIDs;
00047     std::vector<PATypeHolder> NumberedTypes;
00048     /// MetadataCache - This map keeps track of parsed metadata constants.
00049     std::map<unsigned, MetadataBase *> MetadataCache;
00050     std::map<unsigned, std::pair<MetadataBase *, LocTy> > ForwardRefMDNodes;
00051 
00052     struct UpRefRecord {
00053       /// Loc - This is the location of the upref.
00054       LocTy Loc;
00055 
00056       /// NestingLevel - The number of nesting levels that need to be popped
00057       /// before this type is resolved.
00058       unsigned NestingLevel;
00059 
00060       /// LastContainedTy - This is the type at the current binding level for
00061       /// the type.  Every time we reduce the nesting level, this gets updated.
00062       const Type *LastContainedTy;
00063 
00064       /// UpRefTy - This is the actual opaque type that the upreference is
00065       /// represented with.
00066       OpaqueType *UpRefTy;
00067 
00068       UpRefRecord(LocTy L, unsigned NL, OpaqueType *URTy)
00069         : Loc(L), NestingLevel(NL), LastContainedTy((Type*)URTy),
00070           UpRefTy(URTy) {}
00071     };
00072     std::vector<UpRefRecord> UpRefs;
00073 
00074     // Global Value reference information.
00075     std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
00076     std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
00077     std::vector<GlobalValue*> NumberedVals;
00078   public:
00079     LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) : 
00080       Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m) {}
00081     bool Run();
00082 
00083     LLVMContext& getContext() { return Context; }
00084 
00085   private:
00086 
00087     bool Error(LocTy L, const std::string &Msg) const {
00088       return Lex.Error(L, Msg);
00089     }
00090     bool TokError(const std::string &Msg) const {
00091       return Error(Lex.getLoc(), Msg);
00092     }
00093 
00094     /// GetGlobalVal - Get a value with the specified name or ID, creating a
00095     /// forward reference record if needed.  This can return null if the value
00096     /// exists but does not have the right type.
00097     GlobalValue *GetGlobalVal(const std::string &N, const Type *Ty, LocTy Loc);
00098     GlobalValue *GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc);
00099 
00100     // Helper Routines.
00101     bool ParseToken(lltok::Kind T, const char *ErrMsg);
00102     bool EatIfPresent(lltok::Kind T) {
00103       if (Lex.getKind() != T) return false;
00104       Lex.Lex();
00105       return true;
00106     }
00107     bool ParseOptionalToken(lltok::Kind T, bool &Present) {
00108       if (Lex.getKind() != T) {
00109         Present = false;
00110       } else {
00111         Lex.Lex();
00112         Present = true;
00113       }
00114       return false;
00115     }
00116     bool ParseStringConstant(std::string &Result);
00117     bool ParseUInt32(unsigned &Val);
00118     bool ParseUInt32(unsigned &Val, LocTy &Loc) {
00119       Loc = Lex.getLoc();
00120       return ParseUInt32(Val);
00121     }
00122     bool ParseOptionalAddrSpace(unsigned &AddrSpace);
00123     bool ParseOptionalAttrs(unsigned &Attrs, unsigned AttrKind);
00124     bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
00125     bool ParseOptionalLinkage(unsigned &Linkage) {
00126       bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
00127     }
00128     bool ParseOptionalVisibility(unsigned &Visibility);
00129     bool ParseOptionalCallingConv(unsigned &CC);
00130     bool ParseOptionalAlignment(unsigned &Alignment);
00131     bool ParseOptionalCommaAlignment(unsigned &Alignment);
00132     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices);
00133 
00134     // Top-Level Entities
00135     bool ParseTopLevelEntities();
00136     bool ValidateEndOfModule();
00137     bool ParseTargetDefinition();
00138     bool ParseDepLibs();
00139     bool ParseModuleAsm();
00140     bool ParseUnnamedType();
00141     bool ParseNamedType();
00142     bool ParseDeclare();
00143     bool ParseDefine();
00144 
00145     bool ParseGlobalType(bool &IsConstant);
00146     bool ParseUnnamedGlobal();
00147     bool ParseNamedGlobal();
00148     bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
00149                      bool HasLinkage, unsigned Visibility);
00150     bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility);
00151     bool ParseStandaloneMetadata();
00152     bool ParseNamedMetadata();
00153     bool ParseMDString(MetadataBase *&S);
00154     bool ParseMDNode(MetadataBase *&N);
00155 
00156     // Type Parsing.
00157     bool ParseType(PATypeHolder &Result, bool AllowVoid = false);
00158     bool ParseType(PATypeHolder &Result, LocTy &Loc, bool AllowVoid = false) {
00159       Loc = Lex.getLoc();
00160       return ParseType(Result, AllowVoid);
00161     }
00162     bool ParseTypeRec(PATypeHolder &H);
00163     bool ParseStructType(PATypeHolder &H, bool Packed);
00164     bool ParseArrayVectorType(PATypeHolder &H, bool isVector);
00165     bool ParseFunctionType(PATypeHolder &Result);
00166     PATypeHolder HandleUpRefs(const Type *Ty);
00167 
00168     // Constants.
00169     bool ParseValID(ValID &ID);
00170     bool ConvertGlobalValIDToValue(const Type *Ty, ValID &ID, Constant *&V);
00171     bool ParseGlobalValue(const Type *Ty, Constant *&V);
00172     bool ParseGlobalTypeAndValue(Constant *&V);
00173     bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
00174     bool ParseMDNodeVector(SmallVectorImpl<Value*> &);
00175 
00176 
00177     // Function Semantic Analysis.
00178     class PerFunctionState {
00179       LLParser &P;
00180       Function &F;
00181       std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
00182       std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
00183       std::vector<Value*> NumberedVals;
00184     public:
00185       PerFunctionState(LLParser &p, Function &f);
00186       ~PerFunctionState();
00187 
00188       Function &getFunction() const { return F; }
00189 
00190       bool VerifyFunctionComplete();
00191 
00192       /// GetVal - Get a value with the specified name or ID, creating a
00193       /// forward reference record if needed.  This can return null if the value
00194       /// exists but does not have the right type.
00195       Value *GetVal(const std::string &Name, const Type *Ty, LocTy Loc);
00196       Value *GetVal(unsigned ID, const Type *Ty, LocTy Loc);
00197 
00198       /// SetInstName - After an instruction is parsed and inserted into its
00199       /// basic block, this installs its name.
00200       bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
00201                        Instruction *Inst);
00202 
00203       /// GetBB - Get a basic block with the specified name or ID, creating a
00204       /// forward reference record if needed.  This can return null if the value
00205       /// is not a BasicBlock.
00206       BasicBlock *GetBB(const std::string &Name, LocTy Loc);
00207       BasicBlock *GetBB(unsigned ID, LocTy Loc);
00208 
00209       /// DefineBB - Define the specified basic block, which is either named or
00210       /// unnamed.  If there is an error, this returns null otherwise it returns
00211       /// the block being defined.
00212       BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
00213     };
00214 
00215     bool ConvertValIDToValue(const Type *Ty, ValID &ID, Value *&V,
00216                              PerFunctionState &PFS);
00217 
00218     bool ParseValue(const Type *Ty, Value *&V, PerFunctionState &PFS);
00219     bool ParseValue(const Type *Ty, Value *&V, LocTy &Loc,
00220                     PerFunctionState &PFS) {
00221       Loc = Lex.getLoc();
00222       return ParseValue(Ty, V, PFS);
00223     }
00224 
00225     bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS);
00226     bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
00227       Loc = Lex.getLoc();
00228       return ParseTypeAndValue(V, PFS);
00229     }
00230 
00231     struct ParamInfo {
00232       LocTy Loc;
00233       Value *V;
00234       unsigned Attrs;
00235       ParamInfo(LocTy loc, Value *v, unsigned attrs)
00236         : Loc(loc), V(v), Attrs(attrs) {}
00237     };
00238     bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
00239                             PerFunctionState &PFS);
00240 
00241     // Function Parsing.
00242     struct ArgInfo {
00243       LocTy Loc;
00244       PATypeHolder Type;
00245       unsigned Attrs;
00246       std::string Name;
00247       ArgInfo(LocTy L, PATypeHolder Ty, unsigned Attr, const std::string &N)
00248         : Loc(L), Type(Ty), Attrs(Attr), Name(N) {}
00249     };
00250     bool ParseArgumentList(std::vector<ArgInfo> &ArgList,
00251                            bool &isVarArg, bool inType);
00252     bool ParseFunctionHeader(Function *&Fn, bool isDefine);
00253     bool ParseFunctionBody(Function &Fn);
00254     bool ParseBasicBlock(PerFunctionState &PFS);
00255 
00256     // Instruction Parsing.
00257     bool ParseInstruction(Instruction *&Inst, BasicBlock *BB,
00258                           PerFunctionState &PFS);
00259     bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
00260 
00261     bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
00262     bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
00263     bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
00264     bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
00265 
00266     bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
00267                          unsigned OperandType);
00268     bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
00269     bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
00270     bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
00271     bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
00272     bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
00273     bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
00274     bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
00275     bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
00276     bool ParsePHI(Instruction *&I, PerFunctionState &PFS);
00277     bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
00278     bool ParseAlloc(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
00279     bool ParseFree(Instruction *&I, PerFunctionState &PFS);
00280     bool ParseLoad(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
00281     bool ParseStore(Instruction *&I, PerFunctionState &PFS, bool isVolatile);
00282     bool ParseGetResult(Instruction *&I, PerFunctionState &PFS);
00283     bool ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
00284     bool ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
00285     bool ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
00286   };
00287 } // End llvm namespace
00288 
00289 #endif