1 //===- CodeGenTarget.cpp - CodeGen Target Class Wrapper ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class wrap target description classes used by the various code
11 // generation TableGen backends. This makes it easier to access the data and
12 // provides a single place that needs to check it for validity. All of these
13 // classes throw exceptions on error conditions.
15 //===----------------------------------------------------------------------===//
17 #include "CodeGenTarget.h"
21 /// getValueType - Return the MCV::ValueType that the specified TableGen record
23 MVT::ValueType llvm::getValueType(Record *Rec) {
24 return (MVT::ValueType)Rec->getValueAsInt("Value");
27 std::string llvm::getName(MVT::ValueType T) {
29 case MVT::Other: return "UNKNOWN";
30 case MVT::i1: return "i1";
31 case MVT::i8: return "i8";
32 case MVT::i16: return "i16";
33 case MVT::i32: return "i32";
34 case MVT::i64: return "i64";
35 case MVT::i128: return "i128";
36 case MVT::f32: return "f32";
37 case MVT::f64: return "f64";
38 case MVT::f80: return "f80";
39 case MVT::f128: return "f128";
40 case MVT::isVoid:return "void";
41 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
45 std::string llvm::getEnumName(MVT::ValueType T) {
47 case MVT::Other: return "Other";
48 case MVT::i1: return "i1";
49 case MVT::i8: return "i8";
50 case MVT::i16: return "i16";
51 case MVT::i32: return "i32";
52 case MVT::i64: return "i64";
53 case MVT::i128: return "i128";
54 case MVT::f32: return "f32";
55 case MVT::f64: return "f64";
56 case MVT::f80: return "f80";
57 case MVT::f128: return "f128";
58 case MVT::isVoid:return "isVoid";
59 default: assert(0 && "ILLEGAL VALUE TYPE!"); return "";
64 std::ostream &llvm::operator<<(std::ostream &OS, MVT::ValueType T) {
65 return OS << getName(T);
69 /// getTarget - Return the current instance of the Target class.
71 CodeGenTarget::CodeGenTarget() : PointerType(MVT::Other) {
72 std::vector<Record*> Targets = Records.getAllDerivedDefinitions("Target");
73 if (Targets.size() == 0)
74 throw std::string("ERROR: No 'Target' subclasses defined!");
75 if (Targets.size() != 1)
76 throw std::string("ERROR: Multiple subclasses of Target defined!");
77 TargetRec = Targets[0];
79 // Read in all of the CalleeSavedRegisters...
80 ListInit *LI = TargetRec->getValueAsListInit("CalleeSavedRegisters");
81 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
82 if (DefInit *DI = dynamic_cast<DefInit*>(LI->getElement(i)))
83 CalleeSavedRegisters.push_back(DI->getDef());
85 throw "Target: " + TargetRec->getName() +
86 " expected register definition in CalleeSavedRegisters list!";
88 PointerType = getValueType(TargetRec->getValueAsDef("PointerType"));
92 const std::string &CodeGenTarget::getName() const {
93 return TargetRec->getName();
96 Record *CodeGenTarget::getInstructionSet() const {
97 return TargetRec->getValueAsDef("InstructionSet");
100 void CodeGenTarget::ReadInstructions() const {
101 std::vector<Record*> Insts = Records.getAllDerivedDefinitions("Instruction");
103 if (Insts.size() == 0)
104 throw std::string("No 'Instruction' subclasses defined!");
106 for (unsigned i = 0, e = Insts.size(); i != e; ++i)
107 Instructions.insert(std::make_pair(Insts[i]->getName(), Insts[i]));
110 /// getPHIInstruction - Return the designated PHI instruction.
111 const CodeGenInstruction &CodeGenTarget::getPHIInstruction() const {
112 Record *PHI = getInstructionSet()->getValueAsDef("PHIInst");
113 std::map<std::string, CodeGenInstruction>::const_iterator I =
114 getInstructions().find(PHI->getName());
115 if (I == Instructions.end())
116 throw "Could not find PHI instruction named '" + PHI->getName() + "'!";
120 CodeGenInstruction::CodeGenInstruction(Record *R) : TheDef(R) {
121 Name = R->getValueAsString("Name");
122 Namespace = R->getValueAsString("Namespace");
123 AsmString = R->getValueAsString("AsmString");
125 isReturn = R->getValueAsBit("isReturn");
126 isBranch = R->getValueAsBit("isBranch");
127 isBarrier = R->getValueAsBit("isBarrier");
128 isCall = R->getValueAsBit("isCall");
129 isTwoAddress = R->getValueAsBit("isTwoAddress");
130 isTerminator = R->getValueAsBit("isTerminator");
133 DagInit *DI = R->getValueAsDag("OperandList");
135 for (unsigned i = 0, e = DI->getNumArgs(); i != e; ++i)
136 if (DefInit *Arg = dynamic_cast<DefInit*>(DI->getArg(i))) {
137 Record *Rec = Arg->getDef();
139 if (Rec->isSubClassOf("RegisterClass"))
140 Ty = getValueType(Rec->getValueAsDef("RegType"));
141 else if (Rec->getName() == "i8imm")
143 else if (Rec->getName() == "i16imm")
145 else if (Rec->getName() == "i32imm")
147 else if (Rec->getName() == "i64imm")
150 throw "Unknown operand class '" + Rec->getName() +
151 "' in instruction '" + R->getName() + "' instruction!";
153 OperandList.push_back(OperandInfo(Rec, Ty, DI->getArgName(i)));
155 throw "Illegal operand for the '" + R->getName() + "' instruction!";
158 // Error parsing operands list, just ignore it.
166 /// getOperandNamed - Return the index of the operand with the specified
167 /// non-empty name. If the instruction does not have an operand with the
168 /// specified name, throw an exception.
169 unsigned CodeGenInstruction::getOperandNamed(const std::string &Name) const {
170 assert(!Name.empty() && "Cannot search for operand with no name!");
171 for (unsigned i = 0, e = OperandList.size(); i != e; ++i)
172 if (OperandList[i].Name == Name) return i;
173 throw "Instruction '" + TheDef->getName() +
174 "' does not have an operand named '$" + Name + "'!";