1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
3 // This tablegen backend is responsible for emitting a description of the target
4 // instruction set for the code generator.
6 //===----------------------------------------------------------------------===//
8 #include "InstrSelectorEmitter.h"
9 #include "CodeGenWrappers.h"
11 #include "Support/Debug.h"
13 NodeType::ArgResultTypes NodeType::Translate(Record *R) {
14 const std::string &Name = R->getName();
15 if (Name == "DNVT_void") return Void;
16 if (Name == "DNVT_val" ) return Val;
17 if (Name == "DNVT_arg0") return Arg0;
18 if (Name == "DNVT_ptr" ) return Ptr;
19 throw "Unknown DagNodeValType '" + Name + "'!";
22 std::ostream &operator<<(std::ostream &OS, const TreePatternNode &N) {
24 return OS << N.getType() << ":" << *N.getValue();
25 OS << "(" << N.getType() << ":";
26 OS << N.getOperator()->getName();
28 const std::vector<TreePatternNode*> &Children = N.getChildren();
29 if (!Children.empty()) {
30 OS << " " << *Children[0];
31 for (unsigned i = 1, e = Children.size(); i != e; ++i)
32 OS << ", " << *Children[i];
36 void TreePatternNode::dump() const { std::cerr << *this; }
39 /// ProcessNodeTypes - Process all of the node types in the current
40 /// RecordKeeper, turning them into the more accessible NodeTypes data
43 void InstrSelectorEmitter::ProcessNodeTypes() {
44 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("DagNode");
45 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
46 Record *Node = Nodes[i];
48 // Translate the return type...
49 NodeType::ArgResultTypes RetTy =
50 NodeType::Translate(Node->getValueAsDef("RetType"));
52 // Translate the arguments...
53 ListInit *Args = Node->getValueAsListInit("ArgTypes");
54 std::vector<NodeType::ArgResultTypes> ArgTypes;
56 for (unsigned a = 0, e = Args->getSize(); a != e; ++a) {
57 if (DefInit *DI = dynamic_cast<DefInit*>(Args->getElement(a)))
58 ArgTypes.push_back(NodeType::Translate(DI->getDef()));
60 throw "In node " + Node->getName() + ", argument is not a Def!";
62 if (a == 0 && ArgTypes.back() == NodeType::Arg0)
63 throw "In node " + Node->getName() + ", arg 0 cannot have type 'arg0'!";
64 if (ArgTypes.back() == NodeType::Void)
65 throw "In node " + Node->getName() + ", args cannot be void type!";
67 if (RetTy == NodeType::Arg0 && Args->getSize() == 0)
68 throw "In node " + Node->getName() +
69 ", invalid return type for nullary node!";
71 // Add the node type mapping now...
72 NodeTypes[Node] = NodeType(RetTy, ArgTypes);
73 DEBUG(std::cerr << "Got node type '" << Node->getName() << "'\n");
77 static MVT::ValueType getIntrinsicType(Record *R) {
78 // Check to see if this is a register or a register class...
79 const std::vector<Record*> &SuperClasses = R->getSuperClasses();
80 for (unsigned i = 0, e = SuperClasses.size(); i != e; ++i)
81 if (SuperClasses[i]->getName() == "RegisterClass") {
82 return getValueType(R->getValueAsDef("RegType"));
83 } else if (SuperClasses[i]->getName() == "Register") {
84 std::cerr << "WARNING: Explicit registers not handled yet!\n";
86 } else if (SuperClasses[i]->getName() == "Nonterminal") {
87 //std::cerr << "Warning nonterminal type not handled yet:" << R->getName()
91 throw "Error: Unknown value used: " + R->getName();
94 // Parse the specified DagInit into a TreePattern which we can use.
96 TreePatternNode *InstrSelectorEmitter::ParseTreePattern(DagInit *DI,
97 const std::string &RecName) {
98 Record *Operator = DI->getNodeType();
100 if (!NodeTypes.count(Operator))
101 throw "Illegal node for instruction pattern: '" + Operator->getName() +"'!";
103 const std::vector<Init*> &Args = DI->getArgs();
104 std::vector<TreePatternNode*> Children;
106 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
108 if (DagInit *DI = dynamic_cast<DagInit*>(Arg)) {
109 Children.push_back(ParseTreePattern(DI, RecName));
110 } else if (DefInit *DI = dynamic_cast<DefInit*>(Arg)) {
111 Children.push_back(new TreePatternNode(DI));
112 // If it's a regclass or something else known, set the type.
113 Children.back()->setType(getIntrinsicType(DI->getDef()));
116 throw "Unknown value for tree pattern in '" + RecName + "'!";
120 return new TreePatternNode(Operator, Children);
123 // UpdateNodeType - Set the node type of N to VT if VT contains information. If
124 // N already contains a conflicting type, then throw an exception
126 static bool UpdateNodeType(TreePatternNode *N, MVT::ValueType VT,
127 const std::string &RecName) {
128 if (VT == MVT::Other || N->getType() == VT) return false;
130 if (N->getType() == MVT::Other) {
135 throw "Type inferfence contradiction found for pattern " + RecName;
138 // InferTypes - Perform type inference on the tree, returning true if there
139 // are any remaining untyped nodes and setting MadeChange if any changes were
141 bool InstrSelectorEmitter::InferTypes(TreePatternNode *N,
142 const std::string &RecName,
144 if (N->isLeaf()) return N->getType() == MVT::Other;
146 bool AnyUnset = false;
147 Record *Operator = N->getOperator();
148 assert(NodeTypes.count(Operator) && "No node info for node!");
149 const NodeType &NT = NodeTypes[Operator];
151 // Check to see if we can infer anything about the argument types from the
153 const std::vector<TreePatternNode*> &Children = N->getChildren();
154 if (Children.size() != NT.ArgTypes.size())
155 throw "In record " + RecName + " incorrect number of children for " +
156 Operator->getName() + " node!";
158 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
159 AnyUnset |= InferTypes(Children[i], RecName, MadeChange);
162 switch (NT.ArgTypes[i]) {
164 MadeChange |= UpdateNodeType(Children[i], Children[0]->getType(),RecName);
167 if (Children[i]->getType() == MVT::isVoid)
168 throw "In pattern for " + RecName + " should not get a void node!";
171 MadeChange |= UpdateNodeType(Children[i],Target.getPointerType(),RecName);
173 default: assert(0 && "Invalid argument ArgType!");
177 // See if we can infer anything about the return type now...
178 switch (NT.ResultType) {
180 MadeChange |= UpdateNodeType(N, MVT::isVoid, RecName);
183 MadeChange |= UpdateNodeType(N, Children[0]->getType(), RecName);
187 MadeChange |= UpdateNodeType(N, Target.getPointerType(), RecName);
190 if (N->getType() == MVT::isVoid)
191 throw "In pattern for " + RecName + " should not get a void node!";
194 assert(0 && "Unhandled type constraint!");
198 return AnyUnset | N->getType() == MVT::Other;
202 // ReadAndCheckPattern - Parse the specified DagInit into a pattern and then
203 // perform full type inference.
205 TreePatternNode *InstrSelectorEmitter::ReadAndCheckPattern(DagInit *DI,
206 const std::string &RecName) {
207 // First, parse the pattern...
208 TreePatternNode *Pattern = ParseTreePattern(DI, RecName);
210 bool MadeChange, AnyUnset;
213 AnyUnset = InferTypes(Pattern, RecName, MadeChange);
214 if (AnyUnset && !MadeChange) {
215 std::cerr << "In pattern: " << *Pattern << "\n";
216 throw "Cannot infer types for " + RecName;
218 } while (AnyUnset || MadeChange);
223 // ProcessNonTerminals - Read in all nonterminals and incorporate them into
224 // our pattern database.
225 void InstrSelectorEmitter::ProcessNonTerminals() {
226 std::vector<Record*> NTs = Records.getAllDerivedDefinitions("Nonterminal");
227 for (unsigned i = 0, e = NTs.size(); i != e; ++i) {
228 DagInit *DI = NTs[i]->getValueAsDag("Pattern");
230 TreePatternNode *Pattern = ReadAndCheckPattern(DI, NTs[i]->getName());
232 DEBUG(std::cerr << "Parsed nonterm pattern " << NTs[i]->getName() << "\t= "
233 << *Pattern << "\n");
238 /// ProcessInstructionPatterns - Read in all subclasses of Instruction, and
239 /// process those with a useful Pattern field.
241 void InstrSelectorEmitter::ProcessInstructionPatterns() {
242 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
243 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
244 Record *Inst = Insts[i];
245 if (DagInit *DI = dynamic_cast<DagInit*>(Inst->getValueInit("Pattern"))) {
246 TreePatternNode *Pattern = ReadAndCheckPattern(DI, Inst->getName());
248 DEBUG(std::cerr << "Parsed inst pattern " << Inst->getName() << "\t= "
249 << *Pattern << "\n");
255 void InstrSelectorEmitter::run(std::ostream &OS) {
256 // Type-check all of the node types to ensure we "understand" them.
259 // Read in all of the nonterminals...
260 //ProcessNonTerminals();
262 // Read all of the instruction patterns in...
263 ProcessInstructionPatterns();
265 // Read all of the Expander patterns in...