Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 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"); |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 27 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { |
| 28 | Record *Node = Nodes[i]; |
| 29 | |
| 30 | // Translate the return type... |
| 31 | NodeType::ArgResultTypes RetTy = |
| 32 | NodeType::Translate(Node->getValueAsDef("RetType")); |
| 33 | |
| 34 | // Translate the arguments... |
| 35 | ListInit *Args = Node->getValueAsListInit("ArgTypes"); |
| 36 | std::vector<NodeType::ArgResultTypes> ArgTypes; |
| 37 | |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 38 | for (unsigned a = 0, e = Args->getSize(); a != e; ++a) { |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 39 | if (DefInit *DI = dynamic_cast<DefInit*>(Args->getElement(a))) |
| 40 | ArgTypes.push_back(NodeType::Translate(DI->getDef())); |
| 41 | else |
| 42 | throw "In node " + Node->getName() + ", argument is not a Def!"; |
| 43 | |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 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!"; |
| 48 | } |
| 49 | if (RetTy == NodeType::Arg0 && Args->getSize() == 0) |
| 50 | throw "In node " + Node->getName() + |
| 51 | ", invalid return type for nullary node!"; |
| 52 | |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 53 | // Add the node type mapping now... |
| 54 | NodeTypes[Node] = NodeType(RetTy, ArgTypes); |
| 55 | } |
| 56 | } |
| 57 | |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 58 | /// ProcessInstructionPatterns - Read in all subclasses of Instruction, and |
| 59 | /// process those with a useful Pattern field. |
| 60 | /// |
| 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"))) { |
| 67 | |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 73 | void InstrSelectorEmitter::run(std::ostream &OS) { |
| 74 | // Type-check all of the node types to ensure we "understand" them. |
| 75 | ProcessNodeTypes(); |
| 76 | |
Chris Lattner | 2787d1a | 2003-08-06 06:16:35 +0000 | [diff] [blame] | 77 | // Read all of the instruction patterns in... |
| 78 | ProcessInstructionPatterns(); |
| 79 | |
| 80 | // Read all of the Expander patterns in... |
Chris Lattner | faca5ab | 2003-08-06 05:42:05 +0000 | [diff] [blame] | 81 | |
| 82 | } |