blob: 108aeee20c6e0aba9430c947816b3d5d704ac61d [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 Lattnerfaca5ab2003-08-06 05:42:05 +000017
18struct 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 Lattner2787d1a2003-08-06 06:16:35 +000023 Ptr, // Tree node is the type of the target pointer
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000024
25 // Return types
26 Void, // Tree node always returns void
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000027 };
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 Lattnerfaca5ab2003-08-06 05:42:05 +000040};
41
Chris Lattner018c9e42003-08-07 05:40:14 +000042class 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;
56public:
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
79std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N);
80
81
82
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000083class InstrSelectorEmitter : public TableGenBackend {
84 RecordKeeper &Records;
Chris Lattnerbc659dd2003-08-07 06:02:15 +000085 CodeGenTarget Target;
Chris Lattnerfaca5ab2003-08-06 05:42:05 +000086
87 std::map<Record*, NodeType> NodeTypes;
88public:
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
94private:
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();
Chris Lattner2787d1a2003-08-06 06:16:35 +000099
Chris Lattnerbc659dd2003-08-07 06:02:15 +0000100 // ProcessNonTerminals - Read in all nonterminals and incorporate them into
101 // our pattern database.
102 void ProcessNonTerminals();
103
Chris Lattner2787d1a2003-08-06 06:16:35 +0000104 // ProcessInstructionPatterns - Read in all subclasses of Instruction, and
105 // process those with a useful Pattern field.
106 void ProcessInstructionPatterns();
Chris Lattner018c9e42003-08-07 05:40:14 +0000107
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);
Chris Lattnerfaca5ab2003-08-06 05:42:05 +0000122};
123
124#endif