blob: dc16fb20f4709d02cca6fa150f3a1176d214ba8a [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 Lattner018c9e42003-08-07 05:40:14 +000012#include "llvm/CodeGen/ValueTypes.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;
85
86 std::map<Record*, NodeType> NodeTypes;
87public:
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
93private:
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 Lattner2787d1a2003-08-06 06:16:35 +000098
99 // ProcessInstructionPatterns - Read in all subclasses of Instruction, and
100 // process those with a useful Pattern field.
101 void ProcessInstructionPatterns();
Chris Lattner018c9e42003-08-07 05:40:14 +0000102
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 Lattnerfaca5ab2003-08-06 05:42:05 +0000117};
118
119#endif