b385b7639a2c2a59cf95bb36fa2786ce03253362
[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 "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include <string>
21
22 using namespace llvm;
23
24 static void ParseTypes(Record *r, std::string &s,
25                        SmallVectorImpl<StringRef> &TV) {
26   const char *data = s.data();
27   int len = 0;
28   
29   for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
30     if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
31       continue;
32     
33     switch (data[len]) {
34       case 'c':
35       case 's':
36       case 'i':
37       case 'l':
38       case 'h':
39       case 'f':
40         break;
41       default:
42         throw TGError(r->getLoc(),
43                       "Unexpected letter: " + std::string(data + len, 1));
44         break;
45     }
46     TV.push_back(StringRef(data, len + 1));
47     data += len + 1;
48     len = -1;
49   }
50 }
51
52 static char Widen(const char t) {
53   switch (t) {
54     case 'c':
55       return 's';
56     case 's':
57       return 'i';
58     case 'i':
59       return 'l';
60     default: throw "unhandled type in widen!";
61   }
62   return '\0';
63 }
64
65 static char Narrow(const char t) {
66   switch (t) {
67     case 's':
68       return 'c';
69     case 'i':
70       return 's';
71     case 'l':
72       return 'i';
73     default: throw "unhandled type in widen!";
74   }
75   return '\0';
76 }
77
78 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
79   unsigned off = 0;
80   
81   // remember quad.
82   if (ty[off] == 'Q') {
83     quad = true;
84     ++off;
85   }
86   
87   // remember poly.
88   if (ty[off] == 'P') {
89     poly = true;
90     ++off;
91   }
92   
93   // remember unsigned.
94   if (ty[off] == 'U') {
95     usgn = true;
96     ++off;
97   }
98   
99   // base type to get the type string for.
100   return ty[off];
101 }
102
103 static std::string TypeString(const char mod, StringRef typestr) {
104   bool quad = false;
105   bool poly = false;
106   bool usgn = false;
107   bool scal = false;
108   bool cnst = false;
109   bool pntr = false;
110   
111   // base type to get the type string for.
112   char type = ClassifyType(typestr, quad, poly, usgn);
113   
114   // Based on the modifying character, change the type and width if necessary.
115   switch (mod) {
116     case 'v':
117       return "void";
118     case 'i':
119       return "int";
120     case 't':
121       if (poly) {
122         poly = false;
123         usgn = true;
124       }
125       break;
126     case 'x':
127       usgn = true;
128       poly = false;
129       if (type == 'f')
130         type = 'i';
131       break;
132     case 'f':
133       type = 'f';
134       usgn = false;
135       break;
136     case 'w':
137       type = Widen(type);
138       quad = true;
139       break;
140     case 'n':
141       type = Widen(type);
142       break;
143     case 'l':
144       type = 'l';
145       scal = true;
146       usgn = true;
147       break;
148     case 's':
149       scal = true;
150       break;
151     case 'k':
152       quad = true;
153       break;
154     case 'c':
155       cnst = true;
156     case 'p':
157       pntr = true;
158       scal = true;
159       break;
160     case 'h':
161       type = Narrow(type);
162       break;
163     case 'e':
164       type = Narrow(type);
165       usgn = true;
166       break;
167     default:
168       break;
169   }
170   
171   SmallString<128> s;
172   
173   if (usgn)
174     s.push_back('u');
175   
176   switch (type) {
177     case 'c':
178       s += poly ? "poly8" : "int8";
179       if (scal)
180         break;
181       s += quad ? "x16" : "x8";
182       break;
183     case 's':
184       s += poly ? "poly16" : "int16";
185       if (scal)
186         break;
187       s += quad ? "x8" : "x4";
188       break;
189     case 'i':
190       s += "int32";
191       if (scal)
192         break;
193       s += quad ? "x4" : "x2";
194       break;
195     case 'l':
196       s += "int64";
197       if (scal)
198         break;
199       s += quad ? "x2" : "x1";
200       break;
201     case 'h':
202       s += "float16";
203       if (scal)
204         break;
205       s += quad ? "x8" : "x4";
206       break;
207     case 'f':
208       s += "float32";
209       if (scal)
210         break;
211       s += quad ? "x4" : "x2";
212       break;
213     default:
214       throw "unhandled type!";
215       break;
216   }
217
218   if (mod == '2')
219     s += "x2";
220   if (mod == '3')
221     s += "x3";
222   if (mod == '4')
223     s += "x4";
224   
225   // Append _t, finishing the type string typedef type.
226   s += "_t";
227   
228   if (cnst)
229     s += " const";
230   
231   if (pntr)
232     s += " *";
233   
234   return s.str();
235 }
236
237 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
238 static std::string MangleName(const std::string &name, StringRef typestr,
239                               ClassKind ck) {
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     switch (ck) {
250     case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break;
251     case ClassI: s += "_i8"; break;
252     case ClassW: s += "_8"; break;
253     default: break;
254     }
255     break;
256   case 's':
257     switch (ck) {
258     case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break;
259     case ClassI: s += "_i16"; break;
260     case ClassW: s += "_16"; break;
261     default: break;
262     }
263     break;
264   case 'i':
265     switch (ck) {
266     case ClassS: s += usgn ? "_u32" : "_s32"; break;
267     case ClassI: s += "_i32"; break;
268     case ClassW: s += "_32"; break;
269     default: break;
270     }
271     break;
272   case 'l':
273     switch (ck) {
274     case ClassS: s += usgn ? "_u64" : "_s64"; break;
275     case ClassI: s += "_i64"; break;
276     case ClassW: s += "_64"; break;
277     default: break;
278     }
279     break;
280   case 'h':
281     switch (ck) {
282     case ClassS:
283     case ClassI: s += "_f16"; break;
284     case ClassW: s += "_16"; break;
285     default: break;
286     }
287     break;
288   case 'f':
289     switch (ck) {
290     case ClassS:
291     case ClassI: s += "_f32"; break;
292     case ClassW: s += "_32"; break;
293     default: break;
294     }
295     break;
296   default:
297     throw "unhandled type!";
298     break;
299   }
300   if (ck == ClassB)
301     return s += "_v";
302     
303   // Insert a 'q' before the first '_' character so that it ends up before 
304   // _lane or _n on vector-scalar operations.
305   if (quad) {
306     size_t pos = s.find('_');
307     s = s.insert(pos, "q");
308   }
309   return s;
310 }
311
312 // Generate the string "(argtype a, argtype b, ...)"
313 static std::string GenArgs(const std::string &proto, StringRef typestr) {
314   char arg = 'a';
315   
316   std::string s;
317   s += "(";
318   
319   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
320     s += TypeString(proto[i], typestr);
321     s.push_back(' ');
322     s.push_back(arg);
323     if ((i + 1) < e)
324       s += ", ";
325   }
326   
327   s += ")";
328   return s;
329 }
330
331 // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd.
332 // If structTypes is true, the NEON types are structs of vector types rather
333 // than vector types, and the call becomes "a.val + b.val"
334 static std::string GenOpString(OpKind op, const std::string &proto,
335                                StringRef typestr, bool structTypes = true) {
336   std::string s("return ");
337   std::string ts = TypeString(proto[0], typestr);
338   if (structTypes)
339     s += "(" + ts + "){";
340   
341   std::string a, b, c;
342   if (proto.size() > 1)
343     a = (structTypes && proto[1] != 'l') ? "a.val" : "a";
344   b = structTypes ? "b.val" : "b";
345   c = structTypes ? "c.val" : "c";
346   
347   switch(op) {
348   case OpAdd:
349     s += a + " + " + b;
350     break;
351   case OpSub:
352     s += a + " - " + b;
353     break;
354   case OpMul:
355     s += a + " * " + b;
356     break;
357   case OpMla:
358     s += a + " + ( " + b + " * " + c + " )";
359     break;
360   case OpMls:
361     s += a + " - ( " + b + " * " + c + " )";
362     break;
363   case OpEq:
364     s += "(__neon_" + ts + ")(" + a + " == " + b + ")";
365     break;
366   case OpGe:
367     s += "(__neon_" + ts + ")(" + a + " >= " + b + ")";
368     break;
369   case OpLe:
370     s += "(__neon_" + ts + ")(" + a + " <= " + b + ")";
371     break;
372   case OpGt:
373     s += "(__neon_" + ts + ")(" + a + " > " + b + ")";
374     break;
375   case OpLt:
376     s += "(__neon_" + ts + ")(" + a + " < " + b + ")";
377     break;
378   case OpNeg:
379     s += " -" + a;
380     break;
381   case OpNot:
382     s += " ~" + a;
383     break;
384   case OpAnd:
385     s += a + " & " + b;
386     break;
387   case OpOr:
388     s += a + " | " + b;
389     break;
390   case OpXor:
391     s += a + " ^ " + b;
392     break;
393   case OpAndNot:
394     s += a + " & ~" + b;
395     break;
396   case OpOrNot:
397     s += a + " | ~" + b;
398     break;
399   case OpCast:
400     s += "(__neon_" + ts + ")" + a;
401     break;
402   default:
403     throw "unknown OpKind!";
404     break;
405   }
406   
407   if (structTypes)
408     s += "}";
409   s += ";";
410   return s;
411 }
412
413 // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a)
414 // If structTypes is true, the NEON types are structs of vector types rather
415 // than vector types, and the call becomes __builtin_neon_cls(a.val)
416 static std::string GenBuiltin(const std::string &name, const std::string &proto,
417                               StringRef typestr, ClassKind ck,
418                               bool structTypes = true) {
419   char arg = 'a';
420   std::string s;
421   
422   if (proto[0] != 'v') {
423     // FIXME: if return type is 2/3/4, emit unioning code.
424     s += "return ";
425     if (structTypes) {
426       s += "(";
427       s += TypeString(proto[0], typestr);
428       s += "){";
429     }
430   }    
431   
432   s += "__builtin_neon_";
433   s += MangleName(name, typestr, ck);
434   s += "(";
435   
436   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
437     s.push_back(arg);
438     if (structTypes && proto[i] != 's' && proto[i] != 'i' && proto[i] != 'l' &&
439         proto[i] != 'p' && proto[i] != 'c') {
440       s += ".val";
441     }
442     if ((i + 1) < e)
443       s += ", ";
444   }
445   
446   s += ")";
447   if (proto[0] != 'v' && structTypes)
448     s += "}";
449   s += ";";
450   return s;
451 }
452
453 static std::string GenBuiltinDef(const std::string &name, 
454                                  const std::string &proto,
455                                  StringRef typestr, ClassKind ck) {
456   std::string s("BUILTIN(__builtin_neon_");
457   s += MangleName(name, typestr, ck);
458   s += ", \"";
459   
460   for (unsigned i = 0, e = proto.size(); i != e; ++i) {
461   }
462   
463   s += "\", \"n\")";
464   return s;
465 }
466
467 void NeonEmitter::run(raw_ostream &OS) {
468   EmitSourceFileHeader("ARM NEON Header", OS);
469   
470   // FIXME: emit license into file?
471   
472   OS << "#ifndef __ARM_NEON_H\n";
473   OS << "#define __ARM_NEON_H\n\n";
474   
475   OS << "#ifndef __ARM_NEON__\n";
476   OS << "#error \"NEON support not enabled\"\n";
477   OS << "#endif\n\n";
478
479   OS << "#include <stdint.h>\n\n";
480
481   // Emit NEON-specific scalar typedefs.
482   // FIXME: probably need to do something better for polynomial types.
483   // FIXME: is this the correct thing to do for float16?
484   OS << "typedef float float32_t;\n";
485   OS << "typedef uint8_t poly8_t;\n";
486   OS << "typedef uint16_t poly16_t;\n";
487   OS << "typedef uint16_t float16_t;\n";
488   
489   // Emit Neon vector typedefs.
490   std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs");
491   SmallVector<StringRef, 24> TDTypeVec;
492   ParseTypes(0, TypedefTypes, TDTypeVec);
493
494   // Emit vector typedefs.
495   for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
496     bool dummy, quad = false;
497     (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy);
498     OS << "typedef __attribute__(( __vector_size__(";
499     OS << (quad ? "16) )) " : "8) ))  ");
500     OS << TypeString('s', TDTypeVec[i]);
501     OS << " __neon_";
502     OS << TypeString('d', TDTypeVec[i]) << ";\n";
503   }
504   OS << "\n";
505
506   // Emit struct typedefs.
507   for (unsigned vi = 1; vi != 5; ++vi) {
508     for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) {
509       std::string ts = TypeString('d', TDTypeVec[i]);
510       std::string vs = (vi > 1) ? TypeString('0' + vi, TDTypeVec[i]) : ts;
511       OS << "typedef struct __" << vs << " {\n";
512       OS << "  __neon_" << ts << " val";
513       if (vi > 1)
514         OS << "[" << utostr(vi) << "]";
515       OS << ";\n} " << vs << ";\n\n";
516     }
517   }
518   
519   OS << "#define __ai static __attribute__((__always_inline__))\n\n";
520
521   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
522   
523   // Unique the return+pattern types, and assign them.
524   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
525     Record *R = RV[i];
526     std::string name = LowercaseString(R->getName());
527     std::string Proto = R->getValueAsString("Prototype");
528     std::string Types = R->getValueAsString("Types");
529     
530     SmallVector<StringRef, 16> TypeVec;
531     ParseTypes(R, Types, TypeVec);
532     
533     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
534     
535     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
536       assert(!Proto.empty() && "");
537       
538       // static always inline + return type
539       OS << "__ai " << TypeString(Proto[0], TypeVec[ti]);
540       
541       // Function name with type suffix
542       OS << " " << MangleName(name, TypeVec[ti], ClassS);
543       
544       // Function arguments
545       OS << GenArgs(Proto, TypeVec[ti]);
546       
547       // Definition.
548       OS << " { ";
549       
550       if (k != OpNone) {
551         OS << GenOpString(k, Proto, TypeVec[ti]);
552       } else {
553         if (R->getSuperClasses().size() < 2)
554           throw TGError(R->getLoc(), "Builtin has no class kind");
555         
556         ClassKind ck = ClassMap[R->getSuperClasses()[1]];
557
558         if (ck == ClassNone)
559           throw TGError(R->getLoc(), "Builtin has no class kind");
560         OS << GenBuiltin(name, Proto, TypeVec[ti], ck);
561       }
562
563       OS << " }\n";
564     }
565     OS << "\n";
566   }
567   OS << "#undef __ai\n\n";
568   OS << "#endif /* __ARM_NEON_H */\n";
569 }
570
571 void NeonEmitter::runHeader(raw_ostream &OS) {
572   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
573
574   StringMap<OpKind> EmittedMap;
575   
576   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
577     Record *R = RV[i];
578
579     OpKind k = OpMap[R->getValueAsDef("Operand")->getName()];
580     if (k != OpNone)
581       continue;
582     
583     std::string name = LowercaseString(R->getName());
584     std::string Proto = R->getValueAsString("Prototype");
585     std::string Types = R->getValueAsString("Types");
586
587     SmallVector<StringRef, 16> TypeVec;
588     ParseTypes(R, Types, TypeVec);
589
590     if (R->getSuperClasses().size() < 2)
591       throw TGError(R->getLoc(), "Builtin has no class kind");
592     
593     ClassKind ck = ClassMap[R->getSuperClasses()[1]];
594     
595     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
596       std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck);
597       if (EmittedMap.count(bd))
598         continue;
599       
600       EmittedMap[bd] = OpNone;
601       OS << bd << "\n";
602     }
603   }
604 }