Home | History | Annotate | Download | only in TableGen
      1 //===- TGParser.h - Parser for TableGen Files -------------------*- 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 class represents the Parser for tablegen files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef TGPARSER_H
     15 #define TGPARSER_H
     16 
     17 #include "TGLexer.h"
     18 #include "llvm/ADT/Twine.h"
     19 #include "llvm/Support/SourceMgr.h"
     20 #include "llvm/TableGen/Error.h"
     21 #include "llvm/TableGen/Record.h"
     22 #include <map>
     23 
     24 namespace llvm {
     25   class Record;
     26   class RecordVal;
     27   class RecordKeeper;
     28   class RecTy;
     29   class Init;
     30   struct MultiClass;
     31   struct SubClassReference;
     32   struct SubMultiClassReference;
     33 
     34   struct LetRecord {
     35     std::string Name;
     36     std::vector<unsigned> Bits;
     37     Init *Value;
     38     SMLoc Loc;
     39     LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
     40               SMLoc L)
     41       : Name(N), Bits(B), Value(V), Loc(L) {
     42     }
     43   };
     44 
     45   /// ForeachLoop - Record the iteration state associated with a for loop.
     46   /// This is used to instantiate items in the loop body.
     47   struct ForeachLoop {
     48     VarInit *IterVar;
     49     ListInit *ListValue;
     50 
     51     ForeachLoop(VarInit *IVar, ListInit *LValue)
     52       : IterVar(IVar), ListValue(LValue) {}
     53   };
     54 
     55 class TGParser {
     56   TGLexer Lex;
     57   std::vector<std::vector<LetRecord> > LetStack;
     58   std::map<std::string, MultiClass*> MultiClasses;
     59 
     60   /// Loops - Keep track of any foreach loops we are within.
     61   ///
     62   typedef std::vector<ForeachLoop> LoopVector;
     63   LoopVector Loops;
     64 
     65   /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
     66   /// current value.
     67   MultiClass *CurMultiClass;
     68 
     69   // Record tracker
     70   RecordKeeper &Records;
     71 
     72   // A "named boolean" indicating how to parse identifiers.  Usually
     73   // identifiers map to some existing object but in special cases
     74   // (e.g. parsing def names) no such object exists yet because we are
     75   // in the middle of creating in.  For those situations, allow the
     76   // parser to ignore missing object errors.
     77   enum IDParseMode {
     78     ParseValueMode,   // We are parsing a value we expect to look up.
     79     ParseNameMode,    // We are parsing a name of an object that does not yet
     80                       // exist.
     81     ParseForeachMode  // We are parsing a foreach init.
     82   };
     83 
     84 public:
     85   TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
     86     Lex(SrcMgr), CurMultiClass(0), Records(records) {}
     87 
     88   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
     89   /// routines return true on error, or false on success.
     90   bool ParseFile();
     91 
     92   bool Error(SMLoc L, const Twine &Msg) const {
     93     PrintError(L, Msg);
     94     return true;
     95   }
     96   bool TokError(const Twine &Msg) const {
     97     return Error(Lex.getLoc(), Msg);
     98   }
     99   const TGLexer::DependenciesMapTy &getDependencies() const {
    100     return Lex.getDependencies();
    101   }
    102 
    103 private:  // Semantic analysis methods.
    104   bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
    105   bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
    106                 const std::vector<unsigned> &BitList, Init *V);
    107   bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
    108                 const std::vector<unsigned> &BitList, Init *V) {
    109     return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
    110   }
    111   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
    112   bool AddSubMultiClass(MultiClass *CurMC,
    113                         SubMultiClassReference &SubMultiClass);
    114 
    115   // IterRecord: Map an iterator name to a value.
    116   struct IterRecord {
    117     VarInit *IterVar;
    118     Init *IterValue;
    119     IterRecord(VarInit *Var, Init *Val) : IterVar(Var), IterValue(Val) {}
    120   };
    121 
    122   // IterSet: The set of all iterator values at some point in the
    123   // iteration space.
    124   typedef std::vector<IterRecord> IterSet;
    125 
    126   bool ProcessForeachDefs(Record *CurRec, SMLoc Loc);
    127   bool ProcessForeachDefs(Record *CurRec, SMLoc Loc, IterSet &IterVals);
    128 
    129 private:  // Parser methods.
    130   bool ParseObjectList(MultiClass *MC = 0);
    131   bool ParseObject(MultiClass *MC);
    132   bool ParseClass();
    133   bool ParseMultiClass();
    134   Record *InstantiateMulticlassDef(MultiClass &MC,
    135                                    Record *DefProto,
    136                                    Init *DefmPrefix,
    137                                    SMRange DefmPrefixRange);
    138   bool ResolveMulticlassDefArgs(MultiClass &MC,
    139                                 Record *DefProto,
    140                                 SMLoc DefmPrefixLoc,
    141                                 SMLoc SubClassLoc,
    142                                 const std::vector<Init *> &TArgs,
    143                                 std::vector<Init *> &TemplateVals,
    144                                 bool DeleteArgs);
    145   bool ResolveMulticlassDef(MultiClass &MC,
    146                             Record *CurRec,
    147                             Record *DefProto,
    148                             SMLoc DefmPrefixLoc);
    149   bool ParseDefm(MultiClass *CurMultiClass);
    150   bool ParseDef(MultiClass *CurMultiClass);
    151   bool ParseForeach(MultiClass *CurMultiClass);
    152   bool ParseTopLevelLet(MultiClass *CurMultiClass);
    153   std::vector<LetRecord> ParseLetList();
    154 
    155   bool ParseObjectBody(Record *CurRec);
    156   bool ParseBody(Record *CurRec);
    157   bool ParseBodyItem(Record *CurRec);
    158 
    159   bool ParseTemplateArgList(Record *CurRec);
    160   Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
    161   VarInit *ParseForeachDeclaration(ListInit *&ForeachListValue);
    162 
    163   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
    164   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
    165 
    166   Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
    167   Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
    168                      IDParseMode Mode = ParseValueMode);
    169   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
    170                          IDParseMode Mode = ParseValueMode);
    171   Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
    172                    IDParseMode Mode = ParseValueMode);
    173   std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0,
    174                                     RecTy *EltTy = 0);
    175   std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
    176   bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
    177   bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
    178   std::vector<unsigned> ParseRangeList();
    179   bool ParseRangePiece(std::vector<unsigned> &Ranges);
    180   RecTy *ParseType();
    181   Init *ParseOperation(Record *CurRec);
    182   RecTy *ParseOperatorType();
    183   Init *ParseObjectName(MultiClass *CurMultiClass);
    184   Record *ParseClassID();
    185   MultiClass *ParseMultiClassID();
    186   bool ApplyLetStack(Record *CurRec);
    187 };
    188 
    189 } // end namespace llvm
    190 
    191 #endif
    192