Initial support for an instruction selector emitter
[oota-llvm.git] / utils / TableGen / InstrSelectorEmitter.cpp
1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
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 #include "InstrSelectorEmitter.h"
9 #include "Record.h"
10
11 NodeType::ArgResultTypes NodeType::Translate(Record *R) {
12   const std::string &Name = R->getName();
13   if (Name == "DNRT_void") return Void;
14   if (Name == "DNRT_val" || Name == "DNAT_val") return Val;
15   if (Name == "DNRT_arg0" || Name == "DNAT_arg0") return Arg0;
16   if (Name == "DNAT_ptr") return Ptr;
17   throw "Unknown DagNodeResult Type '" + Name + "'!";
18 }
19
20
21 /// ProcessNodeTypes - Process all of the node types in the current
22 /// RecordKeeper, turning them into the more accessible NodeTypes data
23 /// structure.
24 ///
25 void InstrSelectorEmitter::ProcessNodeTypes() {
26   std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("DagNode");
27
28   for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
29     Record *Node = Nodes[i];
30     
31     // Translate the return type...
32     NodeType::ArgResultTypes RetTy =
33       NodeType::Translate(Node->getValueAsDef("RetType"));
34
35     // Translate the arguments...
36     ListInit *Args = Node->getValueAsListInit("ArgTypes");
37     std::vector<NodeType::ArgResultTypes> ArgTypes;
38
39     for (unsigned a = 0, e = Args->getSize(); a != e; ++a)
40       if (DefInit *DI = dynamic_cast<DefInit*>(Args->getElement(a)))
41         ArgTypes.push_back(NodeType::Translate(DI->getDef()));
42       else
43         throw "In node " + Node->getName() + ", argument is not a Def!";
44
45     // Add the node type mapping now...
46     NodeTypes[Node] = NodeType(RetTy, ArgTypes);
47   }  
48 }
49
50 void InstrSelectorEmitter::run(std::ostream &OS) {
51   // Type-check all of the node types to ensure we "understand" them.
52   ProcessNodeTypes();
53   
54   
55   
56 }