Use the new version of isSubClassOf
[oota-llvm.git] / support / tools / TableGen / InstrSelectorEmitter.h
1 //===- InstrInfoEmitter.h - Generate a Instruction Set Desc. ----*- C++ -*-===//
2 //
3 // This tablegen backend is responsible for emitting a description of the target
4 // instruction set for the code generator.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef INSTRSELECTOR_EMITTER_H
9 #define INSTRSELECTOR_EMITTER_H
10
11 #include "TableGenBackend.h"
12 #include "CodeGenWrappers.h"
13 #include <vector>
14 #include <map>
15 class DagInit;
16 class Init;
17 class InstrSelectorEmitter;
18
19 /// NodeType - Represents Information parsed from the DagNode entries.
20 ///
21 struct NodeType {
22   enum ArgResultTypes {
23     // Both argument and return types...
24     Val,            // A non-void type
25     Arg0,           // Value matches the type of Arg0
26     Ptr,            // Tree node is the type of the target pointer
27
28     // Return types
29     Void,           // Tree node always returns void
30   };
31
32   ArgResultTypes ResultType;
33   std::vector<ArgResultTypes> ArgTypes;
34
35   NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
36     AT.swap(ArgTypes);
37   }
38
39   NodeType() : ResultType(Val) {}
40   NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
41
42   static ArgResultTypes Translate(Record *R);
43 };
44
45
46
47 /// TreePatternNode - Represent a node of the tree patterns.
48 ///
49 class TreePatternNode {
50   /// Operator - The operation that this node represents... this is null if this
51   /// is a leaf.
52   Record *Operator;
53
54   /// Type - The inferred value type...
55   ///
56   MVT::ValueType                Type;
57
58   /// Children - If this is not a leaf (Operator != 0), this is the subtrees
59   /// that we contain.
60   std::vector<TreePatternNode*> Children;
61
62   /// Value - If this node is a leaf, this indicates what the thing is.
63   ///
64   Init *Value;
65 public:
66   TreePatternNode(Record *o, const std::vector<TreePatternNode*> &c)
67     : Operator(o), Type(MVT::Other), Children(c), Value(0) {}
68   TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {}
69
70   Record *getOperator() const { return Operator; }
71   MVT::ValueType getType() const { return Type; }
72   void setType(MVT::ValueType T) { Type = T; }
73
74   bool isLeaf() const { return Operator == 0; }
75
76   const std::vector<TreePatternNode*> &getChildren() const {
77     assert(Operator != 0 && "This is a leaf node!");
78     return Children;
79   }
80   TreePatternNode *getChild(unsigned c) const {
81     assert(c < Children.size() && "Child access out of range!");
82     return getChildren()[c];
83   }
84
85   Init *getValue() const {
86     assert(Operator == 0 && "This is not a leaf node!");
87     return Value;
88   }
89
90   void dump() const;
91
92
93   // UpdateNodeType - Set the node type of N to VT if VT contains information.
94   // If N already contains a conflicting type, then throw an exception.  This
95   // returns true if any information was updated.
96   //
97   bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
98 };
99
100 std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
101
102
103
104 /// Pattern - Represent a pattern of one form or another.  Currently, three
105 /// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
106 ///
107 struct Pattern {
108   enum PatternType {
109     Nonterminal, Instruction, Expander
110   };
111 private:
112   /// PTy - The type of pattern this is.
113   ///
114   PatternType PTy;
115
116   /// Tree - The tree pattern which corresponds to this pattern.  Note that if
117   /// there was a (set) node on the outside level that it has been stripped off.
118   ///
119   TreePatternNode *Tree;
120   
121   /// Result - If this is an instruction or expander pattern, this is the
122   /// register result, specified with a (set) in the pattern.
123   ///
124   Record *Result;
125
126   /// TheRecord - The actual TableGen record corresponding to this pattern.
127   ///
128   Record *TheRecord;
129
130   /// Resolved - This is true of the pattern is useful in practice.  In
131   /// particular, some non-terminals will have non-resolvable types.  When a
132   /// user of the non-terminal is later found, they will have inferred a type
133   /// for the result of the non-terminal, which cause a clone of an unresolved
134   /// nonterminal to be made which is "resolved".
135   ///
136   bool Resolved;
137
138   /// ISE - the instruction selector emitter coordinating this madness.
139   ///
140   InstrSelectorEmitter &ISE;
141 public:
142
143   /// Pattern constructor - Parse the specified DagInitializer into the current
144   /// record.
145   Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
146           InstrSelectorEmitter &ise);
147
148   /// getPatternType - Return what flavor of Record this pattern originated from
149   ///
150   PatternType getPatternType() const { return PTy; }
151
152   /// getTree - Return the tree pattern which corresponds to this pattern.
153   ///
154   TreePatternNode *getTree() const { return Tree; }
155   
156   Record *getResult() const { return Result; }
157
158   /// getRecord - Return the actual TableGen record corresponding to this
159   /// pattern.
160   ///
161   Record *getRecord() const { return TheRecord; }
162
163   bool isResolved() const { return Resolved; }
164
165 private:
166   TreePatternNode *ParseTreePattern(DagInit *DI);
167   bool InferTypes(TreePatternNode *N, bool &MadeChange);
168   void error(const std::string &Msg);
169 };
170
171 std::ostream &operator<<(std::ostream &OS, const Pattern &P);
172
173
174
175 /// InstrSelectorEmitter - The top-level class which coordinates construction
176 /// and emission of the instruction selector.
177 ///
178 class InstrSelectorEmitter : public TableGenBackend {
179   RecordKeeper &Records;
180   CodeGenTarget Target;
181
182   std::map<Record*, NodeType> NodeTypes;
183
184   /// Patterns - a list of all of the patterns defined by the target description
185   ///
186   std::map<Record*, Pattern*> Patterns;
187 public:
188   InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
189   
190   // run - Output the instruction set description, returning true on failure.
191   void run(std::ostream &OS);
192
193   const CodeGenTarget &getTarget() const { return Target; }
194   std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
195
196 private:
197   // ProcessNodeTypes - Process all of the node types in the current
198   // RecordKeeper, turning them into the more accessible NodeTypes data
199   // structure.
200   void ProcessNodeTypes();
201
202   // ProcessNonTerminals - Read in all nonterminals and incorporate them into
203   // our pattern database.
204   void ProcessNonterminals();
205
206   // ProcessInstructionPatterns - Read in all subclasses of Instruction, and
207   // process those with a useful Pattern field.
208   void ProcessInstructionPatterns();
209
210   // ProcessExpanderPatterns - Read in all of the expanded patterns.
211   void ProcessExpanderPatterns();
212 };
213
214 #endif