blob: ade1c8c66459d21309e8dd98c624f272af1b5928 [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
Reid Spencer84c614d2007-05-22 19:30:31 +0000141static void EmitTypeGenerate(std::ostream &OS, Record *ArgType,
142 unsigned &ArgNo) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000143 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000144
145 if (ArgType->isSubClassOf("LLVMMatchType")) {
146 unsigned Number = ArgType->getValueAsInt("Number");
147 assert(Number < ArgNo && "Invalid matching number!");
148 OS << "Tys[" << Number << "]";
Dan Gohman0fee3ff2007-08-16 21:57:19 +0000149 } else if (VT == MVT::iAny || VT == MVT::fAny) {
Reid Spencer84c614d2007-05-22 19:30:31 +0000150 // 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 Carruth69940402007-08-04 01:51:18 +0000153 // increment it when we actually hit an overloaded type. Getting this wrong
154 // leads to very subtle bugs!
155 OS << "Tys[" << ArgNo++ << "]";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000156 } else if (MVT(VT).isVector()) {
157 MVT VVT = VT;
Reid Spencer9d6565a2007-02-15 02:26:10 +0000158 OS << "VectorType::get(";
Duncan Sands83ec4b62008-06-06 12:08:01 +0000159 EmitTypeForValueType(OS, VVT.getVectorElementType().getSimpleVT());
160 OS << ", " << VVT.getVectorNumElements() << ")";
Chandler Carruth69940402007-08-04 01:51:18 +0000161 } else if (VT == MVT::iPTR) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +0000162 OS << "PointerType::getUnqual(";
Reid Spencerc4de3de2007-04-01 07:20:02 +0000163 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000164 OS << ")";
Mon P Wange3b3a722008-07-30 04:36:53 +0000165 } else if (VT == MVT::iPTRAny) {
166 // Make sure the user has passed us an argument type to overload. If not,
167 // treat it as an ordinary (not overloaded) intrinsic.
168 OS << "(" << ArgNo << " < numTys) ? Tys[" << ArgNo
169 << "] : PointerType::getUnqual(";
170 EmitTypeGenerate(OS, ArgType->getValueAsDef("ElTy"), ArgNo);
171 OS << ")";
172 ++ArgNo;
Chandler Carruth69940402007-08-04 01:51:18 +0000173 } else if (VT == MVT::isVoid) {
174 if (ArgNo == 0)
175 OS << "Type::VoidTy";
176 else
177 // MVT::isVoid is used to mean varargs here.
178 OS << "...";
Jim Laskey95af5922007-02-07 20:38:26 +0000179 } else {
Chandler Carruth69940402007-08-04 01:51:18 +0000180 EmitTypeForValueType(OS, VT);
Jim Laskey95af5922007-02-07 20:38:26 +0000181 }
182}
183
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000184/// RecordListComparator - Provide a determinstic comparator for lists of
185/// records.
186namespace {
187 struct RecordListComparator {
188 bool operator()(const std::vector<Record*> &LHS,
189 const std::vector<Record*> &RHS) const {
190 unsigned i = 0;
191 do {
192 if (i == RHS.size()) return false; // RHS is shorter than LHS.
193 if (LHS[i] != RHS[i])
194 return LHS[i]->getName() < RHS[i]->getName();
195 } while (++i != LHS.size());
196
197 return i != RHS.size();
198 }
199 };
200}
201
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000202void IntrinsicEmitter::EmitVerifier(const std::vector<CodeGenIntrinsic> &Ints,
203 std::ostream &OS) {
204 OS << "// Verifier::visitIntrinsicFunctionCall code.\n";
205 OS << "#ifdef GET_INTRINSIC_VERIFIER\n";
206 OS << " switch (ID) {\n";
207 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000208
209 // This checking can emit a lot of very common code. To reduce the amount of
210 // code that we emit, batch up cases that have identical types. This avoids
211 // problems where GCC can run out of memory compiling Verifier.cpp.
212 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
213 RecordListComparator> MapTy;
214 MapTy UniqueArgInfos;
215
216 // Compute the unique argument type info.
217 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
218 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
219
220 // Loop through the array, emitting one comparison for each batch.
221 for (MapTy::iterator I = UniqueArgInfos.begin(),
222 E = UniqueArgInfos.end(); I != E; ++I) {
223 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
224 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
225 << Ints[I->second[i]].Name << "\n";
226 }
Chris Lattnerf124b462006-03-31 04:48:26 +0000227
Chris Lattnerc4d9b242006-03-31 04:24:58 +0000228 const std::vector<Record*> &ArgTypes = I->first;
Chandler Carruth69940402007-08-04 01:51:18 +0000229 OS << " VerifyIntrinsicPrototype(ID, IF, " << ArgTypes.size() << ", ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000230 for (unsigned j = 0; j != ArgTypes.size(); ++j) {
Chandler Carruth69940402007-08-04 01:51:18 +0000231 Record *ArgType = ArgTypes[j];
232 if (ArgType->isSubClassOf("LLVMMatchType")) {
233 unsigned Number = ArgType->getValueAsInt("Number");
234 assert(Number < j && "Invalid matching number!");
235 OS << "~" << Number;
236 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000237 MVT::SimpleValueType VT = getValueType(ArgType->getValueAsDef("VT"));
Chandler Carruth69940402007-08-04 01:51:18 +0000238 OS << getEnumName(VT);
239 if (VT == MVT::isVoid && j != 0 && j != ArgTypes.size()-1)
Jim Laskey95d97b92007-02-06 18:30:58 +0000240 throw "Var arg type not last argument";
Jim Laskey95d97b92007-02-06 18:30:58 +0000241 }
Chandler Carruth69940402007-08-04 01:51:18 +0000242 if (j != ArgTypes.size()-1)
243 OS << ", ";
Jim Laskey95d97b92007-02-06 18:30:58 +0000244 }
245
Chandler Carruth69940402007-08-04 01:51:18 +0000246 OS << ");\n";
Chris Lattnerf97a00e2006-03-09 22:05:04 +0000247 OS << " break;\n";
248 }
249 OS << " }\n";
250 OS << "#endif\n\n";
Chris Lattner9b843b22006-03-09 20:34:19 +0000251}
252
Jim Laskey95af5922007-02-07 20:38:26 +0000253void IntrinsicEmitter::EmitGenerator(const std::vector<CodeGenIntrinsic> &Ints,
254 std::ostream &OS) {
255 OS << "// Code for generating Intrinsic function declarations.\n";
256 OS << "#ifdef GET_INTRINSIC_GENERATOR\n";
257 OS << " switch (id) {\n";
258 OS << " default: assert(0 && \"Invalid intrinsic!\");\n";
259
260 // Similar to GET_INTRINSIC_VERIFIER, batch up cases that have identical
261 // types.
262 typedef std::map<std::vector<Record*>, std::vector<unsigned>,
263 RecordListComparator> MapTy;
264 MapTy UniqueArgInfos;
265
266 // Compute the unique argument type info.
267 for (unsigned i = 0, e = Ints.size(); i != e; ++i)
268 UniqueArgInfos[Ints[i].ArgTypeDefs].push_back(i);
269
270 // Loop through the array, emitting one generator for each batch.
271 for (MapTy::iterator I = UniqueArgInfos.begin(),
272 E = UniqueArgInfos.end(); I != E; ++I) {
273 for (unsigned i = 0, e = I->second.size(); i != e; ++i) {
274 OS << " case Intrinsic::" << Ints[I->second[i]].EnumName << ":\t\t// "
275 << Ints[I->second[i]].Name << "\n";
276 }
277
278 const std::vector<Record*> &ArgTypes = I->first;
279 unsigned N = ArgTypes.size();
280
Chandler Carruth69940402007-08-04 01:51:18 +0000281 if (N > 1 &&
282 getValueType(ArgTypes[N-1]->getValueAsDef("VT")) == MVT::isVoid) {
Jim Laskey95af5922007-02-07 20:38:26 +0000283 OS << " IsVarArg = true;\n";
284 --N;
285 }
286
Reid Spencer84c614d2007-05-22 19:30:31 +0000287 unsigned ArgNo = 0;
Jim Laskey95af5922007-02-07 20:38:26 +0000288 OS << " ResultTy = ";
Reid Spencer84c614d2007-05-22 19:30:31 +0000289 EmitTypeGenerate(OS, ArgTypes[0], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000290 OS << ";\n";
291
292 for (unsigned j = 1; j != N; ++j) {
293 OS << " ArgTys.push_back(";
Reid Spencer84c614d2007-05-22 19:30:31 +0000294 EmitTypeGenerate(OS, ArgTypes[j], ArgNo);
Jim Laskey95af5922007-02-07 20:38:26 +0000295 OS << ");\n";
296 }
Jim Laskey95af5922007-02-07 20:38:26 +0000297 OS << " break;\n";
298 }
299 OS << " }\n";
300 OS << "#endif\n\n";
301}
302
Chris Lattner4e5f3592006-03-09 22:37:52 +0000303void IntrinsicEmitter::
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000304EmitAttributes(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS) {
305 OS << "// Add parameter attributes that are not common to all intrinsics.\n";
306 OS << "#ifdef GET_INTRINSIC_ATTRIBUTES\n";
307 OS << " switch (id) {\n";
Chris Lattner7056de32006-03-24 01:13:55 +0000308 OS << " default: break;\n";
309 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
310 switch (Ints[i].ModRef) {
311 default: break;
312 case CodeGenIntrinsic::NoMem:
313 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
314 break;
315 }
316 }
Devang Patel05988662008-09-25 21:00:45 +0000317 OS << " Attr |= Attribute::ReadNone; // These do not access memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000318 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000319 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
320 switch (Ints[i].ModRef) {
Chris Lattner022f64f2006-03-13 23:08:44 +0000321 default: break;
Chris Lattner022f64f2006-03-13 23:08:44 +0000322 case CodeGenIntrinsic::ReadArgMem:
323 case CodeGenIntrinsic::ReadMem:
324 OS << " case Intrinsic::" << Ints[i].EnumName << ":\n";
325 break;
Chris Lattner4e5f3592006-03-09 22:37:52 +0000326 }
327 }
Devang Patel05988662008-09-25 21:00:45 +0000328 OS << " Attr |= Attribute::ReadOnly; // These do not write memory.\n";
Duncan Sandsa3355ff2007-12-03 20:06:50 +0000329 OS << " break;\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000330 OS << " }\n";
331 OS << "#endif\n\n";
Chris Lattner4e5f3592006-03-09 22:37:52 +0000332}
Chris Lattner022f64f2006-03-13 23:08:44 +0000333
334void IntrinsicEmitter::
335EmitGCCBuiltinList(const std::vector<CodeGenIntrinsic> &Ints, std::ostream &OS){
336 OS << "// Get the GCC builtin that corresponds to an LLVM intrinsic.\n";
337 OS << "#ifdef GET_GCC_BUILTIN_NAME\n";
338 OS << " switch (F->getIntrinsicID()) {\n";
339 OS << " default: BuiltinName = \"\"; break;\n";
340 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
341 if (!Ints[i].GCCBuiltinName.empty()) {
342 OS << " case Intrinsic::" << Ints[i].EnumName << ": BuiltinName = \""
343 << Ints[i].GCCBuiltinName << "\"; break;\n";
344 }
345 }
346 OS << " }\n";
347 OS << "#endif\n\n";
Reid Spencer767a25b2006-03-14 05:59:52 +0000348}
Chris Lattner3f8b8912006-03-15 01:33:26 +0000349
Chris Lattner331bf922008-01-04 04:38:35 +0000350/// EmitBuiltinComparisons - Emit comparisons to determine whether the specified
351/// sorted range of builtin names is equal to the current builtin. This breaks
352/// it down into a simple tree.
353///
354/// At this point, we know that all the builtins in the range have the same name
355/// for the first 'CharStart' characters. Only the end of the name needs to be
356/// discriminated.
357typedef std::map<std::string, std::string>::const_iterator StrMapIterator;
358static void EmitBuiltinComparisons(StrMapIterator Start, StrMapIterator End,
359 unsigned CharStart, unsigned Indent,
360 std::ostream &OS) {
361 if (Start == End) return; // empty range.
362
363 // Determine what, if anything, is the same about all these strings.
364 std::string CommonString = Start->first;
365 unsigned NumInRange = 0;
366 for (StrMapIterator I = Start; I != End; ++I, ++NumInRange) {
367 // Find the first character that doesn't match.
368 const std::string &ThisStr = I->first;
369 unsigned NonMatchChar = CharStart;
370 while (NonMatchChar < CommonString.size() &&
371 NonMatchChar < ThisStr.size() &&
372 CommonString[NonMatchChar] == ThisStr[NonMatchChar])
373 ++NonMatchChar;
374 // Truncate off pieces that don't match.
375 CommonString.resize(NonMatchChar);
376 }
377
378 // Just compare the rest of the string.
379 if (NumInRange == 1) {
380 if (CharStart != CommonString.size()) {
381 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
382 if (CharStart) OS << "+" << CharStart;
383 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
384 OS << CommonString.size() - CharStart << "))\n";
385 ++Indent;
386 }
387 OS << std::string(Indent*2, ' ') << "IntrinsicID = Intrinsic::";
388 OS << Start->second << ";\n";
389 return;
390 }
391
392 // At this point, we potentially have a common prefix for these builtins, emit
393 // a check for this common prefix.
394 if (CommonString.size() != CharStart) {
395 OS << std::string(Indent*2, ' ') << "if (!memcmp(BuiltinName";
396 if (CharStart) OS << "+" << CharStart;
397 OS << ", \"" << (CommonString.c_str()+CharStart) << "\", ";
398 OS << CommonString.size()-CharStart << ")) {\n";
399
400 EmitBuiltinComparisons(Start, End, CommonString.size(), Indent+1, OS);
401 OS << std::string(Indent*2, ' ') << "}\n";
402 return;
403 }
404
405 // Output a switch on the character that differs across the set.
406 OS << std::string(Indent*2, ' ') << "switch (BuiltinName[" << CharStart
407 << "]) {";
408 if (CharStart)
409 OS << " // \"" << std::string(Start->first.begin(),
410 Start->first.begin()+CharStart) << "\"";
411 OS << "\n";
412
413 for (StrMapIterator I = Start; I != End; ) {
414 char ThisChar = I->first[CharStart];
415 OS << std::string(Indent*2, ' ') << "case '" << ThisChar << "':\n";
416 // Figure out the range that has this common character.
417 StrMapIterator NextChar = I;
418 for (++NextChar; NextChar != End && NextChar->first[CharStart] == ThisChar;
419 ++NextChar)
420 /*empty*/;
421 EmitBuiltinComparisons(I, NextChar, CharStart+1, Indent+1, OS);
422 OS << std::string(Indent*2, ' ') << " break;\n";
423 I = NextChar;
424 }
425 OS << std::string(Indent*2, ' ') << "}\n";
426}
427
428/// EmitTargetBuiltins - All of the builtins in the specified map are for the
429/// same target, and we already checked it.
430static void EmitTargetBuiltins(const std::map<std::string, std::string> &BIM,
431 std::ostream &OS) {
432 // Rearrange the builtins by length.
433 std::vector<std::map<std::string, std::string> > BuiltinsByLen;
434 BuiltinsByLen.reserve(100);
435
436 for (StrMapIterator I = BIM.begin(), E = BIM.end(); I != E; ++I) {
437 if (I->first.size() >= BuiltinsByLen.size())
438 BuiltinsByLen.resize(I->first.size()+1);
439 BuiltinsByLen[I->first.size()].insert(*I);
440 }
441
442 // Now that we have all the builtins by their length, emit a switch stmt.
443 OS << " switch (strlen(BuiltinName)) {\n";
444 OS << " default: break;\n";
445 for (unsigned i = 0, e = BuiltinsByLen.size(); i != e; ++i) {
446 if (BuiltinsByLen[i].empty()) continue;
447 OS << " case " << i << ":\n";
448 EmitBuiltinComparisons(BuiltinsByLen[i].begin(), BuiltinsByLen[i].end(),
449 0, 3, OS);
450 OS << " break;\n";
451 }
452 OS << " }\n";
453}
454
455
Chris Lattner3f8b8912006-03-15 01:33:26 +0000456void IntrinsicEmitter::
457EmitIntrinsicToGCCBuiltinMap(const std::vector<CodeGenIntrinsic> &Ints,
458 std::ostream &OS) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000459 typedef std::map<std::string, std::map<std::string, std::string> > BIMTy;
Chris Lattner3f8b8912006-03-15 01:33:26 +0000460 BIMTy BuiltinMap;
461 for (unsigned i = 0, e = Ints.size(); i != e; ++i) {
462 if (!Ints[i].GCCBuiltinName.empty()) {
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000463 // Get the map for this target prefix.
464 std::map<std::string, std::string> &BIM =BuiltinMap[Ints[i].TargetPrefix];
465
466 if (!BIM.insert(std::make_pair(Ints[i].GCCBuiltinName,
467 Ints[i].EnumName)).second)
Chris Lattner3f8b8912006-03-15 01:33:26 +0000468 throw "Intrinsic '" + Ints[i].TheDef->getName() +
469 "': duplicate GCC builtin name!";
470 }
471 }
472
473 OS << "// Get the LLVM intrinsic that corresponds to a GCC builtin.\n";
474 OS << "// This is used by the C front-end. The GCC builtin name is passed\n";
475 OS << "// in as BuiltinName, and a target prefix (e.g. 'ppc') is passed\n";
476 OS << "// in as TargetPrefix. The result is assigned to 'IntrinsicID'.\n";
477 OS << "#ifdef GET_LLVM_INTRINSIC_FOR_GCC_BUILTIN\n";
Chris Lattner81442c02008-01-04 03:32:52 +0000478 OS << " IntrinsicID = Intrinsic::not_intrinsic;\n";
Chris Lattner331bf922008-01-04 04:38:35 +0000479
Chris Lattner3f8b8912006-03-15 01:33:26 +0000480 // Note: this could emit significantly better code if we cared.
481 for (BIMTy::iterator I = BuiltinMap.begin(), E = BuiltinMap.end();I != E;++I){
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000482 OS << " ";
483 if (!I->first.empty())
484 OS << "if (!strcmp(TargetPrefix, \"" << I->first << "\")) ";
485 else
486 OS << "/* Target Independent Builtins */ ";
487 OS << "{\n";
488
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000489 // Emit the comparisons for this target prefix.
Chris Lattner331bf922008-01-04 04:38:35 +0000490 EmitTargetBuiltins(I->second, OS);
Chris Lattnerfa0fba12008-01-02 21:24:22 +0000491 OS << " }\n";
Chris Lattner3f8b8912006-03-15 01:33:26 +0000492 }
Chris Lattner3f8b8912006-03-15 01:33:26 +0000493 OS << "#endif\n\n";
494}