blob: e3849043513b58106ecb71d8d4768d1121a2e20d [file] [log] [blame]
Chris Lattnerf4601652007-11-22 20:49:04 +00001//===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerf4601652007-11-22 20:49:04 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This class represents the Parser for tablegen files.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
15#define LLVM_LIB_TABLEGEN_TGPARSER_H
Chris Lattnerf4601652007-11-22 20:49:04 +000016
17#include "TGLexer.h"
Benjamin Kramerd1e17032010-09-27 17:42:11 +000018#include "llvm/ADT/Twine.h"
Chris Lattner099e1982009-06-21 03:36:54 +000019#include "llvm/Support/SourceMgr.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000020#include "llvm/TableGen/Error.h"
21#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000022#include <map>
23
24namespace llvm {
25 class Record;
26 class RecordVal;
Chris Lattner67db8832010-12-13 00:23:57 +000027 class RecordKeeper;
Jakob Stoklund Olesen77f82742011-07-18 17:02:57 +000028 class RecTy;
David Greeneafd54262011-07-13 22:25:51 +000029 class Init;
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000030 struct ForeachLoop;
Chris Lattnerf4601652007-11-22 20:49:04 +000031 struct MultiClass;
Sebastian Redl48fe6352009-03-19 23:26:52 +000032 struct SubClassReference;
David Greenede444af2009-04-22 16:42:54 +000033 struct SubMultiClassReference;
Sean Silva047d3612012-10-04 00:54:27 +000034
Chris Lattnerf4601652007-11-22 20:49:04 +000035 struct LetRecord {
Matthias Braun2b86a872016-12-05 07:35:13 +000036 StringInit *Name;
Chris Lattnerf4601652007-11-22 20:49:04 +000037 std::vector<unsigned> Bits;
David Greene05bce0b2011-07-29 22:43:06 +000038 Init *Value;
Chris Lattner1e3a8a42009-06-21 03:39:35 +000039 SMLoc Loc;
Matthias Braun2b86a872016-12-05 07:35:13 +000040 LetRecord(StringInit *N, ArrayRef<unsigned> B, Init *V, SMLoc L)
Chris Lattnerf4601652007-11-22 20:49:04 +000041 : Name(N), Bits(B), Value(V), Loc(L) {
42 }
43 };
Sean Silva047d3612012-10-04 00:54:27 +000044
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000045 /// RecordsEntry - Can be either a record or a foreach loop.
46 struct RecordsEntry {
47 std::unique_ptr<Record> Rec;
48 std::unique_ptr<ForeachLoop> Loop;
49
50 void dump() const;
51
52 RecordsEntry() {}
53 RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
54 RecordsEntry(std::unique_ptr<ForeachLoop> Loop)
55 : Loop(std::move(Loop)) {}
56 };
57
David Greenecebb4ee2012-02-22 16:09:41 +000058 /// ForeachLoop - Record the iteration state associated with a for loop.
59 /// This is used to instantiate items in the loop body.
60 struct ForeachLoop {
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000061 SMLoc Loc;
Jakob Stoklund Olesen8e5286e2012-05-24 22:17:33 +000062 VarInit *IterVar;
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000063 Init *ListValue;
64 std::vector<RecordsEntry> Entries;
David Greenecebb4ee2012-02-22 16:09:41 +000065
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000066 void dump() const;
67
68 ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
69 : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
David Greenecebb4ee2012-02-22 16:09:41 +000070 };
71
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +000072 struct DefsetRecord {
73 SMLoc Loc;
74 RecTy *EltTy;
75 SmallVector<Init *, 16> Elements;
76 };
77
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000078struct MultiClass {
79 Record Rec; // Placeholder for template args and Name.
80 std::vector<RecordsEntry> Entries;
81
82 void dump() const;
83
84 MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
85 Rec(Name, Loc, Records) {}
86};
87
Chris Lattnerf4601652007-11-22 20:49:04 +000088class TGParser {
89 TGLexer Lex;
Matthias Brauna3f0e482016-12-05 06:41:54 +000090 std::vector<SmallVector<LetRecord, 4>> LetStack;
Craig Topper50083142014-12-11 05:25:30 +000091 std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
Sean Silva047d3612012-10-04 00:54:27 +000092
David Greenecebb4ee2012-02-22 16:09:41 +000093 /// Loops - Keep track of any foreach loops we are within.
94 ///
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +000095 std::vector<std::unique_ptr<ForeachLoop>> Loops;
David Greenecebb4ee2012-02-22 16:09:41 +000096
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +000097 SmallVector<DefsetRecord *, 2> Defsets;
98
Sean Silva047d3612012-10-04 00:54:27 +000099 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 /// current value.
101 MultiClass *CurMultiClass;
Chris Lattner67db8832010-12-13 00:23:57 +0000102
103 // Record tracker
Chris Lattner9c6b60e2010-12-15 04:48:22 +0000104 RecordKeeper &Records;
David Greenef3744a02011-10-19 13:04:20 +0000105
106 // A "named boolean" indicating how to parse identifiers. Usually
107 // identifiers map to some existing object but in special cases
108 // (e.g. parsing def names) no such object exists yet because we are
109 // in the middle of creating in. For those situations, allow the
110 // parser to ignore missing object errors.
111 enum IDParseMode {
David Greenecebb4ee2012-02-22 16:09:41 +0000112 ParseValueMode, // We are parsing a value we expect to look up.
113 ParseNameMode, // We are parsing a name of an object that does not yet
114 // exist.
David Greenef3744a02011-10-19 13:04:20 +0000115 };
116
Chris Lattnerf4601652007-11-22 20:49:04 +0000117public:
Vyacheslav Zakharin6c99d2b2018-11-27 18:57:43 +0000118 TGParser(SourceMgr &SrcMgr, ArrayRef<std::string> Macros,
119 RecordKeeper &records)
120 : Lex(SrcMgr, Macros), CurMultiClass(nullptr), Records(records) {}
Sean Silva047d3612012-10-04 00:54:27 +0000121
Chris Lattnerf4601652007-11-22 20:49:04 +0000122 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
123 /// routines return true on error, or false on success.
124 bool ParseFile();
Sean Silva047d3612012-10-04 00:54:27 +0000125
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000126 bool Error(SMLoc L, const Twine &Msg) const {
Jim Grosbach0b6a44a2011-06-21 22:55:50 +0000127 PrintError(L, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +0000128 return true;
129 }
Benjamin Kramerd1e17032010-09-27 17:42:11 +0000130 bool TokError(const Twine &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +0000131 return Error(Lex.getLoc(), Msg);
132 }
Sean Silvaa170f522013-02-07 04:30:39 +0000133 const TGLexer::DependenciesMapTy &getDependencies() const {
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +0000134 return Lex.getDependencies();
135 }
David Greenecebb4ee2012-02-22 16:09:41 +0000136
Chris Lattnerf4601652007-11-22 20:49:04 +0000137private: // Semantic analysis methods.
Chris Lattner1e3a8a42009-06-21 03:39:35 +0000138 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
Sean Silva047d3612012-10-04 00:54:27 +0000139 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
Craig Topper2507b002016-01-04 03:15:08 +0000140 ArrayRef<unsigned> BitList, Init *V,
Craig Topperae626362016-01-04 03:05:14 +0000141 bool AllowSelfAssignment = false);
Cedric Venetaff9c272009-02-14 16:06:42 +0000142 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000143 bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
Bob Wilson440548d2009-04-30 18:26:19 +0000144 bool AddSubMultiClass(MultiClass *CurMC,
145 SubMultiClassReference &SubMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000146
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000147 using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;
David Greenecebb4ee2012-02-22 16:09:41 +0000148
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000149 bool addEntry(RecordsEntry E);
150 bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
151 std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
152 bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
153 bool Final, std::vector<RecordsEntry> *Dest,
154 SMLoc *Loc = nullptr);
155 bool addDefOne(std::unique_ptr<Record> Rec);
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +0000156
Chris Lattnerf4601652007-11-22 20:49:04 +0000157private: // Parser methods.
Craig Topperc34a25d2014-04-28 04:05:08 +0000158 bool ParseObjectList(MultiClass *MC = nullptr);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000159 bool ParseObject(MultiClass *MC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000160 bool ParseClass();
161 bool ParseMultiClass();
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000162 bool ParseDefm(MultiClass *CurMultiClass);
163 bool ParseDef(MultiClass *CurMultiClass);
Nicolai Haehnled66fa2a2018-03-09 12:24:42 +0000164 bool ParseDefset();
David Greenecebb4ee2012-02-22 16:09:41 +0000165 bool ParseForeach(MultiClass *CurMultiClass);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000166 bool ParseTopLevelLet(MultiClass *CurMultiClass);
Matthias Brauna3f0e482016-12-05 06:41:54 +0000167 void ParseLetList(SmallVectorImpl<LetRecord> &Result);
Chris Lattnerf4601652007-11-22 20:49:04 +0000168
Chris Lattnerf4601652007-11-22 20:49:04 +0000169 bool ParseObjectBody(Record *CurRec);
170 bool ParseBody(Record *CurRec);
171 bool ParseBodyItem(Record *CurRec);
172
173 bool ParseTemplateArgList(Record *CurRec);
David Greenee22b3212011-10-19 13:02:42 +0000174 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000175 VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
Chris Lattnerf4601652007-11-22 20:49:04 +0000176
177 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
Bob Wilson440548d2009-04-30 18:26:19 +0000178 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000179
Matthias Braun2b86a872016-12-05 07:35:13 +0000180 Init *ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
David Greenef3744a02011-10-19 13:04:20 +0000181 IDParseMode Mode = ParseValueMode);
Craig Topperc34a25d2014-04-28 04:05:08 +0000182 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
David Greenef3744a02011-10-19 13:04:20 +0000183 IDParseMode Mode = ParseValueMode);
Craig Topperc34a25d2014-04-28 04:05:08 +0000184 Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
David Greenef3744a02011-10-19 13:04:20 +0000185 IDParseMode Mode = ParseValueMode);
Matthias Brauna3f0e482016-12-05 06:41:54 +0000186 void ParseValueList(SmallVectorImpl<llvm::Init*> &Result, Record *CurRec,
187 Record *ArgsRec = nullptr, RecTy *EltTy = nullptr);
Matthias Braun5a87cb22016-12-05 06:41:51 +0000188 void ParseDagArgList(
189 SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
190 Record *CurRec);
Matthias Brauna3f0e482016-12-05 06:41:54 +0000191 bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
192 bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
193 void ParseRangeList(SmallVectorImpl<unsigned> &Result);
194 bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges);
Chris Lattnerf4601652007-11-22 20:49:04 +0000195 RecTy *ParseType();
Matt Arsenault258e8222014-06-10 20:10:08 +0000196 Init *ParseOperation(Record *CurRec, RecTy *ItemType);
David Greened418c1b2009-05-14 20:54:48 +0000197 RecTy *ParseOperatorType();
David Greenea9e07dd2011-10-19 13:04:29 +0000198 Init *ParseObjectName(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000199 Record *ParseClassID();
David Greenede444af2009-04-22 16:42:54 +0000200 MultiClass *ParseMultiClassID();
Sean Silva9cceede2013-01-09 04:49:14 +0000201 bool ApplyLetStack(Record *CurRec);
Nicolai Haehnle9f2ffce2018-06-21 13:35:44 +0000202 bool ApplyLetStack(RecordsEntry &Entry);
Chris Lattnerf4601652007-11-22 20:49:04 +0000203};
Sean Silva047d3612012-10-04 00:54:27 +0000204
Chris Lattnerf4601652007-11-22 20:49:04 +0000205} // end namespace llvm
206
207#endif