1 //===- InstrInfoEmitter.h - Generate a Instruction Set Desc. ----*- C++ -*-===//
3 // This tablegen backend is responsible for emitting a description of the target
4 // instruction set for the code generator.
6 //===----------------------------------------------------------------------===//
8 #ifndef INSTRSELECTOR_EMITTER_H
9 #define INSTRSELECTOR_EMITTER_H
11 #include "TableGenBackend.h"
12 #include "CodeGenWrappers.h"
17 class InstrSelectorEmitter;
19 /// NodeType - Represents Information parsed from the DagNode entries.
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
29 Void, // Tree node always returns void
32 ArgResultTypes ResultType;
33 std::vector<ArgResultTypes> ArgTypes;
35 NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){
39 NodeType() : ResultType(Val) {}
40 NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){}
42 static ArgResultTypes Translate(Record *R);
47 /// TreePatternNode - Represent a node of the tree patterns.
49 class TreePatternNode {
50 /// Operator - The operation that this node represents... this is null if this
54 /// Type - The inferred value type...
58 /// Children - If this is not a leaf (Operator != 0), this is the subtrees
60 std::vector<TreePatternNode*> Children;
62 /// Value - If this node is a leaf, this indicates what the thing is.
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) {}
70 Record *getOperator() const { return Operator; }
71 MVT::ValueType getType() const { return Type; }
72 void setType(MVT::ValueType T) { Type = T; }
74 bool isLeaf() const { return Operator == 0; }
76 const std::vector<TreePatternNode*> &getChildren() const {
77 assert(Operator != 0 && "This is a leaf node!");
80 TreePatternNode *getChild(unsigned c) const {
81 assert(c < Children.size() && "Child access out of range!");
82 return getChildren()[c];
85 Init *getValue() const {
86 assert(Operator == 0 && "This is not a leaf node!");
93 std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
97 /// Pattern - Represent a pattern of one form or another. Currently, three
98 /// types of patterns are possible: Instruction's, Nonterminals, and Expanders.
102 Nonterminal, Instruction, Expander
105 /// PTy - The type of pattern this is.
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.
112 TreePatternNode *Tree;
114 /// Result - If this is an instruction or expander pattern, this is the
115 /// register result, specified with a (set) in the pattern.
119 /// TheRecord - The actual TableGen record corresponding to this pattern.
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".
131 /// ISE - the instruction selector emitter coordinating this madness.
133 InstrSelectorEmitter &ISE;
136 /// Pattern constructor - Parse the specified DagInitializer into the current
138 Pattern(PatternType pty, DagInit *RawPat, Record *TheRec,
139 InstrSelectorEmitter &ise);
141 /// getPatternType - Return what flavor of Record this pattern originated from
143 PatternType getPatternType() const { return PTy; }
145 /// getTree - Return the tree pattern which corresponds to this pattern.
147 TreePatternNode *getTree() const { return Tree; }
149 Record *getResult() const { return Result; }
151 /// getRecord - Return the actual TableGen record corresponding to this
154 Record *getRecord() const { return TheRecord; }
156 bool isResolved() const { return Resolved; }
159 TreePatternNode *ParseTreePattern(DagInit *DI);
160 bool InferTypes(TreePatternNode *N, bool &MadeChange);
161 void error(const std::string &Msg);
164 std::ostream &operator<<(std::ostream &OS, const Pattern &P);
168 /// InstrSelectorEmitter - The top-level class which coordinates construction
169 /// and emission of the instruction selector.
171 class InstrSelectorEmitter : public TableGenBackend {
172 RecordKeeper &Records;
173 CodeGenTarget Target;
175 std::map<Record*, NodeType> NodeTypes;
177 /// Patterns - a list of all of the patterns defined by the target description
179 std::map<Record*, Pattern*> Patterns;
181 InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
183 // run - Output the instruction set description, returning true on failure.
184 void run(std::ostream &OS);
186 const CodeGenTarget &getTarget() const { return Target; }
187 std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; }
190 // ProcessNodeTypes - Process all of the node types in the current
191 // RecordKeeper, turning them into the more accessible NodeTypes data
193 void ProcessNodeTypes();
195 // ProcessNonTerminals - Read in all nonterminals and incorporate them into
196 // our pattern database.
197 void ProcessNonterminals();
199 // ProcessInstructionPatterns - Read in all subclasses of Instruction, and
200 // process those with a useful Pattern field.
201 void ProcessInstructionPatterns();
203 // ProcessExpanderPatterns - Read in all of the expanded patterns.
204 void ProcessExpanderPatterns();