+void IntrinsicEmitter::
+EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
+ raw_ostream &OS) {
+ OS << "// Intrinsic ID to name table\n";
+ OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n";
+ OS << " // Note that entry #0 is the invalid intrinsic!\n";
+ for (unsigned i = 0, e = Ints.size(); i != e; ++i)
+ OS << " \"" << Ints[i].Name << "\",\n";
+ OS << "#endif\n\n";
+}
+
+void IntrinsicEmitter::
+EmitIntrinsicToOverloadTable(const std::vector<CodeGenIntrinsic> &Ints,
+ raw_ostream &OS) {
+ OS << "// Intrinsic ID to overload table\n";
+ OS << "#ifdef GET_INTRINSIC_OVERLOAD_TABLE\n";
+ OS << " // Note that entry #0 is the invalid intrinsic!\n";
+ for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
+ OS << " ";
+ if (Ints[i].isOverloaded)
+ OS << "true";
+ else
+ OS << "false";
+ OS << ",\n";
+ }
+ OS << "#endif\n\n";
+}
+
+static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
+ if (EVT(VT).isInteger()) {
+ unsigned BitWidth = EVT(VT).getSizeInBits();
+ OS << "IntegerType::get(Context, " << BitWidth << ")";
+ } else if (VT == MVT::Other) {
+ // MVT::OtherVT is used to mean the empty struct type here.
+ OS << "StructType::get(Context)";
+ } else if (VT == MVT::f32) {
+ OS << "Type::getFloatTy(Context)";
+ } else if (VT == MVT::f64) {
+ OS << "Type::getDoubleTy(Context)";
+ } else if (VT == MVT::f80) {
+ OS << "Type::getX86_FP80Ty(Context)";
+ } else if (VT == MVT::f128) {
+ OS << "Type::getFP128Ty(Context)";
+ } else if (VT == MVT::ppcf128) {
+ OS << "Type::getPPC_FP128Ty(Context)";
+ } else if (VT == MVT::isVoid) {
+ OS << "Type::getVoidTy(Context)";
+ } else if (VT == MVT::Metadata) {
+ OS << "Type::getMetadataTy(Context)";
+ } else if (VT == MVT::x86mmx) {
+ OS << "Type::getX86_MMXTy(Context)";
+ } else {
+ assert(false && "Unsupported ValueType!");
+ }
+}
+
+static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
+ unsigned &ArgNo);
+
+static void EmitTypeGenerate(raw_ostream &OS,
+ const std::vector<Record*> &ArgTypes,
+ unsigned &ArgNo) {
+ if (ArgTypes.empty())
+ return EmitTypeForValueType(OS, MVT::isVoid);
+
+ if (ArgTypes.size() == 1)
+ return EmitTypeGenerate(OS, ArgTypes.front(), ArgNo);
+
+ OS << "StructType::get(Context, ";
+
+ for (std::vector<Record*>::const_iterator
+ I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
+ EmitTypeGenerate(OS, *I, ArgNo);
+ OS << ", ";
+ }
+
+ OS << " NULL)";
+}
+
+static void EmitTypeGenerate(raw_ostream &OS, const Record *ArgType,
+ unsigned &ArgNo) {
+ MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
+
+ if (ArgType->isSubClassOf("LLVMMatchType")) {
+ unsigned Number = ArgType->getValueAsInt("Number");
+ assert(Number < ArgNo && "Invalid matching number!");
+ if (ArgType->isSubClassOf("LLVMExtendedElementVectorType"))
+ OS << "VectorType::getExtendedElementVectorType"
+ << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
+ else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType"))
+ OS << "VectorType::getTruncatedElementVectorType"
+ << "(dyn_cast<VectorType>(Tys[" << Number << "]))";
+ else
+ OS << "Tys[" << Number << "]";
+ } else if (VT == MVT::iAny || VT == MVT::fAny || VT == MVT::vAny) {
+ // NOTE: The ArgNo variable here is not the absolute argument number, it is
+ // the index of the "arbitrary" type in the Tys array passed to the
+ // Intrinsic::getDeclaration function. Consequently, we only want to
+ // increment it when we actually hit an overloaded type. Getting this wrong
+ // leads to very subtle bugs!
+ OS << "Tys[" << ArgNo++ << "]";
+ } else if (EVT(VT).isVector()) {
+ EVT VVT = VT;
+ OS << "VectorType::get(";
+ EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT().SimpleTy);
+ OS << ", " << VVT.getVectorNumElements() << ")";
+ } else if (VT == MVT::iPTR) {
+ OS << "PointerType::getUnqual(";
+ EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
+ OS << ")";
+ } else if (VT == MVT::iPTRAny) {
+ // Make sure the user has passed us an argument type to overload. If not,
+ // treat it as an ordinary (not overloaded) intrinsic.
+ OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
+ << "] : PointerType::getUnqual(";
+ EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
+ OS << ")";
+ ++ArgNo;
+ } else if (VT == MVT::isVoid) {
+ if (ArgNo == 0)
+ OS << "Type::getVoidTy(Context)";
+ else
+ // MVT::isVoid is used to mean varargs here.
+ OS << "...";
+ } else {
+ EmitTypeForValueType(OS, VT);
+ }
+}
+
+/// RecordListComparator - Provide a deterministic comparator for lists of
+/// records.
+namespace {
+ typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
+ struct RecordListComparator {
+ bool operator()(const RecPair &LHS,
+ const RecPair &RHS) const {
+ unsigned i = 0;
+ const std::vector<Record*> *LHSVec = &LHS.first;
+ const std::vector<Record*> *RHSVec = &RHS.first;
+ unsigned RHSSize = RHSVec->size();
+ unsigned LHSSize = LHSVec->size();
+
+ for (; i != LHSSize; ++i) {
+ if (i == RHSSize) return false; // RHS is shorter than LHS.
+ if ((*LHSVec)[i] != (*RHSVec)[i])
+ return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
+ }
+
+ if (i != RHSSize) return true;
+
+ i = 0;
+ LHSVec = &LHS.second;
+ RHSVec = &RHS.second;
+ RHSSize = RHSVec->size();
+ LHSSize = LHSVec->size();
+
+ for (i = 0; i != LHSSize; ++i) {
+ if (i == RHSSize) return false; // RHS is shorter than LHS.
+ if ((*LHSVec)[i] != (*RHSVec)[i])
+ return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName();
+ }
+
+ return i != RHSSize;
+ }
+ };
+}
+