arm_neon.h emitter now mostly complete for the purposes of initial testing.
[oota-llvm.git] / utils / TableGen / NeonEmitter.cpp
1 //===- 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"
17 #include "Record.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"
22 #include <string>
23
24 using namespace llvm;
25
26 enum OpKind {
27   OpNone,
28   OpAdd,
29   OpSub,
30   OpMul,
31   OpMla,
32   OpMls,
33   OpEq,
34   OpGe,
35   OpLe,
36   OpGt,
37   OpLt,
38   OpNeg,
39   OpNot,
40   OpAnd,
41   OpOr,
42   OpXor,
43   OpAndNot,
44   OpOrNot
45 };
46
47 static void ParseTypes(Record *r, std::string &s,
48                        SmallVectorImpl<StringRef> &TV) {
49   const char *data = s.data();
50   int len = 0;
51   
52   for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
53     if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
54       continue;
55     
56     switch (data[len]) {
57       case 'c':
58       case 's':
59       case 'i':
60       case 'l':
61       case 'h':
62       case 'f':
63         break;
64       default:
65         throw TGError(r->getLoc(),
66                       "Unexpected letter: " + std::string(data + len, 1));
67         break;
68     }
69     TV.push_back(StringRef(data, len + 1));
70     data += len + 1;
71     len = -1;
72   }
73 }
74
75 static char Widen(const char t) {
76   switch (t) {
77     case 'c':
78       return 's';
79     case 's':
80       return 'i';
81     case 'i':
82       return 'l';
83     default: throw "unhandled type in widen!";
84   }
85   return '\0';
86 }
87
88 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
89   unsigned off = 0;
90   
91   // remember quad.
92   if (ty[off] == 'Q') {
93     quad = true;
94     ++off;
95   }
96   
97   // remember poly.
98   if (ty[off] == 'P') {
99     poly = true;
100     ++off;
101   }
102   
103   // remember unsigned.
104   if (ty[off] == 'U') {
105     usgn = true;
106     ++off;
107   }
108   
109   // base type to get the type string for.
110   return ty[off];
111 }
112
113 static std::string TypeString(const char mod, StringRef typestr) {
114   bool quad = false;
115   bool poly = false;
116   bool usgn = false;
117   bool scal = false;
118   bool cnst = false;
119   bool pntr = false;
120   
121   // base type to get the type string for.
122   char type = ClassifyType(typestr, quad, poly, usgn);
123   
124   // Based on the modifying character, change the type and width if necessary.
125   switch (mod) {
126     case 'v':
127       return "void";
128     case 'i':
129       return "int";
130     case 't':
131       if (poly) {
132         poly = false;
133         usgn = true;
134       }
135       break;
136     case 'x':
137       usgn = true;
138       if (type == 'f')
139         type = 'i';
140       break;
141     case 'f':
142       type = 'f';
143       break;
144     case 'w':
145       type = Widen(type);
146       quad = true;
147       break;
148     case 'n':
149       type = Widen(type);
150       break;
151     case 'l':
152       type = 'l';
153       scal = true;
154       usgn = true;
155       break;
156     case 's':
157       scal = true;
158       break;
159     case 'k':
160       quad = true;
161       break;
162     case 'c':
163       cnst = true;
164     case 'p':
165       pntr = true;
166       scal = true;
167       break;
168     default:
169       break;
170   }
171   
172   SmallString<128> s;
173   
174   if (usgn)
175     s.push_back('u');
176   
177   switch (type) {
178     case 'c':
179       s += poly ? "poly8" : "int8";
180       if (scal)
181         break;
182       s += quad ? "x16" : "x8";
183       break;
184     case 's':
185       s += poly ? "poly16" : "int16";
186       if (scal)
187         break;
188       s += quad ? "x8" : "x4";
189       break;
190     case 'i':
191       s += "int32";
192       if (scal)
193         break;
194       s += quad ? "x4" : "x2";
195       break;
196     case 'l':
197       s += "int64";
198       if (scal)
199         break;
200       s += quad ? "x2" : "x1";
201       break;
202     case 'h':
203       s += "float16";
204       if (scal)
205         break;
206       s += quad ? "x8" : "x4";
207       break;
208     case 'f':
209       s += "float32";
210       if (scal)
211         break;
212       s += quad ? "x4" : "x2";
213       break;
214     default:
215       throw "unhandled type!";
216       break;
217   }
218
219   if (mod == '2')
220     s += "x2";
221   if (mod == '3')
222     s += "x3";
223   if (mod == '4')
224     s += "x4";
225   
226   // Append _t, finishing the type string typedef type.
227   s += "_t";
228   
229   if (cnst)
230     s += " const";
231   
232   if (pntr)
233     s += " *";
234   
235   return s.str();
236 }
237
238 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
239 static std::string MangleName(const std::string &name, StringRef typestr) {
240   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) {
248     case 'c':
249       s += poly ? "_p8" : usgn ? "_u8" : "_s8";
250       break;
251     case 's':
252       s += poly ? "_p16" : usgn ? "_u16" : "_s16";
253       break;
254     case 'i':
255       s += usgn ? "_u32" : "_s32";
256       break;
257     case 'l':
258       s += usgn ? "_u64" : "_s64";
259       break;
260     case 'h':
261       s += "_f16";
262       break;
263     case 'f':
264       s += "_f32";
265       break;
266     default:
267       throw "unhandled type!";
268       break;
269   }
270
271   // Insert a 'q' before the first '_' character so that it ends up before 
272   // _lane or _n on vector-scalar operations.
273   if (quad) {
274     size_t pos = s.find('_');
275     s = s.insert(pos, "q");
276   }
277   return s;
278 }
279
280 // Generate the string "(argtype a, argtype b, ...)"
281 static std::string GenArgs(const std::string &proto, StringRef typestr) {
282   char arg = 'a';
283   
284   std::string s;
285   s += "(";
286   
287   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
288     s += TypeString(proto[i], typestr);
289     s.push_back(' ');
290     s.push_back(arg);
291     if ((i + 1) < e)
292       s += ", ";
293   }
294   
295   s += ")";
296   return s;
297 }
298
299 static OpKind ParseOp(Record *R) {
300   return OpNone;
301 }
302
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) {
308   return "";
309 }
310
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) {
316   char arg = 'a';
317   std::string s("return ");
318   
319   // FIXME: if return type is 2/3/4, emit unioning code.
320   
321   if (structTypes) {
322     s += "(";
323     s += TypeString(proto[0], typestr);
324     s += "){";
325   }
326   
327   s += "__builtin_neon_";
328   s += name;
329   s += "(";
330   
331   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
332     s.push_back(arg);
333     if (structTypes)
334       s += ".val";
335     if ((i + 1) < e)
336       s += ", ";
337   }
338   
339   s += ")";
340   if (structTypes)
341     s += "}";
342   s += ";";
343   return s;
344 }
345
346 void NeonEmitter::run(raw_ostream &OS) {
347   EmitSourceFileHeader("ARM NEON Header", OS);
348   
349   // FIXME: emit license into file?
350   
351   OS << "#ifndef __ARM_NEON_H\n";
352   OS << "#define __ARM_NEON_H\n\n";
353   
354   OS << "#ifndef __ARM_NEON__\n";
355   OS << "#error \"NEON support not enabled\"\n";
356   OS << "#endif\n\n";
357
358   OS << "#include <stdint.h>\n\n";
359
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";
365   
366   // Emit Neon vector typedefs.
367   std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
368   SmallVector<StringRef, 24> TDTypeVec;
369   ParseTypes(0, TypedefTypes, TDTypeVec);
370
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]);
378     OS << " __neon_";
379     OS << TypeString('d', TDTypeVec[i]) << "\n";
380   }
381   OS << "\n";
382
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";
390       if (vi > 1)
391         OS << "[" << utostr(vi) << "]";
392       OS << ";\n} " << vs << ";\n\n";
393     }
394   }
395   
396   OS << "#define __ai static __attribute__((__always_inline__))\n\n";
397
398   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
399   
400   // Unique the return+pattern types, and assign them.
401   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
402     Record *R = RV[i];
403     std::string name = LowercaseString(R->getName());
404     std::string Proto = R->getValueAsString("Prototype");
405     std::string Types = R->getValueAsString("Types");
406     
407     SmallVector<StringRef, 16> TypeVec;
408     ParseTypes(R, Types, TypeVec);
409     
410     OpKind k = ParseOp(R);
411     
412     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
413       assert(!Proto.empty() && "");
414       
415       // static always inline + return type
416       OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
417       
418       // Function name with type suffix
419       OS << " " << MangleName(name, TypeVec[ti]);
420       
421       // Function arguments
422       OS << GenArgs(Proto, TypeVec[ti]);
423       
424       // Definition.
425       OS << " { ";
426       
427       if (k != OpNone)
428         OS << GenOpString(k, Proto);
429       else
430         OS << GenBuiltin(name, Proto, TypeVec[ti]);
431
432       OS << " }\n";
433     }
434     OS << "\n";
435   }
436
437   // TODO: 
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.
441   
442   OS << "#endif /* __ARM_NEON_H */\n";
443 }