blob: 5d5330de51718570c84d22909fb9b71e66a552f1 [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"
Chris Lattner1c8ae592009-03-13 16:01:53 +000018#include "TGSourceMgr.h"
Chris Lattnerf4601652007-11-22 20:49:04 +000019#include <map>
20
21namespace llvm {
22 class Record;
23 class RecordVal;
Chris Lattner41b23402009-01-22 05:10:16 +000024 struct RecTy;
25 struct Init;
Chris Lattnerf4601652007-11-22 20:49:04 +000026 struct MultiClass;
Cedric Venetaff9c272009-02-14 16:06:42 +000027 class SubClassReference;
Chris Lattnerf4601652007-11-22 20:49:04 +000028
29 struct LetRecord {
30 std::string Name;
31 std::vector<unsigned> Bits;
32 Init *Value;
Chris Lattner1c8ae592009-03-13 16:01:53 +000033 TGLoc Loc;
Chris Lattnerf4601652007-11-22 20:49:04 +000034 LetRecord(const std::string &N, const std::vector<unsigned> &B, Init *V,
Chris Lattner1c8ae592009-03-13 16:01:53 +000035 TGLoc L)
Chris Lattnerf4601652007-11-22 20:49:04 +000036 : Name(N), Bits(B), Value(V), Loc(L) {
37 }
38 };
39
40class TGParser {
41 TGLexer Lex;
42 std::vector<std::vector<LetRecord> > LetStack;
43 std::map<std::string, MultiClass*> MultiClasses;
44
45 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
46 /// current value.
47 MultiClass *CurMultiClass;
48public:
Chris Lattnerf4601652007-11-22 20:49:04 +000049
Chris Lattneraa739d22009-03-13 07:05:43 +000050 TGParser(TGSourceMgr &SrcMgr) : Lex(SrcMgr), CurMultiClass(0) {}
Chris Lattnerf4601652007-11-22 20:49:04 +000051
52 void setIncludeDirs(const std::vector<std::string> &D){Lex.setIncludeDirs(D);}
53
54 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
55 /// routines return true on error, or false on success.
56 bool ParseFile();
57
Chris Lattner1c8ae592009-03-13 16:01:53 +000058 bool Error(TGLoc L, const std::string &Msg) const {
Chris Lattnerf4601652007-11-22 20:49:04 +000059 Lex.PrintError(L, Msg);
60 return true;
61 }
62 bool TokError(const std::string &Msg) const {
63 return Error(Lex.getLoc(), Msg);
64 }
65private: // Semantic analysis methods.
Chris Lattner1c8ae592009-03-13 16:01:53 +000066 bool AddValue(Record *TheRec, TGLoc Loc, const RecordVal &RV);
67 bool SetValue(Record *TheRec, TGLoc Loc, const std::string &ValName,
Chris Lattnerf4601652007-11-22 20:49:04 +000068 const std::vector<unsigned> &BitList, Init *V);
Cedric Venetaff9c272009-02-14 16:06:42 +000069 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
Chris Lattnerf4601652007-11-22 20:49:04 +000070
71private: // Parser methods.
72 bool ParseObjectList();
73 bool ParseObject();
74 bool ParseClass();
75 bool ParseMultiClass();
76 bool ParseMultiClassDef(MultiClass *CurMC);
77 bool ParseDefm();
78 bool ParseTopLevelLet();
79 std::vector<LetRecord> ParseLetList();
80
81 Record *ParseDef(MultiClass *CurMultiClass);
82 bool ParseObjectBody(Record *CurRec);
83 bool ParseBody(Record *CurRec);
84 bool ParseBodyItem(Record *CurRec);
85
86 bool ParseTemplateArgList(Record *CurRec);
87 std::string ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
88
89 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
90
91 Init *ParseIDValue(Record *CurRec);
Chris Lattner1c8ae592009-03-13 16:01:53 +000092 Init *ParseIDValue(Record *CurRec, const std::string &Name, TGLoc NameLoc);
Chris Lattnerf4601652007-11-22 20:49:04 +000093 Init *ParseSimpleValue(Record *CurRec);
94 Init *ParseValue(Record *CurRec);
95 std::vector<Init*> ParseValueList(Record *CurRec);
96 std::vector<std::pair<llvm::Init*, std::string> > ParseDagArgList(Record *);
97 bool ParseOptionalRangeList(std::vector<unsigned> &Ranges);
98 bool ParseOptionalBitList(std::vector<unsigned> &Ranges);
99 std::vector<unsigned> ParseRangeList();
100 bool ParseRangePiece(std::vector<unsigned> &Ranges);
101 RecTy *ParseType();
102 std::string ParseObjectName();
103 Record *ParseClassID();
104 Record *ParseDefmID();
105};
106
107} // end namespace llvm
108
109#endif