Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 1 | //===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This class represents the Parser for tablegen files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef TGPARSER_H |
| 15 | #define TGPARSER_H |
| 16 | |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 17 | #include "llvm/TableGen/Record.h" |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 18 | #include "TGLexer.h" |
Peter Collingbourne | 7c78888 | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 19 | #include "llvm/TableGen/Error.h" |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Twine.h" |
Chris Lattner | 099e198 | 2009-06-21 03:36:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 22 | #include <map> |
| 23 | |
| 24 | namespace llvm { |
| 25 | class Record; |
| 26 | class RecordVal; |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 27 | class RecordKeeper; |
Jakob Stoklund Olesen | 77f8274 | 2011-07-18 17:02:57 +0000 | [diff] [blame] | 28 | class RecTy; |
David Greene | afd5426 | 2011-07-13 22:25:51 +0000 | [diff] [blame] | 29 | class Init; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 30 | struct MultiClass; |
Sebastian Redl | 48fe635 | 2009-03-19 23:26:52 +0000 | [diff] [blame] | 31 | struct SubClassReference; |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 32 | struct SubMultiClassReference; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 33 | |
| 34 | struct LetRecord { |
| 35 | std::string Name; |
| 36 | std::vector<unsigned> Bits; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 37 | Init *Value; |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 38 | SMLoc Loc; |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 39 | LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V, |
Eric Christopher | d568b3f | 2011-07-11 23:06:52 +0000 | [diff] [blame] | 40 | SMLoc L) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 41 | : Name(N), Bits(B), Value(V), Loc(L) { |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | class 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 Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 53 | |
| 54 | // Record tracker |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 55 | RecordKeeper &Records; |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 56 | |
| 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 Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 67 | public: |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 68 | TGParser(SourceMgr &SrcMgr, RecordKeeper &records) : |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 69 | Lex(SrcMgr), CurMultiClass(0), Records(records) {} |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 70 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 71 | /// 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 Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 75 | bool Error(SMLoc L, const Twine &Msg) const { |
Jim Grosbach | 0b6a44a | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 76 | PrintError(L, Msg); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 77 | return true; |
| 78 | } |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 79 | bool TokError(const Twine &Msg) const { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 80 | return Error(Lex.getLoc(), Msg); |
| 81 | } |
Joerg Sonnenberger | dd13790 | 2011-06-01 13:10:15 +0000 | [diff] [blame] | 82 | const std::vector<std::string> &getDependencies() const { |
| 83 | return Lex.getDependencies(); |
| 84 | } |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 85 | private: // Semantic analysis methods. |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 86 | bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV); |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 87 | bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName, |
David Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 88 | const std::vector<unsigned> &BitList, Init *V); |
David Greene | 917924d | 2011-10-19 13:02:39 +0000 | [diff] [blame] | 89 | 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 Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 93 | bool AddSubClass(Record *Rec, SubClassReference &SubClass); |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 94 | bool AddSubMultiClass(MultiClass *CurMC, |
| 95 | SubMultiClassReference &SubMultiClass); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 96 | |
| 97 | private: // Parser methods. |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 98 | bool ParseObjectList(MultiClass *MC = 0); |
| 99 | bool ParseObject(MultiClass *MC); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 100 | bool ParseClass(); |
| 101 | bool ParseMultiClass(); |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 102 | Record *InstantiateMulticlassDef(MultiClass &MC, |
| 103 | Record *DefProto, |
David Greene | 7be867e | 2011-10-19 13:04:31 +0000 | [diff] [blame] | 104 | Init *DefmPrefix, |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 105 | SMLoc DefmPrefixLoc); |
| 106 | bool ResolveMulticlassDefArgs(MultiClass &MC, |
| 107 | Record *DefProto, |
| 108 | SMLoc DefmPrefixLoc, |
| 109 | SMLoc SubClassLoc, |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 110 | const std::vector<Init *> &TArgs, |
David Greene | e499a2d | 2011-10-05 22:42:07 +0000 | [diff] [blame] | 111 | std::vector<Init *> &TemplateVals, |
| 112 | bool DeleteArgs); |
| 113 | bool ResolveMulticlassDef(MultiClass &MC, |
| 114 | Record *CurRec, |
| 115 | Record *DefProto, |
| 116 | SMLoc DefmPrefixLoc); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 117 | bool ParseDefm(MultiClass *CurMultiClass); |
| 118 | bool ParseDef(MultiClass *CurMultiClass); |
| 119 | bool ParseTopLevelLet(MultiClass *CurMultiClass); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 120 | std::vector<LetRecord> ParseLetList(); |
| 121 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 122 | bool ParseObjectBody(Record *CurRec); |
| 123 | bool ParseBody(Record *CurRec); |
| 124 | bool ParseBodyItem(Record *CurRec); |
| 125 | |
| 126 | bool ParseTemplateArgList(Record *CurRec); |
David Greene | e22b321 | 2011-10-19 13:02:42 +0000 | [diff] [blame] | 127 | Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 128 | |
| 129 | SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm); |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 130 | SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 131 | |
David Greene | f3744a0 | 2011-10-19 13:04:20 +0000 | [diff] [blame] | 132 | 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 Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 139 | std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0); |
| 140 | std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 141 | 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 Greene | 05bce0b | 2011-07-29 22:43:06 +0000 | [diff] [blame] | 146 | Init *ParseOperation(Record *CurRec); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 147 | RecTy *ParseOperatorType(); |
David Greene | a9e07dd | 2011-10-19 13:04:29 +0000 | [diff] [blame] | 148 | Init *ParseObjectName(MultiClass *CurMultiClass); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 149 | Record *ParseClassID(); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 150 | MultiClass *ParseMultiClassID(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 151 | Record *ParseDefmID(); |
| 152 | }; |
| 153 | |
| 154 | } // end namespace llvm |
| 155 | |
| 156 | #endif |