blob: 7883e7ca655d9c91c8606a586616f9cf6b0886d8 [file] [log] [blame]
Chris Lattner9e493cf2006-03-03 02:32:46 +00001//===- IntrinsicEmitter.cpp - Generate intrinsic information --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner30609102007-12-29 20:37:13 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner9e493cf2006-03-03 02:32:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend emits information about intrinsic functions.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth69940402007-08-04 01:51:18 +000014#include "CodeGenTarget.h"
Chris Lattner9e493cf2006-03-03 02:32:46 +000015#include "IntrinsicEmitter.h"
16#include "Record.h"
Chris Lattner18faf5d2006-03-13 22:38:57 +000017#include "llvm/ADT/StringExtras.h"
Jeff Cohen71c3bc32006-03-15 02:51:05 +000018#include <algorithm>
Chris Lattner9e493cf2006-03-03 02:32:46 +000019using namespace llvm;
20
21//===----------------------------------------------------------------------===//
Chris Lattner9e493cf2006-03-03 02:32:46 +000022// IntrinsicEmitter Implementation
23//===----------------------------------------------------------------------===//
24
25void 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 Lattnerfda6aff2006-03-15 01:55:21 +000032
33 // Emit the intrinsic ID -> name table.
34 EmitIntrinsicToNameTable(Ints, OS);
Chris Lattner9b843b22006-03-09 20:34:19 +000035
36 // Emit the function name recognizer.
37 EmitFnNameRecognizer(Ints, OS);
Chris Lattnerfda6aff2006-03-15 01:55:21 +000038
Chris Lattnerf97a00e2006-03-09 22:05:04 +000039 // Emit the intrinsic verifier.
40 EmitVerifier(Ints, OS);
Chris Lattner6448ee42006-03-09 22:30:49 +000041
Jim Laskey95af5922007-02-07 20:38:26 +000042 // Emit the intrinsic declaration generator.
43 EmitGenerator(Ints, OS);
44
Duncan Sandsa3355ff2007-12-03 20:06:50 +000045 // Emit the intrinsic parameter attributes.
46 EmitAttributes(Ints, OS);
Chris Lattner022f64f2006-03-13 23:08:44 +000047
48 // Emit a list of intrinsics with corresponding GCC builtins.
49 EmitGCCBuiltinList(Ints, OS);
Chris Lattner3f8b8912006-03-15 01:33:26 +000050
51 // Emit code to translate GCC builtins into LLVM intrinsics.
52 EmitIntrinsicToGCCBuiltinMap(Ints, OS);
Chris Lattner9e493cf2006-03-03 02:32:46 +000053}
54
55void IntrinsicEmitter::EmitEnumInfo(const std::vector<CodeGenIntrinsic> &Ints,
56 std::ostream &OS) {
Chris Lattner9b843b22006-03-09 20:34:19 +000057 OS << "// Enum values for Intrinsics.h\n";
Chris Lattner9e493cf2006-03-03 02:32:46 +000058 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 Lattner9b843b22006-03-09 20:34:19 +000067
68void IntrinsicEmitter::
69EmitFnNameRecognizer(const std::vector<CodeGenIntrinsic> &Ints,
70 std::ostream &OS) {
71 // Build a function name -> intrinsic name mapping.
Reid Spencerc4de3de2007-04-01 07:20:02 +000072 std::map<std::string, unsigned> IntMapping;
Chris Lattner9b843b22006-03-09 20:34:19 +000073 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Reid Spencerc4de3de2007-04-01 07:20:02 +000074 IntMapping[Ints[i].Name] = i;
Chris Lattner9b843b22006-03-09 20:34:19 +000075
76 OS << "// Function name -> enum value recognizer code.\n";
77 OS << "#ifdef GET_FUNCTION_RECOGNIZER\n";
78 OS << " switch (Name[5]) {\n";
Chris Lattner3b515802007-02-15 19:17:16 +000079 OS << " default:\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000080 // Emit the intrinsics in sorted order.
81 char LastChar = 0;
Reid Spencerc4de3de2007-04-01 07:20:02 +000082 for (std::map<std::string, unsigned>::iterator I = IntMapping.begin(),
Chris Lattner9b843b22006-03-09 20:34:19 +000083 E = IntMapping.end(); I != E; ++I) {
Chris Lattner9b843b22006-03-09 20:34:19 +000084 if (I->first[5] != LastChar) {
85 LastChar = I->first[5];
Chris Lattner3b515802007-02-15 19:17:16 +000086 OS << " break;\n";
Chris Lattner9b843b22006-03-09 20:34:19 +000087 OS << " case '" << LastChar << "':\n";
88 }
89
Reid Spencerc4de3de2007-04-01 07:20:02 +000090 // For overloaded intrinsics, only the prefix needs to match
91 if (Ints[I->second].isOverloaded)
Chandler Carruth69940402007-08-04 01:51:18 +000092 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 Spencerc4de3de2007-04-01 07:20:02 +000096 else
97 OS << " if (Len == " << I->first.size()
Chandler Carruth69940402007-08-04 01:51:18 +000098 << " && !memcmp(Name, \"" << I->first << "\", "
99 << I->first.size() << ")) return Intrinsic::"
Reid Spencerc4de3de2007-04-01 07:20:02 +0000100 << Ints[I->second].EnumName << ";\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000101 }
102 OS << " }\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000103 OS << "#endif\n\n";
104}
105
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000106void IntrinsicEmitter::
107EmitIntrinsicToNameTable(const std::vector<CodeGenIntrinsic> &Ints,
108 std::ostream &OS) {
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000109 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 Chengf065a6f2006-03-28 22:25:56 +0000112 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
113 OS << " \"" << Ints[i].Name << "\",\n";
Chris Lattnerfda6aff2006-03-15 01:55:21 +0000114 OS << "#endif\n\n";
115}
116
Duncan Sands83ec4b62008-06-06 12:08:01 +0000117static void EmitTypeForValueType(std::ostream &OS, MVT::SimpleValueType VT) {
118 if (MVT(VT).isInteger()) {
119 unsigned BitWidth = MVT(VT).getSizeInBits();
Chandler Carruth69940402007-08-04 01:51:18 +0000120 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 Johannesen317096a2007-09-28 01:08:20 +0000128 } 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 Carruth69940402007-08-04 01:51:18 +0000134 } else if (VT == MVT::isVoid) {
135 OS << "Type::VoidTy";
136 } else {
137 assert(false && "Unsupported ValueType!");
Chris Lattner18faf5d2006-03-13 22:38:57 +0000138 }
139}
140
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000141static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
142 unsigned &ArgNo);
143
144static 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 Wendling20072af2008-11-13 10:18:35 +0000155 I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000156 EmitTypeGenerate(OS, *I, ArgNo);
Bill Wendling20072af2008-11-13 10:18:35 +0000157 OS << ", ";
158 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000159
Bill Wendling20072af2008-11-13 10:18:35 +0000160 OS << " NULL)";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000161}
162
163static void EmitTypeGenerate(std::ostream &OS, const Record *ArgType,
Reid Spencer84c614d2007-05-22 19:30:31 +0000164 unsigned &ArgNo) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000165 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000166
167 if (ArgType->isSubClassOf("LLVMMatchType")) {
168 unsigned Number = ArgType->getValueAsInt("Number");
169 assert(Number < ArgNo && "Invalid matching number!");
170 OS << "Tys[" << Number << "]";
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000171 } else if (VT == MVT::iAny || VT == MVT::fAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000172 // 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 Carruth69940402007-08-04 01:51:18 +0000175 // increment it when we actually hit an overloaded type. Getting this wrong
176 // leads to very subtle bugs!
177 OS << "Tys[" << ArgNo++ << "]";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000178 } else if (MVT(VT).isVector()) {
179 MVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000180 OS << "VectorType::get(";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000181 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT());
182 OS << ", " << VVT.getVectorNumElements() << ")";
Chandler Carruth69940402007-08-04 01:51:18 +0000183 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000184 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000185 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000186 OS << ")";
Mon P Wange3b3a722008-07-30 04:36:53 +0000187 } 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 Carruth69940402007-08-04 01:51:18 +0000195 } 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 Laskey95af5922007-02-07 20:38:26 +0000201 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000202 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000203 }
204}
205
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000206/// RecordListComparator - Provide a determinstic comparator for lists of
207/// records.
208namespace {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000209 typedef std::pair<std::vector<Record*>, std::vector<Record*> > RecPair;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000210 struct RecordListComparator {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000211 bool operator()(const RecPair &LHS,
212 const RecPair &RHS) const {
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000213 unsigned i = 0;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000214 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 Lattnerc4d9b242006-03-31 04:24:58 +0000219 do {
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000220 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 Wendling023422a2008-11-13 12:03:00 +0000225 if (i != RHSSize) return true;
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000226
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 Lattnerc4d9b242006-03-31 04:24:58 +0000240 }
241 };
242}
243
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000244void 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 Lattnerc4d9b242006-03-31 04:24:58 +0000250
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 Wendlingcdcc3e62008-11-13 09:08:33 +0000254 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000255 MapTy UniqueArgInfos;
256
257 // Compute the unique argument type info.
258 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000259 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
260 Ints[i].IS.ParamTypeDefs)].push_back(i);
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000261
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 Wendlingcdcc3e62008-11-13 09:08:33 +0000265 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000266 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
267 << Ints[I->second[i]].Name << "\n";
Chris Lattnerf124b462006-03-31 04:48:26 +0000268
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000269 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 Carruth69940402007-08-04 01:51:18 +0000281 if (ArgType->isSubClassOf("LLVMMatchType")) {
282 unsigned Number = ArgType->getValueAsInt("Number");
283 assert(Number < j && "Invalid matching number!");
284 OS << "~" << Number;
285 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000286 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000287 OS << getEnumName(VT);
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000288
289 if (VT == MVT::isVoid && j != 0 && j != je - 1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000290 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000291 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000292 }
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 Laskey95d97b92007-02-06 18:30:58 +0000310 }
311
Chandler Carruth69940402007-08-04 01:51:18 +0000312 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000313 OS << " break;\n";
314 }
315 OS << " }\n";
316 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000317}
318
Jim Laskey95af5922007-02-07 20:38:26 +0000319void 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 Wendlingcdcc3e62008-11-13 09:08:33 +0000328 typedef std::map<RecPair, std::vector<unsigned>, RecordListComparator> MapTy;
Jim Laskey95af5922007-02-07 20:38:26 +0000329 MapTy UniqueArgInfos;
330
331 // Compute the unique argument type info.
332 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000333 UniqueArgInfos[make_pair(Ints[i].IS.RetTypeDefs,
334 Ints[i].IS.ParamTypeDefs)].push_back(i);
Jim Laskey95af5922007-02-07 20:38:26 +0000335
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 Wendlingcdcc3e62008-11-13 09:08:33 +0000339 for (unsigned i = 0, e = I->second.size(); i != e; ++i)
Jim Laskey95af5922007-02-07 20:38:26 +0000340 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
341 << Ints[I->second[i]].Name << "\n";
Jim Laskey95af5922007-02-07 20:38:26 +0000342
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000343 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 Laskey95af5922007-02-07 20:38:26 +0000348
Chandler Carruth69940402007-08-04 01:51:18 +0000349 if (N > 1 &&
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000350 getValueType(ParamTys[N - 1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000351 OS << " IsVarArg = true;\n";
352 --N;
353 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000354
Reid Spencer84c614d2007-05-22 19:30:31 +0000355 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000356 OS << " ResultTy = ";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000357 EmitTypeGenerate(OS, RetTys, ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000358 OS << ";\n";
359
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000360 for (unsigned j = 0; j != N; ++j) {
Jim Laskey95af5922007-02-07 20:38:26 +0000361 OS << " ArgTys.push_back(";
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000362 EmitTypeGenerate(OS, ParamTys[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000363 OS << ");\n";
364 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000365
Jim Laskey95af5922007-02-07 20:38:26 +0000366 OS << " break;\n";
367 }
Bill Wendlingcdcc3e62008-11-13 09:08:33 +0000368
Jim Laskey95af5922007-02-07 20:38:26 +0000369 OS << " }\n";
370 OS << "#endif\n\n";
371}
372
Chris Lattner4e5f3592006-03-09 22:37:52 +0000373void IntrinsicEmitter::
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000374EmitAttributes(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 Lattner7056de32006-03-24 01:13:55 +0000378 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 Patel05988662008-09-25 21:00:45 +0000387 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000388 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000389 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
390 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000391 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000392 case CodeGenIntrinsic::ReadArgMem:
393 case CodeGenIntrinsic::ReadMem:
394 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
395 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000396 }
397 }
Devang Patel05988662008-09-25 21:00:45 +0000398 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000399 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000400 OS << " }\n";
401 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000402}
Chris Lattner022f64f2006-03-13 23:08:44 +0000403
404void IntrinsicEmitter::
405EmitGCCBuiltinList(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 Spencer767a25b2006-03-14 05:59:52 +0000418}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000419
Chris Lattner331bf922008-01-04 04:38:35 +0000420/// 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.
427typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
428static 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.
500static 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 Lattner3f8b8912006-03-15 01:33:26 +0000526void IntrinsicEmitter::
527EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
528 std::ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000529 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000530 BIMTy BuiltinMap;
531 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
532 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000533 // 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 Lattner3f8b8912006-03-15 01:33:26 +0000538 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 Lattner81442c02008-01-04 03:32:52 +0000548 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000549
Chris Lattner3f8b8912006-03-15 01:33:26 +0000550 // Note: this could emit significantly better code if we cared.
551 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000552 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 Lattnerfa0fba12008-01-02 21:24:22 +0000559 // Emit the comparisons for this target prefix.
Chris Lattner331bf922008-01-04 04:38:35 +0000560 EmitTargetBuiltins(I->second, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000561 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000562 }
Chris Lattner3f8b8912006-03-15 01:33:26 +0000563 OS << "#endif\n\n";
564}