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 | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/ValueTypes.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 | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 17 | |
| 18 | struct NodeType { |
| 19 | enum ArgResultTypes { |
| 20 | // Both argument and return types... |
| 21 | Val, // A non-void type |
| 22 | Arg0, // Value matches the type of Arg0 |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 23 | Ptr, // Tree node is the type of the target pointer |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 24 | |
| 25 | // Return types |
| 26 | Void, // Tree node always returns void |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | ArgResultTypes ResultType; |
| 30 | std::vector<ArgResultTypes> ArgTypes; |
| 31 | |
| 32 | NodeType(ArgResultTypes RT, std::vector<ArgResultTypes> &AT) : ResultType(RT){ |
| 33 | AT.swap(ArgTypes); |
| 34 | } |
| 35 | |
| 36 | NodeType() : ResultType(Val) {} |
| 37 | NodeType(const NodeType &N) : ResultType(N.ResultType), ArgTypes(N.ArgTypes){} |
| 38 | |
| 39 | static ArgResultTypes Translate(Record *R); |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 40 | }; |
| 41 | |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 42 | class TreePatternNode { |
| 43 | /// Operator - The operation that this node represents... this is null if this |
| 44 | /// is a leaf. |
| 45 | Record *Operator; |
| 46 | |
| 47 | /// Type - The inferred value type... |
| 48 | MVT::ValueType Type; |
| 49 | |
| 50 | /// Children - If this is not a leaf (Operator != 0), this is the subtrees |
| 51 | /// that we contain. |
| 52 | std::vector<TreePatternNode*> Children; |
| 53 | |
| 54 | /// Value - If this node is a leaf, this indicates what the thing is. |
| 55 | Init *Value; |
| 56 | public: |
| 57 | TreePatternNode(Record *o, const std::vector<TreePatternNode*> &c) |
| 58 | : Operator(o), Type(MVT::Other), Children(c), Value(0) {} |
| 59 | TreePatternNode(Init *V) : Operator(0), Type(MVT::Other), Value(V) {} |
| 60 | |
| 61 | Record *getOperator() const { return Operator; } |
| 62 | MVT::ValueType getType() const { return Type; } |
| 63 | void setType(MVT::ValueType T) { Type = T; } |
| 64 | |
| 65 | bool isLeaf() const { return Operator == 0; } |
| 66 | |
| 67 | const std::vector<TreePatternNode*> &getChildren() const { |
| 68 | assert(Operator != 0 && "This is a leaf node!"); |
| 69 | return Children; |
| 70 | } |
| 71 | Init *getValue() const { |
| 72 | assert(Operator == 0 && "This is not a leaf node!"); |
| 73 | return Value; |
| 74 | } |
| 75 | |
| 76 | void dump() const; |
| 77 | }; |
| 78 | |
| 79 | std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N); |
| 80 | |
| 81 | |
| 82 | |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 83 | class InstrSelectorEmitter : public TableGenBackend { |
| 84 | RecordKeeper &Records; |
| 85 | |
| 86 | std::map<Record*, NodeType> NodeTypes; |
| 87 | public: |
| 88 | InstrSelectorEmitter(RecordKeeper &R) : Records(R) {} |
| 89 | |
| 90 | // run - Output the instruction set description, returning true on failure. |
| 91 | void run(std::ostream &OS); |
| 92 | |
| 93 | private: |
| 94 | // ProcessNodeTypes - Process all of the node types in the current |
| 95 | // RecordKeeper, turning them into the more accessible NodeTypes data |
| 96 | // structure. |
| 97 | void ProcessNodeTypes(); |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 98 | |
| 99 | // ProcessInstructionPatterns - Read in all subclasses of Instruction, and |
| 100 | // process those with a useful Pattern field. |
| 101 | void ProcessInstructionPatterns(); |
Chris Lattner | 018c9e4 | 2003-08-07 05:40:14 +0000 | [diff] [blame] | 102 | |
| 103 | // ParseTreePattern - Parse the specified DagInit into a TreePattern which we |
| 104 | // can use. |
| 105 | // |
| 106 | TreePatternNode *ParseTreePattern(DagInit *DI, const std::string &RecName); |
| 107 | |
| 108 | // InferTypes - Perform type inference on the tree, returning true if there |
| 109 | // are any remaining untyped nodes and setting MadeChange if any changes were |
| 110 | // made. |
| 111 | bool InferTypes(TreePatternNode *N, const std::string &RecName, |
| 112 | bool &MadeChange); |
| 113 | |
| 114 | // ReadAndCheckPattern - Parse the specified DagInit into a pattern and then |
| 115 | // perform full type inference. |
| 116 | TreePatternNode *ReadAndCheckPattern(DagInit *DI, const std::string &RecName); |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #endif |