blob: b385b7639a2c2a59cf95bb36fa2786ce03253362 [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';
73 default: throw "unhandled type in widen!";
74 }
75 return '\0';
76}
77
Nate Begemanaf905ef2010-06-02 06:17:19 +000078static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
Nate Begeman22237772010-06-02 00:34:55 +000079 unsigned off = 0;
80
Nate Begemanaf905ef2010-06-02 06:17:19 +000081 // remember quad.
82 if (ty[off] == 'Q') {
83 quad = true;
84 ++off;
85 }
86
87 // remember poly.
88 if (ty[off] == 'P') {
89 poly = true;
90 ++off;
91 }
92
93 // remember unsigned.
94 if (ty[off] == 'U') {
95 usgn = true;
96 ++off;
97 }
98
99 // base type to get the type string for.
100 return ty[off];
101}
102
103static std::string TypeString(const char mod, StringRef typestr) {
Nate Begeman22237772010-06-02 00:34:55 +0000104 bool quad = false;
105 bool poly = false;
106 bool usgn = false;
107 bool scal = false;
108 bool cnst = false;
109 bool pntr = false;
110
Nate Begeman22237772010-06-02 00:34:55 +0000111 // base type to get the type string for.
Nate Begemanaf905ef2010-06-02 06:17:19 +0000112 char type = ClassifyType(typestr, quad, poly, usgn);
Nate Begeman22237772010-06-02 00:34:55 +0000113
114 // Based on the modifying character, change the type and width if necessary.
115 switch (mod) {
116 case 'v':
Nate Begemane66aab52010-06-02 07:14:28 +0000117 return "void";
118 case 'i':
119 return "int";
Nate Begeman22237772010-06-02 00:34:55 +0000120 case 't':
121 if (poly) {
122 poly = false;
123 usgn = true;
124 }
125 break;
126 case 'x':
127 usgn = true;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000128 poly = false;
Nate Begeman22237772010-06-02 00:34:55 +0000129 if (type == 'f')
130 type = 'i';
131 break;
132 case 'f':
133 type = 'f';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000134 usgn = false;
Nate Begeman22237772010-06-02 00:34:55 +0000135 break;
136 case 'w':
137 type = Widen(type);
138 quad = true;
139 break;
140 case 'n':
141 type = Widen(type);
142 break;
Nate Begeman22237772010-06-02 00:34:55 +0000143 case 'l':
144 type = 'l';
145 scal = true;
146 usgn = true;
147 break;
148 case 's':
149 scal = true;
150 break;
151 case 'k':
152 quad = true;
153 break;
154 case 'c':
155 cnst = true;
156 case 'p':
157 pntr = true;
158 scal = true;
159 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000160 case 'h':
161 type = Narrow(type);
162 break;
163 case 'e':
164 type = Narrow(type);
165 usgn = true;
166 break;
Nate Begeman22237772010-06-02 00:34:55 +0000167 default:
168 break;
169 }
170
171 SmallString<128> s;
172
173 if (usgn)
174 s.push_back('u');
175
176 switch (type) {
177 case 'c':
178 s += poly ? "poly8" : "int8";
179 if (scal)
180 break;
181 s += quad ? "x16" : "x8";
182 break;
183 case 's':
184 s += poly ? "poly16" : "int16";
185 if (scal)
186 break;
187 s += quad ? "x8" : "x4";
188 break;
189 case 'i':
190 s += "int32";
191 if (scal)
192 break;
193 s += quad ? "x4" : "x2";
194 break;
195 case 'l':
196 s += "int64";
197 if (scal)
198 break;
199 s += quad ? "x2" : "x1";
200 break;
201 case 'h':
202 s += "float16";
203 if (scal)
204 break;
205 s += quad ? "x8" : "x4";
206 break;
207 case 'f':
208 s += "float32";
209 if (scal)
210 break;
211 s += quad ? "x4" : "x2";
212 break;
Nate Begeman22237772010-06-02 00:34:55 +0000213 default:
214 throw "unhandled type!";
215 break;
216 }
217
218 if (mod == '2')
219 s += "x2";
220 if (mod == '3')
221 s += "x3";
222 if (mod == '4')
223 s += "x4";
224
225 // Append _t, finishing the type string typedef type.
226 s += "_t";
227
228 if (cnst)
229 s += " const";
230
231 if (pntr)
232 s += " *";
233
234 return s.str();
235}
236
237// Turn "vst2_lane" into "vst2q_lane_f32", etc.
Nate Begemana8979a02010-06-04 00:21:41 +0000238static std::string MangleName(const std::string &name, StringRef typestr,
239 ClassKind ck) {
Nate Begemanaf905ef2010-06-02 06:17:19 +0000240 bool quad = false;
241 bool poly = false;
242 bool usgn = false;
243 char type = ClassifyType(typestr, quad, poly, usgn);
244
245 std::string s = name;
246
247 switch (type) {
Nate Begemana8979a02010-06-04 00:21:41 +0000248 case 'c':
249 switch (ck) {
250 case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
251 case ClassI: s += "_i8"; break;
252 case ClassW: s += "_8"; break;
253 default: break;
254 }
255 break;
256 case 's':
257 switch (ck) {
258 case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
259 case ClassI: s += "_i16"; break;
260 case ClassW: s += "_16"; break;
261 default: break;
262 }
263 break;
264 case 'i':
265 switch (ck) {
266 case ClassS: s += usgn ? "_u32" : "_s32"; break;
267 case ClassI: s += "_i32"; break;
268 case ClassW: s += "_32"; break;
269 default: break;
270 }
271 break;
272 case 'l':
273 switch (ck) {
274 case ClassS: s += usgn ? "_u64" : "_s64"; break;
275 case ClassI: s += "_i64"; break;
276 case ClassW: s += "_64"; break;
277 default: break;
278 }
279 break;
280 case 'h':
281 switch (ck) {
282 case ClassS:
283 case ClassI: s += "_f16"; break;
284 case ClassW: s += "_16"; break;
285 default: break;
286 }
287 break;
288 case 'f':
289 switch (ck) {
290 case ClassS:
291 case ClassI: s += "_f32"; break;
292 case ClassW: s += "_32"; break;
293 default: break;
294 }
295 break;
296 default:
297 throw "unhandled type!";
298 break;
Nate Begemanaf905ef2010-06-02 06:17:19 +0000299 }
Nate Begemana8979a02010-06-04 00:21:41 +0000300 if (ck == ClassB)
301 return s += "_v";
302
Nate Begemanaf905ef2010-06-02 06:17:19 +0000303 // Insert a 'q' before the first '_' character so that it ends up before
304 // _lane or _n on vector-scalar operations.
305 if (quad) {
306 size_t pos = s.find('_');
307 s = s.insert(pos, "q");
308 }
309 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000310}
311
Nate Begemanaf905ef2010-06-02 06:17:19 +0000312// Generate the string "(argtype a, argtype b, ...)"
Nate Begeman22237772010-06-02 00:34:55 +0000313static std::string GenArgs(const std::string &proto, StringRef typestr) {
Nate Begemanaf905ef2010-06-02 06:17:19 +0000314 char arg = 'a';
315
316 std::string s;
317 s += "(";
318
319 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
320 s += TypeString(proto[i], typestr);
321 s.push_back(' ');
322 s.push_back(arg);
323 if ((i + 1) < e)
324 s += ", ";
325 }
326
327 s += ")";
328 return s;
Nate Begeman22237772010-06-02 00:34:55 +0000329}
330
Nate Begeman7c8c8832010-06-02 21:53:00 +0000331// Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
332// If structTypes is true, the NEON types are structs of vector types rather
333// than vector types, and the call becomes "a.val + b.val"
334static std::string GenOpString(OpKind op, const std::string &proto,
Nate Begeman162d3ba2010-06-03 04:04:09 +0000335 StringRef typestr, bool structTypes = true) {
336 std::string s("return ");
337 std::string ts = TypeString(proto[0], typestr);
338 if (structTypes)
339 s += "(" + ts + "){";
340
Nate Begeman3861e742010-06-03 21:35:22 +0000341 std::string a, b, c;
342 if (proto.size() > 1)
343 a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
344 b = structTypes ? "b.val" : "b";
345 c = structTypes ? "c.val" : "c";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000346
347 switch(op) {
348 case OpAdd:
349 s += a + " + " + b;
350 break;
351 case OpSub:
352 s += a + " - " + b;
353 break;
354 case OpMul:
355 s += a + " * " + b;
356 break;
357 case OpMla:
358 s += a + " + ( " + b + " * " + c + " )";
359 break;
360 case OpMls:
361 s += a + " - ( " + b + " * " + c + " )";
362 break;
363 case OpEq:
364 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
365 break;
366 case OpGe:
367 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
368 break;
369 case OpLe:
370 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
371 break;
372 case OpGt:
373 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
374 break;
375 case OpLt:
376 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
377 break;
378 case OpNeg:
379 s += " -" + a;
380 break;
381 case OpNot:
382 s += " ~" + a;
383 break;
384 case OpAnd:
385 s += a + " & " + b;
386 break;
387 case OpOr:
388 s += a + " | " + b;
389 break;
390 case OpXor:
391 s += a + " ^ " + b;
392 break;
393 case OpAndNot:
394 s += a + " & ~" + b;
395 break;
396 case OpOrNot:
397 s += a + " | ~" + b;
398 break;
Nate Begeman3861e742010-06-03 21:35:22 +0000399 case OpCast:
400 s += "(__neon_" + ts + ")" + a;
401 break;
Nate Begeman162d3ba2010-06-03 04:04:09 +0000402 default:
403 throw "unknown OpKind!";
404 break;
405 }
406
407 if (structTypes)
408 s += "}";
409 s += ";";
410 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000411}
412
Nate Begeman7c8c8832010-06-02 21:53:00 +0000413// Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
414// If structTypes is true, the NEON types are structs of vector types rather
415// than vector types, and the call becomes __builtin_neon_cls(a.val)
416static std::string GenBuiltin(const std::string &name, const std::string &proto,
Nate Begemana8979a02010-06-04 00:21:41 +0000417 StringRef typestr, ClassKind ck,
418 bool structTypes = true) {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000419 char arg = 'a';
Nate Begeman162d3ba2010-06-03 04:04:09 +0000420 std::string s;
Nate Begeman7c8c8832010-06-02 21:53:00 +0000421
Nate Begeman162d3ba2010-06-03 04:04:09 +0000422 if (proto[0] != 'v') {
423 // FIXME: if return type is 2/3/4, emit unioning code.
424 s += "return ";
425 if (structTypes) {
426 s += "(";
427 s += TypeString(proto[0], typestr);
428 s += "){";
429 }
430 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000431
432 s += "__builtin_neon_";
Nate Begemana8979a02010-06-04 00:21:41 +0000433 s += MangleName(name, typestr, ck);
Nate Begeman7c8c8832010-06-02 21:53:00 +0000434 s += "(";
435
436 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
437 s.push_back(arg);
Nate Begeman162d3ba2010-06-03 04:04:09 +0000438 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
439 proto[i] != 'p' && proto[i] != 'c') {
Nate Begeman7c8c8832010-06-02 21:53:00 +0000440 s += ".val";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000441 }
Nate Begeman7c8c8832010-06-02 21:53:00 +0000442 if ((i + 1) < e)
443 s += ", ";
444 }
445
446 s += ")";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000447 if (proto[0] != 'v' && structTypes)
Nate Begeman7c8c8832010-06-02 21:53:00 +0000448 s += "}";
449 s += ";";
450 return s;
Nate Begemane66aab52010-06-02 07:14:28 +0000451}
452
Nate Begeman73cef3e2010-06-04 01:26:15 +0000453static std::string GenBuiltinDef(const std::string &name,
454 const std::string &proto,
455 StringRef typestr, ClassKind ck) {
456 std::string s("BUILTIN(__builtin_neon_");
457 s += MangleName(name, typestr, ck);
458 s += ", \"";
459
460 for (unsigned i = 0, e = proto.size(); i != e; ++i) {
461 }
462
463 s += "\", \"n\")";
464 return s;
465}
466
Nate Begeman5ddb0872010-05-28 01:08:32 +0000467void NeonEmitter::run(raw_ostream &OS) {
468 EmitSourceFileHeader("ARM NEON Header", OS);
469
470 // FIXME: emit license into file?
471
472 OS << "#ifndef __ARM_NEON_H\n";
473 OS << "#define __ARM_NEON_H\n\n";
474
475 OS << "#ifndef __ARM_NEON__\n";
476 OS << "#error \"NEON support not enabled\"\n";
477 OS << "#endif\n\n";
478
479 OS << "#include <stdint.h>\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000480
481 // Emit NEON-specific scalar typedefs.
482 // FIXME: probably need to do something better for polynomial types.
Nate Begeman162d3ba2010-06-03 04:04:09 +0000483 // FIXME: is this the correct thing to do for float16?
Nate Begeman7c8c8832010-06-02 21:53:00 +0000484 OS << "typedef float float32_t;\n";
485 OS << "typedef uint8_t poly8_t;\n";
486 OS << "typedef uint16_t poly16_t;\n";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000487 OS << "typedef uint16_t float16_t;\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000488
Nate Begeman7c8c8832010-06-02 21:53:00 +0000489 // Emit Neon vector typedefs.
490 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
491 SmallVector<StringRef, 24> TDTypeVec;
492 ParseTypes(0, TypedefTypes, TDTypeVec);
493
494 // Emit vector typedefs.
495 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
496 bool dummy, quad = false;
497 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
498 OS << "typedef __attribute__(( __vector_size__(";
499 OS << (quad ? "16) )) " : "8) )) ");
500 OS << TypeString('s', TDTypeVec[i]);
501 OS << " __neon_";
Nate Begeman162d3ba2010-06-03 04:04:09 +0000502 OS << TypeString('d', TDTypeVec[i]) << ";\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000503 }
504 OS << "\n";
505
506 // Emit struct typedefs.
507 for (unsigned vi = 1; vi != 5; ++vi) {
508 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
509 std::string ts = TypeString('d', TDTypeVec[i]);
510 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
511 OS << "typedef struct __" << vs << " {\n";
512 OS << " __neon_" << ts << " val";
513 if (vi > 1)
514 OS << "[" << utostr(vi) << "]";
515 OS << ";\n} " << vs << ";\n\n";
516 }
517 }
Nate Begeman5ddb0872010-05-28 01:08:32 +0000518
Nate Begeman7c8c8832010-06-02 21:53:00 +0000519 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
520
Nate Begeman5ddb0872010-05-28 01:08:32 +0000521 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
522
Nate Begeman22237772010-06-02 00:34:55 +0000523 // Unique the return+pattern types, and assign them.
Nate Begeman5ddb0872010-05-28 01:08:32 +0000524 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
525 Record *R = RV[i];
Nate Begeman22237772010-06-02 00:34:55 +0000526 std::string name = LowercaseString(R->getName());
527 std::string Proto = R->getValueAsString("Prototype");
Nate Begeman5ddb0872010-05-28 01:08:32 +0000528 std::string Types = R->getValueAsString("Types");
Nate Begeman22237772010-06-02 00:34:55 +0000529
530 SmallVector<StringRef, 16> TypeVec;
531 ParseTypes(R, Types, TypeVec);
532
Nate Begeman162d3ba2010-06-03 04:04:09 +0000533 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
Nate Begemane66aab52010-06-02 07:14:28 +0000534
Nate Begeman22237772010-06-02 00:34:55 +0000535 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
536 assert(!Proto.empty() && "");
537
Nate Begeman7c8c8832010-06-02 21:53:00 +0000538 // static always inline + return type
539 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000540
Nate Begemane66aab52010-06-02 07:14:28 +0000541 // Function name with type suffix
Nate Begemana8979a02010-06-04 00:21:41 +0000542 OS << " " << MangleName(name, TypeVec[ti], ClassS);
Nate Begeman22237772010-06-02 00:34:55 +0000543
Nate Begemane66aab52010-06-02 07:14:28 +0000544 // Function arguments
545 OS << GenArgs(Proto, TypeVec[ti]);
Nate Begeman22237772010-06-02 00:34:55 +0000546
Nate Begemane66aab52010-06-02 07:14:28 +0000547 // Definition.
548 OS << " { ";
Nate Begeman22237772010-06-02 00:34:55 +0000549
Nate Begemana8979a02010-06-04 00:21:41 +0000550 if (k != OpNone) {
Nate Begeman162d3ba2010-06-03 04:04:09 +0000551 OS << GenOpString(k, Proto, TypeVec[ti]);
Nate Begemana8979a02010-06-04 00:21:41 +0000552 } else {
553 if (R->getSuperClasses().size() < 2)
554 throw TGError(R->getLoc(), "Builtin has no class kind");
555
556 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
557
558 if (ck == ClassNone)
559 throw TGError(R->getLoc(), "Builtin has no class kind");
560 OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
561 }
Nate Begemane66aab52010-06-02 07:14:28 +0000562
Nate Begeman7c8c8832010-06-02 21:53:00 +0000563 OS << " }\n";
Nate Begeman22237772010-06-02 00:34:55 +0000564 }
565 OS << "\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000566 }
Nate Begeman73cef3e2010-06-04 01:26:15 +0000567 OS << "#undef __ai\n\n";
Nate Begeman7c8c8832010-06-02 21:53:00 +0000568 OS << "#endif /* __ARM_NEON_H */\n";
Nate Begeman5ddb0872010-05-28 01:08:32 +0000569}
Nate Begemana8979a02010-06-04 00:21:41 +0000570
571void NeonEmitter::runHeader(raw_ostream &OS) {
Nate Begeman73cef3e2010-06-04 01:26:15 +0000572 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
573
574 StringMap<OpKind> EmittedMap;
575
576 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
577 Record *R = RV[i];
578
579 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
580 if (k != OpNone)
581 continue;
582
583 std::string name = LowercaseString(R->getName());
584 std::string Proto = R->getValueAsString("Prototype");
585 std::string Types = R->getValueAsString("Types");
586
587 SmallVector<StringRef, 16> TypeVec;
588 ParseTypes(R, Types, TypeVec);
589
590 if (R->getSuperClasses().size() < 2)
591 throw TGError(R->getLoc(), "Builtin has no class kind");
592
593 ClassKind ck = ClassMap[R->getSuperClasses()[1]];
594
595 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
596 std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
597 if (EmittedMap.count(bd))
598 continue;
599
600 EmittedMap[bd] = OpNone;
601 OS << bd << "\n";
602 }
603 }
Nate Begemana8979a02010-06-04 00:21:41 +0000604}