blob: 54cd99a8a4a641a6d6ebc4a3b7d33900fc73c18c [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
14#ifndef TGPARSER_H
15#define TGPARSER_H
16
David Greene917924d2011-10-19 13:02:39 +000017#include "llvm/TableGen/Record.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000018#include "TGLexer.h"
Peter Collingbourne7c788882011-10-01 16:41:13 +000019#include "llvm/TableGen/Error.h"
Benjamin Kramerd1e17032010-09-27 17:42:11 +000020#include "llvm/ADT/Twine.h"
Chris Lattner099e1982009-06-21 03:36:54 +000021#include "llvm/Support/SourceMgr.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;
Chris Lattnerf4601652007-11-22 20:49:04 +000030 struct MultiClass;
Sebastian Redl48fe6352009-03-19 23:26:52 +000031 struct SubClassReference;
David Greenede444af2009-04-22 16:42:54 +000032 struct SubMultiClassReference;
Chris Lattnerf4601652007-11-22 20:49:04 +000033
34 struct LetRecord {
35 std::string Name;
36 std::vector<unsigned> Bits;
David Greene05bce0b2011-07-29 22:43:06 +000037 Init *Value;
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc Loc;
David Greene05bce0b2011-07-29 22:43:06 +000039 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
Eric Christopherd568b3f2011-07-11 23:06:52 +000040 SMLoc L)
Chris Lattnerf4601652007-11-22 20:49:04 +000041 : Name(N), Bits(B), Value(V), Loc(L) {
42 }
43 };
44
45class TGParser {
46 TGLexer Lex;
47 std::vector<std::vector<LetRecord> > LetStack;
48 std::map<std::string, MultiClass*> MultiClasses;
49
50 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
51 /// current value.
52 MultiClass *CurMultiClass;
Chris Lattner67db8832010-12-13 00:23:57 +000053
54 // Record tracker
Chris Lattner9c6b60e2010-12-15 04:48:22 +000055 RecordKeeper &Records;
David Greenef3744a02011-10-19 13:04:20 +000056
57 // A "named boolean" indicating how to parse identifiers. Usually
58 // identifiers map to some existing object but in special cases
59 // (e.g. parsing def names) no such object exists yet because we are
60 // in the middle of creating in. For those situations, allow the
61 // parser to ignore missing object errors.
62 enum IDParseMode {
63 ParseValueMode, // We are parsing a value we expect to look up.
64 ParseNameMode // We are parsing a name of an object that does not yet exist.
65 };
66
Chris Lattnerf4601652007-11-22 20:49:04 +000067public:
Chris Lattner9c6b60e2010-12-15 04:48:22 +000068 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
Chris Lattner67db8832010-12-13 00:23:57 +000069 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
Chris Lattnerf4601652007-11-22 20:49:04 +000070
Chris Lattnerf4601652007-11-22 20:49:04 +000071 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
72 /// routines return true on error, or false on success.
73 bool ParseFile();
74
Benjamin Kramerd1e17032010-09-27 17:42:11 +000075 bool Error(SMLoc L, const Twine &Msg) const {
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000076 PrintError(L, Msg);
Chris Lattnerf4601652007-11-22 20:49:04 +000077 return true;
78 }
Benjamin Kramerd1e17032010-09-27 17:42:11 +000079 bool TokError(const Twine &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000080 return Error(Lex.getLoc(), Msg);
81 }
Joerg Sonnenbergerdd137902011-06-01 13:10:15 +000082 const std::vector<std::string> &getDependencies() const {
83 return Lex.getDependencies();
84 }
Chris Lattnerf4601652007-11-22 20:49:04 +000085private: // Semantic analysis methods.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000086 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
David Greene917924d2011-10-19 13:02:39 +000087 bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
David Greene05bce0b2011-07-29 22:43:06 +000088 const std::vector<unsigned> &BitList, Init *V);
David Greene917924d2011-10-19 13:02:39 +000089 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
90 const std::vector<unsigned> &BitList, Init *V) {
91 return SetValue(TheRec, Loc, StringInit::get(ValName), BitList, V);
92 }
Cedric Venetaff9c272009-02-14 16:06:42 +000093 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
Bob Wilson440548d2009-04-30 18:26:19 +000094 bool AddSubMultiClass(MultiClass *CurMC,
95 SubMultiClassReference &SubMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +000096
97private: // Parser methods.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +000098 bool ParseObjectList(MultiClass *MC = 0);
99 bool ParseObject(MultiClass *MC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000100 bool ParseClass();
101 bool ParseMultiClass();
David Greenee499a2d2011-10-05 22:42:07 +0000102 Record *InstantiateMulticlassDef(MultiClass &MC,
103 Record *DefProto,
David Greene7be867e2011-10-19 13:04:31 +0000104 Init *DefmPrefix,
David Greenee499a2d2011-10-05 22:42:07 +0000105 SMLoc DefmPrefixLoc);
106 bool ResolveMulticlassDefArgs(MultiClass &MC,
107 Record *DefProto,
108 SMLoc DefmPrefixLoc,
109 SMLoc SubClassLoc,
David Greenee22b3212011-10-19 13:02:42 +0000110 const std::vector<Init *> &TArgs,
David Greenee499a2d2011-10-05 22:42:07 +0000111 std::vector<Init *> &TemplateVals,
112 bool DeleteArgs);
113 bool ResolveMulticlassDef(MultiClass &MC,
114 Record *CurRec,
115 Record *DefProto,
116 SMLoc DefmPrefixLoc);
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +0000117 bool ParseDefm(MultiClass *CurMultiClass);
118 bool ParseDef(MultiClass *CurMultiClass);
119 bool ParseTopLevelLet(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000120 std::vector<LetRecord> ParseLetList();
121
Chris Lattnerf4601652007-11-22 20:49:04 +0000122 bool ParseObjectBody(Record *CurRec);
123 bool ParseBody(Record *CurRec);
124 bool ParseBodyItem(Record *CurRec);
125
126 bool ParseTemplateArgList(Record *CurRec);
David Greenee22b3212011-10-19 13:02:42 +0000127 Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
Chris Lattnerf4601652007-11-22 20:49:04 +0000128
129 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
Bob Wilson440548d2009-04-30 18:26:19 +0000130 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
Chris Lattnerf4601652007-11-22 20:49:04 +0000131
David Greenef3744a02011-10-19 13:04:20 +0000132 Init *ParseIDValue(Record *CurRec, IDParseMode Mode = ParseValueMode);
133 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc,
134 IDParseMode Mode = ParseValueMode);
135 Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0,
136 IDParseMode Mode = ParseValueMode);
137 Init *ParseValue(Record *CurRec, RecTy *ItemType = 0,
138 IDParseMode Mode = ParseValueMode);
David Greene05bce0b2011-07-29 22:43:06 +0000139 std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0);
140 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
Chris Lattnerf4601652007-11-22 20:49:04 +0000141 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
142 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
143 std::vector<unsigned> ParseRangeList();
144 bool ParseRangePiece(std::vector<unsigned> &Ranges);
145 RecTy *ParseType();
David Greene05bce0b2011-07-29 22:43:06 +0000146 Init *ParseOperation(Record *CurRec);
David Greened418c1b2009-05-14 20:54:48 +0000147 RecTy *ParseOperatorType();
David Greenea9e07dd2011-10-19 13:04:29 +0000148 Init *ParseObjectName(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +0000149 Record *ParseClassID();
David Greenede444af2009-04-22 16:42:54 +0000150 MultiClass *ParseMultiClassID();
Chris Lattnerf4601652007-11-22 20:49:04 +0000151 Record *ParseDefmID();
152};
153
154} // end namespace llvm
155
156#endif