TGParser.h revision 226633
1219820Sjeff//===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
2219820Sjeff//
3219820Sjeff//                     The LLVM Compiler Infrastructure
4219820Sjeff//
5219820Sjeff// This file is distributed under the University of Illinois Open Source
6219820Sjeff// License. See LICENSE.TXT for details.
7219820Sjeff//
8219820Sjeff//===----------------------------------------------------------------------===//
9219820Sjeff//
10219820Sjeff// This class represents the Parser for tablegen files.
11219820Sjeff//
12219820Sjeff//===----------------------------------------------------------------------===//
13219820Sjeff
14219820Sjeff#ifndef TGPARSER_H
15219820Sjeff#define TGPARSER_H
16219820Sjeff
17219820Sjeff#include "TGLexer.h"
18219820Sjeff#include "llvm/TableGen/Error.h"
19219820Sjeff#include "llvm/ADT/Twine.h"
20219820Sjeff#include "llvm/Support/SourceMgr.h"
21219820Sjeff#include <map>
22219820Sjeff
23219820Sjeffnamespace llvm {
24219820Sjeff  class Record;
25219820Sjeff  class RecordVal;
26219820Sjeff  class RecordKeeper;
27219820Sjeff  class RecTy;
28219820Sjeff  class Init;
29219820Sjeff  struct MultiClass;
30219820Sjeff  struct SubClassReference;
31219820Sjeff  struct SubMultiClassReference;
32219820Sjeff
33219820Sjeff  struct LetRecord {
34219820Sjeff    std::string Name;
35219820Sjeff    std::vector<unsigned> Bits;
36219820Sjeff    Init *Value;
37219820Sjeff    SMLoc Loc;
38219820Sjeff    LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
39219820Sjeff              SMLoc L)
40219820Sjeff      : Name(N), Bits(B), Value(V), Loc(L) {
41219820Sjeff    }
42219820Sjeff  };
43219820Sjeff
44219820Sjeffclass TGParser {
45219820Sjeff  TGLexer Lex;
46219820Sjeff  std::vector<std::vector<LetRecord> > LetStack;
47219820Sjeff  std::map<std::string, MultiClass*> MultiClasses;
48219820Sjeff
49219820Sjeff  /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
50219820Sjeff  /// current value.
51219820Sjeff  MultiClass *CurMultiClass;
52219820Sjeff
53219820Sjeff  // Record tracker
54219820Sjeff  RecordKeeper &Records;
55219820Sjeffpublic:
56219820Sjeff  TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
57219820Sjeff    Lex(SrcMgr), CurMultiClass(0), Records(records) {}
58219820Sjeff
59219820Sjeff  /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
60219820Sjeff  /// routines return true on error, or false on success.
61219820Sjeff  bool ParseFile();
62219820Sjeff
63219820Sjeff  bool Error(SMLoc L, const Twine &Msg) const {
64219820Sjeff    PrintError(L, Msg);
65219820Sjeff    return true;
66219820Sjeff  }
67219820Sjeff  bool TokError(const Twine &Msg) const {
68219820Sjeff    return Error(Lex.getLoc(), Msg);
69219820Sjeff  }
70219820Sjeff  const std::vector<std::string> &getDependencies() const {
71219820Sjeff    return Lex.getDependencies();
72219820Sjeff  }
73219820Sjeffprivate:  // Semantic analysis methods.
74219820Sjeff  bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
75219820Sjeff  bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
76219820Sjeff                const std::vector<unsigned> &BitList, Init *V);
77219820Sjeff  bool AddSubClass(Record *Rec, SubClassReference &SubClass);
78219820Sjeff  bool AddSubMultiClass(MultiClass *CurMC,
79219820Sjeff                        SubMultiClassReference &SubMultiClass);
80219820Sjeff
81219820Sjeffprivate:  // Parser methods.
82219820Sjeff  bool ParseObjectList(MultiClass *MC = 0);
83219820Sjeff  bool ParseObject(MultiClass *MC);
84219820Sjeff  bool ParseClass();
85219820Sjeff  bool ParseMultiClass();
86219820Sjeff  Record *InstantiateMulticlassDef(MultiClass &MC,
87219820Sjeff                                   Record *DefProto,
88219820Sjeff                                   const std::string &DefmPrefix,
89219820Sjeff                                   SMLoc DefmPrefixLoc);
90219820Sjeff  bool ResolveMulticlassDefArgs(MultiClass &MC,
91219820Sjeff                                Record *DefProto,
92219820Sjeff                                SMLoc DefmPrefixLoc,
93219820Sjeff                                SMLoc SubClassLoc,
94219820Sjeff                                const std::vector<std::string> &TArgs,
95219820Sjeff                                std::vector<Init *> &TemplateVals,
96219820Sjeff                                bool DeleteArgs);
97219820Sjeff  bool ResolveMulticlassDef(MultiClass &MC,
98219820Sjeff                            Record *CurRec,
99219820Sjeff                            Record *DefProto,
100219820Sjeff                            SMLoc DefmPrefixLoc);
101219820Sjeff  bool ParseDefm(MultiClass *CurMultiClass);
102219820Sjeff  bool ParseDef(MultiClass *CurMultiClass);
103219820Sjeff  bool ParseTopLevelLet(MultiClass *CurMultiClass);
104219820Sjeff  std::vector<LetRecord> ParseLetList();
105219820Sjeff
106219820Sjeff  bool ParseObjectBody(Record *CurRec);
107219820Sjeff  bool ParseBody(Record *CurRec);
108219820Sjeff  bool ParseBodyItem(Record *CurRec);
109219820Sjeff
110219820Sjeff  bool ParseTemplateArgList(Record *CurRec);
111219820Sjeff  std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
112219820Sjeff
113219820Sjeff  SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
114219820Sjeff  SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
115219820Sjeff
116219820Sjeff  Init *ParseIDValue(Record *CurRec);
117219820Sjeff  Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc);
118219820Sjeff  Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0);
119219820Sjeff  Init *ParseValue(Record *CurRec, RecTy *ItemType = 0);
120219820Sjeff  std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
121219820Sjeff  std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
122219820Sjeff  bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
123219820Sjeff  bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
124219820Sjeff  std::vector<unsigned> ParseRangeList();
125219820Sjeff  bool ParseRangePiece(std::vector<unsigned> &Ranges);
126219820Sjeff  RecTy *ParseType();
127219820Sjeff  Init *ParseOperation(Record *CurRec);
128219820Sjeff  RecTy *ParseOperatorType();
129219820Sjeff  std::string ParseObjectName();
130219820Sjeff  Record *ParseClassID();
131219820Sjeff  MultiClass *ParseMultiClassID();
132219820Sjeff  Record *ParseDefmID();
133219820Sjeff};
134219820Sjeff
135219820Sjeff} // end namespace llvm
136219820Sjeff
137219820Sjeff#endif
138