Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 1 | //===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 3060910 | 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. |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tablegen backend emits information about intrinsic functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 14 | #include "CodeGenTarget.h" |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 15 | #include "IntrinsicEmitter.h" |
| 16 | #include "Record.h" |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringExtras.h" |
Jeff Cohen | 71c3bc3 | 2006-03-15 02:51:05 +0000 | [diff] [blame] | 18 | #include <algorithm> |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 22 | // IntrinsicEmitter Implementation |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | void IntrinsicEmitter::run(std::ostream &OS) { |
| 26 | EmitSourceFileHeader("Intrinsic Function Source Fragment", OS); |
| 27 | |
| 28 | std::vector<CodeGenIntrinsic> Ints = LoadIntrinsics(Records); |
| 29 | |
| 30 | // Emit the enum information. |
| 31 | EmitEnumInfo(Ints, OS); |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 32 | |
| 33 | // Emit the intrinsic ID -> name table. |
| 34 | EmitIntrinsicToNameTable(Ints, OS); |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 35 | |
| 36 | // Emit the function name recognizer. |
| 37 | EmitFnNameRecognizer(Ints, OS); |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 38 | |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 39 | // Emit the intrinsic verifier. |
| 40 | EmitVerifier(Ints, OS); |
Chris Lattner | 6448ee4 | 2006-03-09 22:30:49 +0000 | [diff] [blame] | 41 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 42 | // Emit the intrinsic declaration generator. |
| 43 | EmitGenerator(Ints, OS); |
| 44 | |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 45 | // Emit the intrinsic parameter attributes. |
| 46 | EmitAttributes(Ints, OS); |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 47 | |
| 48 | // Emit a list of intrinsics with corresponding GCC builtins. |
| 49 | EmitGCCBuiltinList(Ints, OS); |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 50 | |
| 51 | // Emit code to translate GCC builtins into LLVM intrinsics. |
| 52 | EmitIntrinsicToGCCBuiltinMap(Ints, OS); |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints, |
| 56 | std::ostream &OS) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 57 | OS << "// Enum values for Intrinsics.h\n"; |
Chris Lattner | 9e493cf | 2006-03-03 02:32:46 +0000 | [diff] [blame] | 58 | OS << "#ifdef GET_INTRINSIC_ENUM_VALUES\n"; |
| 59 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 60 | OS << " " << Ints[i].EnumName; |
| 61 | OS << ((i != e-1) ? ", " : " "); |
| 62 | OS << std::string(40-Ints[i].EnumName.size(), ' ') |
| 63 | << "// " << Ints[i].Name << "\n"; |
| 64 | } |
| 65 | OS << "#endif\n\n"; |
| 66 | } |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 67 | |
| 68 | void IntrinsicEmitter:: |
| 69 | EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints, |
| 70 | std::ostream &OS) { |
| 71 | // Build a function name -> intrinsic name mapping. |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 72 | std::map<std::string, unsigned> IntMapping; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 73 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 74 | IntMapping[Ints[i].Name] = i; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 75 | |
| 76 | OS << "// Function name -> enum value recognizer code.\n"; |
| 77 | OS << "#ifdef GET_FUNCTION_RECOGNIZER\n"; |
| 78 | OS << " switch (Name[5]) {\n"; |
Chris Lattner | 3b51580 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 79 | OS << " default:\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 80 | // Emit the intrinsics in sorted order. |
| 81 | char LastChar = 0; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 82 | for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(), |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 83 | E = IntMapping.end(); I != E; ++I) { |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 84 | if (I->first[5] != LastChar) { |
| 85 | LastChar = I->first[5]; |
Chris Lattner | 3b51580 | 2007-02-15 19:17:16 +0000 | [diff] [blame] | 86 | OS << " break;\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 87 | OS << " case '" << LastChar << "':\n"; |
| 88 | } |
| 89 | |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 90 | // For overloaded intrinsics, only the prefix needs to match |
| 91 | if (Ints[I->second].isOverloaded) |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 92 | OS << " if (Len > " << I->first.size() |
| 93 | << " && !memcmp(Name, \"" << I->first << ".\", " |
| 94 | << (I->first.size() + 1) << ")) return Intrinsic::" |
| 95 | << Ints[I->second].EnumName << ";\n"; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 96 | else |
| 97 | OS << " if (Len == " << I->first.size() |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 98 | << " && !memcmp(Name, \"" << I->first << "\", " |
| 99 | << I->first.size() << ")) return Intrinsic::" |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 100 | << Ints[I->second].EnumName << ";\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 101 | } |
| 102 | OS << " }\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 103 | OS << "#endif\n\n"; |
| 104 | } |
| 105 | |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 106 | void IntrinsicEmitter:: |
| 107 | EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints, |
| 108 | std::ostream &OS) { |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 109 | OS << "// Intrinsic ID to name table\n"; |
| 110 | OS << "#ifdef GET_INTRINSIC_NAME_TABLE\n"; |
| 111 | OS << " // Note that entry #0 is the invalid intrinsic!\n"; |
Evan Cheng | f065a6f | 2006-03-28 22:25:56 +0000 | [diff] [blame] | 112 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 113 | OS << " \"" << Ints[i].Name << "\",\n"; |
Chris Lattner | fda6aff | 2006-03-15 01:55:21 +0000 | [diff] [blame] | 114 | OS << "#endif\n\n"; |
| 115 | } |
| 116 | |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 117 | static void EmitTypeForValueType(std::ostream &OS, MVT::SimpleValueType VT) { |
| 118 | if (MVT(VT).isInteger()) { |
| 119 | unsigned BitWidth = MVT(VT).getSizeInBits(); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 120 | OS << "IntegerType::get(" << BitWidth << ")"; |
| 121 | } else if (VT == MVT::Other) { |
| 122 | // MVT::OtherVT is used to mean the empty struct type here. |
| 123 | OS << "StructType::get(std::vector<const Type *>())"; |
| 124 | } else if (VT == MVT::f32) { |
| 125 | OS << "Type::FloatTy"; |
| 126 | } else if (VT == MVT::f64) { |
| 127 | OS << "Type::DoubleTy"; |
Dale Johannesen | 317096a | 2007-09-28 01:08:20 +0000 | [diff] [blame] | 128 | } else if (VT == MVT::f80) { |
| 129 | OS << "Type::X86_FP80Ty"; |
| 130 | } else if (VT == MVT::f128) { |
| 131 | OS << "Type::FP128Ty"; |
| 132 | } else if (VT == MVT::ppcf128) { |
| 133 | OS << "Type::PPC_FP128Ty"; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 134 | } else if (VT == MVT::isVoid) { |
| 135 | OS << "Type::VoidTy"; |
| 136 | } else { |
| 137 | assert(false && "Unsupported ValueType!"); |
Chris Lattner | 18faf5d | 2006-03-13 22:38:57 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 141 | static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType, |
| 142 | unsigned &ArgNo); |
| 143 | |
| 144 | static void EmitTypeGenerate(std::ostream &OS, |
| 145 | const std::vector<Record*> &ArgTypes, |
| 146 | unsigned &ArgNo) { |
| 147 | if (ArgTypes.size() == 1) { |
| 148 | EmitTypeGenerate(OS, ArgTypes.front(), ArgNo); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | OS << "StructType::get("; |
| 153 | |
| 154 | for (std::vector<Record*>::const_iterator |
Bill Wendling | 20072af | 2008-11-13 10:18:35 +0000 | [diff] [blame] | 155 | I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) { |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 156 | EmitTypeGenerate(OS, *I, ArgNo); |
Bill Wendling | 20072af | 2008-11-13 10:18:35 +0000 | [diff] [blame] | 157 | OS << ", "; |
| 158 | } |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 159 | |
Bill Wendling | 20072af | 2008-11-13 10:18:35 +0000 | [diff] [blame] | 160 | OS << " NULL)"; |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType, |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 164 | unsigned &ArgNo) { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 165 | MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT")); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 166 | |
| 167 | if (ArgType->isSubClassOf("LLVMMatchType")) { |
| 168 | unsigned Number = ArgType->getValueAsInt("Number"); |
| 169 | assert(Number < ArgNo && "Invalid matching number!"); |
Bob Wilson | bc03979 | 2009-01-07 00:09:01 +0000 | [diff] [blame] | 170 | if (ArgType->isSubClassOf("LLVMExtendedElementVectorType")) |
| 171 | OS << "VectorType::getExtendedElementVectorType" |
| 172 | << "(dyn_cast<VectorType>(Tys[" << Number << "]))"; |
| 173 | else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType")) |
| 174 | OS << "VectorType::getTruncatedElementVectorType" |
| 175 | << "(dyn_cast<VectorType>(Tys[" << Number << "]))"; |
| 176 | else |
| 177 | OS << "Tys[" << Number << "]"; |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 178 | } else if (VT == MVT::iAny || VT == MVT::fAny) { |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 179 | // NOTE: The ArgNo variable here is not the absolute argument number, it is |
| 180 | // the index of the "arbitrary" type in the Tys array passed to the |
| 181 | // Intrinsic::getDeclaration function. Consequently, we only want to |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 182 | // increment it when we actually hit an overloaded type. Getting this wrong |
| 183 | // leads to very subtle bugs! |
| 184 | OS << "Tys[" << ArgNo++ << "]"; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 185 | } else if (MVT(VT).isVector()) { |
| 186 | MVT VVT = VT; |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 187 | OS << "VectorType::get("; |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 188 | EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT()); |
| 189 | OS << ", " << VVT.getVectorNumElements() << ")"; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 190 | } else if (VT == MVT::iPTR) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 191 | OS << "PointerType::getUnqual("; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 192 | EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 193 | OS << ")"; |
Mon P Wang | e3b3a72 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 194 | } else if (VT == MVT::iPTRAny) { |
| 195 | // Make sure the user has passed us an argument type to overload. If not, |
| 196 | // treat it as an ordinary (not overloaded) intrinsic. |
| 197 | OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo |
| 198 | << "] : PointerType::getUnqual("; |
| 199 | EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); |
| 200 | OS << ")"; |
| 201 | ++ArgNo; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 202 | } else if (VT == MVT::isVoid) { |
| 203 | if (ArgNo == 0) |
| 204 | OS << "Type::VoidTy"; |
| 205 | else |
| 206 | // MVT::isVoid is used to mean varargs here. |
| 207 | OS << "..."; |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 208 | } else { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 209 | EmitTypeForValueType(OS, VT); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 213 | /// RecordListComparator - Provide a determinstic comparator for lists of |
| 214 | /// records. |
| 215 | namespace { |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 216 | typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 217 | struct RecordListComparator { |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 218 | bool operator()(const RecPair &LHS, |
| 219 | const RecPair &RHS) const { |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 220 | unsigned i = 0; |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 221 | const std::vector<Record*> *LHSVec = &LHS.first; |
| 222 | const std::vector<Record*> *RHSVec = &RHS.first; |
| 223 | unsigned RHSSize = RHSVec->size(); |
| 224 | unsigned LHSSize = LHSVec->size(); |
| 225 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 226 | do { |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 227 | if (i == RHSSize) return false; // RHS is shorter than LHS. |
| 228 | if ((*LHSVec)[i] != (*RHSVec)[i]) |
| 229 | return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName(); |
| 230 | } while (++i != LHSSize); |
| 231 | |
Bill Wendling | 023422a | 2008-11-13 12:03:00 +0000 | [diff] [blame] | 232 | if (i != RHSSize) return true; |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 233 | |
| 234 | i = 0; |
| 235 | LHSVec = &LHS.second; |
| 236 | RHSVec = &RHS.second; |
| 237 | RHSSize = RHSVec->size(); |
| 238 | LHSSize = LHSVec->size(); |
| 239 | |
| 240 | for (i = 0; i != LHSSize; ++i) { |
| 241 | if (i == RHSSize) return false; // RHS is shorter than LHS. |
| 242 | if ((*LHSVec)[i] != (*RHSVec)[i]) |
| 243 | return (*LHSVec)[i]->getName() < (*RHSVec)[i]->getName(); |
| 244 | } |
| 245 | |
| 246 | return i != RHSSize; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 247 | } |
| 248 | }; |
| 249 | } |
| 250 | |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 251 | void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, |
| 252 | std::ostream &OS) { |
| 253 | OS << "// Verifier::visitIntrinsicFunctionCall code.\n"; |
| 254 | OS << "#ifdef GET_INTRINSIC_VERIFIER\n"; |
| 255 | OS << " switch (ID) {\n"; |
| 256 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 257 | |
| 258 | // This checking can emit a lot of very common code. To reduce the amount of |
| 259 | // code that we emit, batch up cases that have identical types. This avoids |
| 260 | // problems where GCC can run out of memory compiling Verifier.cpp. |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 261 | typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 262 | MapTy UniqueArgInfos; |
| 263 | |
| 264 | // Compute the unique argument type info. |
| 265 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 266 | UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs, |
| 267 | Ints[i].IS.ParamTypeDefs)].push_back(i); |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 268 | |
| 269 | // Loop through the array, emitting one comparison for each batch. |
| 270 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 271 | E = UniqueArgInfos.end(); I != E; ++I) { |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 273 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 274 | << Ints[I->second[i]].Name << "\n"; |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 275 | |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 276 | const RecPair &ArgTypes = I->first; |
| 277 | const std::vector<Record*> &RetTys = ArgTypes.first; |
| 278 | const std::vector<Record*> &ParamTys = ArgTypes.second; |
| 279 | |
| 280 | OS << " VerifyIntrinsicPrototype(ID, IF, " << RetTys.size() << ", " |
| 281 | << ParamTys.size(); |
| 282 | |
| 283 | // Emit return types. |
| 284 | for (unsigned j = 0, je = RetTys.size(); j != je; ++j) { |
| 285 | Record *ArgType = RetTys[j]; |
| 286 | OS << ", "; |
| 287 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 288 | if (ArgType->isSubClassOf("LLVMMatchType")) { |
| 289 | unsigned Number = ArgType->getValueAsInt("Number"); |
| 290 | assert(Number < j && "Invalid matching number!"); |
Bob Wilson | bc03979 | 2009-01-07 00:09:01 +0000 | [diff] [blame] | 291 | if (ArgType->isSubClassOf("LLVMExtendedElementVectorType")) |
| 292 | OS << "~(ExtendedElementVectorType | " << Number << ")"; |
| 293 | else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType")) |
| 294 | OS << "~(TruncatedElementVectorType | " << Number << ")"; |
| 295 | else |
| 296 | OS << "~" << Number; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 297 | } else { |
Duncan Sands | 83ec4b6 | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 298 | MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT")); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 299 | OS << getEnumName(VT); |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 300 | |
| 301 | if (VT == MVT::isVoid && j != 0 && j != je - 1) |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 302 | throw "Var arg type not last argument"; |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 303 | } |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | // Emit the parameter types. |
| 307 | for (unsigned j = 0, je = ParamTys.size(); j != je; ++j) { |
| 308 | Record *ArgType = ParamTys[j]; |
| 309 | OS << ", "; |
| 310 | |
| 311 | if (ArgType->isSubClassOf("LLVMMatchType")) { |
| 312 | unsigned Number = ArgType->getValueAsInt("Number"); |
| 313 | assert(Number < j + RetTys.size() && "Invalid matching number!"); |
Bob Wilson | bc03979 | 2009-01-07 00:09:01 +0000 | [diff] [blame] | 314 | if (ArgType->isSubClassOf("LLVMExtendedElementVectorType")) |
| 315 | OS << "~(ExtendedElementVectorType | " << Number << ")"; |
| 316 | else if (ArgType->isSubClassOf("LLVMTruncatedElementVectorType")) |
| 317 | OS << "~(TruncatedElementVectorType | " << Number << ")"; |
| 318 | else |
| 319 | OS << "~" << Number; |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 320 | } else { |
| 321 | MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT")); |
| 322 | OS << getEnumName(VT); |
| 323 | |
| 324 | if (VT == MVT::isVoid && j != 0 && j != je - 1) |
| 325 | throw "Var arg type not last argument"; |
| 326 | } |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 329 | OS << ");\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 330 | OS << " break;\n"; |
| 331 | } |
| 332 | OS << " }\n"; |
| 333 | OS << "#endif\n\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 336 | void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, |
| 337 | std::ostream &OS) { |
| 338 | OS << "// Code for generating Intrinsic function declarations.\n"; |
| 339 | OS << "#ifdef GET_INTRINSIC_GENERATOR\n"; |
| 340 | OS << " switch (id) {\n"; |
| 341 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
| 342 | |
| 343 | // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical |
| 344 | // types. |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 345 | typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy; |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 346 | MapTy UniqueArgInfos; |
| 347 | |
| 348 | // Compute the unique argument type info. |
| 349 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 350 | UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs, |
| 351 | Ints[i].IS.ParamTypeDefs)].push_back(i); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 352 | |
| 353 | // Loop through the array, emitting one generator for each batch. |
| 354 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 355 | E = UniqueArgInfos.end(); I != E; ++I) { |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 356 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 357 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 358 | << Ints[I->second[i]].Name << "\n"; |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 359 | |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 360 | const RecPair &ArgTypes = I->first; |
| 361 | const std::vector<Record*> &RetTys = ArgTypes.first; |
| 362 | const std::vector<Record*> &ParamTys = ArgTypes.second; |
| 363 | |
| 364 | unsigned N = ParamTys.size(); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 365 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 366 | if (N > 1 && |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 367 | getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) { |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 368 | OS << " IsVarArg = true;\n"; |
| 369 | --N; |
| 370 | } |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 371 | |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 372 | unsigned ArgNo = 0; |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 373 | OS << " ResultTy = "; |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 374 | EmitTypeGenerate(OS, RetTys, ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 375 | OS << ";\n"; |
| 376 | |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 377 | for (unsigned j = 0; j != N; ++j) { |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 378 | OS << " ArgTys.push_back("; |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 379 | EmitTypeGenerate(OS, ParamTys[j], ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 380 | OS << ");\n"; |
| 381 | } |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 382 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 383 | OS << " break;\n"; |
| 384 | } |
Bill Wendling | cdcc3e6 | 2008-11-13 09:08:33 +0000 | [diff] [blame] | 385 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 386 | OS << " }\n"; |
| 387 | OS << "#endif\n\n"; |
| 388 | } |
| 389 | |
Chris Lattner | 048ffb2 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 390 | /// EmitAttributes - This emits the Intrinsic::getAttributes method. |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 391 | void IntrinsicEmitter:: |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 392 | EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) { |
| 393 | OS << "// Add parameter attributes that are not common to all intrinsics.\n"; |
| 394 | OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; |
Chris Lattner | 048ffb2 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 395 | OS << "AttrListPtr Intrinsic::getAttributes(ID id) {"; |
| 396 | OS << " // No intrinsic can throw exceptions.\n"; |
| 397 | OS << " Attributes Attr = Attribute::NoUnwind;\n"; |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 398 | OS << " switch (id) {\n"; |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 399 | OS << " default: break;\n"; |
Chris Lattner | 10dae94 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 400 | unsigned MaxArgAttrs = 0; |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 401 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
Chris Lattner | 10dae94 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 402 | MaxArgAttrs = |
| 403 | std::max(MaxArgAttrs, unsigned(Ints[i].ArgumentAttributes.size())); |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 404 | switch (Ints[i].ModRef) { |
| 405 | default: break; |
| 406 | case CodeGenIntrinsic::NoMem: |
| 407 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 408 | break; |
| 409 | } |
| 410 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 411 | OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n"; |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 412 | OS << " break;\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 413 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 414 | switch (Ints[i].ModRef) { |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 415 | default: break; |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 416 | case CodeGenIntrinsic::ReadArgMem: |
| 417 | case CodeGenIntrinsic::ReadMem: |
| 418 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 419 | break; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Devang Patel | 0598866 | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 422 | OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n"; |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 423 | OS << " break;\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 424 | OS << " }\n"; |
Chris Lattner | 10dae94 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 425 | OS << " AttributeWithIndex AWI[" << MaxArgAttrs+1 << "];\n"; |
Chris Lattner | d4a2700 | 2009-01-12 02:41:37 +0000 | [diff] [blame^] | 426 | OS << " unsigned NumAttrs = 0;\n"; |
Chris Lattner | 10dae94 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 427 | OS << " switch (id) {\n"; |
| 428 | OS << " default: break;\n"; |
| 429 | |
| 430 | // Add argument attributes for any intrinsics that have them. |
| 431 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 432 | if (Ints[i].ArgumentAttributes.empty()) continue; |
| 433 | |
| 434 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 435 | |
| 436 | std::vector<std::pair<unsigned, CodeGenIntrinsic::ArgAttribute> > ArgAttrs = |
| 437 | Ints[i].ArgumentAttributes; |
| 438 | // Sort by argument index. |
| 439 | std::sort(ArgAttrs.begin(), ArgAttrs.end()); |
| 440 | |
| 441 | unsigned NumArgsWithAttrs = 0; |
| 442 | |
Chris Lattner | d4a2700 | 2009-01-12 02:41:37 +0000 | [diff] [blame^] | 443 | while (!ArgAttrs.empty()) { |
| 444 | unsigned ArgNo = ArgAttrs[0].first; |
| 445 | |
| 446 | OS << " AWI[" << NumArgsWithAttrs++ << "] = AttributeWithIndex::get(" |
| 447 | << ArgNo+1 << ", 0"; |
| 448 | |
| 449 | while (!ArgAttrs.empty() && ArgAttrs[0].first == ArgNo) { |
| 450 | switch (ArgAttrs[0].second) { |
| 451 | default: assert(0 && "Unknown arg attribute"); |
| 452 | case CodeGenIntrinsic::NoCapture: |
| 453 | OS << "|Attribute::NoCapture"; |
| 454 | break; |
| 455 | } |
| 456 | ArgAttrs.erase(ArgAttrs.begin()); |
| 457 | } |
| 458 | OS << ");\n"; |
| 459 | } |
Chris Lattner | 10dae94 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 460 | |
Chris Lattner | d4a2700 | 2009-01-12 02:41:37 +0000 | [diff] [blame^] | 461 | OS << " NumAttrs = " << NumArgsWithAttrs << ";\n"; |
Chris Lattner | 10dae94 | 2009-01-12 01:27:55 +0000 | [diff] [blame] | 462 | OS << " break;\n"; |
| 463 | } |
| 464 | |
| 465 | OS << " }\n"; |
Chris Lattner | d4a2700 | 2009-01-12 02:41:37 +0000 | [diff] [blame^] | 466 | OS << " AWI[NumAttrs] = AttributeWithIndex::get(~0, Attr);\n"; |
| 467 | OS << " return AttrListPtr::get(AWI, NumAttrs+1);\n"; |
Chris Lattner | 048ffb2 | 2009-01-12 01:18:58 +0000 | [diff] [blame] | 468 | OS << "}\n"; |
Chris Lattner | d4a2700 | 2009-01-12 02:41:37 +0000 | [diff] [blame^] | 469 | OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 470 | } |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 471 | |
| 472 | void IntrinsicEmitter:: |
| 473 | EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){ |
| 474 | OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n"; |
| 475 | OS << "#ifdef GET_GCC_BUILTIN_NAME\n"; |
| 476 | OS << " switch (F->getIntrinsicID()) {\n"; |
| 477 | OS << " default: BuiltinName = \"\"; break;\n"; |
| 478 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 479 | if (!Ints[i].GCCBuiltinName.empty()) { |
| 480 | OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \"" |
| 481 | << Ints[i].GCCBuiltinName << "\"; break;\n"; |
| 482 | } |
| 483 | } |
| 484 | OS << " }\n"; |
| 485 | OS << "#endif\n\n"; |
Reid Spencer | 767a25b | 2006-03-14 05:59:52 +0000 | [diff] [blame] | 486 | } |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 331bf92 | 2008-01-04 04:38:35 +0000 | [diff] [blame] | 488 | /// EmitBuiltinComparisons - Emit comparisons to determine whether the specified |
| 489 | /// sorted range of builtin names is equal to the current builtin. This breaks |
| 490 | /// it down into a simple tree. |
| 491 | /// |
| 492 | /// At this point, we know that all the builtins in the range have the same name |
| 493 | /// for the first 'CharStart' characters. Only the end of the name needs to be |
| 494 | /// discriminated. |
| 495 | typedef std::map<std::string, std::string>::const_iterator StrMapIterator; |
| 496 | static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End, |
| 497 | unsigned CharStart, unsigned Indent, |
| 498 | std::ostream &OS) { |
| 499 | if (Start == End) return; // empty range. |
| 500 | |
| 501 | // Determine what, if anything, is the same about all these strings. |
| 502 | std::string CommonString = Start->first; |
| 503 | unsigned NumInRange = 0; |
| 504 | for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) { |
| 505 | // Find the first character that doesn't match. |
| 506 | const std::string &ThisStr = I->first; |
| 507 | unsigned NonMatchChar = CharStart; |
| 508 | while (NonMatchChar < CommonString.size() && |
| 509 | NonMatchChar < ThisStr.size() && |
| 510 | CommonString[NonMatchChar] == ThisStr[NonMatchChar]) |
| 511 | ++NonMatchChar; |
| 512 | // Truncate off pieces that don't match. |
| 513 | CommonString.resize(NonMatchChar); |
| 514 | } |
| 515 | |
| 516 | // Just compare the rest of the string. |
| 517 | if (NumInRange == 1) { |
| 518 | if (CharStart != CommonString.size()) { |
| 519 | OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName"; |
| 520 | if (CharStart) OS << "+" << CharStart; |
| 521 | OS << ", \"" << (CommonString.c_str()+CharStart) << "\", "; |
| 522 | OS << CommonString.size() - CharStart << "))\n"; |
| 523 | ++Indent; |
| 524 | } |
| 525 | OS << std::string(Indent*2, ' ') << "IntrinsicID = Intrinsic::"; |
| 526 | OS << Start->second << ";\n"; |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | // At this point, we potentially have a common prefix for these builtins, emit |
| 531 | // a check for this common prefix. |
| 532 | if (CommonString.size() != CharStart) { |
| 533 | OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName"; |
| 534 | if (CharStart) OS << "+" << CharStart; |
| 535 | OS << ", \"" << (CommonString.c_str()+CharStart) << "\", "; |
| 536 | OS << CommonString.size()-CharStart << ")) {\n"; |
| 537 | |
| 538 | EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1, OS); |
| 539 | OS << std::string(Indent*2, ' ') << "}\n"; |
| 540 | return; |
| 541 | } |
| 542 | |
| 543 | // Output a switch on the character that differs across the set. |
| 544 | OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart |
| 545 | << "]) {"; |
| 546 | if (CharStart) |
| 547 | OS << " // \"" << std::string(Start->first.begin(), |
| 548 | Start->first.begin()+CharStart) << "\""; |
| 549 | OS << "\n"; |
| 550 | |
| 551 | for (StrMapIterator I = Start; I != End; ) { |
| 552 | char ThisChar = I->first[CharStart]; |
| 553 | OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n"; |
| 554 | // Figure out the range that has this common character. |
| 555 | StrMapIterator NextChar = I; |
| 556 | for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar; |
| 557 | ++NextChar) |
| 558 | /*empty*/; |
| 559 | EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, OS); |
| 560 | OS << std::string(Indent*2, ' ') << " break;\n"; |
| 561 | I = NextChar; |
| 562 | } |
| 563 | OS << std::string(Indent*2, ' ') << "}\n"; |
| 564 | } |
| 565 | |
| 566 | /// EmitTargetBuiltins - All of the builtins in the specified map are for the |
| 567 | /// same target, and we already checked it. |
| 568 | static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM, |
| 569 | std::ostream &OS) { |
| 570 | // Rearrange the builtins by length. |
| 571 | std::vector<std::map<std::string, std::string> > BuiltinsByLen; |
| 572 | BuiltinsByLen.reserve(100); |
| 573 | |
| 574 | for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) { |
| 575 | if (I->first.size() >= BuiltinsByLen.size()) |
| 576 | BuiltinsByLen.resize(I->first.size()+1); |
| 577 | BuiltinsByLen[I->first.size()].insert(*I); |
| 578 | } |
| 579 | |
| 580 | // Now that we have all the builtins by their length, emit a switch stmt. |
| 581 | OS << " switch (strlen(BuiltinName)) {\n"; |
| 582 | OS << " default: break;\n"; |
| 583 | for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) { |
| 584 | if (BuiltinsByLen[i].empty()) continue; |
| 585 | OS << " case " << i << ":\n"; |
| 586 | EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(), |
| 587 | 0, 3, OS); |
| 588 | OS << " break;\n"; |
| 589 | } |
| 590 | OS << " }\n"; |
| 591 | } |
| 592 | |
| 593 | |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 594 | void IntrinsicEmitter:: |
| 595 | EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, |
| 596 | std::ostream &OS) { |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 597 | typedef std::map<std::string, std::map<std::string, std::string> > BIMTy; |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 598 | BIMTy BuiltinMap; |
| 599 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 600 | if (!Ints[i].GCCBuiltinName.empty()) { |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 601 | // Get the map for this target prefix. |
| 602 | std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix]; |
| 603 | |
| 604 | if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName, |
| 605 | Ints[i].EnumName)).second) |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 606 | throw "Intrinsic '" + Ints[i].TheDef->getName() + |
| 607 | "': duplicate GCC builtin name!"; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n"; |
| 612 | OS << "// This is used by the C front-end. The GCC builtin name is passed\n"; |
| 613 | OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; |
| 614 | OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; |
| 615 | OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n"; |
Chris Lattner | 81442c0 | 2008-01-04 03:32:52 +0000 | [diff] [blame] | 616 | OS << " IntrinsicID = Intrinsic::not_intrinsic;\n"; |
Chris Lattner | 331bf92 | 2008-01-04 04:38:35 +0000 | [diff] [blame] | 617 | |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 618 | // Note: this could emit significantly better code if we cared. |
| 619 | for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 620 | OS << " "; |
| 621 | if (!I->first.empty()) |
| 622 | OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) "; |
| 623 | else |
| 624 | OS << "/* Target Independent Builtins */ "; |
| 625 | OS << "{\n"; |
| 626 | |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 627 | // Emit the comparisons for this target prefix. |
Chris Lattner | 331bf92 | 2008-01-04 04:38:35 +0000 | [diff] [blame] | 628 | EmitTargetBuiltins(I->second, OS); |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 629 | OS << " }\n"; |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 630 | } |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 631 | OS << "#endif\n\n"; |
| 632 | } |