108aeee20c6e0aba9430c947816b3d5d704ac61d
[oota-llvm.git] / utils / TableGen / InstrSelectorEmitter.h
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"
12 #include "CodeGenWrappers.h"
13 #include <vector>
14 #include <map>
15 class DagInit;
16 class Init;
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
23     Ptr,            // Tree node is the type of the target pointer
24
25     // Return types
26     Void,           // Tree node always returns void
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);
40 };
41
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
83 class InstrSelectorEmitter : public TableGenBackend {
84   RecordKeeper &Records;
85   CodeGenTarget Target;
86
87   std::map<Record*, NodeType> NodeTypes;
88 public:
89   InstrSelectorEmitter(RecordKeeper &R) : Records(R) {}
90   
91   // run - Output the instruction set description, returning true on failure.
92   void run(std::ostream &OS);
93
94 private:
95   // ProcessNodeTypes - Process all of the node types in the current
96   // RecordKeeper, turning them into the more accessible NodeTypes data
97   // structure.
98   void ProcessNodeTypes();
99
100   // ProcessNonTerminals - Read in all nonterminals and incorporate them into
101   // our pattern database.
102   void ProcessNonTerminals();
103
104   // ProcessInstructionPatterns - Read in all subclasses of Instruction, and
105   // process those with a useful Pattern field.
106   void ProcessInstructionPatterns();
107
108   // ParseTreePattern - Parse the specified DagInit into a TreePattern which we
109   // can use.
110   //
111   TreePatternNode *ParseTreePattern(DagInit *DI, const std::string &RecName);
112
113   // InferTypes - Perform type inference on the tree, returning true if there
114   // are any remaining untyped nodes and setting MadeChange if any changes were
115   // made.
116   bool InferTypes(TreePatternNode *N, const std::string &RecName,
117                   bool &MadeChange);
118
119   // ReadAndCheckPattern - Parse the specified DagInit into a pattern and then
120   // perform full type inference.
121   TreePatternNode *ReadAndCheckPattern(DagInit *DI, const std::string &RecName);
122 };
123
124 #endif