blob: d45910e0f1b42fabf3c52719bc72c1324ce36a23 [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';
Bob Wilsonb055f742010-11-23 19:38:34 +000092 default: throw "unhandled type in narrow!";
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;
Bob Wilson181b76d2010-11-18 21:43:22 +0000137 poly = false;
138 if (type == 'f')
139 type = 'i';
140 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000141 case 'x':
Bob Wilson181b76d2010-11-18 21:43:22 +0000142 usgn = false;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000143 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000144 if (type == 'f')
145 type = 'i';
146 break;
147 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000148 if (type == 'h')
149 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000150 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000151 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000152 break;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000153 case 'g':
154 quad = false;
155 break;
Nate Begeman22237772010-06-02 00:34:55 +0000156 case 'w':
157 type = Widen(type);
158 quad = true;
159 break;
160 case 'n':
161 type = Widen(type);
162 break;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000163 case 'i':
164 type = 'i';
165 scal = true;
166 break;
Nate Begeman22237772010-06-02 00:34:55 +0000167 case 'l':
168 type = 'l';
169 scal = true;
170 usgn = true;
171 break;
172 case 's':
Nate Begeman4b425a82010-06-10 00:16:56 +0000173 case 'a':
Nate Begeman22237772010-06-02 00:34:55 +0000174 scal = true;
175 break;
176 case 'k':
177 quad = true;
178 break;
179 case 'c':
180 cnst = true;
181 case 'p':
182 pntr = true;
183 scal = true;
184 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000185 case 'h':
186 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000187 if (type == 'h')
188 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000189 break;
190 case 'e':
191 type = Narrow(type);
192 usgn = true;
193 break;
Nate Begeman22237772010-06-02 00:34:55 +0000194 default:
195 break;
196 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000197 return type;
198}
199
Nate Begemand72c9002010-06-13 04:47:03 +0000200/// TypeString - for a modifier and type, generate the name of the typedef for
Bob Wilson6904d7a2010-11-16 19:39:14 +0000201/// that type. QUc -> uint8x8_t.
202static std::string TypeString(const char mod, StringRef typestr) {
Nate Begemanb0a4e452010-06-07 16:00:37 +0000203 bool quad = false;
204 bool poly = false;
205 bool usgn = false;
206 bool scal = false;
207 bool cnst = false;
208 bool pntr = false;
209
210 if (mod == 'v')
211 return "void";
212 if (mod == 'i')
213 return "int";
214
215 // base type to get the type string for.
216 char type = ClassifyType(typestr, quad, poly, usgn);
217
218 // Based on the modifying character, change the type and width if necessary.
219 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000220
221 SmallString<128> s;
222
223 if (usgn)
224 s.push_back('u');
225
226 switch (type) {
227 case 'c':
228 s += poly ? "poly8" : "int8";
229 if (scal)
230 break;
231 s += quad ? "x16" : "x8";
232 break;
233 case 's':
234 s += poly ? "poly16" : "int16";
235 if (scal)
236 break;
237 s += quad ? "x8" : "x4";
238 break;
239 case 'i':
240 s += "int32";
241 if (scal)
242 break;
243 s += quad ? "x4" : "x2";
244 break;
245 case 'l':
246 s += "int64";
247 if (scal)
248 break;
249 s += quad ? "x2" : "x1";
250 break;
251 case 'h':
252 s += "float16";
253 if (scal)
254 break;
255 s += quad ? "x8" : "x4";
256 break;
257 case 'f':
258 s += "float32";
259 if (scal)
260 break;
261 s += quad ? "x4" : "x2";
262 break;
Nate Begeman22237772010-06-02 00:34:55 +0000263 default:
264 throw "unhandled type!";
265 break;
266 }
267
268 if (mod == '2')
269 s += "x2";
270 if (mod == '3')
271 s += "x3";
272 if (mod == '4')
273 s += "x4";
274
275 // Append _t, finishing the type string typedef type.
276 s += "_t";
277
278 if (cnst)
279 s += " const";
280
281 if (pntr)
282 s += " *";
283
284 return s.str();
285}
286
Bob Wilson1ac27cf2010-06-24 22:04:30 +0000287/// BuiltinTypeString - for a modifier and type, generate the clang
288/// BuiltinsARM.def prototype code for the function. See the top of clang's
289/// Builtins.def for a description of the type strings.
Nate Begeman7c21f742010-06-04 21:36:00 +0000290static std::string BuiltinTypeString(const char mod, StringRef typestr,
291 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000292 bool quad = false;
293 bool poly = false;
294 bool usgn = false;
295 bool scal = false;
296 bool cnst = false;
297 bool pntr = false;
298
299 if (mod == 'v')
Bob Wilson95148ad2010-12-01 19:49:56 +0000300 return "v"; // void
Nate Begeman92f98af2010-06-04 07:11:25 +0000301 if (mod == 'i')
Bob Wilson95148ad2010-12-01 19:49:56 +0000302 return "i"; // int
Nate Begeman92f98af2010-06-04 07:11:25 +0000303
304 // base type to get the type string for.
305 char type = ClassifyType(typestr, quad, poly, usgn);
306
307 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000308 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
309
Bob Wilson95148ad2010-12-01 19:49:56 +0000310 // All pointers are void* pointers. Change type to 'v' now.
Nate Begemanc4a1b652010-06-20 21:09:52 +0000311 if (pntr) {
312 usgn = false;
313 poly = false;
Nate Begemanb0a4e452010-06-07 16:00:37 +0000314 type = 'v';
Nate Begemanc4a1b652010-06-20 21:09:52 +0000315 }
Bob Wilson95148ad2010-12-01 19:49:56 +0000316 // Treat half-float ('h') types as unsigned short ('s') types.
Nate Begeman92f98af2010-06-04 07:11:25 +0000317 if (type == 'h') {
318 type = 's';
319 usgn = true;
320 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000321 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000322
323 if (scal) {
324 SmallString<128> s;
325
326 if (usgn)
327 s.push_back('U');
Bob Wilson317bafb2010-12-02 00:24:59 +0000328 else if (type == 'c')
329 s.push_back('S'); // make chars explicitly signed
Nate Begeman7c21f742010-06-04 21:36:00 +0000330
Bob Wilson95148ad2010-12-01 19:49:56 +0000331 if (type == 'l') // 64-bit long
Nate Begeman7c21f742010-06-04 21:36:00 +0000332 s += "LLi";
333 else
334 s.push_back(type);
335
Nate Begeman92f98af2010-06-04 07:11:25 +0000336 if (cnst)
337 s.push_back('C');
338 if (pntr)
339 s.push_back('*');
340 return s.str();
341 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000342
343 // Since the return value must be one type, return a vector type of the
Nate Begemanc4a1b652010-06-20 21:09:52 +0000344 // appropriate width which we will bitcast. An exception is made for
345 // returning structs of 2, 3, or 4 vectors which are returned in a sret-like
346 // fashion, storing them to a pointer arg.
Nate Begeman7c21f742010-06-04 21:36:00 +0000347 if (ret) {
Bob Wilson5b7fe592010-12-01 19:49:51 +0000348 if (mod >= '2' && mod <= '4')
Bob Wilson95148ad2010-12-01 19:49:56 +0000349 return "vv*"; // void result with void* first argument
Nate Begemanf50551e2010-06-09 18:02:26 +0000350 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman56387832010-06-08 06:01:16 +0000351 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000352 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000353 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000354 if (ck != ClassB && type == 'i')
Nate Begeman56387832010-06-08 06:01:16 +0000355 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000356 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000357 return quad ? "V2LLi" : "V1LLi";
Nate Begeman900f4672010-06-08 00:14:42 +0000358
Bob Wilson317bafb2010-12-02 00:24:59 +0000359 return quad ? "V16Sc" : "V8Sc";
Nate Begeman7c21f742010-06-04 21:36:00 +0000360 }
361
362 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000363 if (mod == '2')
Bob Wilson317bafb2010-12-02 00:24:59 +0000364 return quad ? "V16ScV16Sc" : "V8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000365 if (mod == '3')
Bob Wilson317bafb2010-12-02 00:24:59 +0000366 return quad ? "V16ScV16ScV16Sc" : "V8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000367 if (mod == '4')
Bob Wilson317bafb2010-12-02 00:24:59 +0000368 return quad ? "V16ScV16ScV16ScV16Sc" : "V8ScV8ScV8ScV8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000369
Nate Begemanf50551e2010-06-09 18:02:26 +0000370 if (mod == 'f' || (ck != ClassB && type == 'f'))
Nate Begeman007afe42010-06-09 05:11:55 +0000371 return quad ? "V4f" : "V2f";
Nate Begemanf50551e2010-06-09 18:02:26 +0000372 if (ck != ClassB && type == 's')
Nate Begeman007afe42010-06-09 05:11:55 +0000373 return quad ? "V8s" : "V4s";
Nate Begemanf50551e2010-06-09 18:02:26 +0000374 if (ck != ClassB && type == 'i')
Nate Begeman007afe42010-06-09 05:11:55 +0000375 return quad ? "V4i" : "V2i";
Nate Begemanf50551e2010-06-09 18:02:26 +0000376 if (ck != ClassB && type == 'l')
Nate Begeman007afe42010-06-09 05:11:55 +0000377 return quad ? "V2LLi" : "V1LLi";
378
Bob Wilson317bafb2010-12-02 00:24:59 +0000379 return quad ? "V16Sc" : "V8Sc";
Nate Begeman92f98af2010-06-04 07:11:25 +0000380}
381
Nate Begemand72c9002010-06-13 04:47:03 +0000382/// MangleName - Append a type or width suffix to a base neon function name,
383/// and insert a 'q' in the appropriate location if the operation works on
384/// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000385static std::string MangleName(const std::string &name, StringRef typestr,
386 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000387 if (name == "vcvt_f32_f16")
388 return name;
389
Nate Begemanaf905ef2010-06-02 06:17:19 +0000390 bool quad = false;
391 bool poly = false;
392 bool usgn = false;
393 char type = ClassifyType(typestr, quad, poly, usgn);
394
395 std::string s = name;
396
397 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000398 case 'c':
399 switch (ck) {
400 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
401 case ClassI: s += "_i8"; break;
402 case ClassW: s += "_8"; break;
403 default: break;
404 }
405 break;
406 case 's':
407 switch (ck) {
408 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
409 case ClassI: s += "_i16"; break;
410 case ClassW: s += "_16"; break;
411 default: break;
412 }
413 break;
414 case 'i':
415 switch (ck) {
416 case ClassS: s += usgn ? "_u32" : "_s32"; break;
417 case ClassI: s += "_i32"; break;
418 case ClassW: s += "_32"; break;
419 default: break;
420 }
421 break;
422 case 'l':
423 switch (ck) {
424 case ClassS: s += usgn ? "_u64" : "_s64"; break;
425 case ClassI: s += "_i64"; break;
426 case ClassW: s += "_64"; break;
427 default: break;
428 }
429 break;
430 case 'h':
431 switch (ck) {
432 case ClassS:
433 case ClassI: s += "_f16"; break;
434 case ClassW: s += "_16"; break;
435 default: break;
436 }
437 break;
438 case 'f':
439 switch (ck) {
440 case ClassS:
441 case ClassI: s += "_f32"; break;
442 case ClassW: s += "_32"; break;
443 default: break;
444 }
445 break;
446 default:
447 throw "unhandled type!";
448 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000449 }
Nate Begemana8979a02010-06-04 00:21:41 +0000450 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000451 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000452
Nate Begemanaf905ef2010-06-02 06:17:19 +0000453 // Insert a 'q' before the first '_' character so that it ends up before
454 // _lane or _n on vector-scalar operations.
455 if (quad) {
456 size_t pos = s.find('_');
457 s = s.insert(pos, "q");
458 }
459 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000460}
461
Nate Begemanaf905ef2010-06-02 06:17:19 +0000462// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000463static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000464 bool define = proto.find('i') != std::string::npos;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000465 char arg = 'a';
466
467 std::string s;
468 s += "(";
469
470 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman6c060db2010-06-09 01:09:00 +0000471 if (!define) {
472 s += TypeString(proto[i], typestr);
473 s.push_back(' ');
474 }
Nate Begemanaf905ef2010-06-02 06:17:19 +0000475 s.push_back(arg);
476 if ((i + 1) < e)
477 s += ", ";
478 }
479
480 s += ")";
481 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000482}
483
Bob Wilson377296e2010-12-02 07:10:39 +0000484// Generate the local temporaries used to provide type checking for macro
485// arguments.
486static std::string GenMacroLocals(const std::string &proto, StringRef typestr) {
487 char arg = 'a';
488 std::string s;
489
490 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
491 // Do not create a temporary for an immediate argument.
492 // That would defeat the whole point of using a macro!
493 if (proto[i] == 'i') continue;
494
495 s += TypeString(proto[i], typestr) + " __";
496 s.push_back(arg);
497 s += " = (";
498 s.push_back(arg);
499 s += "); ";
500 }
501
502 s += "\\\n ";
503 return s;
504}
505
Nate Begemancc3c41a2010-06-12 03:09:49 +0000506static std::string Duplicate(unsigned nElts, StringRef typestr,
507 const std::string &a) {
Nate Begeman4b425a82010-06-10 00:16:56 +0000508 std::string s;
509
Bob Wilson6904d7a2010-11-16 19:39:14 +0000510 s = "(" + TypeString('d', typestr) + "){ ";
Nate Begeman4b425a82010-06-10 00:16:56 +0000511 for (unsigned i = 0; i != nElts; ++i) {
512 s += a;
513 if ((i + 1) < nElts)
514 s += ", ";
515 }
516 s += " }";
517
518 return s;
519}
520
Bob Wilsonb308b622010-11-16 23:57:01 +0000521static unsigned GetNumElements(StringRef typestr, bool &quad) {
522 quad = false;
523 bool dummy = false;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000524 char type = ClassifyType(typestr, quad, dummy, dummy);
525 unsigned nElts = 0;
526 switch (type) {
Bob Wilsonb308b622010-11-16 23:57:01 +0000527 case 'c': nElts = 8; break;
528 case 's': nElts = 4; break;
529 case 'i': nElts = 2; break;
530 case 'l': nElts = 1; break;
531 case 'h': nElts = 4; break;
532 case 'f': nElts = 2; break;
533 default:
534 throw "unhandled type!";
535 break;
Nate Begemancc3c41a2010-06-12 03:09:49 +0000536 }
Bob Wilsonb308b622010-11-16 23:57:01 +0000537 if (quad) nElts <<= 1;
538 return nElts;
539}
540
541// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
542static std::string GenOpString(OpKind op, const std::string &proto,
543 StringRef typestr) {
544 bool quad;
545 unsigned nElts = GetNumElements(typestr, quad);
Nate Begemancc3c41a2010-06-12 03:09:49 +0000546
Nate Begeman4b425a82010-06-10 00:16:56 +0000547 std::string ts = TypeString(proto[0], typestr);
Bob Wilsonee9ca072010-09-15 01:52:33 +0000548 std::string s;
549 if (op == OpHi || op == OpLo) {
550 s = "union { " + ts + " r; double d; } u; u.d";
551 } else {
552 s = ts + " r; r";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000553 }
Nate Begeman900f4672010-06-08 00:14:42 +0000554
555 s += " = ";
556
Nate Begeman162d3ba2010-06-03 04:04:09 +0000557 switch(op) {
558 case OpAdd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000559 s += "a + b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000560 break;
561 case OpSub:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000562 s += "a - b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000563 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000564 case OpMulN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000565 s += "a * " + Duplicate(nElts, typestr, "b");
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000566 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000567 case OpMul:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000568 s += "a * b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000569 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000570 case OpMlaN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000571 s += "a + (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000572 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000573 case OpMla:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000574 s += "a + (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000575 break;
Nate Begeman4b425a82010-06-10 00:16:56 +0000576 case OpMlsN:
Bob Wilsonb308b622010-11-16 23:57:01 +0000577 s += "a - (b * " + Duplicate(nElts, typestr, "c") + ")";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000578 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000579 case OpMls:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000580 s += "a - (b * c)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000581 break;
582 case OpEq:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000583 s += "(" + ts + ")(a == b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000584 break;
585 case OpGe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000586 s += "(" + ts + ")(a >= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000587 break;
588 case OpLe:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000589 s += "(" + ts + ")(a <= b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000590 break;
591 case OpGt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000592 s += "(" + ts + ")(a > b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000593 break;
594 case OpLt:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000595 s += "(" + ts + ")(a < b)";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000596 break;
597 case OpNeg:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000598 s += " -a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000599 break;
600 case OpNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000601 s += " ~a";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000602 break;
603 case OpAnd:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000604 s += "a & b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000605 break;
606 case OpOr:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000607 s += "a | b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000608 break;
609 case OpXor:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000610 s += "a ^ b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000611 break;
612 case OpAndNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000613 s += "a & ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000614 break;
615 case OpOrNot:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000616 s += "a | ~b";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000617 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000618 case OpCast:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000619 s += "(" + ts + ")a";
Nate Begeman3861e742010-06-03 21:35:22 +0000620 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000621 case OpConcat:
Bob Wilsonb322fc22010-12-02 01:18:18 +0000622 s += "(" + ts + ")__builtin_shufflevector((int64x1_t)a";
Bob Wilson6904d7a2010-11-16 19:39:14 +0000623 s += ", (int64x1_t)b, 0, 1)";
Nate Begeman900f4672010-06-08 00:14:42 +0000624 break;
Nate Begeman6c060db2010-06-09 01:09:00 +0000625 case OpHi:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000626 s += "(((float64x2_t)a)[1])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000627 break;
628 case OpLo:
Bob Wilson6904d7a2010-11-16 19:39:14 +0000629 s += "(((float64x2_t)a)[0])";
Nate Begeman6c060db2010-06-09 01:09:00 +0000630 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000631 case OpDup:
Bob Wilsonb308b622010-11-16 23:57:01 +0000632 s += Duplicate(nElts, typestr, "a");
Nate Begemancc3c41a2010-06-12 03:09:49 +0000633 break;
634 case OpSelect:
635 // ((0 & 1) | (~0 & 2))
Bob Wilson1dbfa912010-12-02 01:18:20 +0000636 s += "(" + ts + ")";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000637 ts = TypeString(proto[1], typestr);
Bob Wilson1dbfa912010-12-02 01:18:20 +0000638 s += "((a & (" + ts + ")b) | ";
639 s += "(~a & (" + ts + ")c))";
Nate Begemancc3c41a2010-06-12 03:09:49 +0000640 break;
641 case OpRev16:
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000642 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000643 for (unsigned i = 2; i <= nElts; i += 2)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000644 for (unsigned j = 0; j != 2; ++j)
645 s += ", " + utostr(i - j - 1);
646 s += ")";
647 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000648 case OpRev32: {
649 unsigned WordElts = nElts >> (1 + (int)quad);
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000650 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000651 for (unsigned i = WordElts; i <= nElts; i += WordElts)
652 for (unsigned j = 0; j != WordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000653 s += ", " + utostr(i - j - 1);
654 s += ")";
655 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000656 }
657 case OpRev64: {
658 unsigned DblWordElts = nElts >> (int)quad;
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000659 s += "__builtin_shufflevector(a, a";
Bob Wilsonb308b622010-11-16 23:57:01 +0000660 for (unsigned i = DblWordElts; i <= nElts; i += DblWordElts)
661 for (unsigned j = 0; j != DblWordElts; ++j)
Nate Begemancc3c41a2010-06-12 03:09:49 +0000662 s += ", " + utostr(i - j - 1);
663 s += ")";
Nate Begeman900f4672010-06-08 00:14:42 +0000664 break;
Bob Wilsonb308b622010-11-16 23:57:01 +0000665 }
Nate Begeman162d3ba2010-06-03 04:04:09 +0000666 default:
667 throw "unknown OpKind!";
668 break;
669 }
Bob Wilsonee9ca072010-09-15 01:52:33 +0000670 if (op == OpHi || op == OpLo)
671 s += "; return u.r;";
672 else
673 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000674 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000675}
676
Nate Begemanb0a4e452010-06-07 16:00:37 +0000677static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
678 unsigned mod = proto[0];
679 unsigned ret = 0;
680
Nate Begeman900f4672010-06-08 00:14:42 +0000681 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000682 mod = proto[1];
683
684 bool quad = false;
685 bool poly = false;
686 bool usgn = false;
687 bool scal = false;
688 bool cnst = false;
689 bool pntr = false;
690
Nate Begeman59d70cb2010-08-06 01:24:11 +0000691 // Base type to get the type string for.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000692 char type = ClassifyType(typestr, quad, poly, usgn);
693
694 // Based on the modifying character, change the type and width if necessary.
695 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman59d70cb2010-08-06 01:24:11 +0000696
Nate Begemanb0a4e452010-06-07 16:00:37 +0000697 if (usgn)
698 ret |= 0x08;
Nate Begeman59d70cb2010-08-06 01:24:11 +0000699 if (quad && proto[1] != 'g')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000700 ret |= 0x10;
701
702 switch (type) {
703 case 'c':
704 ret |= poly ? 5 : 0;
705 break;
706 case 's':
707 ret |= poly ? 6 : 1;
708 break;
709 case 'i':
710 ret |= 2;
711 break;
712 case 'l':
713 ret |= 3;
714 break;
715 case 'h':
716 ret |= 7;
717 break;
718 case 'f':
719 ret |= 4;
720 break;
721 default:
722 throw "unhandled type!";
723 break;
724 }
725 return ret;
726}
727
Nate Begeman7c8c8832010-06-02 21:53:00 +0000728// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000729static std::string GenBuiltin(const std::string &name, const std::string &proto,
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000730 StringRef typestr, ClassKind ck) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000731 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000732
Nate Begemanc4a1b652010-06-20 21:09:52 +0000733 // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit
734 // sret-like argument.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000735 bool sret = (proto[0] >= '2' && proto[0] <= '4');
Nate Begemanc4a1b652010-06-20 21:09:52 +0000736
737 // If this builtin takes an immediate argument, we need to #define it rather
738 // than use a standard declaration, so that SemaChecking can range check
739 // the immediate passed by the user.
Nate Begeman6c060db2010-06-09 01:09:00 +0000740 bool define = proto.find('i') != std::string::npos;
Nate Begeman9e584b32010-06-04 22:53:30 +0000741
Bob Wilson95148ad2010-12-01 19:49:56 +0000742 // Check if the prototype has a scalar operand with the type of the vector
743 // elements. If not, bitcasting the args will take care of arg checking.
744 // The actual signedness etc. will be taken care of with special enums.
Nate Begeman9e584b32010-06-04 22:53:30 +0000745 if (proto.find('s') == std::string::npos)
746 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000747
Bob Wilson377296e2010-12-02 07:10:39 +0000748 // Macro arguments are not type-checked like inline function arguments, so
749 // assign them to local temporaries to get the right type checking.
750 if (define)
751 s += GenMacroLocals(proto, typestr);
752
Nate Begeman162d3ba2010-06-03 04:04:09 +0000753 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000754 std::string ts = TypeString(proto[0], typestr);
755
756 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000757 if (sret)
Bob Wilson052008b2010-12-02 02:42:51 +0000758 s += ts + " r; ";
Bob Wilsone106bc12010-12-02 00:24:56 +0000759 else
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000760 s += "(" + ts + ")";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000761 } else if (sret) {
762 s += ts + " r; ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000763 } else {
Bob Wilsonc79528a2010-12-02 01:18:15 +0000764 s += ts + " r; r = (" + ts + ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000765 }
Nate Begeman4b425a82010-06-10 00:16:56 +0000766 }
767
768 bool splat = proto.find('a') != std::string::npos;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000769
770 s += "__builtin_neon_";
Nate Begeman4b425a82010-06-10 00:16:56 +0000771 if (splat) {
Bob Wilson95148ad2010-12-01 19:49:56 +0000772 // Call the non-splat builtin: chop off the "_n" suffix from the name.
Nate Begeman4b425a82010-06-10 00:16:56 +0000773 std::string vname(name, 0, name.size()-2);
774 s += MangleName(vname, typestr, ck);
775 } else {
776 s += MangleName(name, typestr, ck);
777 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000778 s += "(";
Nate Begemanc4a1b652010-06-20 21:09:52 +0000779
780 // Pass the address of the return variable as the first argument to sret-like
781 // builtins.
782 if (sret)
783 s += "&r, ";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000784
Bob Wilson377296e2010-12-02 07:10:39 +0000785 char arg = 'a';
Nate Begeman7c8c8832010-06-02 21:53:00 +0000786 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begemancc3c41a2010-06-12 03:09:49 +0000787 std::string args = std::string(&arg, 1);
Bob Wilson95148ad2010-12-01 19:49:56 +0000788
Bob Wilson377296e2010-12-02 07:10:39 +0000789 // For macros, use the local temporaries instead of the macro arguments.
790 if (define && proto[i] != 'i')
791 args = "__" + args;
Bob Wilsonf00140c2010-12-01 19:49:58 +0000792
793 bool argQuad = false;
794 bool argPoly = false;
795 bool argUsgn = false;
796 bool argScalar = false;
797 bool dummy = false;
798 char argType = ClassifyType(typestr, argQuad, argPoly, argUsgn);
799 argType = ModType(proto[i], argType, argQuad, argPoly, argUsgn, argScalar,
800 dummy, dummy);
801
Nate Begeman9e584b32010-06-04 22:53:30 +0000802 // Handle multiple-vector values specially, emitting each subvector as an
803 // argument to the __builtin.
Bob Wilson5b7fe592010-12-01 19:49:51 +0000804 if (proto[i] >= '2' && proto[i] <= '4') {
Bob Wilsonf00140c2010-12-01 19:49:58 +0000805 // Check if an explicit cast is needed.
806 if (argType != 'c' || argPoly || argUsgn)
807 args = (argQuad ? "(int8x16_t)" : "(int8x8_t)") + args;
808
Nate Begeman9e584b32010-06-04 22:53:30 +0000809 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000810 s += args + ".val[" + utostr(vi) + "]";
Nate Begeman9e584b32010-06-04 22:53:30 +0000811 if ((vi + 1) < ve)
812 s += ", ";
813 }
814 if ((i + 1) < e)
815 s += ", ";
816
817 continue;
818 }
819
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000820 if (splat && (i + 1) == e)
821 args = Duplicate(GetNumElements(typestr, argQuad), typestr, args);
822
Bob Wilsonf00140c2010-12-01 19:49:58 +0000823 // Check if an explicit cast is needed.
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000824 if ((splat || !argScalar) &&
Bob Wilsonf00140c2010-12-01 19:49:58 +0000825 ((ck == ClassB && argType != 'c') || argPoly || argUsgn)) {
826 std::string argTypeStr = "c";
827 if (ck != ClassB)
828 argTypeStr = argType;
829 if (argQuad)
830 argTypeStr = "Q" + argTypeStr;
831 args = "(" + TypeString('d', argTypeStr) + ")" + args;
832 }
833
Bob Wilson4a6c7fc2010-12-02 01:18:23 +0000834 s += args;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000835 if ((i + 1) < e)
836 s += ", ";
837 }
838
Nate Begeman9e584b32010-06-04 22:53:30 +0000839 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000840 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000841 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000842
Bob Wilson052008b2010-12-02 02:42:51 +0000843 s += ");";
Nate Begeman9e584b32010-06-04 22:53:30 +0000844
845 if (proto[0] != 'v') {
Nate Begeman6c060db2010-06-09 01:09:00 +0000846 if (define) {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000847 if (sret)
Bob Wilson052008b2010-12-02 02:42:51 +0000848 s += " r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000849 } else {
Nate Begemanc4a1b652010-06-20 21:09:52 +0000850 s += " return r;";
Nate Begeman6c060db2010-06-09 01:09:00 +0000851 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000852 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000853 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000854}
855
Nate Begeman73cef3e2010-06-04 01:26:15 +0000856static std::string GenBuiltinDef(const std::string &name,
857 const std::string &proto,
858 StringRef typestr, ClassKind ck) {
859 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000860
861 // If all types are the same size, bitcasting the args will take care
862 // of arg checking. The actual signedness etc. will be taken care of with
863 // special enums.
864 if (proto.find('s') == std::string::npos)
865 ck = ClassB;
866
Nate Begeman73cef3e2010-06-04 01:26:15 +0000867 s += MangleName(name, typestr, ck);
868 s += ", \"";
869
Nate Begeman92f98af2010-06-04 07:11:25 +0000870 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000871 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
872
873 // Extra constant integer to hold type class enum for this function, e.g. s8
874 if (ck == ClassB)
875 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000876
877 s += "\", \"n\")";
878 return s;
879}
880
Nate Begemand72c9002010-06-13 04:47:03 +0000881/// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h
882/// is comprised of type definitions and function declarations.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000883void NeonEmitter::run(raw_ostream &OS) {
884 EmitSourceFileHeader("ARM NEON Header", OS);
885
886 // FIXME: emit license into file?
887
888 OS << "#ifndef __ARM_NEON_H\n";
889 OS << "#define __ARM_NEON_H\n\n";
890
891 OS << "#ifndef __ARM_NEON__\n";
892 OS << "#error \"NEON support not enabled\"\n";
893 OS << "#endif\n\n";
894
895 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000896
897 // Emit NEON-specific scalar typedefs.
Nate Begeman7c8c8832010-06-02 21:53:00 +0000898 OS << "typedef float float32_t;\n";
Bob Wilson4fbf6382010-11-16 23:57:03 +0000899 OS << "typedef int8_t poly8_t;\n";
900 OS << "typedef int16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000901 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000902
Nate Begeman7c8c8832010-06-02 21:53:00 +0000903 // Emit Neon vector typedefs.
904 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
905 SmallVector<StringRef, 24> TDTypeVec;
906 ParseTypes(0, TypedefTypes, TDTypeVec);
907
908 // Emit vector typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000909 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000910 bool dummy, quad = false, poly = false;
911 (void) ClassifyType(TDTypeVec[i], quad, poly, dummy);
912 if (poly)
913 OS << "typedef __attribute__((neon_polyvector_type(";
914 else
915 OS << "typedef __attribute__((neon_vector_type(";
Nate Begeman9e584b32010-06-04 22:53:30 +0000916
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000917 unsigned nElts = GetNumElements(TDTypeVec[i], quad);
918 OS << utostr(nElts) << "))) ";
919 if (nElts < 10)
Bob Wilson6904d7a2010-11-16 19:39:14 +0000920 OS << " ";
Nate Begeman9e584b32010-06-04 22:53:30 +0000921
Bob Wilson6904d7a2010-11-16 19:39:14 +0000922 OS << TypeString('s', TDTypeVec[i]);
923 OS << " " << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000924 }
925 OS << "\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000926 OS << "typedef __attribute__((__vector_size__(8))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000927 "double float64x1_t;\n";
Bob Wilson6a8aceb2010-11-16 23:57:06 +0000928 OS << "typedef __attribute__((__vector_size__(16))) "
Bob Wilson6904d7a2010-11-16 19:39:14 +0000929 "double float64x2_t;\n";
Bob Wilsonee9ca072010-09-15 01:52:33 +0000930 OS << "\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000931
932 // Emit struct typedefs.
Bob Wilson6904d7a2010-11-16 19:39:14 +0000933 for (unsigned vi = 2; vi != 5; ++vi) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000934 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
Bob Wilson6904d7a2010-11-16 19:39:14 +0000935 std::string ts = TypeString('d', TDTypeVec[i]);
936 std::string vs = TypeString('0' + vi, TDTypeVec[i]);
937 OS << "typedef struct " << vs << " {\n";
938 OS << " " << ts << " val";
939 OS << "[" << utostr(vi) << "]";
940 OS << ";\n} ";
Bob Wilsonf18dfec2010-11-16 19:16:06 +0000941 OS << vs << ";\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000942 }
943 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000944
Nate Begeman7c8c8832010-06-02 21:53:00 +0000945 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
946
Nate Begeman5ddb0872010-05-28 01:08:32 +0000947 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
948
Nate Begeman22237772010-06-02 00:34:55 +0000949 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000950 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
951 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000952 std::string name = LowercaseString(R->getName());
953 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000954 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000955
956 SmallVector<StringRef, 16> TypeVec;
957 ParseTypes(R, Types, TypeVec);
958
Nate Begeman162d3ba2010-06-03 04:04:09 +0000959 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000960
Nate Begeman6c060db2010-06-09 01:09:00 +0000961 bool define = Proto.find('i') != std::string::npos;
962
Nate Begeman22237772010-06-02 00:34:55 +0000963 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
964 assert(!Proto.empty() && "");
965
Nate Begeman7c8c8832010-06-02 21:53:00 +0000966 // static always inline + return type
Nate Begeman6c060db2010-06-09 01:09:00 +0000967 if (define)
968 OS << "#define";
969 else
970 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000971
Nate Begemane66aab52010-06-02 07:14:28 +0000972 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000973 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000974
Nate Begemane66aab52010-06-02 07:14:28 +0000975 // Function arguments
976 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000977
Nate Begemane66aab52010-06-02 07:14:28 +0000978 // Definition.
Nate Begeman6c060db2010-06-09 01:09:00 +0000979 if (define)
Bob Wilson052008b2010-12-02 02:42:51 +0000980 OS << " __extension__ ({ \\\n ";
Nate Begeman6c060db2010-06-09 01:09:00 +0000981 else
Bob Wilson052008b2010-12-02 02:42:51 +0000982 OS << " { \\\n ";
Nate Begeman22237772010-06-02 00:34:55 +0000983
Nate Begemana8979a02010-06-04 00:21:41 +0000984 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000985 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000986 } else {
987 if (R->getSuperClasses().size() < 2)
988 throw TGError(R->getLoc(), "Builtin has no class kind");
989
990 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
991
992 if (ck == ClassNone)
993 throw TGError(R->getLoc(), "Builtin has no class kind");
994 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
995 }
Bob Wilson052008b2010-12-02 02:42:51 +0000996 if (define)
997 OS << " })";
998 else
Nate Begeman6c060db2010-06-09 01:09:00 +0000999 OS << " }";
1000 OS << "\n";
Nate Begeman22237772010-06-02 00:34:55 +00001001 }
1002 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001003 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001004 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +00001005 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +00001006}
Nate Begemana8979a02010-06-04 00:21:41 +00001007
Nate Begeman918f8e42010-06-14 05:17:23 +00001008static unsigned RangeFromType(StringRef typestr) {
1009 // base type to get the type string for.
1010 bool quad = false, dummy = false;
1011 char type = ClassifyType(typestr, quad, dummy, dummy);
1012
1013 switch (type) {
1014 case 'c':
Nate Begeman4da883a2010-06-15 22:10:31 +00001015 return (8 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001016 case 'h':
1017 case 's':
Nate Begeman4da883a2010-06-15 22:10:31 +00001018 return (4 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001019 case 'f':
1020 case 'i':
Nate Begeman4da883a2010-06-15 22:10:31 +00001021 return (2 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001022 case 'l':
Nate Begeman4da883a2010-06-15 22:10:31 +00001023 return (1 << (int)quad) - 1;
Nate Begeman918f8e42010-06-14 05:17:23 +00001024 default:
1025 throw "unhandled type!";
1026 break;
1027 }
Bob Wilsonfdb530d2010-07-28 18:21:10 +00001028 assert(0 && "unreachable");
1029 return 0;
Nate Begeman918f8e42010-06-14 05:17:23 +00001030}
1031
Nate Begemanf8c4c272010-06-17 04:15:13 +00001032/// runHeader - Emit a file with sections defining:
1033/// 1. the NEON section of BuiltinsARM.def.
1034/// 2. the SemaChecking code for the type overload checking.
1035/// 3. the SemaChecking code for validation of intrinsic immedate arguments.
Nate Begemana8979a02010-06-04 00:21:41 +00001036void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +00001037 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
1038
1039 StringMap<OpKind> EmittedMap;
1040
Nate Begemanf8c4c272010-06-17 04:15:13 +00001041 // Generate BuiltinsARM.def for NEON
1042 OS << "#ifdef GET_NEON_BUILTINS\n";
Nate Begeman73cef3e2010-06-04 01:26:15 +00001043 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1044 Record *R = RV[i];
Nate Begeman73cef3e2010-06-04 01:26:15 +00001045 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1046 if (k != OpNone)
1047 continue;
Nate Begeman73cef3e2010-06-04 01:26:15 +00001048
Nate Begemanf8c4c272010-06-17 04:15:13 +00001049 std::string Proto = R->getValueAsString("Prototype");
1050
Nate Begemand72c9002010-06-13 04:47:03 +00001051 // Functions with 'a' (the splat code) in the type prototype should not get
1052 // their own builtin as they use the non-splat variant.
Nate Begeman4b425a82010-06-10 00:16:56 +00001053 if (Proto.find('a') != std::string::npos)
1054 continue;
Nate Begemand72c9002010-06-13 04:47:03 +00001055
Nate Begemanf8c4c272010-06-17 04:15:13 +00001056 std::string Types = R->getValueAsString("Types");
Nate Begeman73cef3e2010-06-04 01:26:15 +00001057 SmallVector<StringRef, 16> TypeVec;
1058 ParseTypes(R, Types, TypeVec);
Nate Begemand72c9002010-06-13 04:47:03 +00001059
Nate Begeman73cef3e2010-06-04 01:26:15 +00001060 if (R->getSuperClasses().size() < 2)
1061 throw TGError(R->getLoc(), "Builtin has no class kind");
1062
Nate Begemanf8c4c272010-06-17 04:15:13 +00001063 std::string name = LowercaseString(R->getName());
Nate Begeman73cef3e2010-06-04 01:26:15 +00001064 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1065
1066 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
Nate Begemand72c9002010-06-13 04:47:03 +00001067 // Generate the BuiltinsARM.def declaration for this builtin, ensuring
1068 // that each unique BUILTIN() macro appears only once in the output
1069 // stream.
Nate Begeman73cef3e2010-06-04 01:26:15 +00001070 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
1071 if (EmittedMap.count(bd))
1072 continue;
1073
1074 EmittedMap[bd] = OpNone;
1075 OS << bd << "\n";
1076 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001077 }
1078 OS << "#endif\n\n";
1079
1080 // Generate the overloaded type checking code for SemaChecking.cpp
1081 OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n";
1082 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1083 Record *R = RV[i];
1084 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1085 if (k != OpNone)
Nate Begemand72c9002010-06-13 04:47:03 +00001086 continue;
Nate Begemanf8c4c272010-06-17 04:15:13 +00001087
1088 std::string Proto = R->getValueAsString("Prototype");
1089 std::string Types = R->getValueAsString("Types");
1090 std::string name = LowercaseString(R->getName());
1091
1092 // Functions with 'a' (the splat code) in the type prototype should not get
1093 // their own builtin as they use the non-splat variant.
1094 if (Proto.find('a') != std::string::npos)
1095 continue;
1096
1097 // Functions which have a scalar argument cannot be overloaded, no need to
1098 // check them if we are emitting the type checking code.
1099 if (Proto.find('s') != std::string::npos)
1100 continue;
1101
1102 SmallVector<StringRef, 16> TypeVec;
1103 ParseTypes(R, Types, TypeVec);
1104
1105 if (R->getSuperClasses().size() < 2)
1106 throw TGError(R->getLoc(), "Builtin has no class kind");
1107
1108 int si = -1, qi = -1;
1109 unsigned mask = 0, qmask = 0;
1110 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1111 // Generate the switch case(s) for this builtin for the type validation.
1112 bool quad = false, poly = false, usgn = false;
1113 (void) ClassifyType(TypeVec[ti], quad, poly, usgn);
1114
1115 if (quad) {
1116 qi = ti;
1117 qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1118 } else {
1119 si = ti;
1120 mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]);
1121 }
1122 }
1123 if (mask)
1124 OS << "case ARM::BI__builtin_neon_"
1125 << MangleName(name, TypeVec[si], ClassB)
1126 << ": mask = " << "0x" << utohexstr(mask) << "; break;\n";
1127 if (qmask)
1128 OS << "case ARM::BI__builtin_neon_"
1129 << MangleName(name, TypeVec[qi], ClassB)
1130 << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n";
1131 }
1132 OS << "#endif\n\n";
1133
1134 // Generate the intrinsic range checking code for shift/lane immediates.
1135 OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n";
1136 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
1137 Record *R = RV[i];
1138
1139 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
1140 if (k != OpNone)
1141 continue;
1142
1143 std::string name = LowercaseString(R->getName());
1144 std::string Proto = R->getValueAsString("Prototype");
1145 std::string Types = R->getValueAsString("Types");
1146
1147 // Functions with 'a' (the splat code) in the type prototype should not get
1148 // their own builtin as they use the non-splat variant.
1149 if (Proto.find('a') != std::string::npos)
1150 continue;
1151
1152 // Functions which do not have an immediate do not need to have range
1153 // checking code emitted.
1154 if (Proto.find('i') == std::string::npos)
1155 continue;
1156
1157 SmallVector<StringRef, 16> TypeVec;
1158 ParseTypes(R, Types, TypeVec);
1159
1160 if (R->getSuperClasses().size() < 2)
1161 throw TGError(R->getLoc(), "Builtin has no class kind");
1162
1163 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
1164
1165 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
1166 std::string namestr, shiftstr, rangestr;
1167
1168 // Builtins which are overloaded by type will need to have their upper
1169 // bound computed at Sema time based on the type constant.
1170 if (Proto.find('s') == std::string::npos) {
1171 ck = ClassB;
1172 if (R->getValueAsBit("isShift")) {
1173 shiftstr = ", true";
1174
1175 // Right shifts have an 'r' in the name, left shifts do not.
1176 if (name.find('r') != std::string::npos)
1177 rangestr = "l = 1; ";
1178 }
1179 rangestr += "u = RFT(TV" + shiftstr + ")";
1180 } else {
1181 rangestr = "u = " + utostr(RangeFromType(TypeVec[ti]));
1182 }
Nate Begemanc4a1b652010-06-20 21:09:52 +00001183 // Make sure cases appear only once by uniquing them in a string map.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001184 namestr = MangleName(name, TypeVec[ti], ck);
1185 if (EmittedMap.count(namestr))
1186 continue;
1187 EmittedMap[namestr] = OpNone;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001188
1189 // Calculate the index of the immediate that should be range checked.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001190 unsigned immidx = 0;
Nate Begemanc4a1b652010-06-20 21:09:52 +00001191
1192 // Builtins that return a struct of multiple vectors have an extra
1193 // leading arg for the struct return.
Bob Wilson5b7fe592010-12-01 19:49:51 +00001194 if (Proto[0] >= '2' && Proto[0] <= '4')
Nate Begemanc4a1b652010-06-20 21:09:52 +00001195 ++immidx;
1196
1197 // Add one to the index for each argument until we reach the immediate
1198 // to be checked. Structs of vectors are passed as multiple arguments.
Nate Begemanf8c4c272010-06-17 04:15:13 +00001199 for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) {
1200 switch (Proto[ii]) {
1201 default: immidx += 1; break;
1202 case '2': immidx += 2; break;
1203 case '3': immidx += 3; break;
1204 case '4': immidx += 4; break;
1205 case 'i': ie = ii + 1; break;
1206 }
1207 }
1208 OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck)
1209 << ": i = " << immidx << "; " << rangestr << "; break;\n";
Nate Begemand72c9002010-06-13 04:47:03 +00001210 }
Nate Begeman73cef3e2010-06-04 01:26:15 +00001211 }
Nate Begemanf8c4c272010-06-17 04:15:13 +00001212 OS << "#endif\n\n";
Nate Begemana8979a02010-06-04 00:21:41 +00001213}