blob: 9cdf68ff974947e43409d62981461d59e919702b [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
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"
Chris Lattnerf4601652007-11-22 20:49:04 +000020#include <map>
21
22namespace llvm {
23 class Record;
24 class RecordVal;
Chris Lattner67db8832010-12-13 00:23:57 +000025 class RecordKeeper;
Chris Lattner41b23402009-01-22 05:10:16 +000026 struct RecTy;
27 struct Init;
Chris Lattnerf4601652007-11-22 20:49:04 +000028 struct MultiClass;
Sebastian Redl48fe6352009-03-19 23:26:52 +000029 struct SubClassReference;
David Greenede444af2009-04-22 16:42:54 +000030 struct SubMultiClassReference;
Chris Lattnerf4601652007-11-22 20:49:04 +000031
32 struct LetRecord {
33 std::string Name;
34 std::vector<unsigned> Bits;
35 Init *Value;
Chris Lattner1e3a8a42009-06-21 03:39:35 +000036 SMLoc Loc;
Chris Lattnerf4601652007-11-22 20:49:04 +000037 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
Chris Lattner1e3a8a42009-06-21 03:39:35 +000038 SMLoc L)
Chris Lattnerf4601652007-11-22 20:49:04 +000039 : Name(N), Bits(B), Value(V), Loc(L) {
40 }
41 };
42
43class 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 Lattner67db8832010-12-13 00:23:57 +000051
52 // Record tracker
Chris Lattner9c6b60e2010-12-15 04:48:22 +000053 RecordKeeper &Records;
Chris Lattnerf4601652007-11-22 20:49:04 +000054public:
Chris Lattner9c6b60e2010-12-15 04:48:22 +000055 TGParser(SourceMgr &SrcMgr, RecordKeeper &records) :
Chris Lattner67db8832010-12-13 00:23:57 +000056 Lex(SrcMgr), CurMultiClass(0), Records(records) {}
Chris Lattnerf4601652007-11-22 20:49:04 +000057
Chris Lattnerf4601652007-11-22 20:49:04 +000058 /// 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 Kramerd1e17032010-09-27 17:42:11 +000062 bool Error(SMLoc L, const Twine &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000063 Lex.PrintError(L, Msg);
64 return true;
65 }
Benjamin Kramerd1e17032010-09-27 17:42:11 +000066 bool TokError(const Twine &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000067 return Error(Lex.getLoc(), Msg);
68 }
69private: // Semantic analysis methods.
Chris Lattner1e3a8a42009-06-21 03:39:35 +000070 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
71 bool SetValue(Record *TheRec, SMLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000072 const std::vector<unsigned> &BitList, Init *V);
Cedric Venetaff9c272009-02-14 16:06:42 +000073 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
Bob Wilson440548d2009-04-30 18:26:19 +000074 bool AddSubMultiClass(MultiClass *CurMC,
75 SubMultiClassReference &SubMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +000076
77private: // Parser methods.
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +000078 bool ParseObjectList(MultiClass *MC = 0);
79 bool ParseObject(MultiClass *MC);
Chris Lattnerf4601652007-11-22 20:49:04 +000080 bool ParseClass();
81 bool ParseMultiClass();
Bruno Cardoso Lopesee65db32010-06-10 02:42:59 +000082 bool ParseDefm(MultiClass *CurMultiClass);
83 bool ParseDef(MultiClass *CurMultiClass);
84 bool ParseTopLevelLet(MultiClass *CurMultiClass);
Chris Lattnerf4601652007-11-22 20:49:04 +000085 std::vector<LetRecord> ParseLetList();
86
Chris Lattnerf4601652007-11-22 20:49:04 +000087 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 Wilson440548d2009-04-30 18:26:19 +000095 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
Chris Lattnerf4601652007-11-22 20:49:04 +000096
97 Init *ParseIDValue(Record *CurRec);
Chris Lattner1e3a8a42009-06-21 03:39:35 +000098 Init *ParseIDValue(Record *CurRec, const std::string &Name, SMLoc NameLoc);
David Greenee1b46912009-06-08 20:23:18 +000099 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 Lattnerf4601652007-11-22 20:49:04 +0000102 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 Greened418c1b2009-05-14 20:54:48 +0000108 Init *ParseOperation(Record *CurRec);
109 RecTy *ParseOperatorType();
Chris Lattnerf4601652007-11-22 20:49:04 +0000110 std::string ParseObjectName();
111 Record *ParseClassID();
David Greenede444af2009-04-22 16:42:54 +0000112 MultiClass *ParseMultiClassID();
Chris Lattnerf4601652007-11-22 20:49:04 +0000113 Record *ParseDefmID();
114};
115
116} // end namespace llvm
117
118#endif