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.
179 s += poly ? "poly8" : "int8";
182 s += quad ? "x16" : "x8";
185 s += poly ? "poly16" : "int16";
188 s += quad ? "x8" : "x4";
194 s += quad ? "x4" : "x2";
200 s += quad ? "x2" : "x1";
206 s += quad ? "x8" : "x4";
212 s += quad ? "x4" : "x2";
215 throw "unhandled type!";
226 // Append _t, finishing the type string typedef type.
238 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
239 static std::string MangleName(const std::string &name, StringRef typestr) {
243 char type = ClassifyType(typestr, quad, poly, usgn);
245 std::string s = name;
249 s += poly ? "_p8" : usgn ? "_u8" : "_s8";
252 s += poly ? "_p16" : usgn ? "_u16" : "_s16";
255 s += usgn ? "_u32" : "_s32";
258 s += usgn ? "_u64" : "_s64";
267 throw "unhandled type!";
271 // Insert a 'q' before the first '_' character so that it ends up before
272 // _lane or _n on vector-scalar operations.
274 size_t pos = s.find('_');
275 s = s.insert(pos, "q");
280 // Generate the string "(argtype a, argtype b, ...)"
281 static std::string GenArgs(const std::string &proto, StringRef typestr) {
287 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
288 s += TypeString(proto[i], typestr);
299 static OpKind ParseOp(Record *R) {
303 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
304 // If structTypes is true, the NEON types are structs of vector types rather
305 // than vector types, and the call becomes "a.val + b.val"
306 static std::string GenOpString(OpKind op, const std::string &proto,
307 bool structTypes = true) {
311 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
312 // If structTypes is true, the NEON types are structs of vector types rather
313 // than vector types, and the call becomes __builtin_neon_cls(a.val)
314 static std::string GenBuiltin(const std::string &name, const std::string &proto,
315 StringRef typestr, bool structTypes = true) {
317 std::string s("return ");
319 // FIXME: if return type is 2/3/4, emit unioning code.
323 s += TypeString(proto[0], typestr);
327 s += "__builtin_neon_";
331 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
346 void NeonEmitter::run(raw_ostream &OS) {
347 EmitSourceFileHeader("ARM NEON Header", OS);
349 // FIXME: emit license into file?
351 OS << "#ifndef __ARM_NEON_H\n";
352 OS << "#define __ARM_NEON_H\n\n";
354 OS << "#ifndef __ARM_NEON__\n";
355 OS << "#error \"NEON support not enabled\"\n";
358 OS << "#include <stdint.h>\n\n";
360 // Emit NEON-specific scalar typedefs.
361 // FIXME: probably need to do something better for polynomial types.
362 OS << "typedef float float32_t;\n";
363 OS << "typedef uint8_t poly8_t;\n";
364 OS << "typedef uint16_t poly16_t;\n";
366 // Emit Neon vector typedefs.
367 std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
368 SmallVector<StringRef, 24> TDTypeVec;
369 ParseTypes(0, TypedefTypes, TDTypeVec);
371 // Emit vector typedefs.
372 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
373 bool dummy, quad = false;
374 (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
375 OS << "typedef __attribute__(( __vector_size__(";
376 OS << (quad ? "16) )) " : "8) )) ");
377 OS << TypeString('s', TDTypeVec[i]);
379 OS << TypeString('d', TDTypeVec[i]) << "\n";
383 // Emit struct typedefs.
384 for (unsigned vi = 1; vi != 5; ++vi) {
385 for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
386 std::string ts = TypeString('d', TDTypeVec[i]);
387 std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
388 OS << "typedef struct __" << vs << " {\n";
389 OS << " __neon_" << ts << " val";
391 OS << "[" << utostr(vi) << "]";
392 OS << ";\n} " << vs << ";\n\n";
396 OS << "#define __ai static __attribute__((__always_inline__))\n\n";
398 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
400 // Unique the return+pattern types, and assign them.
401 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
403 std::string name = LowercaseString(R->getName());
404 std::string Proto = R->getValueAsString("Prototype");
405 std::string Types = R->getValueAsString("Types");
407 SmallVector<StringRef, 16> TypeVec;
408 ParseTypes(R, Types, TypeVec);
410 OpKind k = ParseOp(R);
412 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
413 assert(!Proto.empty() && "");
415 // static always inline + return type
416 OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
418 // Function name with type suffix
419 OS << " " << MangleName(name, TypeVec[ti]);
421 // Function arguments
422 OS << GenArgs(Proto, TypeVec[ti]);
428 OS << GenOpString(k, Proto);
430 OS << GenBuiltin(name, Proto, TypeVec[ti]);
438 // Unique the return+pattern types, and assign them to each record
439 // Emit a #define for each unique "type" of intrinsic declaring all variants.
440 // Emit a #define for each intrinsic mapping it to a particular type.
442 OS << "#endif /* __ARM_NEON_H */\n";