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