Home | History | Annotate | Download | only in AsmParser
      1 //===-- LLParser.h - Parser Class -------------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines the parser class for .ll files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ASMPARSER_LLPARSER_H
     15 #define LLVM_ASMPARSER_LLPARSER_H
     16 
     17 #include "LLLexer.h"
     18 #include "llvm/ADT/DenseMap.h"
     19 #include "llvm/ADT/StringMap.h"
     20 #include "llvm/IR/Attributes.h"
     21 #include "llvm/IR/Instructions.h"
     22 #include "llvm/IR/Module.h"
     23 #include "llvm/IR/Operator.h"
     24 #include "llvm/IR/Type.h"
     25 #include "llvm/IR/ValueHandle.h"
     26 #include <map>
     27 
     28 namespace llvm {
     29   class Module;
     30   class OpaqueType;
     31   class Function;
     32   class Value;
     33   class BasicBlock;
     34   class Instruction;
     35   class Constant;
     36   class GlobalValue;
     37   class Comdat;
     38   class MDString;
     39   class MDNode;
     40   class StructType;
     41 
     42   /// ValID - Represents a reference of a definition of some sort with no type.
     43   /// There are several cases where we have to parse the value but where the
     44   /// type can depend on later context.  This may either be a numeric reference
     45   /// or a symbolic (%var) reference.  This is just a discriminated union.
     46   struct ValID {
     47     enum {
     48       t_LocalID, t_GlobalID,      // ID in UIntVal.
     49       t_LocalName, t_GlobalName,  // Name in StrVal.
     50       t_APSInt, t_APFloat,        // Value in APSIntVal/APFloatVal.
     51       t_Null, t_Undef, t_Zero,    // No value.
     52       t_EmptyArray,               // No value:  []
     53       t_Constant,                 // Value in ConstantVal.
     54       t_InlineAsm,                // Value in StrVal/StrVal2/UIntVal.
     55       t_MDNode,                   // Value in MDNodeVal.
     56       t_MDString,                 // Value in MDStringVal.
     57       t_ConstantStruct,           // Value in ConstantStructElts.
     58       t_PackedConstantStruct      // Value in ConstantStructElts.
     59     } Kind;
     60 
     61     LLLexer::LocTy Loc;
     62     unsigned UIntVal;
     63     std::string StrVal, StrVal2;
     64     APSInt APSIntVal;
     65     APFloat APFloatVal;
     66     Constant *ConstantVal;
     67     MDNode *MDNodeVal;
     68     MDString *MDStringVal;
     69     Constant **ConstantStructElts;
     70 
     71     ValID() : Kind(t_LocalID), APFloatVal(0.0) {}
     72     ~ValID() {
     73       if (Kind == t_ConstantStruct || Kind == t_PackedConstantStruct)
     74         delete [] ConstantStructElts;
     75     }
     76 
     77     bool operator<(const ValID &RHS) const {
     78       if (Kind == t_LocalID || Kind == t_GlobalID)
     79         return UIntVal < RHS.UIntVal;
     80       assert((Kind == t_LocalName || Kind == t_GlobalName ||
     81               Kind == t_ConstantStruct || Kind == t_PackedConstantStruct) &&
     82              "Ordering not defined for this ValID kind yet");
     83       return StrVal < RHS.StrVal;
     84     }
     85   };
     86 
     87   class LLParser {
     88   public:
     89     typedef LLLexer::LocTy LocTy;
     90   private:
     91     LLVMContext &Context;
     92     LLLexer Lex;
     93     Module *M;
     94 
     95     // Instruction metadata resolution.  Each instruction can have a list of
     96     // MDRef info associated with them.
     97     //
     98     // The simpler approach of just creating temporary MDNodes and then calling
     99     // RAUW on them when the definition is processed doesn't work because some
    100     // instruction metadata kinds, such as dbg, get stored in the IR in an
    101     // "optimized" format which doesn't participate in the normal value use
    102     // lists. This means that RAUW doesn't work, even on temporary MDNodes
    103     // which otherwise support RAUW. Instead, we defer resolving MDNode
    104     // references until the definitions have been processed.
    105     struct MDRef {
    106       SMLoc Loc;
    107       unsigned MDKind, MDSlot;
    108     };
    109     DenseMap<Instruction*, std::vector<MDRef> > ForwardRefInstMetadata;
    110 
    111     SmallVector<Instruction*, 64> InstsWithTBAATag;
    112 
    113     // Type resolution handling data structures.  The location is set when we
    114     // have processed a use of the type but not a definition yet.
    115     StringMap<std::pair<Type*, LocTy> > NamedTypes;
    116     std::vector<std::pair<Type*, LocTy> > NumberedTypes;
    117 
    118     std::vector<TrackingVH<MDNode> > NumberedMetadata;
    119     std::map<unsigned, std::pair<TrackingVH<MDNode>, LocTy> > ForwardRefMDNodes;
    120 
    121     // Global Value reference information.
    122     std::map<std::string, std::pair<GlobalValue*, LocTy> > ForwardRefVals;
    123     std::map<unsigned, std::pair<GlobalValue*, LocTy> > ForwardRefValIDs;
    124     std::vector<GlobalValue*> NumberedVals;
    125 
    126     // Comdat forward reference information.
    127     std::map<std::string, LocTy> ForwardRefComdats;
    128 
    129     // References to blockaddress.  The key is the function ValID, the value is
    130     // a list of references to blocks in that function.
    131     std::map<ValID, std::vector<std::pair<ValID, GlobalValue*> > >
    132       ForwardRefBlockAddresses;
    133 
    134     // Attribute builder reference information.
    135     std::map<Value*, std::vector<unsigned> > ForwardRefAttrGroups;
    136     std::map<unsigned, AttrBuilder> NumberedAttrBuilders;
    137 
    138   public:
    139     LLParser(MemoryBuffer *F, SourceMgr &SM, SMDiagnostic &Err, Module *m) :
    140       Context(m->getContext()), Lex(F, SM, Err, m->getContext()),
    141       M(m) {}
    142     bool Run();
    143 
    144     LLVMContext &getContext() { return Context; }
    145 
    146   private:
    147 
    148     bool Error(LocTy L, const Twine &Msg) const {
    149       return Lex.Error(L, Msg);
    150     }
    151     bool TokError(const Twine &Msg) const {
    152       return Error(Lex.getLoc(), Msg);
    153     }
    154 
    155     /// GetGlobalVal - Get a value with the specified name or ID, creating a
    156     /// forward reference record if needed.  This can return null if the value
    157     /// exists but does not have the right type.
    158     GlobalValue *GetGlobalVal(const std::string &N, Type *Ty, LocTy Loc);
    159     GlobalValue *GetGlobalVal(unsigned ID, Type *Ty, LocTy Loc);
    160 
    161     /// Get a Comdat with the specified name, creating a forward reference
    162     /// record if needed.
    163     Comdat *getComdat(const std::string &N, LocTy Loc);
    164 
    165     // Helper Routines.
    166     bool ParseToken(lltok::Kind T, const char *ErrMsg);
    167     bool EatIfPresent(lltok::Kind T) {
    168       if (Lex.getKind() != T) return false;
    169       Lex.Lex();
    170       return true;
    171     }
    172 
    173     FastMathFlags EatFastMathFlagsIfPresent() {
    174       FastMathFlags FMF;
    175       while (true)
    176         switch (Lex.getKind()) {
    177         case lltok::kw_fast: FMF.setUnsafeAlgebra();   Lex.Lex(); continue;
    178         case lltok::kw_nnan: FMF.setNoNaNs();          Lex.Lex(); continue;
    179         case lltok::kw_ninf: FMF.setNoInfs();          Lex.Lex(); continue;
    180         case lltok::kw_nsz:  FMF.setNoSignedZeros();   Lex.Lex(); continue;
    181         case lltok::kw_arcp: FMF.setAllowReciprocal(); Lex.Lex(); continue;
    182         default: return FMF;
    183         }
    184       return FMF;
    185     }
    186 
    187     bool ParseOptionalToken(lltok::Kind T, bool &Present,
    188                             LocTy *Loc = nullptr) {
    189       if (Lex.getKind() != T) {
    190         Present = false;
    191       } else {
    192         if (Loc)
    193           *Loc = Lex.getLoc();
    194         Lex.Lex();
    195         Present = true;
    196       }
    197       return false;
    198     }
    199     bool ParseStringConstant(std::string &Result);
    200     bool ParseUInt32(unsigned &Val);
    201     bool ParseUInt32(unsigned &Val, LocTy &Loc) {
    202       Loc = Lex.getLoc();
    203       return ParseUInt32(Val);
    204     }
    205 
    206     bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
    207     bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
    208     bool parseOptionalUnnamedAddr(bool &UnnamedAddr) {
    209       return ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr);
    210     }
    211     bool ParseOptionalAddrSpace(unsigned &AddrSpace);
    212     bool ParseOptionalParamAttrs(AttrBuilder &B);
    213     bool ParseOptionalReturnAttrs(AttrBuilder &B);
    214     bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
    215     bool ParseOptionalLinkage(unsigned &Linkage) {
    216       bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
    217     }
    218     bool ParseOptionalVisibility(unsigned &Visibility);
    219     bool ParseOptionalDLLStorageClass(unsigned &DLLStorageClass);
    220     bool ParseOptionalCallingConv(CallingConv::ID &CC);
    221     bool ParseOptionalAlignment(unsigned &Alignment);
    222     bool ParseScopeAndOrdering(bool isAtomic, SynchronizationScope &Scope,
    223                                AtomicOrdering &Ordering);
    224     bool ParseOrdering(AtomicOrdering &Ordering);
    225     bool ParseOptionalStackAlignment(unsigned &Alignment);
    226     bool ParseOptionalCommaAlign(unsigned &Alignment, bool &AteExtraComma);
    227     bool ParseOptionalCommaInAlloca(bool &IsInAlloca);
    228     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices,bool &AteExtraComma);
    229     bool ParseIndexList(SmallVectorImpl<unsigned> &Indices) {
    230       bool AteExtraComma;
    231       if (ParseIndexList(Indices, AteExtraComma)) return true;
    232       if (AteExtraComma)
    233         return TokError("expected index");
    234       return false;
    235     }
    236 
    237     // Top-Level Entities
    238     bool ParseTopLevelEntities();
    239     bool ValidateEndOfModule();
    240     bool ParseTargetDefinition();
    241     bool ParseModuleAsm();
    242     bool ParseDepLibs();        // FIXME: Remove in 4.0.
    243     bool ParseUnnamedType();
    244     bool ParseNamedType();
    245     bool ParseDeclare();
    246     bool ParseDefine();
    247 
    248     bool ParseGlobalType(bool &IsConstant);
    249     bool ParseUnnamedGlobal();
    250     bool ParseNamedGlobal();
    251     bool ParseGlobal(const std::string &Name, LocTy Loc, unsigned Linkage,
    252                      bool HasLinkage, unsigned Visibility,
    253                      unsigned DLLStorageClass,
    254                      GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
    255     bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility,
    256                     unsigned DLLStorageClass,
    257                     GlobalVariable::ThreadLocalMode TLM, bool UnnamedAddr);
    258     bool parseComdat();
    259     bool ParseStandaloneMetadata();
    260     bool ParseNamedMetadata();
    261     bool ParseMDString(MDString *&Result);
    262     bool ParseMDNodeID(MDNode *&Result);
    263     bool ParseMDNodeID(MDNode *&Result, unsigned &SlotNo);
    264     bool ParseUnnamedAttrGrp();
    265     bool ParseFnAttributeValuePairs(AttrBuilder &B,
    266                                     std::vector<unsigned> &FwdRefAttrGrps,
    267                                     bool inAttrGrp, LocTy &BuiltinLoc);
    268 
    269     // Type Parsing.
    270     bool ParseType(Type *&Result, bool AllowVoid = false);
    271     bool ParseType(Type *&Result, LocTy &Loc, bool AllowVoid = false) {
    272       Loc = Lex.getLoc();
    273       return ParseType(Result, AllowVoid);
    274     }
    275     bool ParseAnonStructType(Type *&Result, bool Packed);
    276     bool ParseStructBody(SmallVectorImpl<Type*> &Body);
    277     bool ParseStructDefinition(SMLoc TypeLoc, StringRef Name,
    278                                std::pair<Type*, LocTy> &Entry,
    279                                Type *&ResultTy);
    280 
    281     bool ParseArrayVectorType(Type *&Result, bool isVector);
    282     bool ParseFunctionType(Type *&Result);
    283 
    284     // Function Semantic Analysis.
    285     class PerFunctionState {
    286       LLParser &P;
    287       Function &F;
    288       std::map<std::string, std::pair<Value*, LocTy> > ForwardRefVals;
    289       std::map<unsigned, std::pair<Value*, LocTy> > ForwardRefValIDs;
    290       std::vector<Value*> NumberedVals;
    291 
    292       /// FunctionNumber - If this is an unnamed function, this is the slot
    293       /// number of it, otherwise it is -1.
    294       int FunctionNumber;
    295     public:
    296       PerFunctionState(LLParser &p, Function &f, int FunctionNumber);
    297       ~PerFunctionState();
    298 
    299       Function &getFunction() const { return F; }
    300 
    301       bool FinishFunction();
    302 
    303       /// GetVal - Get a value with the specified name or ID, creating a
    304       /// forward reference record if needed.  This can return null if the value
    305       /// exists but does not have the right type.
    306       Value *GetVal(const std::string &Name, Type *Ty, LocTy Loc);
    307       Value *GetVal(unsigned ID, Type *Ty, LocTy Loc);
    308 
    309       /// SetInstName - After an instruction is parsed and inserted into its
    310       /// basic block, this installs its name.
    311       bool SetInstName(int NameID, const std::string &NameStr, LocTy NameLoc,
    312                        Instruction *Inst);
    313 
    314       /// GetBB - Get a basic block with the specified name or ID, creating a
    315       /// forward reference record if needed.  This can return null if the value
    316       /// is not a BasicBlock.
    317       BasicBlock *GetBB(const std::string &Name, LocTy Loc);
    318       BasicBlock *GetBB(unsigned ID, LocTy Loc);
    319 
    320       /// DefineBB - Define the specified basic block, which is either named or
    321       /// unnamed.  If there is an error, this returns null otherwise it returns
    322       /// the block being defined.
    323       BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
    324     };
    325 
    326     bool ConvertValIDToValue(Type *Ty, ValID &ID, Value *&V,
    327                              PerFunctionState *PFS);
    328 
    329     bool ParseValue(Type *Ty, Value *&V, PerFunctionState *PFS);
    330     bool ParseValue(Type *Ty, Value *&V, PerFunctionState &PFS) {
    331       return ParseValue(Ty, V, &PFS);
    332     }
    333     bool ParseValue(Type *Ty, Value *&V, LocTy &Loc,
    334                     PerFunctionState &PFS) {
    335       Loc = Lex.getLoc();
    336       return ParseValue(Ty, V, &PFS);
    337     }
    338 
    339     bool ParseTypeAndValue(Value *&V, PerFunctionState *PFS);
    340     bool ParseTypeAndValue(Value *&V, PerFunctionState &PFS) {
    341       return ParseTypeAndValue(V, &PFS);
    342     }
    343     bool ParseTypeAndValue(Value *&V, LocTy &Loc, PerFunctionState &PFS) {
    344       Loc = Lex.getLoc();
    345       return ParseTypeAndValue(V, PFS);
    346     }
    347     bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
    348                                 PerFunctionState &PFS);
    349     bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
    350       LocTy Loc;
    351       return ParseTypeAndBasicBlock(BB, Loc, PFS);
    352     }
    353 
    354 
    355     struct ParamInfo {
    356       LocTy Loc;
    357       Value *V;
    358       AttributeSet Attrs;
    359       ParamInfo(LocTy loc, Value *v, AttributeSet attrs)
    360         : Loc(loc), V(v), Attrs(attrs) {}
    361     };
    362     bool ParseParameterList(SmallVectorImpl<ParamInfo> &ArgList,
    363                             PerFunctionState &PFS);
    364 
    365     // Constant Parsing.
    366     bool ParseValID(ValID &ID, PerFunctionState *PFS = nullptr);
    367     bool ParseGlobalValue(Type *Ty, Constant *&V);
    368     bool ParseGlobalTypeAndValue(Constant *&V);
    369     bool ParseGlobalValueVector(SmallVectorImpl<Constant*> &Elts);
    370     bool parseOptionalComdat(Comdat *&C);
    371     bool ParseMetadataListValue(ValID &ID, PerFunctionState *PFS);
    372     bool ParseMetadataValue(ValID &ID, PerFunctionState *PFS);
    373     bool ParseMDNodeVector(SmallVectorImpl<Value*> &, PerFunctionState *PFS);
    374     bool ParseInstructionMetadata(Instruction *Inst, PerFunctionState *PFS);
    375 
    376     // Function Parsing.
    377     struct ArgInfo {
    378       LocTy Loc;
    379       Type *Ty;
    380       AttributeSet Attrs;
    381       std::string Name;
    382       ArgInfo(LocTy L, Type *ty, AttributeSet Attr, const std::string &N)
    383         : Loc(L), Ty(ty), Attrs(Attr), Name(N) {}
    384     };
    385     bool ParseArgumentList(SmallVectorImpl<ArgInfo> &ArgList, bool &isVarArg);
    386     bool ParseFunctionHeader(Function *&Fn, bool isDefine);
    387     bool ParseFunctionBody(Function &Fn);
    388     bool ParseBasicBlock(PerFunctionState &PFS);
    389 
    390     enum TailCallType { TCT_None, TCT_Tail, TCT_MustTail };
    391 
    392     // Instruction Parsing.  Each instruction parsing routine can return with a
    393     // normal result, an error result, or return having eaten an extra comma.
    394     enum InstResult { InstNormal = 0, InstError = 1, InstExtraComma = 2 };
    395     int ParseInstruction(Instruction *&Inst, BasicBlock *BB,
    396                          PerFunctionState &PFS);
    397     bool ParseCmpPredicate(unsigned &Pred, unsigned Opc);
    398 
    399     bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
    400     bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
    401     bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
    402     bool ParseIndirectBr(Instruction *&Inst, PerFunctionState &PFS);
    403     bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
    404     bool ParseResume(Instruction *&Inst, PerFunctionState &PFS);
    405 
    406     bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
    407                          unsigned OperandType);
    408     bool ParseLogical(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
    409     bool ParseCompare(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
    410     bool ParseCast(Instruction *&I, PerFunctionState &PFS, unsigned Opc);
    411     bool ParseSelect(Instruction *&I, PerFunctionState &PFS);
    412     bool ParseVA_Arg(Instruction *&I, PerFunctionState &PFS);
    413     bool ParseExtractElement(Instruction *&I, PerFunctionState &PFS);
    414     bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
    415     bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
    416     int ParsePHI(Instruction *&I, PerFunctionState &PFS);
    417     bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
    418     bool ParseCall(Instruction *&I, PerFunctionState &PFS,
    419                    CallInst::TailCallKind IsTail);
    420     int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
    421     int ParseLoad(Instruction *&I, PerFunctionState &PFS);
    422     int ParseStore(Instruction *&I, PerFunctionState &PFS);
    423     int ParseCmpXchg(Instruction *&I, PerFunctionState &PFS);
    424     int ParseAtomicRMW(Instruction *&I, PerFunctionState &PFS);
    425     int ParseFence(Instruction *&I, PerFunctionState &PFS);
    426     int ParseGetElementPtr(Instruction *&I, PerFunctionState &PFS);
    427     int ParseExtractValue(Instruction *&I, PerFunctionState &PFS);
    428     int ParseInsertValue(Instruction *&I, PerFunctionState &PFS);
    429 
    430     bool ResolveForwardRefBlockAddresses(Function *TheFn,
    431                              std::vector<std::pair<ValID, GlobalValue*> > &Refs,
    432                                          PerFunctionState *PFS);
    433   };
    434 } // End llvm namespace
    435 
    436 #endif
    437