Read in expanders too
[oota-llvm.git] / utils / 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 std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
94
95
96
97 /// Pattern - Represent a pattern of one form or another.  Currently, three
98 /// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
99 ///
100 struct Pattern {
101   enum PatternType {
102     Nonterminal, Instruction, Expander
103   };
104 private:
105   /// PTy - The type of pattern this is.
106   ///
107   PatternType PTy;
108
109   /// Tree - The tree pattern which corresponds to this pattern.  Note that if
110   /// there was a (set) node on the outside level that it has been stripped off.
111   ///
112   TreePatternNode *Tree;
113   
114   /// Result - If this is an instruction or expander pattern, this is the
115   /// register result, specified with a (set) in the pattern.
116   ///
117   Record *Result;
118
119   /// TheRecord - The actual TableGen record corresponding to this pattern.
120   ///
121   Record *TheRecord;
122
123   /// Resolved - This is true of the pattern is useful in practice.  In
124   /// particular, some non-terminals will have non-resolvable types.  When a
125   /// user of the non-terminal is later found, they will have inferred a type
126   /// for the result of the non-terminal, which cause a clone of an unresolved
127   /// nonterminal to be made which is "resolved".
128   ///
129   bool Resolved;
130
131   /// ISE - the instruction selector emitter coordinating this madness.
132   ///
133   InstrSelectorEmitter &ISE;
134 public:
135
136   /// Pattern constructor - Parse the specified DagInitializer into the current
137   /// record.
138   Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
139           InstrSelectorEmitter &ise);
140
141   /// getPatternType - Return what flavor of Record this pattern originated from
142   ///
143   PatternType getPatternType() const { return PTy; }
144
145   /// getTree - Return the tree pattern which corresponds to this pattern.
146   ///
147   TreePatternNode *getTree() const { return Tree; }
148   
149   Record *getResult() const { return Result; }
150
151   /// getRecord - Return the actual TableGen record corresponding to this
152   /// pattern.
153   ///
154   Record *getRecord() const { return TheRecord; }
155
156   bool isResolved() const { return Resolved; }
157
158 private:
159   TreePatternNode *ParseTreePattern(DagInit *DI);
160   bool InferTypes(TreePatternNode *N, bool &MadeChange);
161   void error(const std::string &Msg);
162 };
163
164 std::ostream &operator<<(std::ostream &OS, const Pattern &P);
165
166
167
168 /// InstrSelectorEmitter - The top-level class which coordinates construction
169 /// and emission of the instruction selector.
170 ///
171 class InstrSelectorEmitter : public TableGenBackend {
172   RecordKeeper &Records;
173   CodeGenTarget Target;
174
175   std::map<Record*, NodeType> NodeTypes;
176
177   /// Patterns - a list of all of the patterns defined by the target description
178   ///
179   std::map<Record*, Pattern*> Patterns;
180 public:
181   InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
182   
183   // run - Output the instruction set description, returning true on failure.
184   void run(std::ostream &OS);
185
186   const CodeGenTarget &getTarget() const { return Target; }
187   std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
188
189 private:
190   // ProcessNodeTypes - Process all of the node types in the current
191   // RecordKeeper, turning them into the more accessible NodeTypes data
192   // structure.
193   void ProcessNodeTypes();
194
195   // ProcessNonTerminals - Read in all nonterminals and incorporate them into
196   // our pattern database.
197   void ProcessNonterminals();
198
199   // ProcessInstructionPatterns - Read in all subclasses of Instruction, and
200   // process those with a useful Pattern field.
201   void ProcessInstructionPatterns();
202
203   // ProcessExpanderPatterns - Read in all of the expanded patterns.
204   void ProcessExpanderPatterns();
205 };
206
207 #endif