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"
26 static void ParseTypes(Record *r, std::string &s,
27 SmallVectorImpl<StringRef> &TV) {
28 const char *data = s.data();
31 for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
32 if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
44 throw TGError(r->getLoc(),
45 "Unexpected letter: " + std::string(data + len, 1));
48 TV.push_back(StringRef(data, len + 1));
54 static const char Widen(const char t) {
62 default: throw "unhandled type in widen!";
67 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
88 // base type to get the type string for.
92 static std::string TypeString(const char mod, StringRef typestr) {
100 // base type to get the type string for.
101 char type = ClassifyType(typestr, quad, poly, usgn);
103 // Based on the modifying character, change the type and width if necessary.
164 s += poly ? "poly8" : "int8";
167 s += quad ? "x16" : "x8";
170 s += poly ? "poly16" : "int16";
173 s += quad ? "x8" : "x4";
179 s += quad ? "x4" : "x2";
185 s += quad ? "x2" : "x1";
191 s += quad ? "x8" : "x4";
197 s += quad ? "x4" : "x2";
203 throw "unhandled type!";
214 // Append _t, finishing the type string typedef type.
226 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
227 static std::string MangleName(const std::string &name, StringRef typestr) {
231 char type = ClassifyType(typestr, quad, poly, usgn);
233 std::string s = name;
237 s += poly ? "_p8" : usgn ? "_u8" : "_s8";
240 s += poly ? "_p16" : usgn ? "_u16" : "_s16";
243 s += usgn ? "_u32" : "_s32";
246 s += usgn ? "_u64" : "_s64";
255 throw "unhandled type!";
259 // Insert a 'q' before the first '_' character so that it ends up before
260 // _lane or _n on vector-scalar operations.
262 size_t pos = s.find('_');
263 s = s.insert(pos, "q");
268 // Generate the string "(argtype a, argtype b, ...)"
269 static std::string GenArgs(const std::string &proto, StringRef typestr) {
275 for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
276 s += TypeString(proto[i], typestr);
287 void NeonEmitter::run(raw_ostream &OS) {
288 EmitSourceFileHeader("ARM NEON Header", OS);
290 // FIXME: emit license into file?
292 OS << "#ifndef __ARM_NEON_H\n";
293 OS << "#define __ARM_NEON_H\n\n";
295 OS << "#ifndef __ARM_NEON__\n";
296 OS << "#error \"NEON support not enabled\"\n";
299 OS << "#include <stdint.h>\n\n";
303 std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
305 // Initialize Type Map
307 // Unique the return+pattern types, and assign them.
308 for (unsigned i = 0, e = RV.size(); i != e; ++i) {
310 std::string name = LowercaseString(R->getName());
311 std::string Proto = R->getValueAsString("Prototype");
312 std::string Types = R->getValueAsString("Types");
314 SmallVector<StringRef, 16> TypeVec;
315 ParseTypes(R, Types, TypeVec);
317 for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
318 assert(!Proto.empty() && "");
320 SmallString<128> Prototype;
321 Prototype += TypeString(Proto[0], TypeVec[ti]);
323 Prototype += MangleName(name, TypeVec[ti]);
324 Prototype += GenArgs(Proto, TypeVec[ti]);
326 OS << Prototype << ";\n";
334 // gen builtin (args)
340 // Unique the return+pattern types, and assign them to each record
341 // Emit a #define for each unique "type" of intrinsic declaring all variants.
342 // Emit a #define for each intrinsic mapping it to a particular type.
344 OS << "\n#endif /* __ARM_NEON_H */\n";