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 const 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 static std::string GenOpstring(OpKind op) {
307 static std::string GenBuiltin(std::string &name) {
311 void NeonEmitter::run(raw_ostream &OS) {
312 EmitSourceFileHeader("ARM NEON Header", OS);
314 // FIXME: emit license into file?
316 OS << "#ifndef __ARM_NEON_H\n";
317 OS << "#define __ARM_NEON_H\n\n";
319 OS << "#ifndef __ARM_NEON__\n";
320 OS << "#error \"NEON support not enabled\"\n";
323 OS << "#include <stdint.h>\n\n";
327 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
329 // Unique the return+pattern types, and assign them.
330 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
332 std::string name = LowercaseString(R->getName());
333 std::string Proto = R->getValueAsString("Prototype");
334 std::string Types = R->getValueAsString("Types");
336 SmallVector<StringRef, 16> TypeVec;
337 ParseTypes(R, Types, TypeVec);
339 OpKind k = ParseOp(R);
341 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
342 assert(!Proto.empty() && "");
345 OS << TypeString(Proto[0], TypeVec[ti]);
347 // Function name with type suffix
348 OS << " " << MangleName(name, TypeVec[ti]);
350 // Function arguments
351 OS << GenArgs(Proto, TypeVec[ti]);
357 OS << GenOpstring(k);
359 OS << GenBuiltin(name);
367 // Unique the return+pattern types, and assign them to each record
368 // Emit a #define for each unique "type" of intrinsic declaring all variants.
369 // Emit a #define for each intrinsic mapping it to a particular type.
371 OS << "\n#endif /* __ARM_NEON_H */\n";