blob: 86615eb9320bad02249aab7e93880f4888f86928 [file] [log] [blame]
Chris Lattner33ccf7e2003-08-03 17:24:10 +00001//===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell01d45822003-10-20 20:20:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell01d45822003-10-20 20:20:30 +00008//===----------------------------------------------------------------------===//
Chris Lattner33ccf7e2003-08-03 17:24:10 +00009//
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"
Chris Lattner803a5f62004-08-01 04:04:35 +000016#include "CodeGenTarget.h"
Chris Lattner33ccf7e2003-08-03 17:24:10 +000017#include "Record.h"
Jeff Cohencb366d92005-11-01 18:04:06 +000018#include <algorithm>
Chris Lattner2c36aff2007-12-30 00:25:23 +000019#include <iostream>
Chris Lattner2082ebe2004-08-01 03:55:39 +000020using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000021
Chris Lattner5fbe2752008-01-06 01:21:51 +000022static void PrintDefList(const std::vector<Record*> &Uses,
23 unsigned Num, std::ostream &OS) {
Chris Lattnera3ac88d2005-08-18 21:36:47 +000024 OS << "static const unsigned ImplicitList" << Num << "[] = { ";
25 for (unsigned i = 0, e = Uses.size(); i != e; ++i)
26 OS << getQualifiedName(Uses[i]) << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +000027 OS << "0 };\n";
28}
29
Chris Lattneref8339b2008-01-06 01:20:13 +000030//===----------------------------------------------------------------------===//
31// Instruction Itinerary Information.
32//===----------------------------------------------------------------------===//
33
34struct RecordNameComparator {
35 bool operator()(const Record *Rec1, const Record *Rec2) const {
36 return Rec1->getName() < Rec2->getName();
37 }
38};
39
40void 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
49unsigned InstrInfoEmitter::getItinClassNumber(const Record *InstRec) {
50 return ItinClassMap[InstRec->getValueAsDef("Itinerary")->getName()];
51}
52
53//===----------------------------------------------------------------------===//
54// Operand Info Emission.
55//===----------------------------------------------------------------------===//
56
Chris Lattnera0cca4a2006-11-06 23:49:51 +000057std::vector<std::string>
58InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
59 std::vector<std::string> Result;
Chris Lattnerf1968392006-11-10 02:01:40 +000060
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000061 for (unsigned i = 0, e = Inst.OperandList.size(); i != e; ++i) {
Chris Lattnerf1968392006-11-10 02:01:40 +000062 // 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;
Chris Lattnera0cca4a2006-11-06 23:49:51 +000065
Chris Lattnerf1968392006-11-10 02:01:40 +000066 // 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]);
Chris Lattner65303d62005-11-19 07:05:57 +000075 } else {
Chris Lattner65303d62005-11-19 07:05:57 +000076 for (unsigned j = 0, e = Inst.OperandList[i].MINumOperands; j != e; ++j) {
Chris Lattnerf1968392006-11-10 02:01:40 +000077 OperandList.push_back(Inst.OperandList[i]);
Chris Lattnera0cca4a2006-11-06 23:49:51 +000078
Chris Lattnerf1968392006-11-10 02:01:40 +000079 Record *OpR = dynamic_cast<DefInit*>(MIOI->getArg(j))->getDef();
80 OperandList.back().Rec = OpR;
Chris Lattner65303d62005-11-19 07:05:57 +000081 }
Chris Lattnerd5aa3e22005-08-19 18:46:26 +000082 }
Chris Lattnerf1968392006-11-10 02:01:40 +000083
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")
97 Res += "|M_LOOK_UP_PTR_REG_CLASS";
98
99 // Predicate operands. Check to see if the original unexpanded operand
100 // was of type PredicateOperand.
Evan Chengc419bd32007-07-06 23:23:38 +0000101 if (Inst.OperandList[i].Rec->isSubClassOf("PredicateOperand"))
Chris Lattnerf1968392006-11-10 02:01:40 +0000102 Res += "|M_PREDICATE_OPERAND";
103
Evan Cheng88cc0922007-07-10 18:05:01 +0000104 // Optional def operands. Check to see if the original unexpanded operand
105 // was of type OptionalDefOperand.
106 if (Inst.OperandList[i].Rec->isSubClassOf("OptionalDefOperand"))
107 Res += "|M_OPTIONAL_DEF_OPERAND";
108
Chris Lattnerf1968392006-11-10 02:01:40 +0000109 // Fill in constraint info.
Chris Lattner0bb75002006-11-15 02:38:17 +0000110 Res += ", " + Inst.OperandList[i].Constraints[j];
Chris Lattnerf1968392006-11-10 02:01:40 +0000111 Result.push_back(Res);
112 }
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000113 }
Evan Chenge2ba8972006-11-01 00:27:05 +0000114
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000115 return Result;
116}
117
Chris Lattneref8339b2008-01-06 01:20:13 +0000118void 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 Lattnera529a372008-01-06 01:53:37 +0000141// Instruction Analysis
142//===----------------------------------------------------------------------===//
143
Chris Lattner2d51a4c2008-01-06 02:16:26 +0000144class InstAnalyzer {
145 const CodeGenDAGPatterns &CDP;
146 bool &isStore;
147 bool &isLoad;
148 bool &NeverHasSideEffects;
149public:
150 InstAnalyzer(const CodeGenDAGPatterns &cdp,
151 bool &isstore, bool &isload, bool &nhse)
152 : CDP(cdp), isStore(isstore), isLoad(isload), NeverHasSideEffects(nhse) {
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 Lattnerc8478d82008-01-06 06:44:58 +0000160 NeverHasSideEffects = true;
Chris Lattner2d51a4c2008-01-06 02:16:26 +0000161
162 // FIXME: Assume only the first tree is the pattern. The others are clobber
163 // nodes.
164 AnalyzeNode(Pattern->getTree(0));
165 }
166
167private:
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 Lattnerc8478d82008-01-06 06:44:58 +0000177 // If node writes to memory, it obviously stores to memory.
178 if (OpInfo.hasProperty(SDNPMayStore)) {
Chris Lattner2d51a4c2008-01-06 02:16:26 +0000179 isStore = true;
Chris Lattnere67bde52008-01-06 05:36:50 +0000180 } else if (const CodeGenIntrinsic *IntInfo = N->getIntrinsicInfo(CDP)) {
181 // If this is an intrinsic, analyze it.
182 if (IntInfo->ModRef >= CodeGenIntrinsic::WriteArgMem)
183 isStore = true; // Intrinsics that can write to memory are 'isStore'.
184 }
Chris Lattner2d51a4c2008-01-06 02:16:26 +0000185 }
186
187 for (unsigned i = 0, e = N->getNumChildren(); i != e; ++i)
188 AnalyzeNode(N->getChild(i));
189 }
190
191};
192
Chris Lattnera529a372008-01-06 01:53:37 +0000193void InstrInfoEmitter::InferFromPattern(const CodeGenInstruction &Inst,
194 bool &isStore, bool &isLoad,
195 bool &NeverHasSideEffects) {
Chris Lattnerc8478d82008-01-06 06:44:58 +0000196 isStore = isLoad = NeverHasSideEffects = false;
Chris Lattnera529a372008-01-06 01:53:37 +0000197
Chris Lattner2d51a4c2008-01-06 02:16:26 +0000198 InstAnalyzer(CDP, isStore, isLoad, NeverHasSideEffects).Analyze(Inst.TheDef);
Chris Lattnerc8478d82008-01-06 06:44:58 +0000199
200 // InstAnalyzer only correctly analyzes isStore so far.
201 if (Inst.isStore) { // If the .td file explicitly sets isStore, use it.
202 // If we decided that this is a store from the pattern, then the .td file
203 // entry is redundant.
204 if (isStore)
205 fprintf(stderr, "Warning: isStore flag explicitly set on instruction '%s'"
206 " but flag already inferred from pattern.\n",
207 Inst.getName().c_str());
208 isStore = true;
209 }
210
211 // These two override everything.
212 isLoad = Inst.isLoad;
213 NeverHasSideEffects = Inst.neverHasSideEffects;
214
215#if 0
Chris Lattner2d51a4c2008-01-06 02:16:26 +0000216 // If the .td file explicitly says there is no side effect, believe it.
217 if (Inst.neverHasSideEffects)
218 NeverHasSideEffects = true;
Chris Lattnerc8478d82008-01-06 06:44:58 +0000219#endif
Chris Lattnera529a372008-01-06 01:53:37 +0000220}
221
222
223//===----------------------------------------------------------------------===//
Chris Lattneref8339b2008-01-06 01:20:13 +0000224// Main Output.
225//===----------------------------------------------------------------------===//
Chris Lattnera3ae6142003-08-03 21:57:51 +0000226
227// run - Emit the main instruction description records for the target...
228void InstrInfoEmitter::run(std::ostream &OS) {
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000229 GatherItinClasses();
230
Chris Lattnerbc017232003-08-06 04:32:07 +0000231 EmitSourceFileHeader("Target Instruction Descriptors", OS);
Chris Lattner2c384132004-08-17 03:08:28 +0000232 OS << "namespace llvm {\n\n";
233
Chris Lattner7884b752003-08-07 05:39:09 +0000234 CodeGenTarget Target;
235 const std::string &TargetName = Target.getName();
236 Record *InstrInfo = Target.getInstructionSet();
Chris Lattnera3ae6142003-08-03 21:57:51 +0000237
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000238 // Keep track of all of the def lists we have emitted already.
239 std::map<std::vector<Record*>, unsigned> EmittedLists;
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000240 unsigned ListNumber = 0;
241
242 // Emit all of the instruction's implicit uses and defs.
Chris Lattnerec352402004-08-01 05:04:00 +0000243 for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
244 E = Target.inst_end(); II != E; ++II) {
245 Record *Inst = II->second.TheDef;
Chris Lattner366080c2005-10-28 22:59:53 +0000246 std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
247 if (!Uses.empty()) {
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000248 unsigned &IL = EmittedLists[Uses];
Chris Lattner5fbe2752008-01-06 01:21:51 +0000249 if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000250 }
Chris Lattner366080c2005-10-28 22:59:53 +0000251 std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
252 if (!Defs.empty()) {
253 unsigned &IL = EmittedLists[Defs];
Chris Lattner5fbe2752008-01-06 01:21:51 +0000254 if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000255 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000256 }
257
Chris Lattneref8339b2008-01-06 01:20:13 +0000258 OperandInfoMapTy OperandInfoIDs;
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000259
Chris Lattner0e384b62005-08-19 16:57:28 +0000260 // Emit all of the operand info records.
Chris Lattneref8339b2008-01-06 01:20:13 +0000261 EmitOperandInfo(OS, OperandInfoIDs);
Chris Lattner0e384b62005-08-19 16:57:28 +0000262
Chris Lattnerf52e2612006-01-27 01:44:09 +0000263 // Emit all of the TargetInstrDescriptor records in their ENUM ordering.
Chris Lattner0e384b62005-08-19 16:57:28 +0000264 //
Chris Lattnera3ae6142003-08-03 21:57:51 +0000265 OS << "\nstatic const TargetInstrDescriptor " << TargetName
266 << "Insts[] = {\n";
Chris Lattnerf52e2612006-01-27 01:44:09 +0000267 std::vector<const CodeGenInstruction*> NumberedInstructions;
268 Target.getInstructionsByEnumValue(NumberedInstructions);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000269
Chris Lattnerf52e2612006-01-27 01:44:09 +0000270 for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
271 emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
Chris Lattneref8339b2008-01-06 01:20:13 +0000272 OperandInfoIDs, OS);
Chris Lattnera3ae6142003-08-03 21:57:51 +0000273 OS << "};\n";
Chris Lattner2c384132004-08-17 03:08:28 +0000274 OS << "} // End llvm namespace \n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000275}
276
Chris Lattnerec352402004-08-01 05:04:00 +0000277void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000278 Record *InstrInfo,
Chris Lattner366080c2005-10-28 22:59:53 +0000279 std::map<std::vector<Record*>, unsigned> &EmittedLists,
Chris Lattneref8339b2008-01-06 01:20:13 +0000280 const OperandInfoMapTy &OpInfo,
Chris Lattnera3ac88d2005-08-18 21:36:47 +0000281 std::ostream &OS) {
Chris Lattnera529a372008-01-06 01:53:37 +0000282 // Determine properties of the instruction from its pattern.
283 bool isStore, isLoad, NeverHasSideEffects;
284 InferFromPattern(Inst, isStore, isLoad, NeverHasSideEffects);
285
286 if (NeverHasSideEffects && Inst.mayHaveSideEffects) {
287 std::cerr << "error: Instruction '" << Inst.getName()
288 << "' is marked with 'mayHaveSideEffects', but it can never have them!\n";
289 exit(1);
290 }
291
292 int MinOperands = 0;
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000293 if (!Inst.OperandList.empty())
Chris Lattnerd98958f2005-08-19 00:59:49 +0000294 // Each logical operand can be multiple MI operands.
Evan Cheng8d3af5e2006-06-15 07:22:16 +0000295 MinOperands = Inst.OperandList.back().MIOperandNo +
Chris Lattnerd98958f2005-08-19 00:59:49 +0000296 Inst.OperandList.back().MINumOperands;
Chris Lattnerd98958f2005-08-19 00:59:49 +0000297
Evan Chengfb1aab02006-11-17 01:46:27 +0000298 OS << " { ";
Evan Chengb5910822007-08-02 00:20:17 +0000299 OS << Num << ",\t" << MinOperands << ",\t"
Chris Lattnera529a372008-01-06 01:53:37 +0000300 << Inst.NumDefs << ",\t\"" << Inst.getName();
Chris Lattner951740a2008-01-06 01:12:44 +0000301 OS << "\",\t" << getItinClassNumber(Inst.TheDef) << ", 0";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000302
303 // Emit all of the target indepedent flags...
Chris Lattnerec352402004-08-01 05:04:00 +0000304 if (Inst.isReturn) OS << "|M_RET_FLAG";
305 if (Inst.isBranch) OS << "|M_BRANCH_FLAG";
Owen Anderson20ab2902007-11-12 07:39:39 +0000306 if (Inst.isIndirectBranch) OS << "|M_INDIRECT_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000307 if (Inst.isBarrier) OS << "|M_BARRIER_FLAG";
Chris Lattner5b71d3a2004-09-28 18:38:01 +0000308 if (Inst.hasDelaySlot) OS << "|M_DELAY_SLOT_FLAG";
Chris Lattnerec352402004-08-01 05:04:00 +0000309 if (Inst.isCall) OS << "|M_CALL_FLAG";
Chris Lattnera529a372008-01-06 01:53:37 +0000310 if (isLoad) OS << "|M_LOAD_FLAG";
311 if (isStore) OS << "|M_STORE_FLAG";
Evan Cheng3dd298f2007-12-13 00:42:35 +0000312 if (Inst.isImplicitDef)OS << "|M_IMPLICIT_DEF_FLAG";
Evan Cheng5127ce02007-05-16 20:45:24 +0000313 if (Inst.isPredicable) OS << "|M_PREDICABLE";
Chris Lattneraad75aa2005-01-02 02:29:04 +0000314 if (Inst.isConvertibleToThreeAddress) OS << "|M_CONVERTIBLE_TO_3_ADDR";
315 if (Inst.isCommutable) OS << "|M_COMMUTABLE";
Chris Lattnerec352402004-08-01 05:04:00 +0000316 if (Inst.isTerminator) OS << "|M_TERMINATOR_FLAG";
Dan Gohmand45eddd2007-06-26 00:48:07 +0000317 if (Inst.isReMaterializable) OS << "|M_REMATERIALIZIBLE";
Chris Lattnera529a372008-01-06 01:53:37 +0000318 if (Inst.isNotDuplicable) OS << "|M_NOT_DUPLICABLE";
319 if (Inst.hasOptionalDef) OS << "|M_HAS_OPTIONAL_DEF";
Chris Lattner5f89bf02005-08-26 20:42:52 +0000320 if (Inst.usesCustomDAGSchedInserter)
Chris Lattner8b50f9b2005-08-26 20:40:46 +0000321 OS << "|M_USES_CUSTOM_DAG_SCHED_INSERTION";
Bill Wendling6b1da9c2007-12-14 01:48:59 +0000322 if (Inst.hasVariableNumberOfOperands) OS << "|M_VARIABLE_OPS";
Chris Lattnera529a372008-01-06 01:53:37 +0000323 if (Inst.mayHaveSideEffects) OS << "|M_MAY_HAVE_SIDE_EFFECTS";
324 if (NeverHasSideEffects) OS << "|M_NEVER_HAS_SIDE_EFFECTS";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000325 OS << ", 0";
326
327 // Emit all of the target-specific flags...
328 ListInit *LI = InstrInfo->getValueAsListInit("TSFlagsFields");
329 ListInit *Shift = InstrInfo->getValueAsListInit("TSFlagsShifts");
330 if (LI->getSize() != Shift->getSize())
331 throw "Lengths of " + InstrInfo->getName() +
332 ":(TargetInfoFields, TargetInfoPositions) must be equal!";
333
334 for (unsigned i = 0, e = LI->getSize(); i != e; ++i)
Chris Lattnerec352402004-08-01 05:04:00 +0000335 emitShiftedValue(Inst.TheDef, dynamic_cast<StringInit*>(LI->getElement(i)),
Chris Lattnera3ae6142003-08-03 21:57:51 +0000336 dynamic_cast<IntInit*>(Shift->getElement(i)), OS);
337
338 OS << ", ";
339
340 // Emit the implicit uses and defs lists...
Chris Lattner366080c2005-10-28 22:59:53 +0000341 std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
342 if (UseList.empty())
Jim Laskeycd4317e2006-07-21 21:15:20 +0000343 OS << "NULL, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000344 else
Chris Lattner366080c2005-10-28 22:59:53 +0000345 OS << "ImplicitList" << EmittedLists[UseList] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000346
Chris Lattner366080c2005-10-28 22:59:53 +0000347 std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
348 if (DefList.empty())
Jim Laskeycd4317e2006-07-21 21:15:20 +0000349 OS << "NULL, ";
Misha Brukman3da94ae2005-04-22 00:00:37 +0000350 else
Chris Lattner366080c2005-10-28 22:59:53 +0000351 OS << "ImplicitList" << EmittedLists[DefList] << ", ";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000352
Chris Lattner0e384b62005-08-19 16:57:28 +0000353 // Emit the operand info.
Chris Lattnera0cca4a2006-11-06 23:49:51 +0000354 std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
Chris Lattnerd5aa3e22005-08-19 18:46:26 +0000355 if (OperandInfo.empty())
356 OS << "0";
Chris Lattner0e384b62005-08-19 16:57:28 +0000357 else
Chris Lattneref8339b2008-01-06 01:20:13 +0000358 OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
Chris Lattner0e384b62005-08-19 16:57:28 +0000359
Chris Lattnerec352402004-08-01 05:04:00 +0000360 OS << " }, // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000361}
362
Jim Laskeyb5a0c0e2005-10-31 17:16:46 +0000363
Chris Lattnera3ae6142003-08-03 21:57:51 +0000364void InstrInfoEmitter::emitShiftedValue(Record *R, StringInit *Val,
365 IntInit *ShiftInt, std::ostream &OS) {
366 if (Val == 0 || ShiftInt == 0)
367 throw std::string("Illegal value or shift amount in TargetInfo*!");
368 RecordVal *RV = R->getValue(Val->getValue());
369 int Shift = ShiftInt->getValue();
370
Chris Lattnerf52e2612006-01-27 01:44:09 +0000371 if (RV == 0 || RV->getValue() == 0) {
372 // This isn't an error if this is a builtin instruction.
Jim Laskeya683f9b2007-01-26 17:29:20 +0000373 if (R->getName() != "PHI" &&
374 R->getName() != "INLINEASM" &&
Christopher Lamb08d52072007-07-26 07:48:21 +0000375 R->getName() != "LABEL" &&
376 R->getName() != "EXTRACT_SUBREG" &&
377 R->getName() != "INSERT_SUBREG")
Chris Lattnerf52e2612006-01-27 01:44:09 +0000378 throw R->getName() + " doesn't have a field named '" +
379 Val->getValue() + "'!";
380 return;
381 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000382
383 Init *Value = RV->getValue();
384 if (BitInit *BI = dynamic_cast<BitInit*>(Value)) {
385 if (BI->getValue()) OS << "|(1<<" << Shift << ")";
386 return;
387 } else if (BitsInit *BI = dynamic_cast<BitsInit*>(Value)) {
388 // Convert the Bits to an integer to print...
389 Init *I = BI->convertInitializerTo(new IntRecTy());
390 if (I)
391 if (IntInit *II = dynamic_cast<IntInit*>(I)) {
Chris Lattnerf52e2612006-01-27 01:44:09 +0000392 if (II->getValue()) {
393 if (Shift)
394 OS << "|(" << II->getValue() << "<<" << Shift << ")";
395 else
396 OS << "|" << II->getValue();
397 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000398 return;
399 }
400
401 } else if (IntInit *II = dynamic_cast<IntInit*>(Value)) {
Chris Lattnerf52e2612006-01-27 01:44:09 +0000402 if (II->getValue()) {
403 if (Shift)
404 OS << "|(" << II->getValue() << "<<" << Shift << ")";
405 else
406 OS << II->getValue();
407 }
Chris Lattnera3ae6142003-08-03 21:57:51 +0000408 return;
409 }
410
Chris Lattner2c36aff2007-12-30 00:25:23 +0000411 std::cerr << "Unhandled initializer: " << *Val << "\n";
Chris Lattnera3ae6142003-08-03 21:57:51 +0000412 throw "In record '" + R->getName() + "' for TSFlag emission.";
413}
Brian Gaeked0fde302003-11-11 22:41:34 +0000414