Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 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" |
Chris Lattner | bc659dd | 2003-08-07 06:02:15 +0000 | [diff] [blame] | 12 | #include "CodeGenWrappers.h" |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 13 | #include <vector> |
| 14 | #include <map> |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 15 | class DagInit; |
| 16 | class Init; |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 17 | class InstrSelectorEmitter; |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 18 | |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 19 | /// NodeType - Represents Information parsed from the DagNode entries. |
| 20 | /// |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 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 |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 26 | Ptr, // Tree node is the type of the target pointer |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 27 | |
| 28 | // Return types |
| 29 | Void, // Tree node always returns void |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 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); |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | /// TreePatternNode - Represent a node of the tree patterns. |
| 48 | /// |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 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... |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 55 | /// |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 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. |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 63 | /// |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 80 | TreePatternNode *getChild(unsigned c) const { |
| 81 | assert(c < Children.size() && "Child access out of range!"); |
| 82 | return getChildren()[c]; |
| 83 | } |
| 84 | |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 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 | |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 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 | /// |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 171 | class InstrSelectorEmitter : public TableGenBackend { |
| 172 | RecordKeeper &Records; |
Chris Lattner | bc659dd | 2003-08-07 06:02:15 +0000 | [diff] [blame] | 173 | CodeGenTarget Target; |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 174 | |
| 175 | std::map<Record*, NodeType> NodeTypes; |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 176 | |
| 177 | /// Patterns - a list of all of the patterns defined by the target description |
| 178 | /// |
| 179 | std::map<Record*, Pattern*> Patterns; |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 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 | |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 186 | const CodeGenTarget &getTarget() const { return Target; } |
| 187 | std::map<Record*, NodeType> &getNodeTypes() { return NodeTypes; } |
| 188 | |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 194 | |
Chris Lattner | bc659dd | 2003-08-07 06:02:15 +0000 | [diff] [blame] | 195 | // ProcessNonTerminals - Read in all nonterminals and incorporate them into |
| 196 | // our pattern database. |
Chris Lattner | f8e9683 | 2003-08-07 19:12:24 +0000 | [diff] [blame] | 197 | void ProcessNonterminals(); |
Chris Lattner | bc659dd | 2003-08-07 06:02:15 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 199 | // ProcessInstructionPatterns - Read in all subclasses of Instruction, and |
| 200 | // process those with a useful Pattern field. |
| 201 | void ProcessInstructionPatterns(); |
Chris Lattner | b356a24 | 2003-08-07 19:21:10 +0000 | [diff] [blame^] | 202 | |
| 203 | // ProcessExpanderPatterns - Read in all of the expanded patterns. |
| 204 | void ProcessExpanderPatterns(); |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | #endif |