6bedb03ad5763e243422c4efc4449c321c9ee6fd
[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 static void ParseTypes(Record *r, std::string &s,
27                        SmallVectorImpl<StringRef> &TV) {
28   const char *data = s.data();
29   int len = 0;
30   
31   for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) {
32     if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U')
33       continue;
34     
35     switch (data[len]) {
36       case 'c':
37       case 's':
38       case 'i':
39       case 'l':
40       case 'h':
41       case 'f':
42         break;
43       default:
44         throw TGError(r->getLoc(),
45                       "Unexpected letter: " + std::string(data + len, 1));
46         break;
47     }
48     TV.push_back(StringRef(data, len + 1));
49     data += len + 1;
50     len = -1;
51   }
52 }
53
54 static const char Widen(const char t) {
55   switch (t) {
56     case 'c':
57       return 's';
58     case 's':
59       return 'i';
60     case 'i':
61       return 'l';
62     default: throw "unhandled type in widen!";
63   }
64   return '\0';
65 }
66
67 static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) {
68   unsigned off = 0;
69   
70   // remember quad.
71   if (ty[off] == 'Q') {
72     quad = true;
73     ++off;
74   }
75   
76   // remember poly.
77   if (ty[off] == 'P') {
78     poly = true;
79     ++off;
80   }
81   
82   // remember unsigned.
83   if (ty[off] == 'U') {
84     usgn = true;
85     ++off;
86   }
87   
88   // base type to get the type string for.
89   return ty[off];
90 }
91
92 static std::string TypeString(const char mod, StringRef typestr) {
93   bool quad = false;
94   bool poly = false;
95   bool usgn = false;
96   bool scal = false;
97   bool cnst = false;
98   bool pntr = false;
99   
100   // base type to get the type string for.
101   char type = ClassifyType(typestr, quad, poly, usgn);
102   
103   // Based on the modifying character, change the type and width if necessary.
104   switch (mod) {
105     case 'v':
106       type = 'v';
107       scal = true;
108       usgn = false;
109       break;
110     case 't':
111       if (poly) {
112         poly = false;
113         usgn = true;
114       }
115       break;
116     case 'x':
117       usgn = true;
118       if (type == 'f')
119         type = 'i';
120       break;
121     case 'f':
122       type = 'f';
123       break;
124     case 'w':
125       type = Widen(type);
126       quad = true;
127       break;
128     case 'n':
129       type = Widen(type);
130       break;
131     case 'i':
132       type = 'i';
133       scal = true;
134       usgn = false;
135       break;
136     case 'l':
137       type = 'l';
138       scal = true;
139       usgn = true;
140       break;
141     case 's':
142       scal = true;
143       break;
144     case 'k':
145       quad = true;
146       break;
147     case 'c':
148       cnst = true;
149     case 'p':
150       pntr = true;
151       scal = true;
152       break;
153     default:
154       break;
155   }
156   
157   SmallString<128> s;
158   
159   if (usgn)
160     s.push_back('u');
161   
162   switch (type) {
163     case 'c':
164       s += poly ? "poly8" : "int8";
165       if (scal)
166         break;
167       s += quad ? "x16" : "x8";
168       break;
169     case 's':
170       s += poly ? "poly16" : "int16";
171       if (scal)
172         break;
173       s += quad ? "x8" : "x4";
174       break;
175     case 'i':
176       s += "int32";
177       if (scal)
178         break;
179       s += quad ? "x4" : "x2";
180       break;
181     case 'l':
182       s += "int64";
183       if (scal)
184         break;
185       s += quad ? "x2" : "x1";
186       break;
187     case 'h':
188       s += "float16";
189       if (scal)
190         break;
191       s += quad ? "x8" : "x4";
192       break;
193     case 'f':
194       s += "float32";
195       if (scal)
196         break;
197       s += quad ? "x4" : "x2";
198       break;
199     case 'v':
200       s += "void";
201       break;
202     default:
203       throw "unhandled type!";
204       break;
205   }
206
207   if (mod == '2')
208     s += "x2";
209   if (mod == '3')
210     s += "x3";
211   if (mod == '4')
212     s += "x4";
213   
214   // Append _t, finishing the type string typedef type.
215   s += "_t";
216   
217   if (cnst)
218     s += " const";
219   
220   if (pntr)
221     s += " *";
222   
223   return s.str();
224 }
225
226 // Turn "vst2_lane" into "vst2q_lane_f32", etc.
227 static std::string MangleName(const std::string &name, StringRef typestr) {
228   bool quad = false;
229   bool poly = false;
230   bool usgn = false;
231   char type = ClassifyType(typestr, quad, poly, usgn);
232
233   std::string s = name;
234   
235   switch (type) {
236     case 'c':
237       s += poly ? "_p8" : usgn ? "_u8" : "_s8";
238       break;
239     case 's':
240       s += poly ? "_p16" : usgn ? "_u16" : "_s16";
241       break;
242     case 'i':
243       s += usgn ? "_u32" : "_s32";
244       break;
245     case 'l':
246       s += usgn ? "_u64" : "_s64";
247       break;
248     case 'h':
249       s += "_f16";
250       break;
251     case 'f':
252       s += "_f32";
253       break;
254     default:
255       throw "unhandled type!";
256       break;
257   }
258
259   // Insert a 'q' before the first '_' character so that it ends up before 
260   // _lane or _n on vector-scalar operations.
261   if (quad) {
262     size_t pos = s.find('_');
263     s = s.insert(pos, "q");
264   }
265   return s;
266 }
267
268 // Generate the string "(argtype a, argtype b, ...)"
269 static std::string GenArgs(const std::string &proto, StringRef typestr) {
270   char arg = 'a';
271   
272   std::string s;
273   s += "(";
274   
275   for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) {
276     s += TypeString(proto[i], typestr);
277     s.push_back(' ');
278     s.push_back(arg);
279     if ((i + 1) < e)
280       s += ", ";
281   }
282   
283   s += ")";
284   return s;
285 }
286
287 void NeonEmitter::run(raw_ostream &OS) {
288   EmitSourceFileHeader("ARM NEON Header", OS);
289   
290   // FIXME: emit license into file?
291   
292   OS << "#ifndef __ARM_NEON_H\n";
293   OS << "#define __ARM_NEON_H\n\n";
294   
295   OS << "#ifndef __ARM_NEON__\n";
296   OS << "#error \"NEON support not enabled\"\n";
297   OS << "#endif\n\n";
298
299   OS << "#include <stdint.h>\n\n";
300   
301   // EmitTypedefs(OS);
302   
303   std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst");
304   
305   // Initialize Type Map
306   
307   // Unique the return+pattern types, and assign them.
308   for (unsigned i = 0, e = RV.size(); i != e; ++i) {
309     Record *R = RV[i];
310     std::string name = LowercaseString(R->getName());
311     std::string Proto = R->getValueAsString("Prototype");
312     std::string Types = R->getValueAsString("Types");
313     
314     SmallVector<StringRef, 16> TypeVec;
315     ParseTypes(R, Types, TypeVec);
316     
317     for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) {
318       assert(!Proto.empty() && "");
319       
320       SmallString<128> Prototype;
321       Prototype += TypeString(Proto[0], TypeVec[ti]);
322       Prototype += " ";
323       Prototype += MangleName(name, TypeVec[ti]);
324       Prototype += GenArgs(Proto, TypeVec[ti]);
325       
326       OS << Prototype << ";\n";
327       
328       // gen definition
329     
330         // if (opcode)
331       
332           // gen opstring
333       
334         // gen builtin (args)
335     }
336     OS << "\n";
337   }
338
339   // TODO: 
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.
343   
344   OS << "\n#endif /* __ARM_NEON_H */\n";
345 }