blob: 0a8ca982feb35bd3108c677929b3934bf565a8ef [file] [log] [blame]
Chris Lattnerfaca5ab2003-08-06 05:42:05 +00001//===- 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"
Chris Lattnerbc659dd2003-08-07 06:02:15 +000012#include "CodeGenWrappers.h"
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000013#include <vector>
14#include <map>
Chris Lattner018c9e42003-08-07 05:40:14 +000015class DagInit;
16class Init;
Chris Lattnerf8e96832003-08-07 19:12:24 +000017class InstrSelectorEmitter;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000018
Chris Lattnerf8e96832003-08-07 19:12:24 +000019/// NodeType - Represents Information parsed from the DagNode entries.
20///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000021struct NodeType {
22 enum ArgResultTypes {
23 // Both argument and return types...
24 Val, // A non-void type
25 Arg0, // Value matches the type of Arg0
Chris Lattner2787d1a2003-08-06 06:16:35 +000026 Ptr, // Tree node is the type of the target pointer
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000027
28 // Return types
29 Void, // Tree node always returns void
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000030 };
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);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000043};
44
Chris Lattnerf8e96832003-08-07 19:12:24 +000045
46
47/// TreePatternNode - Represent a node of the tree patterns.
48///
Chris Lattner018c9e42003-08-07 05:40:14 +000049class 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...
Chris Lattnerf8e96832003-08-07 19:12:24 +000055 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000056 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.
Chris Lattnerf8e96832003-08-07 19:12:24 +000063 ///
Chris Lattner018c9e42003-08-07 05:40:14 +000064 Init *Value;
65public:
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 }
Chris Lattnerf8e96832003-08-07 19:12:24 +000080 TreePatternNode *getChild(unsigned c) const {
81 assert(c < Children.size() && "Child access out of range!");
82 return getChildren()[c];
83 }
84
Chris Lattner018c9e42003-08-07 05:40:14 +000085 Init *getValue() const {
86 assert(Operator == 0 && "This is not a leaf node!");
87 return Value;
88 }
89
Chris Lattneref0ce6a2003-08-07 23:16:20 +000090 /// clone - Make a copy of this tree and all of its children.
91 ///
92 TreePatternNode *clone() const;
93
Chris Lattner018c9e42003-08-07 05:40:14 +000094 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +000095
Chris Lattneref0ce6a2003-08-07 23:16:20 +000096 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
97 /// are not themselves completely resolved, clone the nonterminal and resolve
98 /// it with the using context we provide.
99 void InstantiateNonterminals(InstrSelectorEmitter &ISE);
Chris Lattner2b8b2b42003-08-07 19:28:55 +0000100
101 // UpdateNodeType - Set the node type of N to VT if VT contains information.
102 // If N already contains a conflicting type, then throw an exception. This
103 // returns true if any information was updated.
104 //
105 bool updateNodeType(MVT::ValueType VT, const std::string &RecName);
Chris Lattner018c9e42003-08-07 05:40:14 +0000106};
107
108std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
109
110
111
Chris Lattnerf8e96832003-08-07 19:12:24 +0000112/// Pattern - Represent a pattern of one form or another. Currently, three
113/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
114///
115struct Pattern {
116 enum PatternType {
117 Nonterminal, Instruction, Expander
118 };
119private:
120 /// PTy - The type of pattern this is.
121 ///
122 PatternType PTy;
123
124 /// Tree - The tree pattern which corresponds to this pattern. Note that if
125 /// there was a (set) node on the outside level that it has been stripped off.
126 ///
127 TreePatternNode *Tree;
128
129 /// Result - If this is an instruction or expander pattern, this is the
130 /// register result, specified with a (set) in the pattern.
131 ///
132 Record *Result;
133
134 /// TheRecord - The actual TableGen record corresponding to this pattern.
135 ///
136 Record *TheRecord;
137
138 /// Resolved - This is true of the pattern is useful in practice. In
139 /// particular, some non-terminals will have non-resolvable types. When a
140 /// user of the non-terminal is later found, they will have inferred a type
141 /// for the result of the non-terminal, which cause a clone of an unresolved
142 /// nonterminal to be made which is "resolved".
143 ///
144 bool Resolved;
145
146 /// ISE - the instruction selector emitter coordinating this madness.
147 ///
148 InstrSelectorEmitter &ISE;
149public:
150
151 /// Pattern constructor - Parse the specified DagInitializer into the current
152 /// record.
153 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
154 InstrSelectorEmitter &ise);
155
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000156 /// Pattern - Constructor used for cloning nonterminal patterns
157 Pattern(TreePatternNode *tree, Record *rec, bool res,
158 InstrSelectorEmitter &ise) : PTy(Nonterminal), Tree(tree), Result(0),
159 TheRecord(rec), Resolved(res), ISE(ise){}
160
Chris Lattnerf8e96832003-08-07 19:12:24 +0000161 /// getPatternType - Return what flavor of Record this pattern originated from
162 ///
163 PatternType getPatternType() const { return PTy; }
164
165 /// getTree - Return the tree pattern which corresponds to this pattern.
166 ///
167 TreePatternNode *getTree() const { return Tree; }
168
169 Record *getResult() const { return Result; }
170
171 /// getRecord - Return the actual TableGen record corresponding to this
172 /// pattern.
173 ///
174 Record *getRecord() const { return TheRecord; }
175
176 bool isResolved() const { return Resolved; }
177
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000178 /// InferAllTypes - Runs the type inference engine on the current pattern,
179 /// stopping when nothing can be inferred, then updating the Resolved field.
180 void InferAllTypes();
181
182 /// InstantiateNonterminals - If this pattern refers to any nonterminals which
183 /// are not themselves completely resolved, clone the nonterminal and resolve
184 /// it with the using context we provide.
185 void InstantiateNonterminals() {
186 Tree->InstantiateNonterminals(ISE);
187 }
188
189 /// clone - This method is used to make an exact copy of the current pattern,
190 /// then change the "TheRecord" instance variable to the specified record.
191 ///
192 Pattern *clone(Record *R) const;
193
194 /// error - Throw an exception, prefixing it with information about this
195 /// pattern.
196 void error(const std::string &Msg) const;
197
Chris Lattnerf8e96832003-08-07 19:12:24 +0000198private:
Chris Lattner5709e512003-08-07 21:02:56 +0000199 MVT::ValueType getIntrinsicType(Record *R) const;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000200 TreePatternNode *ParseTreePattern(DagInit *DI);
201 bool InferTypes(TreePatternNode *N, bool &MadeChange);
Chris Lattnerf8e96832003-08-07 19:12:24 +0000202};
203
204std::ostream &operator<<(std::ostream &OS, const Pattern &P);
205
206
207
208/// InstrSelectorEmitter - The top-level class which coordinates construction
209/// and emission of the instruction selector.
210///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000211class InstrSelectorEmitter : public TableGenBackend {
212 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000213 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000214
215 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000216
217 /// Patterns - a list of all of the patterns defined by the target description
218 ///
219 std::map<Record*, Pattern*> Patterns;
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000220
221 /// InstantiatedNTs - A data structure to keep track of which nonterminals
222 /// have been instantiated already...
223 ///
224 std::map<std::pair<Pattern*,MVT::ValueType>, Record*> InstantiatedNTs;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000225public:
226 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
227
228 // run - Output the instruction set description, returning true on failure.
229 void run(std::ostream &OS);
230
Chris Lattnerf8e96832003-08-07 19:12:24 +0000231 const CodeGenTarget &getTarget() const { return Target; }
232 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
233
Chris Lattner5709e512003-08-07 21:02:56 +0000234 /// getPattern - return the pattern corresponding to the specified record, or
235 /// null if there is none.
236 Pattern *getPattern(Record *R) const {
237 std::map<Record*, Pattern*>::const_iterator I = Patterns.find(R);
238 return I != Patterns.end() ? I->second : 0;
239 }
240
241 /// ReadNonterminal - This method parses the specified record as a
242 /// nonterminal, but only if it hasn't been read in already.
243 Pattern *ReadNonterminal(Record *R);
244
Chris Lattneref0ce6a2003-08-07 23:16:20 +0000245 /// InstantiateNonterminal - This method takes the nonterminal specified by
246 /// NT, which should not be completely resolved, clones it, applies ResultTy
247 /// to its root, then runs the type inference stuff on it. This should
248 /// produce a newly resolved nonterminal, which we make a record for and
249 /// return. To be extra fancy and efficient, this only makes one clone for
250 /// each type it is instantiated with.
251 Record *InstantiateNonterminal(Pattern *NT, MVT::ValueType ResultTy);
252
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000253private:
Chris Lattneree858d22003-08-07 20:42:23 +0000254 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
255 // turning them into the more accessible NodeTypes data structure.
256 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000257
Chris Lattneree858d22003-08-07 20:42:23 +0000258 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
259 // pattern database.
260 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000261
Chris Lattneree858d22003-08-07 20:42:23 +0000262 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000263 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000264 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000265
Chris Lattneree858d22003-08-07 20:42:23 +0000266 // ReadExpanderPatterns - Read in all of the expanded patterns.
267 void ReadExpanderPatterns();
268
269 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
270 // information from the context that they are used in.
271 void InstantiateNonterminals();
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000272};
273
274#endif