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 | |
| 17 | #include "TGLexer.h" |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Twine.h" |
Chris Lattner | 099e198 | 2009-06-21 03:36:54 +0000 | [diff] [blame] | 19 | #include "llvm/Support/SourceMgr.h" |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 20 | #include <map> |
| 21 | |
| 22 | namespace llvm { |
| 23 | class Record; |
| 24 | class RecordVal; |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 25 | class RecordKeeper; |
Chris Lattner | 41b2340 | 2009-01-22 05:10:16 +0000 | [diff] [blame] | 26 | struct RecTy; |
| 27 | struct Init; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 28 | struct MultiClass; |
Sebastian Redl | 48fe635 | 2009-03-19 23:26:52 +0000 | [diff] [blame] | 29 | struct SubClassReference; |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 30 | struct SubMultiClassReference; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 31 | |
| 32 | struct LetRecord { |
| 33 | std::string Name; |
| 34 | std::vector<unsigned> Bits; |
| 35 | Init *Value; |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 36 | SMLoc Loc; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 37 | LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V, |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 38 | SMLoc L) |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 39 | : Name(N), Bits(B), Value(V), Loc(L) { |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | class TGParser { |
| 44 | TGLexer Lex; |
| 45 | std::vector<std::vector<LetRecord> > LetStack; |
| 46 | std::map<std::string, MultiClass*> MultiClasses; |
| 47 | |
| 48 | /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the |
| 49 | /// current value. |
| 50 | MultiClass *CurMultiClass; |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 51 | |
| 52 | // Record tracker |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 53 | RecordKeeper &Records; |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 54 | public: |
Chris Lattner | 9c6b60e | 2010-12-15 04:48:22 +0000 | [diff] [blame] | 55 | TGParser(SourceMgr &SrcMgr, RecordKeeper &records) : |
Chris Lattner | 67db883 | 2010-12-13 00:23:57 +0000 | [diff] [blame] | 56 | Lex(SrcMgr), CurMultiClass(0), Records(records) {} |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 57 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 58 | /// ParseFile - Main entrypoint for parsing a tblgen file. These parser |
| 59 | /// routines return true on error, or false on success. |
| 60 | bool ParseFile(); |
| 61 | |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 62 | bool Error(SMLoc L, const Twine &Msg) const { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 63 | Lex.PrintError(L, Msg); |
| 64 | return true; |
| 65 | } |
Benjamin Kramer | d1e1703 | 2010-09-27 17:42:11 +0000 | [diff] [blame] | 66 | bool TokError(const Twine &Msg) const { |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 67 | return Error(Lex.getLoc(), Msg); |
| 68 | } |
| 69 | private: // Semantic analysis methods. |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 70 | bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV); |
| 71 | bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName, |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 72 | const std::vector<unsigned> &BitList, Init *V); |
Cedric Venet | aff9c27 | 2009-02-14 16:06:42 +0000 | [diff] [blame] | 73 | bool AddSubClass(Record *Rec, SubClassReference &SubClass); |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 74 | bool AddSubMultiClass(MultiClass *CurMC, |
| 75 | SubMultiClassReference &SubMultiClass); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 76 | |
| 77 | private: // Parser methods. |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 78 | bool ParseObjectList(MultiClass *MC = 0); |
| 79 | bool ParseObject(MultiClass *MC); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 80 | bool ParseClass(); |
| 81 | bool ParseMultiClass(); |
Bruno Cardoso Lopes | ee65db3 | 2010-06-10 02:42:59 +0000 | [diff] [blame] | 82 | bool ParseDefm(MultiClass *CurMultiClass); |
| 83 | bool ParseDef(MultiClass *CurMultiClass); |
| 84 | bool ParseTopLevelLet(MultiClass *CurMultiClass); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 85 | std::vector<LetRecord> ParseLetList(); |
| 86 | |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 87 | bool ParseObjectBody(Record *CurRec); |
| 88 | bool ParseBody(Record *CurRec); |
| 89 | bool ParseBodyItem(Record *CurRec); |
| 90 | |
| 91 | bool ParseTemplateArgList(Record *CurRec); |
| 92 | std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs); |
| 93 | |
| 94 | SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm); |
Bob Wilson | 440548d | 2009-04-30 18:26:19 +0000 | [diff] [blame] | 95 | SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 96 | |
| 97 | Init *ParseIDValue(Record *CurRec); |
Chris Lattner | 1e3a8a4 | 2009-06-21 03:39:35 +0000 | [diff] [blame] | 98 | Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc); |
David Greene | e1b4691 | 2009-06-08 20:23:18 +0000 | [diff] [blame] | 99 | Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = 0); |
| 100 | Init *ParseValue(Record *CurRec, RecTy *ItemType = 0); |
| 101 | std::vector<Init*> ParseValueList(Record *CurRec, Record *ArgsRec = 0, RecTy *EltTy = 0); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 102 | std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *); |
| 103 | bool ParseOptionalRangeList(std::vector<unsigned> &Ranges); |
| 104 | bool ParseOptionalBitList(std::vector<unsigned> &Ranges); |
| 105 | std::vector<unsigned> ParseRangeList(); |
| 106 | bool ParseRangePiece(std::vector<unsigned> &Ranges); |
| 107 | RecTy *ParseType(); |
David Greene | d418c1b | 2009-05-14 20:54:48 +0000 | [diff] [blame] | 108 | Init *ParseOperation(Record *CurRec); |
| 109 | RecTy *ParseOperatorType(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 110 | std::string ParseObjectName(); |
| 111 | Record *ParseClassID(); |
David Greene | de444af | 2009-04-22 16:42:54 +0000 | [diff] [blame] | 112 | MultiClass *ParseMultiClassID(); |
Chris Lattner | f460165 | 2007-11-22 20:49:04 +0000 | [diff] [blame] | 113 | Record *ParseDefmID(); |
| 114 | }; |
| 115 | |
| 116 | } // end namespace llvm |
| 117 | |
| 118 | #endif |