blob: 4fe8d8e634a0db9fc25a8c161e7ccec5a7cf509e [file] [log] [blame]
Nate Begeman5ddb0872010-05-28 01:08:32 +00001//===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This tablegen backend is responsible for emitting arm_neon.h, which includes
11// a declaration and definition of each function specified by the ARM NEON
12// compiler interface. See ARM document DUI0348B.
13//
Nate Begemand72c9002010-06-13 04:47:03 +000014// Each NEON instruction is implemented in terms of 1 or more functions which
15// are suffixed with the element type of the input vectors. Functions may be
16// implemented in terms of generic vector operations such as +, *, -, etc. or
17// by calling a __builtin_-prefixed function which will be handled by clang's
18// CodeGen library.
19//
20// Additional validation code can be generated by this file when runHeader() is
21// called, rather than the normal run() entry point.
22//
Nate Begeman5ddb0872010-05-28 01:08:32 +000023//===----------------------------------------------------------------------===//
24
25#include "NeonEmitter.h"
Nate Begeman22237772010-06-02 00:34:55 +000026#include "llvm/ADT/SmallString.h"
27#include "llvm/ADT/SmallVector.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000028#include "llvm/ADT/StringExtras.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000029#include <string>
30
31using namespace llvm;
32
Nate Begemand72c9002010-06-13 04:47:03 +000033/// ParseTypes - break down a string such as "fQf" into a vector of StringRefs,
34/// which each StringRef representing a single type declared in the string.
35/// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing
36/// 2xfloat and 4xfloat respectively.
Nate Begeman22237772010-06-02 00:34:55 +000037static void ParseTypes(Record *r, std::string &s,
38 SmallVectorImpl<StringRef> &TV) {
39 const char *data = s.data();
40 int len = 0;
41
42 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
43 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
44 continue;
45
46 switch (data[len]) {
47 case 'c':
48 case 's':
49 case 'i':
50 case 'l':
51 case 'h':
52 case 'f':
53 break;
54 default:
55 throw TGError(r->getLoc(),
56 "Unexpected letter: " + std::string(data + len, 1));
57 break;
58 }
59 TV.push_back(StringRef(data, len + 1));
60 data += len + 1;
61 len = -1;
62 }
63}
64
Nate Begemand72c9002010-06-13 04:47:03 +000065/// Widen - Convert a type code into the next wider type. char -> short,
66/// short -> int, etc.
Duncan Sands8dbbace2010-06-02 08:37:30 +000067static char Widen(const char t) {
Nate Begeman22237772010-06-02 00:34:55 +000068 switch (t) {
69 case 'c':
70 return 's';
71 case 's':
72 return 'i';
73 case 'i':
74 return 'l';
75 default: throw "unhandled type in widen!";
76 }
77 return '\0';
78}
79
Nate Begemand72c9002010-06-13 04:47:03 +000080/// Narrow - Convert a type code into the next smaller type. short -> char,
81/// float -> half float, etc.
Nate Begeman3861e742010-06-03 21:35:22 +000082static char Narrow(const char t) {
83 switch (t) {
84 case 's':
85 return 'c';
86 case 'i':
87 return 's';
88 case 'l':
89 return 'i';
Nate Begeman900f4672010-06-08 00:14:42 +000090 case 'f':
91 return 'h';
Nate Begeman8a6e7e12010-09-23 16:49:17 +000092 default: throw "unhandled type in widen!";
Nate Begeman3861e742010-06-03 21:35:22 +000093 }
94 return '\0';
95}
96
Nate Begemand72c9002010-06-13 04:47:03 +000097/// For a particular StringRef, return the base type code, and whether it has
98/// the quad-vector, polynomial, or unsigned modifiers set.
Nate Begemanaf905ef2010-06-02 06:17:19 +000099static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +0000100 unsigned off = 0;
101
Nate Begemanaf905ef2010-06-02 06:17:19 +0000102 // remember quad.
103 if (ty[off] == 'Q') {
104 quad = true;
105 ++off;
106 }
107
108 // remember poly.
109 if (ty[off] == 'P') {
110 poly = true;
111 ++off;
112 }
113
114 // remember unsigned.
115 if (ty[off] == 'U') {
116 usgn = true;
117 ++off;
118 }
119
120 // base type to get the type string for.
121 return ty[off];
122}
123
Nate Begemand72c9002010-06-13 04:47:03 +0000124/// ModType - Transform a type code and its modifiers based on a mod code. The
125/// mod code definitions may be found at the top of arm_neon.td.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000126static char ModType(const char mod, char type, bool &quad, bool &poly,
127 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000128 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000129 case 't':
130 if (poly) {
131 poly = false;
132 usgn = true;
133 }
134 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000135 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000136 usgn = true;
Nate Begeman900f4672010-06-08 00:14:42 +0000137 case 'x':
Nate Begeman162d3ba2010-06-03 04:04:09 +0000138 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000139 if (type == 'f')
140 type = 'i';
141 break;
142 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000143 if (type == 'h')
144 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000145 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000146 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000147 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000148 case 'g':
149 quad = false;
150 break;
Nate Begeman22237772010-06-02 00:34:55 +0000151 case 'w':
152 type = Widen(type);
153 quad = true;
154 break;
155 case 'n':
156 type = Widen(type);
157 break;
Nate Begeman22237772010-06-02 00:34:55 +0000158 case 'l':
159 type = 'l';
160 scal = true;
161 usgn = true;
162 break;
163 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000164 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000165 scal = true;
166 break;
167 case 'k':
168 quad = true;
169 break;
170 case 'c':
171 cnst = true;
172 case 'p':
173 pntr = true;
174 scal = true;
175 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000176 case 'h':
177 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000178 if (type == 'h')
179 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000180 break;
181 case 'e':
182 type = Narrow(type);
183 usgn = true;
184 break;
Nate Begeman22237772010-06-02 00:34:55 +0000185 default:
186 break;
187 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000188 return type;
189}
190
Nate Begemand72c9002010-06-13 04:47:03 +0000191/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000192/// that type. QUc -> uint8x8_t.
193static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000194 bool quad = false;
195 bool poly = false;
196 bool usgn = false;
197 bool scal = false;
198 bool cnst = false;
199 bool pntr = false;
200
201 if (mod == 'v')
202 return "void";
203 if (mod == 'i')
204 return "int";
205
206 // base type to get the type string for.
207 char type = ClassifyType(typestr, quad, poly, usgn);
208
209 // Based on the modifying character, change the type and width if necessary.
210 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000211
212 SmallString<128> s;
213
214 if (usgn)
215 s.push_back('u');
216
217 switch (type) {
218 case 'c':
219 s += poly ? "poly8" : "int8";
220 if (scal)
221 break;
222 s += quad ? "x16" : "x8";
223 break;
224 case 's':
225 s += poly ? "poly16" : "int16";
226 if (scal)
227 break;
228 s += quad ? "x8" : "x4";
229 break;
230 case 'i':
231 s += "int32";
232 if (scal)
233 break;
234 s += quad ? "x4" : "x2";
235 break;
236 case 'l':
237 s += "int64";
238 if (scal)
239 break;
240 s += quad ? "x2" : "x1";
241 break;
242 case 'h':
243 s += "float16";
244 if (scal)
245 break;
246 s += quad ? "x8" : "x4";
247 break;
248 case 'f':
249 s += "float32";
250 if (scal)
251 break;
252 s += quad ? "x4" : "x2";
253 break;
Nate Begeman22237772010-06-02 00:34:55 +0000254 default:
255 throw "unhandled type!";
256 break;
257 }
258
259 if (mod == '2')
260 s += "x2";
261 if (mod == '3')
262 s += "x3";
263 if (mod == '4')
264 s += "x4";
265
266 // Append _t, finishing the type string typedef type.
267 s += "_t";
268
269 if (cnst)
270 s += " const";
271
272 if (pntr)
273 s += " *";
274
275 return s.str();
276}
277
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000278/// BuiltinTypeString - for a modifier and type, generate the clang
279/// BuiltinsARM.def prototype code for the function. See the top of clang's
280/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000281static std::string BuiltinTypeString(const char mod, StringRef typestr,
282 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000283 bool quad = false;
284 bool poly = false;
285 bool usgn = false;
286 bool scal = false;
287 bool cnst = false;
288 bool pntr = false;
289
290 if (mod == 'v')
291 return "v";
292 if (mod == 'i')
293 return "i";
294
295 // base type to get the type string for.
296 char type = ClassifyType(typestr, quad, poly, usgn);
297
298 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000299 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
300
Nate Begemanc4a1b652010-06-20 21:09:52 +0000301 if (pntr) {
302 usgn = false;
303 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000304 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000305 }
Nate Begeman92f98af2010-06-04 07:11:25 +0000306 if (type == 'h') {
307 type = 's';
308 usgn = true;
309 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000310 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000311
312 if (scal) {
313 SmallString<128> s;
314
315 if (usgn)
316 s.push_back('U');
Nate Begeman7c21f742010-06-04 21:36:00 +0000317
318 if (type == 'l')
319 s += "LLi";
320 else
321 s.push_back(type);
322
Nate Begeman92f98af2010-06-04 07:11:25 +0000323 if (cnst)
324 s.push_back('C');
325 if (pntr)
326 s.push_back('*');
327 return s.str();
328 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000329
330 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000331 // appropriate width which we will bitcast. An exception is made for
332 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
333 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000334 if (ret) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000335 if (mod == '2' || mod == '3' || mod == '4')
336 return "vv*";
Nate Begemanf50551e2010-06-09 18:02:26 +0000337 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000338 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000339 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000340 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000341 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000342 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000343 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000344 return quad ? "V2LLi" : "V1LLi";
Nate Begeman900f4672010-06-08 00:14:42 +0000345
Nate Begeman7c21f742010-06-04 21:36:00 +0000346 return quad ? "V16c" : "V8c";
347 }
348
349 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000350 if (mod == '2')
351 return quad ? "V16cV16c" : "V8cV8c";
352 if (mod == '3')
353 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
354 if (mod == '4')
355 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
356
Nate Begemanf50551e2010-06-09 18:02:26 +0000357 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000358 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000359 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000360 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000361 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000362 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000363 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000364 return quad ? "V2LLi" : "V1LLi";
365
Nate Begeman92f98af2010-06-04 07:11:25 +0000366 return quad ? "V16c" : "V8c";
367}
368
Nate Begemand72c9002010-06-13 04:47:03 +0000369/// MangleName - Append a type or width suffix to a base neon function name,
370/// and insert a 'q' in the appropriate location if the operation works on
371/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000372static std::string MangleName(const std::string &name, StringRef typestr,
373 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000374 if (name == "vcvt_f32_f16")
375 return name;
376
Nate Begemanaf905ef2010-06-02 06:17:19 +0000377 bool quad = false;
378 bool poly = false;
379 bool usgn = false;
380 char type = ClassifyType(typestr, quad, poly, usgn);
381
382 std::string s = name;
383
384 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000385 case 'c':
386 switch (ck) {
387 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
388 case ClassI: s += "_i8"; break;
389 case ClassW: s += "_8"; break;
390 default: break;
391 }
392 break;
393 case 's':
394 switch (ck) {
395 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
396 case ClassI: s += "_i16"; break;
397 case ClassW: s += "_16"; break;
398 default: break;
399 }
400 break;
401 case 'i':
402 switch (ck) {
403 case ClassS: s += usgn ? "_u32" : "_s32"; break;
404 case ClassI: s += "_i32"; break;
405 case ClassW: s += "_32"; break;
406 default: break;
407 }
408 break;
409 case 'l':
410 switch (ck) {
411 case ClassS: s += usgn ? "_u64" : "_s64"; break;
412 case ClassI: s += "_i64"; break;
413 case ClassW: s += "_64"; break;
414 default: break;
415 }
416 break;
417 case 'h':
418 switch (ck) {
419 case ClassS:
420 case ClassI: s += "_f16"; break;
421 case ClassW: s += "_16"; break;
422 default: break;
423 }
424 break;
425 case 'f':
426 switch (ck) {
427 case ClassS:
428 case ClassI: s += "_f32"; break;
429 case ClassW: s += "_32"; break;
430 default: break;
431 }
432 break;
433 default:
434 throw "unhandled type!";
435 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000436 }
Nate Begemana8979a02010-06-04 00:21:41 +0000437 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000438 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000439
Nate Begemanaf905ef2010-06-02 06:17:19 +0000440 // Insert a 'q' before the first '_' character so that it ends up before
441 // _lane or _n on vector-scalar operations.
442 if (quad) {
443 size_t pos = s.find('_');
444 s = s.insert(pos, "q");
445 }
446 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000447}
448
Nate Begemanaf905ef2010-06-02 06:17:19 +0000449// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000450static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000451 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000452 char arg = 'a';
453
454 std::string s;
455 s += "(";
456
457 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000458 if (!define) {
459 s += TypeString(proto[i], typestr);
460 s.push_back(' ');
461 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000462 s.push_back(arg);
463 if ((i + 1) < e)
464 s += ", ";
465 }
466
467 s += ")";
468 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000469}
470
Nate Begemancc3c41a2010-06-12 03:09:49 +0000471static std::string Duplicate(unsigned nElts, StringRef typestr,
472 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000473 std::string s;
474
Bob Wilson6904d7a2010-11-16 19:39:14 +0000475 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000476 for (unsigned i = 0; i != nElts; ++i) {
477 s += a;
478 if ((i + 1) < nElts)
479 s += ", ";
480 }
481 s += " }";
482
483 return s;
484}
485
486// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
Nate Begeman4b425a82010-06-10 00:16:56 +0000487static std::string GenOpString(OpKind op, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000488 StringRef typestr) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000489 bool dummy, quad = false;
490 char type = ClassifyType(typestr, quad, dummy, dummy);
491 unsigned nElts = 0;
492 switch (type) {
493 case 'c': nElts = 8; break;
494 case 's': nElts = 4; break;
495 case 'i': nElts = 2; break;
496 case 'l': nElts = 1; break;
497 case 'h': nElts = 4; break;
498 case 'f': nElts = 2; break;
499 }
500
Nate Begeman4b425a82010-06-10 00:16:56 +0000501 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000502 std::string s;
503 if (op == OpHi || op == OpLo) {
504 s = "union { " + ts + " r; double d; } u; u.d";
505 } else {
506 s = ts + " r; r";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000507 }
Nate Begeman900f4672010-06-08 00:14:42 +0000508
509 s += " = ";
510
Nate Begeman162d3ba2010-06-03 04:04:09 +0000511 switch(op) {
512 case OpAdd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000513 s += "a + b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000514 break;
515 case OpSub:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000516 s += "a - b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000517 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000518 case OpMulN:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000519 s += "a * " + Duplicate(nElts << (int)quad, typestr, "b");
520 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000521 case OpMul:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000522 s += "a * b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000523 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000524 case OpMlaN:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000525 s += "a + (b * " + Duplicate(nElts << (int)quad, typestr, "c") + ")";
526 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000527 case OpMla:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000528 s += "a + (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000529 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000530 case OpMlsN:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000531 s += "a - (b * " + Duplicate(nElts << (int)quad, typestr, "c") + ")";
532 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000533 case OpMls:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000534 s += "a - (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000535 break;
536 case OpEq:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000537 s += "(" + ts + ")(a == b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000538 break;
539 case OpGe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000540 s += "(" + ts + ")(a >= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000541 break;
542 case OpLe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000543 s += "(" + ts + ")(a <= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000544 break;
545 case OpGt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000546 s += "(" + ts + ")(a > b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000547 break;
548 case OpLt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000549 s += "(" + ts + ")(a < b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000550 break;
551 case OpNeg:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000552 s += " -a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000553 break;
554 case OpNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000555 s += " ~a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000556 break;
557 case OpAnd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000558 s += "a & b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000559 break;
560 case OpOr:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000561 s += "a | b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000562 break;
563 case OpXor:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000564 s += "a ^ b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000565 break;
566 case OpAndNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000567 s += "a & ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000568 break;
569 case OpOrNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000570 s += "a | ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000571 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000572 case OpCast:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000573 s += "(" + ts + ")a";
Nate Begeman3861e742010-06-03 21:35:22 +0000574 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000575 case OpConcat:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000576 s += "__builtin_shufflevector((int64x1_t)a";
577 s += ", (int64x1_t)b, 0, 1)";
Nate Begeman900f4672010-06-08 00:14:42 +0000578 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000579 case OpHi:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000580 s += "(((float64x2_t)a)[1])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000581 break;
582 case OpLo:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000583 s += "(((float64x2_t)a)[0])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000584 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000585 case OpDup:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000586 s += Duplicate(nElts << (int)quad, typestr, "a");
Nate Begemancc3c41a2010-06-12 03:09:49 +0000587 break;
588 case OpSelect:
589 // ((0 & 1) | (~0 & 2))
590 ts = TypeString(proto[1], typestr);
Bob Wilson6904d7a2010-11-16 19:39:14 +0000591 s += "(a & (" + ts + ")b) | ";
592 s += "(~a & (" + ts + ")c)";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000593 break;
594 case OpRev16:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000595 s += "__builtin_shufflevector(a, a";
Nate Begeman4da883a2010-06-15 22:10:31 +0000596 for (unsigned i = 2; i <= nElts << (int)quad; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000597 for (unsigned j = 0; j != 2; ++j)
598 s += ", " + utostr(i - j - 1);
599 s += ")";
600 break;
601 case OpRev32:
602 nElts >>= 1;
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000603 s += "__builtin_shufflevector(a, a";
Nate Begeman4da883a2010-06-15 22:10:31 +0000604 for (unsigned i = nElts; i <= nElts << (1 + (int)quad); i += nElts)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000605 for (unsigned j = 0; j != nElts; ++j)
606 s += ", " + utostr(i - j - 1);
607 s += ")";
608 break;
609 case OpRev64:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000610 s += "__builtin_shufflevector(a, a";
Nate Begeman4da883a2010-06-15 22:10:31 +0000611 for (unsigned i = nElts; i <= nElts << (int)quad; i += nElts)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000612 for (unsigned j = 0; j != nElts; ++j)
613 s += ", " + utostr(i - j - 1);
614 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000615 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000616 default:
617 throw "unknown OpKind!";
618 break;
619 }
Bob Wilsonee9ca072010-09-15 01:52:33 +0000620 if (op == OpHi || op == OpLo)
621 s += "; return u.r;";
622 else
623 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000624 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000625}
626
Nate Begemanb0a4e452010-06-07 16:00:37 +0000627static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
628 unsigned mod = proto[0];
629 unsigned ret = 0;
630
Nate Begeman900f4672010-06-08 00:14:42 +0000631 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000632 mod = proto[1];
633
634 bool quad = false;
635 bool poly = false;
636 bool usgn = false;
637 bool scal = false;
638 bool cnst = false;
639 bool pntr = false;
640
Nate Begeman59d70cb2010-08-06 01:24:11 +0000641 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000642 char type = ClassifyType(typestr, quad, poly, usgn);
643
644 // Based on the modifying character, change the type and width if necessary.
645 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000646
Nate Begemanb0a4e452010-06-07 16:00:37 +0000647 if (usgn)
648 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000649 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000650 ret |= 0x10;
651
652 switch (type) {
653 case 'c':
654 ret |= poly ? 5 : 0;
655 break;
656 case 's':
657 ret |= poly ? 6 : 1;
658 break;
659 case 'i':
660 ret |= 2;
661 break;
662 case 'l':
663 ret |= 3;
664 break;
665 case 'h':
666 ret |= 7;
667 break;
668 case 'f':
669 ret |= 4;
670 break;
671 default:
672 throw "unhandled type!";
673 break;
674 }
675 return ret;
676}
677
Nate Begeman7c8c8832010-06-02 21:53:00 +0000678// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000679static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000680 StringRef typestr, ClassKind ck) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000681 bool dummy, quad = false;
682 char type = ClassifyType(typestr, quad, dummy, dummy);
683 unsigned nElts = 0;
684 switch (type) {
685 case 'c': nElts = 8; break;
686 case 's': nElts = 4; break;
687 case 'i': nElts = 2; break;
688 case 'l': nElts = 1; break;
689 case 'h': nElts = 4; break;
690 case 'f': nElts = 2; break;
691 }
Chris Lattner5ca96982010-06-12 15:46:56 +0000692 if (quad) nElts <<= 1;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000693
Nate Begeman7c8c8832010-06-02 21:53:00 +0000694 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000695 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000696
Nate Begemanc4a1b652010-06-20 21:09:52 +0000697 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
698 // sret-like argument.
699 bool sret = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
700
701 // If this builtin takes an immediate argument, we need to #define it rather
702 // than use a standard declaration, so that SemaChecking can range check
703 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000704 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000705
706 // If all types are the same size, bitcasting the args will take care
707 // of arg checking. The actual signedness etc. will be taken care of with
708 // special enums.
709 if (proto.find('s') == std::string::npos)
710 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000711
Nate Begeman162d3ba2010-06-03 04:04:09 +0000712 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000713 std::string ts = TypeString(proto[0], typestr);
714
715 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000716 if (sret)
717 s += "({ " + ts + " r; ";
718 else if (proto[0] != 's')
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000719 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000720 } else if (sret) {
721 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000722 } else {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000723 s += ts + " r; r = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000724 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000725 }
726
727 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000728
729 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000730 if (splat) {
731 std::string vname(name, 0, name.size()-2);
732 s += MangleName(vname, typestr, ck);
733 } else {
734 s += MangleName(name, typestr, ck);
735 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000736 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000737
738 // Pass the address of the return variable as the first argument to sret-like
739 // builtins.
740 if (sret)
741 s += "&r, ";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000742
743 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000744 std::string args = std::string(&arg, 1);
745 if (define)
746 args = "(" + args + ")";
747
Nate Begeman9e584b32010-06-04 22:53:30 +0000748 // Handle multiple-vector values specially, emitting each subvector as an
749 // argument to the __builtin.
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000750 if (proto[i] == '2' || proto[i] == '3' || proto[i] == '4') {
Nate Begeman9e584b32010-06-04 22:53:30 +0000751 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000752 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000753 if ((vi + 1) < ve)
754 s += ", ";
755 }
756 if ((i + 1) < e)
757 s += ", ";
758
759 continue;
760 }
761
Nate Begeman4b425a82010-06-10 00:16:56 +0000762 if (splat && (i + 1) == e)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000763 s += Duplicate(nElts, typestr, args);
Nate Begeman4b425a82010-06-10 00:16:56 +0000764 else
Nate Begemancc3c41a2010-06-12 03:09:49 +0000765 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000766 if ((i + 1) < e)
767 s += ", ";
768 }
769
Nate Begeman9e584b32010-06-04 22:53:30 +0000770 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000771 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000772 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000773
Nate Begeman6c060db2010-06-09 01:09:00 +0000774 if (define)
775 s += ")";
776 else
777 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000778
779 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000780 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000781 if (sret)
782 s += "; r; })";
Nate Begeman6c060db2010-06-09 01:09:00 +0000783 } else {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000784 s += " return r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000785 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000786 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000787 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000788}
789
Nate Begeman73cef3e2010-06-04 01:26:15 +0000790static std::string GenBuiltinDef(const std::string &name,
791 const std::string &proto,
792 StringRef typestr, ClassKind ck) {
793 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000794
795 // If all types are the same size, bitcasting the args will take care
796 // of arg checking. The actual signedness etc. will be taken care of with
797 // special enums.
798 if (proto.find('s') == std::string::npos)
799 ck = ClassB;
800
Nate Begeman73cef3e2010-06-04 01:26:15 +0000801 s += MangleName(name, typestr, ck);
802 s += ", \"";
803
Nate Begeman92f98af2010-06-04 07:11:25 +0000804 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000805 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
806
807 // Extra constant integer to hold type class enum for this function, e.g. s8
808 if (ck == ClassB)
809 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000810
811 s += "\", \"n\")";
812 return s;
813}
814
Nate Begemand72c9002010-06-13 04:47:03 +0000815/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
816/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000817void NeonEmitter::run(raw_ostream &OS) {
818 EmitSourceFileHeader("ARM NEON Header", OS);
819
820 // FIXME: emit license into file?
821
822 OS << "#ifndef __ARM_NEON_H\n";
823 OS << "#define __ARM_NEON_H\n\n";
824
825 OS << "#ifndef __ARM_NEON__\n";
826 OS << "#error \"NEON support not enabled\"\n";
827 OS << "#endif\n\n";
828
829 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000830
831 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000832 OS << "typedef float float32_t;\n";
833 OS << "typedef uint8_t poly8_t;\n";
834 OS << "typedef uint16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000835 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000836
Nate Begeman7c8c8832010-06-02 21:53:00 +0000837 // Emit Neon vector typedefs.
838 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
839 SmallVector<StringRef, 24> TDTypeVec;
840 ParseTypes(0, TypedefTypes, TDTypeVec);
841
842 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000843 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
844 bool dummy, quad = false;
845 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
846 OS << "typedef __attribute__(( __vector_size__(";
Nate Begeman9e584b32010-06-04 22:53:30 +0000847
Bob Wilson6904d7a2010-11-16 19:39:14 +0000848 OS << utostr(8*(quad ? 2 : 1)) << ") )) ";
849 if (!quad)
850 OS << " ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000851
Bob Wilson6904d7a2010-11-16 19:39:14 +0000852 OS << TypeString('s', TDTypeVec[i]);
853 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000854 }
855 OS << "\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000856 OS << "typedef __attribute__(( __vector_size__(8) )) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000857 "double float64x1_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000858 OS << "typedef __attribute__(( __vector_size__(16) )) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000859 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000860 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000861
862 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000863 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000864 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +0000865 std::string ts = TypeString('d', TDTypeVec[i]);
866 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
867 OS << "typedef struct " << vs << " {\n";
868 OS << " " << ts << " val";
869 OS << "[" << utostr(vi) << "]";
870 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000871 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000872 }
873 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000874
Nate Begeman7c8c8832010-06-02 21:53:00 +0000875 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
876
Nate Begeman5ddb0872010-05-28 01:08:32 +0000877 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
878
Nate Begeman22237772010-06-02 00:34:55 +0000879 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000880 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
881 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000882 std::string name = LowercaseString(R->getName());
883 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000884 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000885
886 SmallVector<StringRef, 16> TypeVec;
887 ParseTypes(R, Types, TypeVec);
888
Nate Begeman162d3ba2010-06-03 04:04:09 +0000889 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000890
Nate Begeman6c060db2010-06-09 01:09:00 +0000891 bool define = Proto.find('i') != std::string::npos;
892
Nate Begeman22237772010-06-02 00:34:55 +0000893 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
894 assert(!Proto.empty() && "");
895
Nate Begeman7c8c8832010-06-02 21:53:00 +0000896 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000897 if (define)
898 OS << "#define";
899 else
900 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000901
Nate Begemane66aab52010-06-02 07:14:28 +0000902 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000903 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000904
Nate Begemane66aab52010-06-02 07:14:28 +0000905 // Function arguments
906 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000907
Nate Begemane66aab52010-06-02 07:14:28 +0000908 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000909 if (define)
910 OS << " ";
911 else
912 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000913
Nate Begemana8979a02010-06-04 00:21:41 +0000914 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000915 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000916 } else {
917 if (R->getSuperClasses().size() < 2)
918 throw TGError(R->getLoc(), "Builtin has no class kind");
919
920 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
921
922 if (ck == ClassNone)
923 throw TGError(R->getLoc(), "Builtin has no class kind");
924 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
925 }
Nate Begeman6c060db2010-06-09 01:09:00 +0000926 if (!define)
927 OS << " }";
928 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +0000929 }
930 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000931 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000932 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000933 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000934}
Nate Begemana8979a02010-06-04 00:21:41 +0000935
Nate Begeman918f8e42010-06-14 05:17:23 +0000936static unsigned RangeFromType(StringRef typestr) {
937 // base type to get the type string for.
938 bool quad = false, dummy = false;
939 char type = ClassifyType(typestr, quad, dummy, dummy);
940
941 switch (type) {
942 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +0000943 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000944 case 'h':
945 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +0000946 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000947 case 'f':
948 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +0000949 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000950 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +0000951 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +0000952 default:
953 throw "unhandled type!";
954 break;
955 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +0000956 assert(0 && "unreachable");
957 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +0000958}
959
Nate Begemanf8c4c272010-06-17 04:15:13 +0000960/// runHeader - Emit a file with sections defining:
961/// 1. the NEON section of BuiltinsARM.def.
962/// 2. the SemaChecking code for the type overload checking.
963/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +0000964void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +0000965 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
966
967 StringMap<OpKind> EmittedMap;
968
Nate Begemanf8c4c272010-06-17 04:15:13 +0000969 // Generate BuiltinsARM.def for NEON
970 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000971 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
972 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +0000973 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
974 if (k != OpNone)
975 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +0000976
Nate Begemanf8c4c272010-06-17 04:15:13 +0000977 std::string Proto = R->getValueAsString("Prototype");
978
Nate Begemand72c9002010-06-13 04:47:03 +0000979 // Functions with 'a' (the splat code) in the type prototype should not get
980 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +0000981 if (Proto.find('a') != std::string::npos)
982 continue;
Nate Begemand72c9002010-06-13 04:47:03 +0000983
Nate Begemanf8c4c272010-06-17 04:15:13 +0000984 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +0000985 SmallVector<StringRef, 16> TypeVec;
986 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +0000987
Nate Begeman73cef3e2010-06-04 01:26:15 +0000988 if (R->getSuperClasses().size() < 2)
989 throw TGError(R->getLoc(), "Builtin has no class kind");
990
Nate Begemanf8c4c272010-06-17 04:15:13 +0000991 std::string name = LowercaseString(R->getName());
Nate Begeman73cef3e2010-06-04 01:26:15 +0000992 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
993
994 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +0000995 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
996 // that each unique BUILTIN() macro appears only once in the output
997 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +0000998 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
999 if (EmittedMap.count(bd))
1000 continue;
1001
1002 EmittedMap[bd] = OpNone;
1003 OS << bd << "\n";
1004 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001005 }
1006 OS << "#endif\n\n";
1007
1008 // Generate the overloaded type checking code for SemaChecking.cpp
1009 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1010 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1011 Record *R = RV[i];
1012 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1013 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001014 continue;
Nate Begemanf8c4c272010-06-17 04:15:13 +00001015
1016 std::string Proto = R->getValueAsString("Prototype");
1017 std::string Types = R->getValueAsString("Types");
1018 std::string name = LowercaseString(R->getName());
1019
1020 // Functions with 'a' (the splat code) in the type prototype should not get
1021 // their own builtin as they use the non-splat variant.
1022 if (Proto.find('a') != std::string::npos)
1023 continue;
1024
1025 // Functions which have a scalar argument cannot be overloaded, no need to
1026 // check them if we are emitting the type checking code.
1027 if (Proto.find('s') != std::string::npos)
1028 continue;
1029
1030 SmallVector<StringRef, 16> TypeVec;
1031 ParseTypes(R, Types, TypeVec);
1032
1033 if (R->getSuperClasses().size() < 2)
1034 throw TGError(R->getLoc(), "Builtin has no class kind");
1035
1036 int si = -1, qi = -1;
1037 unsigned mask = 0, qmask = 0;
1038 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1039 // Generate the switch case(s) for this builtin for the type validation.
1040 bool quad = false, poly = false, usgn = false;
1041 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1042
1043 if (quad) {
1044 qi = ti;
1045 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1046 } else {
1047 si = ti;
1048 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1049 }
1050 }
1051 if (mask)
1052 OS << "case ARM::BI__builtin_neon_"
1053 << MangleName(name, TypeVec[si], ClassB)
1054 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1055 if (qmask)
1056 OS << "case ARM::BI__builtin_neon_"
1057 << MangleName(name, TypeVec[qi], ClassB)
1058 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1059 }
1060 OS << "#endif\n\n";
1061
1062 // Generate the intrinsic range checking code for shift/lane immediates.
1063 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1064 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1065 Record *R = RV[i];
1066
1067 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1068 if (k != OpNone)
1069 continue;
1070
1071 std::string name = LowercaseString(R->getName());
1072 std::string Proto = R->getValueAsString("Prototype");
1073 std::string Types = R->getValueAsString("Types");
1074
1075 // Functions with 'a' (the splat code) in the type prototype should not get
1076 // their own builtin as they use the non-splat variant.
1077 if (Proto.find('a') != std::string::npos)
1078 continue;
1079
1080 // Functions which do not have an immediate do not need to have range
1081 // checking code emitted.
1082 if (Proto.find('i') == std::string::npos)
1083 continue;
1084
1085 SmallVector<StringRef, 16> TypeVec;
1086 ParseTypes(R, Types, TypeVec);
1087
1088 if (R->getSuperClasses().size() < 2)
1089 throw TGError(R->getLoc(), "Builtin has no class kind");
1090
1091 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1092
1093 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1094 std::string namestr, shiftstr, rangestr;
1095
1096 // Builtins which are overloaded by type will need to have their upper
1097 // bound computed at Sema time based on the type constant.
1098 if (Proto.find('s') == std::string::npos) {
1099 ck = ClassB;
1100 if (R->getValueAsBit("isShift")) {
1101 shiftstr = ", true";
1102
1103 // Right shifts have an 'r' in the name, left shifts do not.
1104 if (name.find('r') != std::string::npos)
1105 rangestr = "l = 1; ";
1106 }
1107 rangestr += "u = RFT(TV" + shiftstr + ")";
1108 } else {
1109 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1110 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001111 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001112 namestr = MangleName(name, TypeVec[ti], ck);
1113 if (EmittedMap.count(namestr))
1114 continue;
1115 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001116
1117 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001118 unsigned immidx = 0;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001119
1120 // Builtins that return a struct of multiple vectors have an extra
1121 // leading arg for the struct return.
1122 if (Proto[0] == '2' || Proto[0] == '3' || Proto[0] == '4')
1123 ++immidx;
1124
1125 // Add one to the index for each argument until we reach the immediate
1126 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001127 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1128 switch (Proto[ii]) {
1129 default: immidx += 1; break;
1130 case '2': immidx += 2; break;
1131 case '3': immidx += 3; break;
1132 case '4': immidx += 4; break;
1133 case 'i': ie = ii + 1; break;
1134 }
1135 }
1136 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1137 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001138 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001139 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001140 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001141}