1 //===- NeonEmitter.cpp - Generate arm_neon.h for use with clang -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "NeonEmitter.h"
18 #include "llvm/ADT/SmallString.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/StringMap.h"
47 static void ParseTypes(Record *r, std::string &s,
48 SmallVectorImpl<StringRef> &TV) {
49 const char *data = s.data();
52 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
53 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
65 throw TGError(r->getLoc(),
66 "Unexpected letter: " + std::string(data + len, 1));
69 TV.push_back(StringRef(data, len + 1));
75 static char Widen(const char t) {
83 default: throw "unhandled type in widen!";
88 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
103 // remember unsigned.
104 if (ty[off] == 'U') {
109 // base type to get the type string for.
113 static std::string TypeString(const char mod, StringRef typestr) {
121 // base type to get the type string for.
122 char type = ClassifyType(typestr, quad, poly, usgn);
124 // Based on the modifying character, change the type and width if necessary.
181 s += poly ? "poly8" : "int8";
184 s += quad ? "x16" : "x8";
187 s += poly ? "poly16" : "int16";
190 s += quad ? "x8" : "x4";
196 s += quad ? "x4" : "x2";
202 s += quad ? "x2" : "x1";
208 s += quad ? "x8" : "x4";
214 s += quad ? "x4" : "x2";
217 throw "unhandled type!";
228 // Append _t, finishing the type string typedef type.
240 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
241 static std::string MangleName(const std::string &name, StringRef typestr) {
245 char type = ClassifyType(typestr, quad, poly, usgn);
247 std::string s = name;
251 s += poly ? "_p8" : usgn ? "_u8" : "_s8";
254 s += poly ? "_p16" : usgn ? "_u16" : "_s16";
257 s += usgn ? "_u32" : "_s32";
260 s += usgn ? "_u64" : "_s64";
269 throw "unhandled type!";
273 // Insert a 'q' before the first '_' character so that it ends up before
274 // _lane or _n on vector-scalar operations.
276 size_t pos = s.find('_');
277 s = s.insert(pos, "q");
282 // Generate the string "(argtype a, argtype b, ...)"
283 static std::string GenArgs(const std::string &proto, StringRef typestr) {
289 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
290 s += TypeString(proto[i], typestr);
301 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
302 // If structTypes is true, the NEON types are structs of vector types rather
303 // than vector types, and the call becomes "a.val + b.val"
304 static std::string GenOpString(OpKind op, const std::string &proto,
305 StringRef typestr, bool structTypes = true) {
306 std::string s("return ");
307 std::string ts = TypeString(proto[0], typestr);
309 s += "(" + ts + "){";
311 std::string a = structTypes ? "a.val" : "a";
312 std::string b = structTypes ? "b.val" : "b";
313 std::string c = structTypes ? "c.val" : "c";
326 s += a + " + ( " + b + " * " + c + " )";
329 s += a + " - ( " + b + " * " + c + " )";
332 s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
335 s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
338 s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
341 s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
344 s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
368 throw "unknown OpKind!";
378 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
379 // If structTypes is true, the NEON types are structs of vector types rather
380 // than vector types, and the call becomes __builtin_neon_cls(a.val)
381 static std::string GenBuiltin(const std::string &name, const std::string &proto,
382 StringRef typestr, bool structTypes = true) {
386 if (proto[0] != 'v') {
387 // FIXME: if return type is 2/3/4, emit unioning code.
391 s += TypeString(proto[0], typestr);
396 s += "__builtin_neon_";
400 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
402 if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
403 proto[i] != 'p' && proto[i] != 'c') {
411 if (proto[0] != 'v' && structTypes)
417 void NeonEmitter::run(raw_ostream &OS) {
418 EmitSourceFileHeader("ARM NEON Header", OS);
420 // FIXME: emit license into file?
422 OS << "#ifndef __ARM_NEON_H\n";
423 OS << "#define __ARM_NEON_H\n\n";
425 OS << "#ifndef __ARM_NEON__\n";
426 OS << "#error \"NEON support not enabled\"\n";
429 OS << "#include <stdint.h>\n\n";
431 // Emit NEON-specific scalar typedefs.
432 // FIXME: probably need to do something better for polynomial types.
433 // FIXME: is this the correct thing to do for float16?
434 OS << "typedef float float32_t;\n";
435 OS << "typedef uint8_t poly8_t;\n";
436 OS << "typedef uint16_t poly16_t;\n";
437 OS << "typedef uint16_t float16_t;\n";
439 // Emit Neon vector typedefs.
440 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
441 SmallVector<StringRef, 24> TDTypeVec;
442 ParseTypes(0, TypedefTypes, TDTypeVec);
444 // Emit vector typedefs.
445 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
446 bool dummy, quad = false;
447 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
448 OS << "typedef __attribute__(( __vector_size__(";
449 OS << (quad ? "16) )) " : "8) )) ");
450 OS << TypeString('s', TDTypeVec[i]);
452 OS << TypeString('d', TDTypeVec[i]) << ";\n";
456 // Emit struct typedefs.
457 for (unsigned vi = 1; vi != 5; ++vi) {
458 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
459 std::string ts = TypeString('d', TDTypeVec[i]);
460 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
461 OS << "typedef struct __" << vs << " {\n";
462 OS << " __neon_" << ts << " val";
464 OS << "[" << utostr(vi) << "]";
465 OS << ";\n} " << vs << ";\n\n";
469 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
471 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
473 StringMap<OpKind> OpMap;
474 OpMap["OP_NONE"] = OpNone;
475 OpMap["OP_ADD"] = OpAdd;
476 OpMap["OP_SUB"] = OpSub;
477 OpMap["OP_MUL"] = OpMul;
478 OpMap["OP_MLA"] = OpMla;
479 OpMap["OP_MLS"] = OpMls;
480 OpMap["OP_EQ"] = OpEq;
481 OpMap["OP_GE"] = OpGe;
482 OpMap["OP_LE"] = OpLe;
483 OpMap["OP_GT"] = OpGt;
484 OpMap["OP_LT"] = OpLt;
485 OpMap["OP_NEG"] = OpNeg;
486 OpMap["OP_NOT"] = OpNot;
487 OpMap["OP_AND"] = OpAnd;
488 OpMap["OP_OR"] = OpOr;
489 OpMap["OP_XOR"] = OpXor;
490 OpMap["OP_ANDN"] = OpAndNot;
491 OpMap["OP_ORN"] = OpOrNot;
493 // Unique the return+pattern types, and assign them.
494 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
496 std::string name = LowercaseString(R->getName());
497 std::string Proto = R->getValueAsString("Prototype");
498 std::string Types = R->getValueAsString("Types");
500 SmallVector<StringRef, 16> TypeVec;
501 ParseTypes(R, Types, TypeVec);
503 OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
505 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
506 assert(!Proto.empty() && "");
508 // static always inline + return type
509 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
511 // Function name with type suffix
512 OS << " " << MangleName(name, TypeVec[ti]);
514 // Function arguments
515 OS << GenArgs(Proto, TypeVec[ti]);
521 OS << GenOpString(k, Proto, TypeVec[ti]);
523 OS << GenBuiltin(name, Proto, TypeVec[ti]);
531 // Unique the return+pattern types, and assign them to each record
532 // Emit a #define for each unique "type" of intrinsic declaring all variants.
533 // Emit a #define for each intrinsic mapping it to a particular type.
535 OS << "#endif /* __ARM_NEON_H */\n";