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"
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 + "'!";
21 /// ProcessNodeTypes - Process all of the node types in the current
22 /// RecordKeeper, turning them into the more accessible NodeTypes data
25 void InstrSelectorEmitter::ProcessNodeTypes() {
26 std::vector<Record*> Nodes = Records.getAllDerivedDefinitions("DagNode");
27 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
28 Record *Node = Nodes[i];
30 // Translate the return type...
31 NodeType::ArgResultTypes RetTy =
32 NodeType::Translate(Node->getValueAsDef("RetType"));
34 // Translate the arguments...
35 ListInit *Args = Node->getValueAsListInit("ArgTypes");
36 std::vector<NodeType::ArgResultTypes> ArgTypes;
38 for (unsigned a = 0, e = Args->getSize(); a != e; ++a) {
39 if (DefInit *DI = dynamic_cast<DefInit*>(Args->getElement(a)))
40 ArgTypes.push_back(NodeType::Translate(DI->getDef()));
42 throw "In node " + Node->getName() + ", argument is not a Def!";
44 if (a == 0 && ArgTypes.back() == NodeType::Arg0)
45 throw "In node " + Node->getName() + ", arg 0 cannot have type 'arg0'!";
46 if (ArgTypes.back() == NodeType::Void)
47 throw "In node " + Node->getName() + ", args cannot be void type!";
49 if (RetTy == NodeType::Arg0 && Args->getSize() == 0)
50 throw "In node " + Node->getName() +
51 ", invalid return type for nullary node!";
53 // Add the node type mapping now...
54 NodeTypes[Node] = NodeType(RetTy, ArgTypes);
58 /// ProcessInstructionPatterns - Read in all subclasses of Instruction, and
59 /// process those with a useful Pattern field.
61 void InstrSelectorEmitter::ProcessInstructionPatterns() {
62 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
63 for (unsigned i = 0, e = Insts.size(); i != e; ++i) {
64 Record *Inst = Insts[i];
65 if (DagInit *PatternInit =
66 dynamic_cast<DagInit*>(Inst->getValueInit("Pattern"))) {
73 void InstrSelectorEmitter::run(std::ostream &OS) {
74 // Type-check all of the node types to ensure we "understand" them.
77 // Read all of the instruction patterns in...
78 ProcessInstructionPatterns();
80 // Read all of the Expander patterns in...