blob: 08c8f953bfb3e5993a74cc43c5ce1afad3ab00d2 [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//
14//===----------------------------------------------------------------------===//
15
16#include "NeonEmitter.h"
Nate Begeman22237772010-06-02 00:34:55 +000017#include "llvm/ADT/SmallString.h"
18#include "llvm/ADT/SmallVector.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000019#include "llvm/ADT/StringExtras.h"
Nate Begeman5ddb0872010-05-28 01:08:32 +000020#include <string>
21
22using namespace llvm;
23
Nate Begeman22237772010-06-02 00:34:55 +000024static void ParseTypes(Record *r, std::string &s,
25 SmallVectorImpl<StringRef> &TV) {
26 const char *data = s.data();
27 int len = 0;
28
29 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
30 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
31 continue;
32
33 switch (data[len]) {
34 case 'c':
35 case 's':
36 case 'i':
37 case 'l':
38 case 'h':
39 case 'f':
40 break;
41 default:
42 throw TGError(r->getLoc(),
43 "Unexpected letter: " + std::string(data + len, 1));
44 break;
45 }
46 TV.push_back(StringRef(data, len + 1));
47 data += len + 1;
48 len = -1;
49 }
50}
51
Duncan Sands8dbbace2010-06-02 08:37:30 +000052static char Widen(const char t) {
Nate Begeman22237772010-06-02 00:34:55 +000053 switch (t) {
54 case 'c':
55 return 's';
56 case 's':
57 return 'i';
58 case 'i':
59 return 'l';
60 default: throw "unhandled type in widen!";
61 }
62 return '\0';
63}
64
Nate Begeman3861e742010-06-03 21:35:22 +000065static char Narrow(const char t) {
66 switch (t) {
67 case 's':
68 return 'c';
69 case 'i':
70 return 's';
71 case 'l':
72 return 'i';
Nate Begeman900f4672010-06-08 00:14:42 +000073 case 'f':
74 return 'h';
Nate Begeman3861e742010-06-03 21:35:22 +000075 default: throw "unhandled type in widen!";
76 }
77 return '\0';
78}
79
Nate Begemanaf905ef2010-06-02 06:17:19 +000080static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +000081 unsigned off = 0;
82
Nate Begemanaf905ef2010-06-02 06:17:19 +000083 // remember quad.
84 if (ty[off] == 'Q') {
85 quad = true;
86 ++off;
87 }
88
89 // remember poly.
90 if (ty[off] == 'P') {
91 poly = true;
92 ++off;
93 }
94
95 // remember unsigned.
96 if (ty[off] == 'U') {
97 usgn = true;
98 ++off;
99 }
100
101 // base type to get the type string for.
102 return ty[off];
103}
104
Nate Begemanb0a4e452010-06-07 16:00:37 +0000105static char ModType(const char mod, char type, bool &quad, bool &poly,
106 bool &usgn, bool &scal, bool &cnst, bool &pntr) {
Nate Begeman22237772010-06-02 00:34:55 +0000107 switch (mod) {
Nate Begeman22237772010-06-02 00:34:55 +0000108 case 't':
109 if (poly) {
110 poly = false;
111 usgn = true;
112 }
113 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000114 case 'u':
Nate Begeman22237772010-06-02 00:34:55 +0000115 usgn = true;
Nate Begeman900f4672010-06-08 00:14:42 +0000116 case 'x':
Nate Begeman162d3ba2010-06-03 04:04:09 +0000117 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000118 if (type == 'f')
119 type = 'i';
120 break;
121 case 'f':
Nate Begeman900f4672010-06-08 00:14:42 +0000122 if (type == 'h')
123 quad = true;
Nate Begeman22237772010-06-02 00:34:55 +0000124 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000125 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000126 break;
127 case 'w':
128 type = Widen(type);
129 quad = true;
130 break;
131 case 'n':
132 type = Widen(type);
133 break;
Nate Begeman22237772010-06-02 00:34:55 +0000134 case 'l':
135 type = 'l';
136 scal = true;
137 usgn = true;
138 break;
139 case 's':
140 scal = true;
141 break;
142 case 'k':
143 quad = true;
144 break;
145 case 'c':
146 cnst = true;
147 case 'p':
Nate Begemanb0a4e452010-06-07 16:00:37 +0000148 usgn = false;
149 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000150 pntr = true;
151 scal = true;
152 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000153 case 'h':
154 type = Narrow(type);
Nate Begeman900f4672010-06-08 00:14:42 +0000155 if (type == 'h')
156 quad = false;
Nate Begeman3861e742010-06-03 21:35:22 +0000157 break;
158 case 'e':
159 type = Narrow(type);
160 usgn = true;
161 break;
Nate Begeman22237772010-06-02 00:34:55 +0000162 default:
163 break;
164 }
Nate Begemanb0a4e452010-06-07 16:00:37 +0000165 return type;
166}
167
168static std::string TypeString(const char mod, StringRef typestr,
169 bool ret = false) {
170 bool quad = false;
171 bool poly = false;
172 bool usgn = false;
173 bool scal = false;
174 bool cnst = false;
175 bool pntr = false;
176
177 if (mod == 'v')
178 return "void";
179 if (mod == 'i')
180 return "int";
181
182 // base type to get the type string for.
183 char type = ClassifyType(typestr, quad, poly, usgn);
184
185 // Based on the modifying character, change the type and width if necessary.
186 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
Nate Begeman22237772010-06-02 00:34:55 +0000187
188 SmallString<128> s;
189
Nate Begeman9e584b32010-06-04 22:53:30 +0000190 if (ret)
191 s += "__neon_";
192
Nate Begeman22237772010-06-02 00:34:55 +0000193 if (usgn)
194 s.push_back('u');
195
196 switch (type) {
197 case 'c':
198 s += poly ? "poly8" : "int8";
199 if (scal)
200 break;
201 s += quad ? "x16" : "x8";
202 break;
203 case 's':
204 s += poly ? "poly16" : "int16";
205 if (scal)
206 break;
207 s += quad ? "x8" : "x4";
208 break;
209 case 'i':
210 s += "int32";
211 if (scal)
212 break;
213 s += quad ? "x4" : "x2";
214 break;
215 case 'l':
216 s += "int64";
217 if (scal)
218 break;
219 s += quad ? "x2" : "x1";
220 break;
221 case 'h':
222 s += "float16";
223 if (scal)
224 break;
225 s += quad ? "x8" : "x4";
226 break;
227 case 'f':
228 s += "float32";
229 if (scal)
230 break;
231 s += quad ? "x4" : "x2";
232 break;
Nate Begeman22237772010-06-02 00:34:55 +0000233 default:
234 throw "unhandled type!";
235 break;
236 }
237
238 if (mod == '2')
239 s += "x2";
240 if (mod == '3')
241 s += "x3";
242 if (mod == '4')
243 s += "x4";
244
245 // Append _t, finishing the type string typedef type.
246 s += "_t";
247
248 if (cnst)
249 s += " const";
250
251 if (pntr)
252 s += " *";
253
254 return s.str();
255}
256
Nate Begeman7c21f742010-06-04 21:36:00 +0000257static std::string BuiltinTypeString(const char mod, StringRef typestr,
258 ClassKind ck, bool ret) {
Nate Begeman92f98af2010-06-04 07:11:25 +0000259 bool quad = false;
260 bool poly = false;
261 bool usgn = false;
262 bool scal = false;
263 bool cnst = false;
264 bool pntr = false;
265
266 if (mod == 'v')
267 return "v";
268 if (mod == 'i')
269 return "i";
270
271 // base type to get the type string for.
272 char type = ClassifyType(typestr, quad, poly, usgn);
273
274 // Based on the modifying character, change the type and width if necessary.
Nate Begemanb0a4e452010-06-07 16:00:37 +0000275 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
276
277 if (pntr)
278 type = 'v';
279
Nate Begeman92f98af2010-06-04 07:11:25 +0000280 if (type == 'h') {
281 type = 's';
282 usgn = true;
283 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000284 usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f');
Nate Begeman92f98af2010-06-04 07:11:25 +0000285
286 if (scal) {
287 SmallString<128> s;
288
289 if (usgn)
290 s.push_back('U');
Nate Begeman7c21f742010-06-04 21:36:00 +0000291
292 if (type == 'l')
293 s += "LLi";
294 else
295 s.push_back(type);
296
Nate Begeman92f98af2010-06-04 07:11:25 +0000297 if (cnst)
298 s.push_back('C');
299 if (pntr)
300 s.push_back('*');
301 return s.str();
302 }
Nate Begeman7c21f742010-06-04 21:36:00 +0000303
304 // Since the return value must be one type, return a vector type of the
305 // appropriate width which we will bitcast.
306 if (ret) {
307 if (mod == '2')
308 return quad ? "V32c" : "V16c";
309 if (mod == '3')
310 return quad ? "V48c" : "V24c";
311 if (mod == '4')
312 return quad ? "V64c" : "V32c";
Nate Begeman900f4672010-06-08 00:14:42 +0000313 if (mod == 'f')
314 return quad ? "V4f" : "V2f";
315
Nate Begeman7c21f742010-06-04 21:36:00 +0000316 return quad ? "V16c" : "V8c";
317 }
318
319 // Non-return array types are passed as individual vectors.
Nate Begeman92f98af2010-06-04 07:11:25 +0000320 if (mod == '2')
321 return quad ? "V16cV16c" : "V8cV8c";
322 if (mod == '3')
323 return quad ? "V16cV16cV16c" : "V8cV8cV8c";
324 if (mod == '4')
325 return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c";
Nate Begeman900f4672010-06-08 00:14:42 +0000326 if (mod == 'f')
327 return quad ? "V4f" : "V2f";
Nate Begeman92f98af2010-06-04 07:11:25 +0000328
329 return quad ? "V16c" : "V8c";
330}
331
Nate Begeman22237772010-06-02 00:34:55 +0000332// Turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000333static std::string MangleName(const std::string &name, StringRef typestr,
334 ClassKind ck) {
Nate Begeman900f4672010-06-08 00:14:42 +0000335 if (name == "vcvt_f32_f16")
336 return name;
337
Nate Begemanaf905ef2010-06-02 06:17:19 +0000338 bool quad = false;
339 bool poly = false;
340 bool usgn = false;
341 char type = ClassifyType(typestr, quad, poly, usgn);
342
343 std::string s = name;
344
345 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000346 case 'c':
347 switch (ck) {
348 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
349 case ClassI: s += "_i8"; break;
350 case ClassW: s += "_8"; break;
351 default: break;
352 }
353 break;
354 case 's':
355 switch (ck) {
356 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
357 case ClassI: s += "_i16"; break;
358 case ClassW: s += "_16"; break;
359 default: break;
360 }
361 break;
362 case 'i':
363 switch (ck) {
364 case ClassS: s += usgn ? "_u32" : "_s32"; break;
365 case ClassI: s += "_i32"; break;
366 case ClassW: s += "_32"; break;
367 default: break;
368 }
369 break;
370 case 'l':
371 switch (ck) {
372 case ClassS: s += usgn ? "_u64" : "_s64"; break;
373 case ClassI: s += "_i64"; break;
374 case ClassW: s += "_64"; break;
375 default: break;
376 }
377 break;
378 case 'h':
379 switch (ck) {
380 case ClassS:
381 case ClassI: s += "_f16"; break;
382 case ClassW: s += "_16"; break;
383 default: break;
384 }
385 break;
386 case 'f':
387 switch (ck) {
388 case ClassS:
389 case ClassI: s += "_f32"; break;
390 case ClassW: s += "_32"; break;
391 default: break;
392 }
393 break;
394 default:
395 throw "unhandled type!";
396 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000397 }
Nate Begemana8979a02010-06-04 00:21:41 +0000398 if (ck == ClassB)
Nate Begeman92f98af2010-06-04 07:11:25 +0000399 s += "_v";
Nate Begemana8979a02010-06-04 00:21:41 +0000400
Nate Begemanaf905ef2010-06-02 06:17:19 +0000401 // Insert a 'q' before the first '_' character so that it ends up before
402 // _lane or _n on vector-scalar operations.
403 if (quad) {
404 size_t pos = s.find('_');
405 s = s.insert(pos, "q");
406 }
407 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000408}
409
Nate Begemanaf905ef2010-06-02 06:17:19 +0000410// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000411static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begemanaf905ef2010-06-02 06:17:19 +0000412 char arg = 'a';
413
414 std::string s;
415 s += "(";
416
417 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
418 s += TypeString(proto[i], typestr);
419 s.push_back(' ');
420 s.push_back(arg);
421 if ((i + 1) < e)
422 s += ", ";
423 }
424
425 s += ")";
426 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000427}
428
Nate Begeman7c8c8832010-06-02 21:53:00 +0000429// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
430// If structTypes is true, the NEON types are structs of vector types rather
431// than vector types, and the call becomes "a.val + b.val"
432static std::string GenOpString(OpKind op, const std::string &proto,
Nate Begeman162d3ba2010-06-03 04:04:09 +0000433 StringRef typestr, bool structTypes = true) {
Nate Begeman9e584b32010-06-04 22:53:30 +0000434 std::string ts = TypeString(proto[0], typestr);
Nate Begeman900f4672010-06-08 00:14:42 +0000435 std::string s = ts + " r; r";
436
437 bool quad, dummy;
438 char type = ClassifyType(typestr, quad, dummy, dummy);
439 unsigned nElts = 0;
440 switch (type) {
441 case 'c': nElts = 8; break;
442 case 's': nElts = 4; break;
443 case 'i': nElts = 2; break;
444 case 'l': nElts = 1; break;
445 case 'h': nElts = 4; break;
446 case 'f': nElts = 2; break;
447 }
448 nElts <<= quad;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000449
Nate Begeman900f4672010-06-08 00:14:42 +0000450 if (structTypes)
451 s += ".val";
452
453 s += " = ";
454
Nate Begeman3861e742010-06-03 21:35:22 +0000455 std::string a, b, c;
456 if (proto.size() > 1)
Nate Begeman900f4672010-06-08 00:14:42 +0000457 a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a";
Nate Begeman3861e742010-06-03 21:35:22 +0000458 b = structTypes ? "b.val" : "b";
459 c = structTypes ? "c.val" : "c";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000460
461 switch(op) {
462 case OpAdd:
463 s += a + " + " + b;
464 break;
465 case OpSub:
466 s += a + " - " + b;
467 break;
468 case OpMul:
469 s += a + " * " + b;
470 break;
471 case OpMla:
472 s += a + " + ( " + b + " * " + c + " )";
473 break;
474 case OpMls:
475 s += a + " - ( " + b + " * " + c + " )";
476 break;
477 case OpEq:
478 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
479 break;
480 case OpGe:
481 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
482 break;
483 case OpLe:
484 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
485 break;
486 case OpGt:
487 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
488 break;
489 case OpLt:
490 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
491 break;
492 case OpNeg:
493 s += " -" + a;
494 break;
495 case OpNot:
496 s += " ~" + a;
497 break;
498 case OpAnd:
499 s += a + " & " + b;
500 break;
501 case OpOr:
502 s += a + " | " + b;
503 break;
504 case OpXor:
505 s += a + " ^ " + b;
506 break;
507 case OpAndNot:
508 s += a + " & ~" + b;
509 break;
510 case OpOrNot:
511 s += a + " | ~" + b;
512 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000513 case OpCast:
514 s += "(__neon_" + ts + ")" + a;
515 break;
Nate Begeman900f4672010-06-08 00:14:42 +0000516 case OpConcat:
517 s += "__builtin_shufflevector((__neon_int64x1_t)" + a;
518 s += ", (__neon_int64x1_t)" + b + ", 0, 1)";
519 break;
520 case OpDup:
521 s += "(__neon_" + ts + "){ ";
522 for (unsigned i = 0; i != nElts; ++i) {
523 s += a;
524 if ((i + 1) < nElts)
525 s += ", ";
526 }
527 s += " }";
528 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000529 default:
530 throw "unknown OpKind!";
531 break;
532 }
Nate Begeman900f4672010-06-08 00:14:42 +0000533 s += "; return r;";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000534 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000535}
536
Nate Begemanb0a4e452010-06-07 16:00:37 +0000537static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) {
538 unsigned mod = proto[0];
539 unsigned ret = 0;
540
Nate Begeman900f4672010-06-08 00:14:42 +0000541 if (mod == 'v' || mod == 'f')
Nate Begemanb0a4e452010-06-07 16:00:37 +0000542 mod = proto[1];
543
544 bool quad = false;
545 bool poly = false;
546 bool usgn = false;
547 bool scal = false;
548 bool cnst = false;
549 bool pntr = false;
550
551 // base type to get the type string for.
552 char type = ClassifyType(typestr, quad, poly, usgn);
553
554 // Based on the modifying character, change the type and width if necessary.
555 type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr);
556
557 if (usgn)
558 ret |= 0x08;
559 if (quad)
560 ret |= 0x10;
561
562 switch (type) {
563 case 'c':
564 ret |= poly ? 5 : 0;
565 break;
566 case 's':
567 ret |= poly ? 6 : 1;
568 break;
569 case 'i':
570 ret |= 2;
571 break;
572 case 'l':
573 ret |= 3;
574 break;
575 case 'h':
576 ret |= 7;
577 break;
578 case 'f':
579 ret |= 4;
580 break;
581 default:
582 throw "unhandled type!";
583 break;
584 }
585 return ret;
586}
587
Nate Begeman7c8c8832010-06-02 21:53:00 +0000588// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
589// If structTypes is true, the NEON types are structs of vector types rather
590// than vector types, and the call becomes __builtin_neon_cls(a.val)
591static std::string GenBuiltin(const std::string &name, const std::string &proto,
Nate Begemana8979a02010-06-04 00:21:41 +0000592 StringRef typestr, ClassKind ck,
593 bool structTypes = true) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000594 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000595 std::string s;
Nate Begeman9e584b32010-06-04 22:53:30 +0000596
597 bool unioning = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4');
598
599 // If all types are the same size, bitcasting the args will take care
600 // of arg checking. The actual signedness etc. will be taken care of with
601 // special enums.
602 if (proto.find('s') == std::string::npos)
603 ck = ClassB;
Nate Begeman7c21f742010-06-04 21:36:00 +0000604
Nate Begeman162d3ba2010-06-03 04:04:09 +0000605 if (proto[0] != 'v') {
Nate Begeman9e584b32010-06-04 22:53:30 +0000606 if (unioning) {
607 s += "union { ";
608 s += TypeString(proto[0], typestr, true) + " val; ";
609 s += TypeString(proto[0], typestr, false) + " s; ";
610 s += "} r;";
611 } else {
612 s += TypeString(proto[0], typestr);
Nate Begeman162d3ba2010-06-03 04:04:09 +0000613 }
Nate Begeman9e584b32010-06-04 22:53:30 +0000614
615 s += " r; r";
616 if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l')
617 s += ".val";
618
619 s += " = ";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000620 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000621
622 s += "__builtin_neon_";
Nate Begemana8979a02010-06-04 00:21:41 +0000623 s += MangleName(name, typestr, ck);
Nate Begeman7c8c8832010-06-02 21:53:00 +0000624 s += "(";
625
626 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
Nate Begeman9e584b32010-06-04 22:53:30 +0000627 // Handle multiple-vector values specially, emitting each subvector as an
628 // argument to the __builtin.
629 if (structTypes && (proto[i] == '2' || proto[i] == '3' || proto[i] == '4')){
630 for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) {
631 s.push_back(arg);
632 s += ".val[" + utostr(vi) + "]";
633 if ((vi + 1) < ve)
634 s += ", ";
635 }
636 if ((i + 1) < e)
637 s += ", ";
638
639 continue;
640 }
641
Nate Begeman7c8c8832010-06-02 21:53:00 +0000642 s.push_back(arg);
Nate Begeman9e584b32010-06-04 22:53:30 +0000643
Nate Begeman162d3ba2010-06-03 04:04:09 +0000644 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
645 proto[i] != 'p' && proto[i] != 'c') {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000646 s += ".val";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000647 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000648 if ((i + 1) < e)
649 s += ", ";
650 }
651
Nate Begeman9e584b32010-06-04 22:53:30 +0000652 // Extra constant integer to hold type class enum for this function, e.g. s8
Nate Begeman9e584b32010-06-04 22:53:30 +0000653 if (ck == ClassB)
Nate Begemanb0a4e452010-06-07 16:00:37 +0000654 s += ", " + utostr(GetNeonEnum(proto, typestr));
Nate Begeman9e584b32010-06-04 22:53:30 +0000655
656 s += ");";
657
658 if (proto[0] != 'v') {
659 if (unioning)
660 s += " return r.s;";
661 else
662 s += " return r;";
663 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000664 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000665}
666
Nate Begeman73cef3e2010-06-04 01:26:15 +0000667static std::string GenBuiltinDef(const std::string &name,
668 const std::string &proto,
669 StringRef typestr, ClassKind ck) {
670 std::string s("BUILTIN(__builtin_neon_");
Nate Begeman92f98af2010-06-04 07:11:25 +0000671
672 // If all types are the same size, bitcasting the args will take care
673 // of arg checking. The actual signedness etc. will be taken care of with
674 // special enums.
675 if (proto.find('s') == std::string::npos)
676 ck = ClassB;
677
Nate Begeman73cef3e2010-06-04 01:26:15 +0000678 s += MangleName(name, typestr, ck);
679 s += ", \"";
680
Nate Begeman92f98af2010-06-04 07:11:25 +0000681 for (unsigned i = 0, e = proto.size(); i != e; ++i)
Nate Begeman7c21f742010-06-04 21:36:00 +0000682 s += BuiltinTypeString(proto[i], typestr, ck, i == 0);
683
684 // Extra constant integer to hold type class enum for this function, e.g. s8
685 if (ck == ClassB)
686 s += "i";
Nate Begeman73cef3e2010-06-04 01:26:15 +0000687
688 s += "\", \"n\")";
689 return s;
690}
691
Nate Begeman5ddb0872010-05-28 01:08:32 +0000692void NeonEmitter::run(raw_ostream &OS) {
693 EmitSourceFileHeader("ARM NEON Header", OS);
694
695 // FIXME: emit license into file?
696
697 OS << "#ifndef __ARM_NEON_H\n";
698 OS << "#define __ARM_NEON_H\n\n";
699
700 OS << "#ifndef __ARM_NEON__\n";
701 OS << "#error \"NEON support not enabled\"\n";
702 OS << "#endif\n\n";
703
704 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000705
706 // Emit NEON-specific scalar typedefs.
707 // FIXME: probably need to do something better for polynomial types.
Nate Begeman162d3ba2010-06-03 04:04:09 +0000708 // FIXME: is this the correct thing to do for float16?
Nate Begeman7c8c8832010-06-02 21:53:00 +0000709 OS << "typedef float float32_t;\n";
710 OS << "typedef uint8_t poly8_t;\n";
711 OS << "typedef uint16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000712 OS << "typedef uint16_t float16_t;\n";
Nate Begeman9e584b32010-06-04 22:53:30 +0000713
Nate Begeman7c8c8832010-06-02 21:53:00 +0000714 // Emit Neon vector typedefs.
715 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
716 SmallVector<StringRef, 24> TDTypeVec;
717 ParseTypes(0, TypedefTypes, TDTypeVec);
718
719 // Emit vector typedefs.
Nate Begeman9e584b32010-06-04 22:53:30 +0000720 for (unsigned v = 1; v != 5; ++v) {
721 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
722 bool dummy, quad = false;
723 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
724 OS << "typedef __attribute__(( __vector_size__(";
725
726 OS << utostr(8*v*(quad ? 2 : 1)) << ") )) ";
727 if (!quad)
728 OS << " ";
729
730 OS << TypeString('s', TDTypeVec[i]);
731 OS << " __neon_";
732
733 char t = (v == 1) ? 'd' : '0' + v;
734 OS << TypeString(t, TDTypeVec[i]) << ";\n";
735 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000736 }
737 OS << "\n";
738
739 // Emit struct typedefs.
740 for (unsigned vi = 1; vi != 5; ++vi) {
741 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
742 std::string ts = TypeString('d', TDTypeVec[i]);
743 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
744 OS << "typedef struct __" << vs << " {\n";
745 OS << " __neon_" << ts << " val";
746 if (vi > 1)
747 OS << "[" << utostr(vi) << "]";
748 OS << ";\n} " << vs << ";\n\n";
749 }
750 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000751
Nate Begeman7c8c8832010-06-02 21:53:00 +0000752 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
753
Nate Begeman5ddb0872010-05-28 01:08:32 +0000754 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
755
Nate Begeman22237772010-06-02 00:34:55 +0000756 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000757 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
758 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000759 std::string name = LowercaseString(R->getName());
760 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000761 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000762
763 SmallVector<StringRef, 16> TypeVec;
764 ParseTypes(R, Types, TypeVec);
765
Nate Begeman162d3ba2010-06-03 04:04:09 +0000766 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000767
Nate Begeman22237772010-06-02 00:34:55 +0000768 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
769 assert(!Proto.empty() && "");
770
Nate Begeman7c8c8832010-06-02 21:53:00 +0000771 // static always inline + return type
772 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000773
Nate Begemane66aab52010-06-02 07:14:28 +0000774 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000775 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000776
Nate Begemane66aab52010-06-02 07:14:28 +0000777 // Function arguments
778 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000779
Nate Begemane66aab52010-06-02 07:14:28 +0000780 // Definition.
781 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000782
Nate Begemana8979a02010-06-04 00:21:41 +0000783 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000784 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000785 } else {
786 if (R->getSuperClasses().size() < 2)
787 throw TGError(R->getLoc(), "Builtin has no class kind");
788
789 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
790
791 if (ck == ClassNone)
792 throw TGError(R->getLoc(), "Builtin has no class kind");
793 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
794 }
Nate Begemane66aab52010-06-02 07:14:28 +0000795
Nate Begeman7c8c8832010-06-02 21:53:00 +0000796 OS << " }\n";
Nate Begeman22237772010-06-02 00:34:55 +0000797 }
798 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000799 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000800 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000801 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000802}
Nate Begemana8979a02010-06-04 00:21:41 +0000803
804void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +0000805 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
806
807 StringMap<OpKind> EmittedMap;
808
809 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
810 Record *R = RV[i];
811
812 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
813 if (k != OpNone)
814 continue;
815
816 std::string name = LowercaseString(R->getName());
817 std::string Proto = R->getValueAsString("Prototype");
818 std::string Types = R->getValueAsString("Types");
819
820 SmallVector<StringRef, 16> TypeVec;
821 ParseTypes(R, Types, TypeVec);
822
823 if (R->getSuperClasses().size() < 2)
824 throw TGError(R->getLoc(), "Builtin has no class kind");
825
826 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
827
828 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
829 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
830 if (EmittedMap.count(bd))
831 continue;
832
833 EmittedMap[bd] = OpNone;
834 OS << bd << "\n";
835 }
836 }
Nate Begemana8979a02010-06-04 00:21:41 +0000837}