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 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 117 | static void EmitTypeForValueType(std::ostream &OS, MVT::ValueType VT) { |
| 118 | if (MVT::isInteger(VT)) { |
| 119 | unsigned BitWidth = MVT::getSizeInBits(VT); |
| 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 | |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 141 | static void EmitTypeGenerate(std::ostream &OS, Record *ArgType, |
| 142 | unsigned &ArgNo) { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 143 | MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT")); |
| 144 | |
| 145 | if (ArgType->isSubClassOf("LLVMMatchType")) { |
| 146 | unsigned Number = ArgType->getValueAsInt("Number"); |
| 147 | assert(Number < ArgNo && "Invalid matching number!"); |
| 148 | OS << "Tys[" << Number << "]"; |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 149 | } else if (VT == MVT::iAny || VT == MVT::fAny) { |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 150 | // NOTE: The ArgNo variable here is not the absolute argument number, it is |
| 151 | // the index of the "arbitrary" type in the Tys array passed to the |
| 152 | // Intrinsic::getDeclaration function. Consequently, we only want to |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 153 | // increment it when we actually hit an overloaded type. Getting this wrong |
| 154 | // leads to very subtle bugs! |
| 155 | OS << "Tys[" << ArgNo++ << "]"; |
| 156 | } else if (MVT::isVector(VT)) { |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 157 | OS << "VectorType::get("; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 158 | EmitTypeForValueType(OS, MVT::getVectorElementType(VT)); |
| 159 | OS << ", " << MVT::getVectorNumElements(VT) << ")"; |
| 160 | } else if (VT == MVT::iPTR) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 161 | OS << "PointerType::getUnqual("; |
Reid Spencer | c4de3de | 2007-04-01 07:20:02 +0000 | [diff] [blame] | 162 | EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 163 | OS << ")"; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 164 | } else if (VT == MVT::isVoid) { |
| 165 | if (ArgNo == 0) |
| 166 | OS << "Type::VoidTy"; |
| 167 | else |
| 168 | // MVT::isVoid is used to mean varargs here. |
| 169 | OS << "..."; |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 170 | } else { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 171 | EmitTypeForValueType(OS, VT); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 175 | /// RecordListComparator - Provide a determinstic comparator for lists of |
| 176 | /// records. |
| 177 | namespace { |
| 178 | struct RecordListComparator { |
| 179 | bool operator()(const std::vector<Record*> &LHS, |
| 180 | const std::vector<Record*> &RHS) const { |
| 181 | unsigned i = 0; |
| 182 | do { |
| 183 | if (i == RHS.size()) return false; // RHS is shorter than LHS. |
| 184 | if (LHS[i] != RHS[i]) |
| 185 | return LHS[i]->getName() < RHS[i]->getName(); |
| 186 | } while (++i != LHS.size()); |
| 187 | |
| 188 | return i != RHS.size(); |
| 189 | } |
| 190 | }; |
| 191 | } |
| 192 | |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 193 | void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints, |
| 194 | std::ostream &OS) { |
| 195 | OS << "// Verifier::visitIntrinsicFunctionCall code.\n"; |
| 196 | OS << "#ifdef GET_INTRINSIC_VERIFIER\n"; |
| 197 | OS << " switch (ID) {\n"; |
| 198 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 199 | |
| 200 | // This checking can emit a lot of very common code. To reduce the amount of |
| 201 | // code that we emit, batch up cases that have identical types. This avoids |
| 202 | // problems where GCC can run out of memory compiling Verifier.cpp. |
| 203 | typedef std::map<std::vector<Record*>, std::vector<unsigned>, |
| 204 | RecordListComparator> MapTy; |
| 205 | MapTy UniqueArgInfos; |
| 206 | |
| 207 | // Compute the unique argument type info. |
| 208 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 209 | UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i); |
| 210 | |
| 211 | // Loop through the array, emitting one comparison for each batch. |
| 212 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 213 | E = UniqueArgInfos.end(); I != E; ++I) { |
| 214 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 215 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 216 | << Ints[I->second[i]].Name << "\n"; |
| 217 | } |
Chris Lattner | f124b46 | 2006-03-31 04:48:26 +0000 | [diff] [blame] | 218 | |
Chris Lattner | c4d9b24 | 2006-03-31 04:24:58 +0000 | [diff] [blame] | 219 | const std::vector<Record*> &ArgTypes = I->first; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 220 | OS << " VerifyIntrinsicPrototype(ID, IF, " << ArgTypes.size() << ", "; |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 221 | for (unsigned j = 0; j != ArgTypes.size(); ++j) { |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 222 | Record *ArgType = ArgTypes[j]; |
| 223 | if (ArgType->isSubClassOf("LLVMMatchType")) { |
| 224 | unsigned Number = ArgType->getValueAsInt("Number"); |
| 225 | assert(Number < j && "Invalid matching number!"); |
| 226 | OS << "~" << Number; |
| 227 | } else { |
| 228 | MVT::ValueType VT = getValueType(ArgType->getValueAsDef("VT")); |
| 229 | OS << getEnumName(VT); |
| 230 | if (VT == MVT::isVoid && j != 0 && j != ArgTypes.size()-1) |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 231 | throw "Var arg type not last argument"; |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 232 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 233 | if (j != ArgTypes.size()-1) |
| 234 | OS << ", "; |
Jim Laskey | 95d97b9 | 2007-02-06 18:30:58 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 237 | OS << ");\n"; |
Chris Lattner | f97a00e | 2006-03-09 22:05:04 +0000 | [diff] [blame] | 238 | OS << " break;\n"; |
| 239 | } |
| 240 | OS << " }\n"; |
| 241 | OS << "#endif\n\n"; |
Chris Lattner | 9b843b2 | 2006-03-09 20:34:19 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 244 | void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints, |
| 245 | std::ostream &OS) { |
| 246 | OS << "// Code for generating Intrinsic function declarations.\n"; |
| 247 | OS << "#ifdef GET_INTRINSIC_GENERATOR\n"; |
| 248 | OS << " switch (id) {\n"; |
| 249 | OS << " default: assert(0 && \"Invalid intrinsic!\");\n"; |
| 250 | |
| 251 | // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical |
| 252 | // types. |
| 253 | typedef std::map<std::vector<Record*>, std::vector<unsigned>, |
| 254 | RecordListComparator> MapTy; |
| 255 | MapTy UniqueArgInfos; |
| 256 | |
| 257 | // Compute the unique argument type info. |
| 258 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) |
| 259 | UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i); |
| 260 | |
| 261 | // Loop through the array, emitting one generator for each batch. |
| 262 | for (MapTy::iterator I = UniqueArgInfos.begin(), |
| 263 | E = UniqueArgInfos.end(); I != E; ++I) { |
| 264 | for (unsigned i = 0, e = I->second.size(); i != e; ++i) { |
| 265 | OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// " |
| 266 | << Ints[I->second[i]].Name << "\n"; |
| 267 | } |
| 268 | |
| 269 | const std::vector<Record*> &ArgTypes = I->first; |
| 270 | unsigned N = ArgTypes.size(); |
| 271 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 272 | if (N > 1 && |
| 273 | getValueType(ArgTypes[N-1]->getValueAsDef("VT")) == MVT::isVoid) { |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 274 | OS << " IsVarArg = true;\n"; |
| 275 | --N; |
| 276 | } |
| 277 | |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 278 | unsigned ArgNo = 0; |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 279 | OS << " ResultTy = "; |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 280 | EmitTypeGenerate(OS, ArgTypes[0], ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 281 | OS << ";\n"; |
| 282 | |
| 283 | for (unsigned j = 1; j != N; ++j) { |
| 284 | OS << " ArgTys.push_back("; |
Reid Spencer | 84c614d | 2007-05-22 19:30:31 +0000 | [diff] [blame] | 285 | EmitTypeGenerate(OS, ArgTypes[j], ArgNo); |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 286 | OS << ");\n"; |
| 287 | } |
Jim Laskey | 95af592 | 2007-02-07 20:38:26 +0000 | [diff] [blame] | 288 | OS << " break;\n"; |
| 289 | } |
| 290 | OS << " }\n"; |
| 291 | OS << "#endif\n\n"; |
| 292 | } |
| 293 | |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 294 | void IntrinsicEmitter:: |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 295 | EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) { |
| 296 | OS << "// Add parameter attributes that are not common to all intrinsics.\n"; |
| 297 | OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n"; |
| 298 | OS << " switch (id) {\n"; |
Chris Lattner | 7056de3 | 2006-03-24 01:13:55 +0000 | [diff] [blame] | 299 | OS << " default: break;\n"; |
| 300 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 301 | switch (Ints[i].ModRef) { |
| 302 | default: break; |
| 303 | case CodeGenIntrinsic::NoMem: |
| 304 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 305 | break; |
| 306 | } |
| 307 | } |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 308 | OS << " Attr |= ParamAttr::ReadNone; // These do not access memory.\n"; |
| 309 | OS << " break;\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 310 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 311 | switch (Ints[i].ModRef) { |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 312 | default: break; |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 313 | case CodeGenIntrinsic::ReadArgMem: |
| 314 | case CodeGenIntrinsic::ReadMem: |
| 315 | OS << " case Intrinsic::" << Ints[i].EnumName << ":\n"; |
| 316 | break; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
Duncan Sands | a3355ff | 2007-12-03 20:06:50 +0000 | [diff] [blame] | 319 | OS << " Attr |= ParamAttr::ReadOnly; // These do not write memory.\n"; |
| 320 | OS << " break;\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 321 | OS << " }\n"; |
| 322 | OS << "#endif\n\n"; |
Chris Lattner | 4e5f359 | 2006-03-09 22:37:52 +0000 | [diff] [blame] | 323 | } |
Chris Lattner | 022f64f | 2006-03-13 23:08:44 +0000 | [diff] [blame] | 324 | |
| 325 | void IntrinsicEmitter:: |
| 326 | EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){ |
| 327 | OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n"; |
| 328 | OS << "#ifdef GET_GCC_BUILTIN_NAME\n"; |
| 329 | OS << " switch (F->getIntrinsicID()) {\n"; |
| 330 | OS << " default: BuiltinName = \"\"; break;\n"; |
| 331 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 332 | if (!Ints[i].GCCBuiltinName.empty()) { |
| 333 | OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \"" |
| 334 | << Ints[i].GCCBuiltinName << "\"; break;\n"; |
| 335 | } |
| 336 | } |
| 337 | OS << " }\n"; |
| 338 | OS << "#endif\n\n"; |
Reid Spencer | 767a25b | 2006-03-14 05:59:52 +0000 | [diff] [blame] | 339 | } |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 340 | |
Chris Lattner | 331bf92 | 2008-01-04 04:38:35 +0000 | [diff] [blame^] | 341 | /// EmitBuiltinComparisons - Emit comparisons to determine whether the specified |
| 342 | /// sorted range of builtin names is equal to the current builtin. This breaks |
| 343 | /// it down into a simple tree. |
| 344 | /// |
| 345 | /// At this point, we know that all the builtins in the range have the same name |
| 346 | /// for the first 'CharStart' characters. Only the end of the name needs to be |
| 347 | /// discriminated. |
| 348 | typedef std::map<std::string, std::string>::const_iterator StrMapIterator; |
| 349 | static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End, |
| 350 | unsigned CharStart, unsigned Indent, |
| 351 | std::ostream &OS) { |
| 352 | if (Start == End) return; // empty range. |
| 353 | |
| 354 | // Determine what, if anything, is the same about all these strings. |
| 355 | std::string CommonString = Start->first; |
| 356 | unsigned NumInRange = 0; |
| 357 | for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) { |
| 358 | // Find the first character that doesn't match. |
| 359 | const std::string &ThisStr = I->first; |
| 360 | unsigned NonMatchChar = CharStart; |
| 361 | while (NonMatchChar < CommonString.size() && |
| 362 | NonMatchChar < ThisStr.size() && |
| 363 | CommonString[NonMatchChar] == ThisStr[NonMatchChar]) |
| 364 | ++NonMatchChar; |
| 365 | // Truncate off pieces that don't match. |
| 366 | CommonString.resize(NonMatchChar); |
| 367 | } |
| 368 | |
| 369 | // Just compare the rest of the string. |
| 370 | if (NumInRange == 1) { |
| 371 | if (CharStart != CommonString.size()) { |
| 372 | OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName"; |
| 373 | if (CharStart) OS << "+" << CharStart; |
| 374 | OS << ", \"" << (CommonString.c_str()+CharStart) << "\", "; |
| 375 | OS << CommonString.size() - CharStart << "))\n"; |
| 376 | ++Indent; |
| 377 | } |
| 378 | OS << std::string(Indent*2, ' ') << "IntrinsicID = Intrinsic::"; |
| 379 | OS << Start->second << ";\n"; |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | // At this point, we potentially have a common prefix for these builtins, emit |
| 384 | // a check for this common prefix. |
| 385 | if (CommonString.size() != CharStart) { |
| 386 | OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName"; |
| 387 | if (CharStart) OS << "+" << CharStart; |
| 388 | OS << ", \"" << (CommonString.c_str()+CharStart) << "\", "; |
| 389 | OS << CommonString.size()-CharStart << ")) {\n"; |
| 390 | |
| 391 | EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1, OS); |
| 392 | OS << std::string(Indent*2, ' ') << "}\n"; |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | // Output a switch on the character that differs across the set. |
| 397 | OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart |
| 398 | << "]) {"; |
| 399 | if (CharStart) |
| 400 | OS << " // \"" << std::string(Start->first.begin(), |
| 401 | Start->first.begin()+CharStart) << "\""; |
| 402 | OS << "\n"; |
| 403 | |
| 404 | for (StrMapIterator I = Start; I != End; ) { |
| 405 | char ThisChar = I->first[CharStart]; |
| 406 | OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n"; |
| 407 | // Figure out the range that has this common character. |
| 408 | StrMapIterator NextChar = I; |
| 409 | for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar; |
| 410 | ++NextChar) |
| 411 | /*empty*/; |
| 412 | EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, OS); |
| 413 | OS << std::string(Indent*2, ' ') << " break;\n"; |
| 414 | I = NextChar; |
| 415 | } |
| 416 | OS << std::string(Indent*2, ' ') << "}\n"; |
| 417 | } |
| 418 | |
| 419 | /// EmitTargetBuiltins - All of the builtins in the specified map are for the |
| 420 | /// same target, and we already checked it. |
| 421 | static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM, |
| 422 | std::ostream &OS) { |
| 423 | // Rearrange the builtins by length. |
| 424 | std::vector<std::map<std::string, std::string> > BuiltinsByLen; |
| 425 | BuiltinsByLen.reserve(100); |
| 426 | |
| 427 | for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) { |
| 428 | if (I->first.size() >= BuiltinsByLen.size()) |
| 429 | BuiltinsByLen.resize(I->first.size()+1); |
| 430 | BuiltinsByLen[I->first.size()].insert(*I); |
| 431 | } |
| 432 | |
| 433 | // Now that we have all the builtins by their length, emit a switch stmt. |
| 434 | OS << " switch (strlen(BuiltinName)) {\n"; |
| 435 | OS << " default: break;\n"; |
| 436 | for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) { |
| 437 | if (BuiltinsByLen[i].empty()) continue; |
| 438 | OS << " case " << i << ":\n"; |
| 439 | EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(), |
| 440 | 0, 3, OS); |
| 441 | OS << " break;\n"; |
| 442 | } |
| 443 | OS << " }\n"; |
| 444 | } |
| 445 | |
| 446 | |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 447 | void IntrinsicEmitter:: |
| 448 | EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints, |
| 449 | std::ostream &OS) { |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 450 | typedef std::map<std::string, std::map<std::string, std::string> > BIMTy; |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 451 | BIMTy BuiltinMap; |
| 452 | for (unsigned i = 0, e = Ints.size(); i != e; ++i) { |
| 453 | if (!Ints[i].GCCBuiltinName.empty()) { |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 454 | // Get the map for this target prefix. |
| 455 | std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix]; |
| 456 | |
| 457 | if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName, |
| 458 | Ints[i].EnumName)).second) |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 459 | throw "Intrinsic '" + Ints[i].TheDef->getName() + |
| 460 | "': duplicate GCC builtin name!"; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n"; |
| 465 | OS << "// This is used by the C front-end. The GCC builtin name is passed\n"; |
| 466 | OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n"; |
| 467 | OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n"; |
| 468 | OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n"; |
Chris Lattner | 81442c0 | 2008-01-04 03:32:52 +0000 | [diff] [blame] | 469 | OS << " IntrinsicID = Intrinsic::not_intrinsic;\n"; |
Chris Lattner | 331bf92 | 2008-01-04 04:38:35 +0000 | [diff] [blame^] | 470 | |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 471 | // Note: this could emit significantly better code if we cared. |
| 472 | for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){ |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 473 | OS << " "; |
| 474 | if (!I->first.empty()) |
| 475 | OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) "; |
| 476 | else |
| 477 | OS << "/* Target Independent Builtins */ "; |
| 478 | OS << "{\n"; |
| 479 | |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 480 | // Emit the comparisons for this target prefix. |
Chris Lattner | 331bf92 | 2008-01-04 04:38:35 +0000 | [diff] [blame^] | 481 | EmitTargetBuiltins(I->second, OS); |
Chris Lattner | fa0fba1 | 2008-01-02 21:24:22 +0000 | [diff] [blame] | 482 | OS << " }\n"; |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 483 | } |
Chris Lattner | 3f8b891 | 2006-03-15 01:33:26 +0000 | [diff] [blame] | 484 | OS << "#endif\n\n"; |
| 485 | } |