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