Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fd6c2f0 | 2007-12-29 20:37:13 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend is responsible for emitting a description of the target |
| 11 | // instruction set for the code generator. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "InstrInfoEmitter.h" |
| 16 | #include "CodeGenTarget.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "Record.h" |
| 18 | #include <algorithm> |
Chris Lattner | 04a3a4c | 2007-12-30 00:25:23 +0000 | [diff] [blame] | 19 | #include <iostream> |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chris Lattner | 4533a72 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 22 | static void PrintDefList(const std::vector<Record*> &Uses, |
| 23 | unsigned Num, std::ostream &OS) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | OS << "static const unsigned ImplicitList" << Num << "[] = { "; |
| 25 | for (unsigned i = 0, e = Uses.size(); i != e; ++i) |
| 26 | OS << getQualifiedName(Uses[i]) << ", "; |
| 27 | OS << "0 };\n"; |
| 28 | } |
| 29 | |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 30 | //===----------------------------------------------------------------------===// |
| 31 | // Instruction Itinerary Information. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | struct RecordNameComparator { |
| 35 | bool operator()(const Record *Rec1, const Record *Rec2) const { |
| 36 | return Rec1->getName() < Rec2->getName(); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | void InstrInfoEmitter::GatherItinClasses() { |
| 41 | std::vector<Record*> DefList = |
| 42 | Records.getAllDerivedDefinitions("InstrItinClass"); |
| 43 | std::sort(DefList.begin(), DefList.end(), RecordNameComparator()); |
| 44 | |
| 45 | for (unsigned i = 0, N = DefList.size(); i < N; i++) |
| 46 | ItinClassMap[DefList[i]->getName()] = i; |
| 47 | } |
| 48 | |
| 49 | unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) { |
| 50 | return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()]; |
| 51 | } |
| 52 | |
| 53 | //===----------------------------------------------------------------------===// |
| 54 | // Operand Info Emission. |
| 55 | //===----------------------------------------------------------------------===// |
| 56 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | std::vector<std::string> |
| 58 | InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) { |
| 59 | std::vector<std::string> Result; |
| 60 | |
| 61 | for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) { |
| 62 | // Handle aggregate operands and normal operands the same way by expanding |
| 63 | // either case into a list of operands for this op. |
| 64 | std::vector<CodeGenInstruction::OperandInfo> OperandList; |
| 65 | |
| 66 | // This might be a multiple operand thing. Targets like X86 have |
| 67 | // registers in their multi-operand operands. It may also be an anonymous |
| 68 | // operand, which has a single operand, but no declared class for the |
| 69 | // operand. |
| 70 | DagInit *MIOI = Inst.OperandList[i].MIOperandInfo; |
| 71 | |
| 72 | if (!MIOI || MIOI->getNumArgs() == 0) { |
| 73 | // Single, anonymous, operand. |
| 74 | OperandList.push_back(Inst.OperandList[i]); |
| 75 | } else { |
| 76 | for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) { |
| 77 | OperandList.push_back(Inst.OperandList[i]); |
| 78 | |
| 79 | Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef(); |
| 80 | OperandList.back().Rec = OpR; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | for (unsigned j = 0, e = OperandList.size(); j != e; ++j) { |
| 85 | Record *OpR = OperandList[j].Rec; |
| 86 | std::string Res; |
| 87 | |
| 88 | if (OpR->isSubClassOf("RegisterClass")) |
| 89 | Res += getQualifiedName(OpR) + "RegClassID, "; |
| 90 | else |
| 91 | Res += "0, "; |
| 92 | // Fill in applicable flags. |
| 93 | Res += "0"; |
| 94 | |
| 95 | // Ptr value whose register class is resolved via callback. |
| 96 | if (OpR->getName() == "ptr_rc") |
Chris Lattner | eeedb48 | 2008-01-07 02:39:19 +0000 | [diff] [blame] | 97 | Res += "|TOI::LookupPtrRegClass"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 98 | |
| 99 | // Predicate operands. Check to see if the original unexpanded operand |
| 100 | // was of type PredicateOperand. |
| 101 | if (Inst.OperandList[i].Rec->isSubClassOf("PredicateOperand")) |
Chris Lattner | eeedb48 | 2008-01-07 02:39:19 +0000 | [diff] [blame] | 102 | Res += "|TOI::Predicate"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | |
| 104 | // Optional def operands. Check to see if the original unexpanded operand |
| 105 | // was of type OptionalDefOperand. |
| 106 | if (Inst.OperandList[i].Rec->isSubClassOf("OptionalDefOperand")) |
Chris Lattner | eeedb48 | 2008-01-07 02:39:19 +0000 | [diff] [blame] | 107 | Res += "|TOI::OptionalDef"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 108 | |
| 109 | // Fill in constraint info. |
| 110 | Res += ", " + Inst.OperandList[i].Constraints[j]; |
| 111 | Result.push_back(Res); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return Result; |
| 116 | } |
| 117 | |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 118 | void InstrInfoEmitter::EmitOperandInfo(std::ostream &OS, |
| 119 | OperandInfoMapTy &OperandInfoIDs) { |
| 120 | // ID #0 is for no operand info. |
| 121 | unsigned OperandListNum = 0; |
| 122 | OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum; |
| 123 | |
| 124 | OS << "\n"; |
| 125 | const CodeGenTarget &Target = CDP.getTargetInfo(); |
| 126 | for (CodeGenTarget::inst_iterator II = Target.inst_begin(), |
| 127 | E = Target.inst_end(); II != E; ++II) { |
| 128 | std::vector<std::string> OperandInfo = GetOperandInfo(II->second); |
| 129 | unsigned &N = OperandInfoIDs[OperandInfo]; |
| 130 | if (N != 0) continue; |
| 131 | |
| 132 | N = ++OperandListNum; |
| 133 | OS << "static const TargetOperandInfo OperandInfo" << N << "[] = { "; |
| 134 | for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i) |
| 135 | OS << "{ " << OperandInfo[i] << " }, "; |
| 136 | OS << "};\n"; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | //===----------------------------------------------------------------------===// |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 141 | // Instruction Analysis |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
Chris Lattner | ccc5d9c | 2008-01-06 02:16:26 +0000 | [diff] [blame] | 144 | class InstAnalyzer { |
| 145 | const CodeGenDAGPatterns &CDP; |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 146 | bool &mayStore; |
Chris Lattner | ccc5d9c | 2008-01-06 02:16:26 +0000 | [diff] [blame] | 147 | bool &isLoad; |
| 148 | bool &NeverHasSideEffects; |
| 149 | public: |
| 150 | InstAnalyzer(const CodeGenDAGPatterns &cdp, |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 151 | bool &maystore, bool &isload, bool &nhse) |
| 152 | : CDP(cdp), mayStore(maystore), isLoad(isload), NeverHasSideEffects(nhse) { |
Chris Lattner | ccc5d9c | 2008-01-06 02:16:26 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Analyze(Record *InstRecord) { |
| 156 | const TreePattern *Pattern = CDP.getInstruction(InstRecord).getPattern(); |
| 157 | if (Pattern == 0) return; // No pattern. |
| 158 | |
| 159 | // Assume there is no side-effect unless we see one. |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 160 | NeverHasSideEffects = true; |
Chris Lattner | ccc5d9c | 2008-01-06 02:16:26 +0000 | [diff] [blame] | 161 | |
| 162 | // FIXME: Assume only the first tree is the pattern. The others are clobber |
| 163 | // nodes. |
| 164 | AnalyzeNode(Pattern->getTree(0)); |
| 165 | } |
| 166 | |
| 167 | private: |
| 168 | void AnalyzeNode(const TreePatternNode *N) { |
| 169 | if (N->isLeaf()) { |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (N->getOperator()->getName() != "set") { |
| 174 | // Get information about the SDNode for the operator. |
| 175 | const SDNodeInfo &OpInfo = CDP.getSDNodeInfo(N->getOperator()); |
| 176 | |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 177 | // If node writes to memory, it obviously stores to memory. |
| 178 | if (OpInfo.hasProperty(SDNPMayStore)) { |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 179 | mayStore = true; |
Chris Lattner | d99f236 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 180 | } else if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) { |
| 181 | // If this is an intrinsic, analyze it. |
| 182 | if (IntInfo->ModRef >= CodeGenIntrinsic::WriteArgMem) |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 183 | mayStore = true;// Intrinsics that can write to memory are 'mayStore'. |
Chris Lattner | d99f236 | 2008-01-06 05:36:50 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | ccc5d9c | 2008-01-06 02:16:26 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i) |
| 188 | AnalyzeNode(N->getChild(i)); |
| 189 | } |
| 190 | |
| 191 | }; |
| 192 | |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 193 | void InstrInfoEmitter::InferFromPattern(const CodeGenInstruction &Inst, |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 194 | bool &mayStore, bool &isLoad, |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 195 | bool &NeverHasSideEffects) { |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 196 | mayStore = isLoad = NeverHasSideEffects = false; |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 198 | InstAnalyzer(CDP, mayStore, isLoad, NeverHasSideEffects).Analyze(Inst.TheDef); |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 200 | // InstAnalyzer only correctly analyzes mayStore so far. |
| 201 | if (Inst.mayStore) { // If the .td file explicitly sets mayStore, use it. |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 202 | // If we decided that this is a store from the pattern, then the .td file |
| 203 | // entry is redundant. |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 204 | if (mayStore) |
| 205 | fprintf(stderr, |
| 206 | "Warning: mayStore flag explicitly set on instruction '%s'" |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 207 | " but flag already inferred from pattern.\n", |
Chris Lattner | 59484fa | 2008-01-07 04:57:31 +0000 | [diff] [blame^] | 208 | Inst.TheDef->getName().c_str()); |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 209 | mayStore = true; |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // These two override everything. |
Chris Lattner | 1a1932c | 2008-01-06 23:38:27 +0000 | [diff] [blame] | 213 | isLoad = Inst.isSimpleLoad; |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 214 | NeverHasSideEffects = Inst.neverHasSideEffects; |
| 215 | |
| 216 | #if 0 |
Chris Lattner | ccc5d9c | 2008-01-06 02:16:26 +0000 | [diff] [blame] | 217 | // If the .td file explicitly says there is no side effect, believe it. |
| 218 | if (Inst.neverHasSideEffects) |
| 219 | NeverHasSideEffects = true; |
Chris Lattner | ef8d608 | 2008-01-06 06:44:58 +0000 | [diff] [blame] | 220 | #endif |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | |
| 224 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 225 | // Main Output. |
| 226 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 227 | |
| 228 | // run - Emit the main instruction description records for the target... |
| 229 | void InstrInfoEmitter::run(std::ostream &OS) { |
| 230 | GatherItinClasses(); |
| 231 | |
| 232 | EmitSourceFileHeader("Target Instruction Descriptors", OS); |
| 233 | OS << "namespace llvm {\n\n"; |
| 234 | |
| 235 | CodeGenTarget Target; |
| 236 | const std::string &TargetName = Target.getName(); |
| 237 | Record *InstrInfo = Target.getInstructionSet(); |
| 238 | |
| 239 | // Keep track of all of the def lists we have emitted already. |
| 240 | std::map<std::vector<Record*>, unsigned> EmittedLists; |
| 241 | unsigned ListNumber = 0; |
| 242 | |
| 243 | // Emit all of the instruction's implicit uses and defs. |
| 244 | for (CodeGenTarget::inst_iterator II = Target.inst_begin(), |
| 245 | E = Target.inst_end(); II != E; ++II) { |
| 246 | Record *Inst = II->second.TheDef; |
| 247 | std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses"); |
| 248 | if (!Uses.empty()) { |
| 249 | unsigned &IL = EmittedLists[Uses]; |
Chris Lattner | 4533a72 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 250 | if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 251 | } |
| 252 | std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs"); |
| 253 | if (!Defs.empty()) { |
| 254 | unsigned &IL = EmittedLists[Defs]; |
Chris Lattner | 4533a72 | 2008-01-06 01:21:51 +0000 | [diff] [blame] | 255 | if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 259 | OperandInfoMapTy OperandInfoIDs; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | |
| 261 | // Emit all of the operand info records. |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 262 | EmitOperandInfo(OS, OperandInfoIDs); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 263 | |
| 264 | // Emit all of the TargetInstrDescriptor records in their ENUM ordering. |
| 265 | // |
| 266 | OS << "\nstatic const TargetInstrDescriptor " << TargetName |
| 267 | << "Insts[] = {\n"; |
| 268 | std::vector<const CodeGenInstruction*> NumberedInstructions; |
| 269 | Target.getInstructionsByEnumValue(NumberedInstructions); |
| 270 | |
| 271 | for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) |
| 272 | emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists, |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 273 | OperandInfoIDs, OS); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 274 | OS << "};\n"; |
| 275 | OS << "} // End llvm namespace \n"; |
| 276 | } |
| 277 | |
| 278 | void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num, |
| 279 | Record *InstrInfo, |
| 280 | std::map<std::vector<Record*>, unsigned> &EmittedLists, |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 281 | const OperandInfoMapTy &OpInfo, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | std::ostream &OS) { |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 283 | // Determine properties of the instruction from its pattern. |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 284 | bool mayStore, isLoad, NeverHasSideEffects; |
| 285 | InferFromPattern(Inst, mayStore, isLoad, NeverHasSideEffects); |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 286 | |
| 287 | if (NeverHasSideEffects && Inst.mayHaveSideEffects) { |
Chris Lattner | 59484fa | 2008-01-07 04:57:31 +0000 | [diff] [blame^] | 288 | std::cerr << "error: Instruction '" << Inst.TheDef->getName() |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 289 | << "' is marked with 'mayHaveSideEffects', but it can never have them!\n"; |
| 290 | exit(1); |
| 291 | } |
| 292 | |
| 293 | int MinOperands = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 294 | if (!Inst.OperandList.empty()) |
| 295 | // Each logical operand can be multiple MI operands. |
| 296 | MinOperands = Inst.OperandList.back().MIOperandNo + |
| 297 | Inst.OperandList.back().MINumOperands; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 298 | |
| 299 | OS << " { "; |
Evan Cheng | 84c52f6 | 2007-08-02 00:20:17 +0000 | [diff] [blame] | 300 | OS << Num << ",\t" << MinOperands << ",\t" |
Chris Lattner | 59484fa | 2008-01-07 04:57:31 +0000 | [diff] [blame^] | 301 | << Inst.NumDefs << ",\t\"" << Inst.TheDef->getName(); |
Chris Lattner | 126489a | 2008-01-06 01:12:44 +0000 | [diff] [blame] | 302 | OS << "\",\t" << getItinClassNumber(Inst.TheDef) << ", 0"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 303 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 304 | // Emit all of the target indepedent flags... |
| 305 | if (Inst.isReturn) OS << "|M_RET_FLAG"; |
| 306 | if (Inst.isBranch) OS << "|M_BRANCH_FLAG"; |
Owen Anderson | f805308 | 2007-11-12 07:39:39 +0000 | [diff] [blame] | 307 | if (Inst.isIndirectBranch) OS << "|M_INDIRECT_FLAG"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 308 | if (Inst.isBarrier) OS << "|M_BARRIER_FLAG"; |
| 309 | if (Inst.hasDelaySlot) OS << "|M_DELAY_SLOT_FLAG"; |
| 310 | if (Inst.isCall) OS << "|M_CALL_FLAG"; |
Chris Lattner | 1a1932c | 2008-01-06 23:38:27 +0000 | [diff] [blame] | 311 | if (isLoad) OS << "|M_SIMPLE_LOAD_FLAG"; |
Chris Lattner | 6887b14 | 2008-01-06 08:36:04 +0000 | [diff] [blame] | 312 | if (mayStore) OS << "|M_MAY_STORE_FLAG"; |
Evan Cheng | 6782f7f | 2007-12-13 00:42:35 +0000 | [diff] [blame] | 313 | if (Inst.isImplicitDef)OS << "|M_IMPLICIT_DEF_FLAG"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 314 | if (Inst.isPredicable) OS << "|M_PREDICABLE"; |
| 315 | if (Inst.isConvertibleToThreeAddress) OS << "|M_CONVERTIBLE_TO_3_ADDR"; |
| 316 | if (Inst.isCommutable) OS << "|M_COMMUTABLE"; |
| 317 | if (Inst.isTerminator) OS << "|M_TERMINATOR_FLAG"; |
| 318 | if (Inst.isReMaterializable) OS << "|M_REMATERIALIZIBLE"; |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 319 | if (Inst.isNotDuplicable) OS << "|M_NOT_DUPLICABLE"; |
| 320 | if (Inst.hasOptionalDef) OS << "|M_HAS_OPTIONAL_DEF"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 321 | if (Inst.usesCustomDAGSchedInserter) |
| 322 | OS << "|M_USES_CUSTOM_DAG_SCHED_INSERTION"; |
Bill Wendling | af109da | 2007-12-14 01:48:59 +0000 | [diff] [blame] | 323 | if (Inst.hasVariableNumberOfOperands) OS << "|M_VARIABLE_OPS"; |
Chris Lattner | 44435a7 | 2008-01-06 01:53:37 +0000 | [diff] [blame] | 324 | if (Inst.mayHaveSideEffects) OS << "|M_MAY_HAVE_SIDE_EFFECTS"; |
| 325 | if (NeverHasSideEffects) OS << "|M_NEVER_HAS_SIDE_EFFECTS"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 326 | OS << ", 0"; |
| 327 | |
| 328 | // Emit all of the target-specific flags... |
| 329 | ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields"); |
| 330 | ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts"); |
| 331 | if (LI->getSize() != Shift->getSize()) |
| 332 | throw "Lengths of " + InstrInfo->getName() + |
| 333 | ":(TargetInfoFields, TargetInfoPositions) must be equal!"; |
| 334 | |
| 335 | for (unsigned i = 0, e = LI->getSize(); i != e; ++i) |
| 336 | emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)), |
| 337 | dynamic_cast<IntInit*>(Shift->getElement(i)), OS); |
| 338 | |
| 339 | OS << ", "; |
| 340 | |
| 341 | // Emit the implicit uses and defs lists... |
| 342 | std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses"); |
| 343 | if (UseList.empty()) |
| 344 | OS << "NULL, "; |
| 345 | else |
| 346 | OS << "ImplicitList" << EmittedLists[UseList] << ", "; |
| 347 | |
| 348 | std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs"); |
| 349 | if (DefList.empty()) |
| 350 | OS << "NULL, "; |
| 351 | else |
| 352 | OS << "ImplicitList" << EmittedLists[DefList] << ", "; |
| 353 | |
| 354 | // Emit the operand info. |
| 355 | std::vector<std::string> OperandInfo = GetOperandInfo(Inst); |
| 356 | if (OperandInfo.empty()) |
| 357 | OS << "0"; |
| 358 | else |
Chris Lattner | 0d58d02 | 2008-01-06 01:20:13 +0000 | [diff] [blame] | 359 | OS << "OperandInfo" << OpInfo.find(OperandInfo)->second; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 360 | |
| 361 | OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n"; |
| 362 | } |
| 363 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 364 | |
| 365 | void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val, |
| 366 | IntInit *ShiftInt, std::ostream &OS) { |
| 367 | if (Val == 0 || ShiftInt == 0) |
| 368 | throw std::string("Illegal value or shift amount in TargetInfo*!"); |
| 369 | RecordVal *RV = R->getValue(Val->getValue()); |
| 370 | int Shift = ShiftInt->getValue(); |
| 371 | |
| 372 | if (RV == 0 || RV->getValue() == 0) { |
| 373 | // This isn't an error if this is a builtin instruction. |
| 374 | if (R->getName() != "PHI" && |
| 375 | R->getName() != "INLINEASM" && |
Christopher Lamb | 071a2a7 | 2007-07-26 07:48:21 +0000 | [diff] [blame] | 376 | R->getName() != "LABEL" && |
| 377 | R->getName() != "EXTRACT_SUBREG" && |
| 378 | R->getName() != "INSERT_SUBREG") |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 379 | throw R->getName() + " doesn't have a field named '" + |
| 380 | Val->getValue() + "'!"; |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | Init *Value = RV->getValue(); |
| 385 | if (BitInit *BI = dynamic_cast<BitInit*>(Value)) { |
| 386 | if (BI->getValue()) OS << "|(1<<" << Shift << ")"; |
| 387 | return; |
| 388 | } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) { |
| 389 | // Convert the Bits to an integer to print... |
| 390 | Init *I = BI->convertInitializerTo(new IntRecTy()); |
| 391 | if (I) |
| 392 | if (IntInit *II = dynamic_cast<IntInit*>(I)) { |
| 393 | if (II->getValue()) { |
| 394 | if (Shift) |
| 395 | OS << "|(" << II->getValue() << "<<" << Shift << ")"; |
| 396 | else |
| 397 | OS << "|" << II->getValue(); |
| 398 | } |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) { |
| 403 | if (II->getValue()) { |
| 404 | if (Shift) |
| 405 | OS << "|(" << II->getValue() << "<<" << Shift << ")"; |
| 406 | else |
| 407 | OS << II->getValue(); |
| 408 | } |
| 409 | return; |
| 410 | } |
| 411 | |
Chris Lattner | 04a3a4c | 2007-12-30 00:25:23 +0000 | [diff] [blame] | 412 | std::cerr << "Unhandled initializer: " << *Val << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 413 | throw "In record '" + R->getName() + "' for TSFlag emission."; |
| 414 | } |
| 415 | |