Revert r194865 and r194874.
[oota-llvm.git] / utils / TableGen / InstrInfoEmitter.cpp
1 //===- InstrInfoEmitter.cpp - Generate a Instruction Set Desc. ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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
16 #include "CodeGenDAGPatterns.h"
17 #include "CodeGenSchedule.h"
18 #include "CodeGenTarget.h"
19 #include "SequenceToOffsetTable.h"
20 #include "TableGenBackends.h"
21 #include "llvm/ADT/StringExtras.h"
22 #include "llvm/TableGen/Error.h"
23 #include "llvm/TableGen/Record.h"
24 #include "llvm/TableGen/TableGenBackend.h"
25 #include <algorithm>
26 #include <cstdio>
27 #include <map>
28 #include <vector>
29 using namespace llvm;
30
31 namespace {
32 class InstrInfoEmitter {
33   RecordKeeper &Records;
34   CodeGenDAGPatterns CDP;
35   const CodeGenSchedModels &SchedModels;
36
37 public:
38   InstrInfoEmitter(RecordKeeper &R):
39     Records(R), CDP(R), SchedModels(CDP.getTargetInfo().getSchedModels()) {}
40
41   // run - Output the instruction set description.
42   void run(raw_ostream &OS);
43
44 private:
45   void emitEnums(raw_ostream &OS);
46
47   typedef std::map<std::vector<std::string>, unsigned> OperandInfoMapTy;
48
49   /// The keys of this map are maps which have OpName enum values as their keys
50   /// and instruction operand indices as their values.  The values of this map
51   /// are lists of instruction names.
52   typedef std::map<std::map<unsigned, unsigned>,
53                    std::vector<std::string> > OpNameMapTy;
54   typedef std::map<std::string, unsigned>::iterator StrUintMapIter;
55   void emitRecord(const CodeGenInstruction &Inst, unsigned Num,
56                   Record *InstrInfo,
57                   std::map<std::vector<Record*>, unsigned> &EL,
58                   const OperandInfoMapTy &OpInfo,
59                   raw_ostream &OS);
60   void emitOperandTypesEnum(raw_ostream &OS, const CodeGenTarget &Target);
61   void initOperandMapData(
62              const std::vector<const CodeGenInstruction *> NumberedInstructions,
63              const std::string &Namespace,
64              std::map<std::string, unsigned> &Operands,
65              OpNameMapTy &OperandMap);
66   void emitOperandNameMappings(raw_ostream &OS, const CodeGenTarget &Target,
67             const std::vector<const CodeGenInstruction*> &NumberedInstructions);
68
69   // Operand information.
70   void EmitOperandInfo(raw_ostream &OS, OperandInfoMapTy &OperandInfoIDs);
71   std::vector<std::string> GetOperandInfo(const CodeGenInstruction &Inst);
72 };
73 } // End anonymous namespace
74
75 static void PrintDefList(const std::vector<Record*> &Uses,
76                          unsigned Num, raw_ostream &OS) {
77   OS << "static const uint16_t ImplicitList" << Num << "[] = { ";
78   for (unsigned i = 0, e = Uses.size(); i != e; ++i)
79     OS << getQualifiedName(Uses[i]) << ", ";
80   OS << "0 };\n";
81 }
82
83 //===----------------------------------------------------------------------===//
84 // Operand Info Emission.
85 //===----------------------------------------------------------------------===//
86
87 std::vector<std::string>
88 InstrInfoEmitter::GetOperandInfo(const CodeGenInstruction &Inst) {
89   std::vector<std::string> Result;
90
91   for (unsigned i = 0, e = Inst.Operands.size(); i != e; ++i) {
92     // Handle aggregate operands and normal operands the same way by expanding
93     // either case into a list of operands for this op.
94     std::vector<CGIOperandList::OperandInfo> OperandList;
95
96     // This might be a multiple operand thing.  Targets like X86 have
97     // registers in their multi-operand operands.  It may also be an anonymous
98     // operand, which has a single operand, but no declared class for the
99     // operand.
100     DagInit *MIOI = Inst.Operands[i].MIOperandInfo;
101
102     if (!MIOI || MIOI->getNumArgs() == 0) {
103       // Single, anonymous, operand.
104       OperandList.push_back(Inst.Operands[i]);
105     } else {
106       for (unsigned j = 0, e = Inst.Operands[i].MINumOperands; j != e; ++j) {
107         OperandList.push_back(Inst.Operands[i]);
108
109         Record *OpR = cast<DefInit>(MIOI->getArg(j))->getDef();
110         OperandList.back().Rec = OpR;
111       }
112     }
113
114     for (unsigned j = 0, e = OperandList.size(); j != e; ++j) {
115       Record *OpR = OperandList[j].Rec;
116       std::string Res;
117
118       if (OpR->isSubClassOf("RegisterOperand"))
119         OpR = OpR->getValueAsDef("RegClass");
120       if (OpR->isSubClassOf("RegisterClass"))
121         Res += getQualifiedName(OpR) + "RegClassID, ";
122       else if (OpR->isSubClassOf("PointerLikeRegClass"))
123         Res += utostr(OpR->getValueAsInt("RegClassKind")) + ", ";
124       else
125         // -1 means the operand does not have a fixed register class.
126         Res += "-1, ";
127
128       // Fill in applicable flags.
129       Res += "0";
130
131       // Ptr value whose register class is resolved via callback.
132       if (OpR->isSubClassOf("PointerLikeRegClass"))
133         Res += "|(1<<MCOI::LookupPtrRegClass)";
134
135       // Predicate operands.  Check to see if the original unexpanded operand
136       // was of type PredicateOp.
137       if (Inst.Operands[i].Rec->isSubClassOf("PredicateOp"))
138         Res += "|(1<<MCOI::Predicate)";
139
140       // Optional def operands.  Check to see if the original unexpanded operand
141       // was of type OptionalDefOperand.
142       if (Inst.Operands[i].Rec->isSubClassOf("OptionalDefOperand"))
143         Res += "|(1<<MCOI::OptionalDef)";
144
145       // Fill in operand type.
146       Res += ", MCOI::";
147       assert(!Inst.Operands[i].OperandType.empty() && "Invalid operand type.");
148       Res += Inst.Operands[i].OperandType;
149
150       // Fill in constraint info.
151       Res += ", ";
152
153       const CGIOperandList::ConstraintInfo &Constraint =
154         Inst.Operands[i].Constraints[j];
155       if (Constraint.isNone())
156         Res += "0";
157       else if (Constraint.isEarlyClobber())
158         Res += "(1 << MCOI::EARLY_CLOBBER)";
159       else {
160         assert(Constraint.isTied());
161         Res += "((" + utostr(Constraint.getTiedOperand()) +
162                     " << 16) | (1 << MCOI::TIED_TO))";
163       }
164
165       Result.push_back(Res);
166     }
167   }
168
169   return Result;
170 }
171
172 void InstrInfoEmitter::EmitOperandInfo(raw_ostream &OS,
173                                        OperandInfoMapTy &OperandInfoIDs) {
174   // ID #0 is for no operand info.
175   unsigned OperandListNum = 0;
176   OperandInfoIDs[std::vector<std::string>()] = ++OperandListNum;
177
178   OS << "\n";
179   const CodeGenTarget &Target = CDP.getTargetInfo();
180   for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
181        E = Target.inst_end(); II != E; ++II) {
182     std::vector<std::string> OperandInfo = GetOperandInfo(**II);
183     unsigned &N = OperandInfoIDs[OperandInfo];
184     if (N != 0) continue;
185
186     N = ++OperandListNum;
187     OS << "static const MCOperandInfo OperandInfo" << N << "[] = { ";
188     for (unsigned i = 0, e = OperandInfo.size(); i != e; ++i)
189       OS << "{ " << OperandInfo[i] << " }, ";
190     OS << "};\n";
191   }
192 }
193
194
195 /// Initialize data structures for generating operand name mappings.
196 /// 
197 /// \param Operands [out] A map used to generate the OpName enum with operand
198 ///        names as its keys and operand enum values as its values.
199 /// \param OperandMap [out] A map for representing the operand name mappings for
200 ///        each instructions.  This is used to generate the OperandMap table as
201 ///        well as the getNamedOperandIdx() function.
202 void InstrInfoEmitter::initOperandMapData(
203         const std::vector<const CodeGenInstruction *> NumberedInstructions,
204         const std::string &Namespace,
205         std::map<std::string, unsigned> &Operands,
206         OpNameMapTy &OperandMap) {
207
208   unsigned NumOperands = 0;
209   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
210     const CodeGenInstruction *Inst = NumberedInstructions[i];
211     if (!Inst->TheDef->getValueAsBit("UseNamedOperandTable")) {
212       continue;
213     }
214     std::map<unsigned, unsigned> OpList;
215     for (unsigned j = 0, je = Inst->Operands.size(); j != je; ++j) {
216       const CGIOperandList::OperandInfo &Info = Inst->Operands[j];
217       StrUintMapIter I = Operands.find(Info.Name);
218
219       if (I == Operands.end()) {
220         I = Operands.insert(Operands.begin(),
221                     std::pair<std::string, unsigned>(Info.Name, NumOperands++));
222       }
223       OpList[I->second] = Info.MIOperandNo;
224     }
225     OperandMap[OpList].push_back(Namespace + "::" + Inst->TheDef->getName());
226   }
227 }
228
229 /// Generate a table and function for looking up the indices of operands by
230 /// name.
231 ///
232 /// This code generates:
233 /// - An enum in the llvm::TargetNamespace::OpName namespace, with one entry
234 ///   for each operand name.
235 /// - A 2-dimensional table called OperandMap for mapping OpName enum values to
236 ///   operand indices.
237 /// - A function called getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx)
238 ///   for looking up the operand index for an instruction, given a value from
239 ///   OpName enum
240 void InstrInfoEmitter::emitOperandNameMappings(raw_ostream &OS,
241            const CodeGenTarget &Target,
242            const std::vector<const CodeGenInstruction*> &NumberedInstructions) {
243
244   const std::string &Namespace = Target.getInstNamespace();
245   std::string OpNameNS = "OpName";
246   // Map of operand names to their enumeration value.  This will be used to
247   // generate the OpName enum.
248   std::map<std::string, unsigned> Operands;
249   OpNameMapTy OperandMap;
250
251   initOperandMapData(NumberedInstructions, Namespace, Operands, OperandMap);
252
253   OS << "#ifdef GET_INSTRINFO_OPERAND_ENUM\n";
254   OS << "#undef GET_INSTRINFO_OPERAND_ENUM\n";
255   OS << "namespace llvm {";
256   OS << "namespace " << Namespace << " {\n";
257   OS << "namespace " << OpNameNS << " { \n";
258   OS << "enum {\n";
259   for (StrUintMapIter i = Operands.begin(), e = Operands.end(); i != e; ++i)
260     OS << "  " << i->first << " = " << i->second << ",\n";
261
262   OS << "OPERAND_LAST";
263   OS << "\n};\n";
264   OS << "} // End namespace OpName\n";
265   OS << "} // End namespace " << Namespace << "\n";
266   OS << "} // End namespace llvm\n";
267   OS << "#endif //GET_INSTRINFO_OPERAND_ENUM\n";
268
269   OS << "#ifdef GET_INSTRINFO_NAMED_OPS\n";
270   OS << "#undef GET_INSTRINFO_NAMED_OPS\n";
271   OS << "namespace llvm {";
272   OS << "namespace " << Namespace << " {\n";
273   OS << "int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIdx) {\n";
274   if (!Operands.empty()) {
275     OS << "  static const int16_t OperandMap [][" << Operands.size()
276        << "] = {\n";
277     for (OpNameMapTy::iterator i = OperandMap.begin(), e = OperandMap.end();
278                                                        i != e; ++i) {
279       const std::map<unsigned, unsigned> &OpList = i->first;
280       OS << "{";
281
282       // Emit a row of the OperandMap table
283       for (unsigned ii = 0, ie = Operands.size(); ii != ie; ++ii)
284         OS << (OpList.count(ii) == 0 ? -1 : (int)OpList.find(ii)->second)
285            << ", ";
286
287       OS << "},\n";
288     }
289     OS << "};\n";
290
291     OS << "  switch(Opcode) {\n";
292     unsigned TableIndex = 0;
293     for (OpNameMapTy::iterator i = OperandMap.begin(), e = OperandMap.end();
294                                                        i != e; ++i) {
295       std::vector<std::string> &OpcodeList = i->second;
296
297       for (unsigned ii = 0, ie = OpcodeList.size(); ii != ie; ++ii)
298         OS << "  case " << OpcodeList[ii] << ":\n";
299
300       OS << "    return OperandMap[" << TableIndex++ << "][NamedIdx];\n";
301     }
302     OS << "    default: return -1;\n";
303     OS << "  }\n";
304   } else {
305     // There are no operands, so no need to emit anything
306     OS << "  return -1;\n";
307   }
308   OS << "}\n";
309   OS << "} // End namespace " << Namespace << "\n";
310   OS << "} // End namespace llvm\n";
311   OS << "#endif //GET_INSTRINFO_NAMED_OPS\n";
312
313 }
314
315 /// Generate an enum for all the operand types for this target, under the
316 /// llvm::TargetNamespace::OpTypes namespace.
317 /// Operand types are all definitions derived of the Operand Target.td class.
318 void InstrInfoEmitter::emitOperandTypesEnum(raw_ostream &OS,
319                                             const CodeGenTarget &Target) {
320
321   const std::string &Namespace = Target.getInstNamespace();
322   std::vector<Record *> Operands = Records.getAllDerivedDefinitions("Operand");
323
324   OS << "\n#ifdef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
325   OS << "#undef GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
326   OS << "namespace llvm {";
327   OS << "namespace " << Namespace << " {\n";
328   OS << "namespace OpTypes { \n";
329   OS << "enum OperandType {\n";
330
331   for (unsigned oi = 0, oe = Operands.size(); oi != oe; ++oi) {
332     if (!Operands[oi]->isAnonymous())
333       OS << "  " << Operands[oi]->getName() << " = " << oi << ",\n";
334   }
335
336   OS << "  OPERAND_TYPE_LIST_END" << "\n};\n";
337   OS << "} // End namespace OpTypes\n";
338   OS << "} // End namespace " << Namespace << "\n";
339   OS << "} // End namespace llvm\n";
340   OS << "#endif // GET_INSTRINFO_OPERAND_TYPES_ENUM\n";
341 }
342
343 //===----------------------------------------------------------------------===//
344 // Main Output.
345 //===----------------------------------------------------------------------===//
346
347 // run - Emit the main instruction description records for the target...
348 void InstrInfoEmitter::run(raw_ostream &OS) {
349   emitSourceFileHeader("Target Instruction Enum Values", OS);
350   emitEnums(OS);
351
352   emitSourceFileHeader("Target Instruction Descriptors", OS);
353
354   OS << "\n#ifdef GET_INSTRINFO_MC_DESC\n";
355   OS << "#undef GET_INSTRINFO_MC_DESC\n";
356
357   OS << "namespace llvm {\n\n";
358
359   CodeGenTarget &Target = CDP.getTargetInfo();
360   const std::string &TargetName = Target.getName();
361   Record *InstrInfo = Target.getInstructionSet();
362
363   // Keep track of all of the def lists we have emitted already.
364   std::map<std::vector<Record*>, unsigned> EmittedLists;
365   unsigned ListNumber = 0;
366
367   // Emit all of the instruction's implicit uses and defs.
368   for (CodeGenTarget::inst_iterator II = Target.inst_begin(),
369          E = Target.inst_end(); II != E; ++II) {
370     Record *Inst = (*II)->TheDef;
371     std::vector<Record*> Uses = Inst->getValueAsListOfDefs("Uses");
372     if (!Uses.empty()) {
373       unsigned &IL = EmittedLists[Uses];
374       if (!IL) PrintDefList(Uses, IL = ++ListNumber, OS);
375     }
376     std::vector<Record*> Defs = Inst->getValueAsListOfDefs("Defs");
377     if (!Defs.empty()) {
378       unsigned &IL = EmittedLists[Defs];
379       if (!IL) PrintDefList(Defs, IL = ++ListNumber, OS);
380     }
381   }
382
383   OperandInfoMapTy OperandInfoIDs;
384
385   // Emit all of the operand info records.
386   EmitOperandInfo(OS, OperandInfoIDs);
387
388   // Emit all of the MCInstrDesc records in their ENUM ordering.
389   //
390   OS << "\nextern const MCInstrDesc " << TargetName << "Insts[] = {\n";
391   const std::vector<const CodeGenInstruction*> &NumberedInstructions =
392     Target.getInstructionsByEnumValue();
393
394   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i)
395     emitRecord(*NumberedInstructions[i], i, InstrInfo, EmittedLists,
396                OperandInfoIDs, OS);
397   OS << "};\n\n";
398
399   // Build an array of instruction names
400   SequenceToOffsetTable<std::string> InstrNames;
401   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
402     const CodeGenInstruction *Instr = NumberedInstructions[i];
403     InstrNames.add(Instr->TheDef->getName());
404   }
405
406   InstrNames.layout();
407   OS << "extern const char " << TargetName << "InstrNameData[] = {\n";
408   InstrNames.emit(OS, printChar);
409   OS << "};\n\n";
410
411   OS << "extern const unsigned " << TargetName <<"InstrNameIndices[] = {";
412   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
413     if (i % 8 == 0)
414       OS << "\n    ";
415     const CodeGenInstruction *Instr = NumberedInstructions[i];
416     OS << InstrNames.get(Instr->TheDef->getName()) << "U, ";
417   }
418
419   OS << "\n};\n\n";
420
421   // MCInstrInfo initialization routine.
422   OS << "static inline void Init" << TargetName
423      << "MCInstrInfo(MCInstrInfo *II) {\n";
424   OS << "  II->InitMCInstrInfo(" << TargetName << "Insts, "
425      << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
426      << NumberedInstructions.size() << ");\n}\n\n";
427
428   OS << "} // End llvm namespace \n";
429
430   OS << "#endif // GET_INSTRINFO_MC_DESC\n\n";
431
432   // Create a TargetInstrInfo subclass to hide the MC layer initialization.
433   OS << "\n#ifdef GET_INSTRINFO_HEADER\n";
434   OS << "#undef GET_INSTRINFO_HEADER\n";
435
436   std::string ClassName = TargetName + "GenInstrInfo";
437   OS << "namespace llvm {\n";
438   OS << "struct " << ClassName << " : public TargetInstrInfo {\n"
439      << "  explicit " << ClassName << "(int SO = -1, int DO = -1);\n"
440      << "};\n";
441   OS << "} // End llvm namespace \n";
442
443   OS << "#endif // GET_INSTRINFO_HEADER\n\n";
444
445   OS << "\n#ifdef GET_INSTRINFO_CTOR\n";
446   OS << "#undef GET_INSTRINFO_CTOR\n";
447
448   OS << "namespace llvm {\n";
449   OS << "extern const MCInstrDesc " << TargetName << "Insts[];\n";
450   OS << "extern const unsigned " << TargetName << "InstrNameIndices[];\n";
451   OS << "extern const char " << TargetName << "InstrNameData[];\n";
452   OS << ClassName << "::" << ClassName << "(int SO, int DO)\n"
453      << "  : TargetInstrInfo(SO, DO) {\n"
454      << "  InitMCInstrInfo(" << TargetName << "Insts, "
455      << TargetName << "InstrNameIndices, " << TargetName << "InstrNameData, "
456      << NumberedInstructions.size() << ");\n}\n";
457   OS << "} // End llvm namespace \n";
458
459   OS << "#endif // GET_INSTRINFO_CTOR\n\n";
460
461   emitOperandNameMappings(OS, Target, NumberedInstructions);
462
463   emitOperandTypesEnum(OS, Target);
464 }
465
466 void InstrInfoEmitter::emitRecord(const CodeGenInstruction &Inst, unsigned Num,
467                                   Record *InstrInfo,
468                          std::map<std::vector<Record*>, unsigned> &EmittedLists,
469                                   const OperandInfoMapTy &OpInfo,
470                                   raw_ostream &OS) {
471   int MinOperands = 0;
472   if (!Inst.Operands.empty())
473     // Each logical operand can be multiple MI operands.
474     MinOperands = Inst.Operands.back().MIOperandNo +
475                   Inst.Operands.back().MINumOperands;
476
477   OS << "  { ";
478   OS << Num << ",\t" << MinOperands << ",\t"
479      << Inst.Operands.NumDefs << ",\t"
480      << SchedModels.getSchedClassIdx(Inst) << ",\t"
481      << Inst.TheDef->getValueAsInt("Size") << ",\t0";
482
483   // Emit all of the target indepedent flags...
484   if (Inst.isPseudo)           OS << "|(1<<MCID::Pseudo)";
485   if (Inst.isReturn)           OS << "|(1<<MCID::Return)";
486   if (Inst.isBranch)           OS << "|(1<<MCID::Branch)";
487   if (Inst.isIndirectBranch)   OS << "|(1<<MCID::IndirectBranch)";
488   if (Inst.isCompare)          OS << "|(1<<MCID::Compare)";
489   if (Inst.isMoveImm)          OS << "|(1<<MCID::MoveImm)";
490   if (Inst.isBitcast)          OS << "|(1<<MCID::Bitcast)";
491   if (Inst.isSelect)           OS << "|(1<<MCID::Select)";
492   if (Inst.isBarrier)          OS << "|(1<<MCID::Barrier)";
493   if (Inst.hasDelaySlot)       OS << "|(1<<MCID::DelaySlot)";
494   if (Inst.isCall)             OS << "|(1<<MCID::Call)";
495   if (Inst.canFoldAsLoad)      OS << "|(1<<MCID::FoldableAsLoad)";
496   if (Inst.mayLoad)            OS << "|(1<<MCID::MayLoad)";
497   if (Inst.mayStore)           OS << "|(1<<MCID::MayStore)";
498   if (Inst.isPredicable)       OS << "|(1<<MCID::Predicable)";
499   if (Inst.isConvertibleToThreeAddress) OS << "|(1<<MCID::ConvertibleTo3Addr)";
500   if (Inst.isCommutable)       OS << "|(1<<MCID::Commutable)";
501   if (Inst.isTerminator)       OS << "|(1<<MCID::Terminator)";
502   if (Inst.isReMaterializable) OS << "|(1<<MCID::Rematerializable)";
503   if (Inst.isNotDuplicable)    OS << "|(1<<MCID::NotDuplicable)";
504   if (Inst.Operands.hasOptionalDef) OS << "|(1<<MCID::HasOptionalDef)";
505   if (Inst.usesCustomInserter) OS << "|(1<<MCID::UsesCustomInserter)";
506   if (Inst.hasPostISelHook)    OS << "|(1<<MCID::HasPostISelHook)";
507   if (Inst.Operands.isVariadic)OS << "|(1<<MCID::Variadic)";
508   if (Inst.hasSideEffects)     OS << "|(1<<MCID::UnmodeledSideEffects)";
509   if (Inst.isAsCheapAsAMove)   OS << "|(1<<MCID::CheapAsAMove)";
510   if (Inst.hasExtraSrcRegAllocReq) OS << "|(1<<MCID::ExtraSrcRegAllocReq)";
511   if (Inst.hasExtraDefRegAllocReq) OS << "|(1<<MCID::ExtraDefRegAllocReq)";
512
513   // Emit all of the target-specific flags...
514   BitsInit *TSF = Inst.TheDef->getValueAsBitsInit("TSFlags");
515   if (!TSF)
516     PrintFatalError("no TSFlags?");
517   uint64_t Value = 0;
518   for (unsigned i = 0, e = TSF->getNumBits(); i != e; ++i) {
519     if (BitInit *Bit = dyn_cast<BitInit>(TSF->getBit(i)))
520       Value |= uint64_t(Bit->getValue()) << i;
521     else
522       PrintFatalError("Invalid TSFlags bit in " + Inst.TheDef->getName());
523   }
524   OS << ", 0x";
525   OS.write_hex(Value);
526   OS << "ULL, ";
527
528   // Emit the implicit uses and defs lists...
529   std::vector<Record*> UseList = Inst.TheDef->getValueAsListOfDefs("Uses");
530   if (UseList.empty())
531     OS << "NULL, ";
532   else
533     OS << "ImplicitList" << EmittedLists[UseList] << ", ";
534
535   std::vector<Record*> DefList = Inst.TheDef->getValueAsListOfDefs("Defs");
536   if (DefList.empty())
537     OS << "NULL, ";
538   else
539     OS << "ImplicitList" << EmittedLists[DefList] << ", ";
540
541   // Emit the operand info.
542   std::vector<std::string> OperandInfo = GetOperandInfo(Inst);
543   if (OperandInfo.empty())
544     OS << "0";
545   else
546     OS << "OperandInfo" << OpInfo.find(OperandInfo)->second;
547
548   CodeGenTarget &Target = CDP.getTargetInfo();
549   if (Inst.HasComplexDeprecationPredicate)
550     // Emit a function pointer to the complex predicate method.
551     OS << ",0"
552        << ",&get" << Inst.DeprecatedReason << "DeprecationInfo";
553   else if (!Inst.DeprecatedReason.empty())
554     // Emit the Subtarget feature.
555     OS << "," << Target.getInstNamespace() << "::" << Inst.DeprecatedReason
556        << ",0";
557   else
558     // Instruction isn't deprecated.
559     OS << ",0,0";
560
561   OS << " },  // Inst #" << Num << " = " << Inst.TheDef->getName() << "\n";
562 }
563
564 // emitEnums - Print out enum values for all of the instructions.
565 void InstrInfoEmitter::emitEnums(raw_ostream &OS) {
566
567   OS << "\n#ifdef GET_INSTRINFO_ENUM\n";
568   OS << "#undef GET_INSTRINFO_ENUM\n";
569
570   OS << "namespace llvm {\n\n";
571
572   CodeGenTarget Target(Records);
573
574   // We must emit the PHI opcode first...
575   std::string Namespace = Target.getInstNamespace();
576
577   if (Namespace.empty()) {
578     fprintf(stderr, "No instructions defined!\n");
579     exit(1);
580   }
581
582   const std::vector<const CodeGenInstruction*> &NumberedInstructions =
583     Target.getInstructionsByEnumValue();
584
585   OS << "namespace " << Namespace << " {\n";
586   OS << "  enum {\n";
587   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
588     OS << "    " << NumberedInstructions[i]->TheDef->getName()
589        << "\t= " << i << ",\n";
590   }
591   OS << "    INSTRUCTION_LIST_END = " << NumberedInstructions.size() << "\n";
592   OS << "  };\n";
593   OS << "namespace Sched {\n";
594   OS << "  enum {\n";
595   for (unsigned i = 0, e = SchedModels.numInstrSchedClasses(); i != e; ++i) {
596     OS << "    " << SchedModels.getSchedClass(i).Name
597        << "\t= " << i << ",\n";
598   }
599   OS << "    SCHED_LIST_END = " << SchedModels.numInstrSchedClasses() << "\n";
600   OS << "  };\n}\n}\n";
601   OS << "} // End llvm namespace \n";
602
603   OS << "#endif // GET_INSTRINFO_ENUM\n\n";
604 }
605
606 namespace llvm {
607
608 void EmitInstrInfo(RecordKeeper &RK, raw_ostream &OS) {
609   InstrInfoEmitter(RK).run(OS);
610   EmitMapTable(RK, OS);
611 }
612
613 } // End llvm namespace