blob: 8a8734b9349d7b3817f4e811ab3273ef981984bc [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
90 void dump() const;
Chris Lattner2b8b2b42003-08-07 19:28:55 +000091
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);
Chris Lattner018c9e42003-08-07 05:40:14 +000098};
99
100std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
101
102
103
Chris Lattnerf8e96832003-08-07 19:12:24 +0000104/// Pattern - Represent a pattern of one form or another. Currently, three
105/// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
106///
107struct Pattern {
108 enum PatternType {
109 Nonterminal, Instruction, Expander
110 };
111private:
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;
141public:
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
165private:
166 TreePatternNode *ParseTreePattern(DagInit *DI);
167 bool InferTypes(TreePatternNode *N, bool &MadeChange);
168 void error(const std::string &Msg);
169};
170
171std::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///
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000178class InstrSelectorEmitter : public TableGenBackend {
179 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000180 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000181
182 std::map<Record*, NodeType> NodeTypes;
Chris Lattnerf8e96832003-08-07 19:12:24 +0000183
184 /// Patterns - a list of all of the patterns defined by the target description
185 ///
186 std::map<Record*, Pattern*> Patterns;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000187public:
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
Chris Lattnerf8e96832003-08-07 19:12:24 +0000193 const CodeGenTarget &getTarget() const { return Target; }
194 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
195
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000196private:
Chris Lattneree858d22003-08-07 20:42:23 +0000197 // ReadNodeTypes - Read in all of the node types in the current RecordKeeper,
198 // turning them into the more accessible NodeTypes data structure.
199 void ReadNodeTypes();
Chris Lattner2787d1a2003-08-06 06:16:35 +0000200
Chris Lattneree858d22003-08-07 20:42:23 +0000201 // ReadNonTerminals - Read in all nonterminals and incorporate them into our
202 // pattern database.
203 void ReadNonterminals();
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000204
Chris Lattneree858d22003-08-07 20:42:23 +0000205 // ReadInstructionPatterns - Read in all subclasses of Instruction, and
Chris Lattner2787d1a2003-08-06 06:16:35 +0000206 // process those with a useful Pattern field.
Chris Lattneree858d22003-08-07 20:42:23 +0000207 void ReadInstructionPatterns();
Chris Lattnerb356a242003-08-07 19:21:10 +0000208
Chris Lattneree858d22003-08-07 20:42:23 +0000209 // ReadExpanderPatterns - Read in all of the expanded patterns.
210 void ReadExpanderPatterns();
211
212 // InstantiateNonterminals - Instantiate any unresolved nonterminals with
213 // information from the context that they are used in.
214 void InstantiateNonterminals();
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000215};
216
217#endif