Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 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 | // |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 14 | // Each NEON instruction is implemented in terms of 1 or more functions which |
| 15 | // are suffixed with the element type of the input vectors. Functions may be |
| 16 | // implemented in terms of generic vector operations such as +, *, -, etc. or |
| 17 | // by calling a __builtin_-prefixed function which will be handled by clang's |
| 18 | // CodeGen library. |
| 19 | // |
| 20 | // Additional validation code can be generated by this file when runHeader() is |
| 21 | // called, rather than the normal run() entry point. |
| 22 | // |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "NeonEmitter.h" |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 29 | #include <string> |
| 30 | |
| 31 | using namespace llvm; |
| 32 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 33 | /// ParseTypes - break down a string such as "fQf" into a vector of StringRefs, |
| 34 | /// which each StringRef representing a single type declared in the string. |
| 35 | /// for "fQf" we would end up with 2 StringRefs, "f", and "Qf", representing |
| 36 | /// 2xfloat and 4xfloat respectively. |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 37 | static void ParseTypes(Record *r, std::string &s, |
| 38 | SmallVectorImpl<StringRef> &TV) { |
| 39 | const char *data = s.data(); |
| 40 | int len = 0; |
| 41 | |
| 42 | for (unsigned i = 0, e = s.size(); i != e; ++i, ++len) { |
| 43 | if (data[len] == 'P' || data[len] == 'Q' || data[len] == 'U') |
| 44 | continue; |
| 45 | |
| 46 | switch (data[len]) { |
| 47 | case 'c': |
| 48 | case 's': |
| 49 | case 'i': |
| 50 | case 'l': |
| 51 | case 'h': |
| 52 | case 'f': |
| 53 | break; |
| 54 | default: |
| 55 | throw TGError(r->getLoc(), |
| 56 | "Unexpected letter: " + std::string(data + len, 1)); |
| 57 | break; |
| 58 | } |
| 59 | TV.push_back(StringRef(data, len + 1)); |
| 60 | data += len + 1; |
| 61 | len = -1; |
| 62 | } |
| 63 | } |
| 64 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 65 | /// Widen - Convert a type code into the next wider type. char -> short, |
| 66 | /// short -> int, etc. |
Duncan Sands | 8dbbace | 2010-06-02 08:37:30 +0000 | [diff] [blame] | 67 | static char Widen(const char t) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 68 | switch (t) { |
| 69 | case 'c': |
| 70 | return 's'; |
| 71 | case 's': |
| 72 | return 'i'; |
| 73 | case 'i': |
| 74 | return 'l'; |
| 75 | default: throw "unhandled type in widen!"; |
| 76 | } |
| 77 | return '\0'; |
| 78 | } |
| 79 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 80 | /// Narrow - Convert a type code into the next smaller type. short -> char, |
| 81 | /// float -> half float, etc. |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 82 | static char Narrow(const char t) { |
| 83 | switch (t) { |
| 84 | case 's': |
| 85 | return 'c'; |
| 86 | case 'i': |
| 87 | return 's'; |
| 88 | case 'l': |
| 89 | return 'i'; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 90 | case 'f': |
| 91 | return 'h'; |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 92 | default: throw "unhandled type in narrow!"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 93 | } |
| 94 | return '\0'; |
| 95 | } |
| 96 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 97 | /// For a particular StringRef, return the base type code, and whether it has |
| 98 | /// the quad-vector, polynomial, or unsigned modifiers set. |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 99 | static char ClassifyType(StringRef ty, bool &quad, bool &poly, bool &usgn) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 100 | unsigned off = 0; |
| 101 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 102 | // remember quad. |
| 103 | if (ty[off] == 'Q') { |
| 104 | quad = true; |
| 105 | ++off; |
| 106 | } |
| 107 | |
| 108 | // remember poly. |
| 109 | if (ty[off] == 'P') { |
| 110 | poly = true; |
| 111 | ++off; |
| 112 | } |
| 113 | |
| 114 | // remember unsigned. |
| 115 | if (ty[off] == 'U') { |
| 116 | usgn = true; |
| 117 | ++off; |
| 118 | } |
| 119 | |
| 120 | // base type to get the type string for. |
| 121 | return ty[off]; |
| 122 | } |
| 123 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 124 | /// ModType - Transform a type code and its modifiers based on a mod code. The |
| 125 | /// mod code definitions may be found at the top of arm_neon.td. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 126 | static char ModType(const char mod, char type, bool &quad, bool &poly, |
| 127 | bool &usgn, bool &scal, bool &cnst, bool &pntr) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 128 | switch (mod) { |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 129 | case 't': |
| 130 | if (poly) { |
| 131 | poly = false; |
| 132 | usgn = true; |
| 133 | } |
| 134 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 135 | case 'u': |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 136 | usgn = true; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 137 | case 'x': |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 138 | poly = false; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 139 | if (type == 'f') |
| 140 | type = 'i'; |
| 141 | break; |
| 142 | case 'f': |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 143 | if (type == 'h') |
| 144 | quad = true; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 145 | type = 'f'; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 146 | usgn = false; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 147 | break; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 148 | case 'g': |
| 149 | quad = false; |
| 150 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 151 | case 'w': |
| 152 | type = Widen(type); |
| 153 | quad = true; |
| 154 | break; |
| 155 | case 'n': |
| 156 | type = Widen(type); |
| 157 | break; |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 158 | case 'i': |
| 159 | type = 'i'; |
| 160 | scal = true; |
| 161 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 162 | case 'l': |
| 163 | type = 'l'; |
| 164 | scal = true; |
| 165 | usgn = true; |
| 166 | break; |
| 167 | case 's': |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 168 | case 'a': |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 169 | scal = true; |
| 170 | break; |
| 171 | case 'k': |
| 172 | quad = true; |
| 173 | break; |
| 174 | case 'c': |
| 175 | cnst = true; |
| 176 | case 'p': |
| 177 | pntr = true; |
| 178 | scal = true; |
| 179 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 180 | case 'h': |
| 181 | type = Narrow(type); |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 182 | if (type == 'h') |
| 183 | quad = false; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 184 | break; |
| 185 | case 'e': |
| 186 | type = Narrow(type); |
| 187 | usgn = true; |
| 188 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 189 | default: |
| 190 | break; |
| 191 | } |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 192 | return type; |
| 193 | } |
| 194 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 195 | /// TypeString - for a modifier and type, generate the name of the typedef for |
| 196 | /// that type. If generic is true, emit the generic vector type rather than |
Bob Wilson | 1ac27cf | 2010-06-24 22:04:30 +0000 | [diff] [blame] | 197 | /// the public NEON type. QUc -> uint8x8_t / __neon_uint8x8_t. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 198 | static std::string TypeString(const char mod, StringRef typestr, |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 199 | bool generic = false) { |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 200 | bool quad = false; |
| 201 | bool poly = false; |
| 202 | bool usgn = false; |
| 203 | bool scal = false; |
| 204 | bool cnst = false; |
| 205 | bool pntr = false; |
| 206 | |
| 207 | if (mod == 'v') |
| 208 | return "void"; |
| 209 | if (mod == 'i') |
| 210 | return "int"; |
| 211 | |
| 212 | // base type to get the type string for. |
| 213 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 214 | |
| 215 | // Based on the modifying character, change the type and width if necessary. |
| 216 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 217 | |
| 218 | SmallString<128> s; |
| 219 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 220 | if (generic) |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 221 | s += "__neon_"; |
| 222 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 223 | if (usgn) |
| 224 | s.push_back('u'); |
| 225 | |
| 226 | switch (type) { |
| 227 | case 'c': |
| 228 | s += poly ? "poly8" : "int8"; |
| 229 | if (scal) |
| 230 | break; |
| 231 | s += quad ? "x16" : "x8"; |
| 232 | break; |
| 233 | case 's': |
| 234 | s += poly ? "poly16" : "int16"; |
| 235 | if (scal) |
| 236 | break; |
| 237 | s += quad ? "x8" : "x4"; |
| 238 | break; |
| 239 | case 'i': |
| 240 | s += "int32"; |
| 241 | if (scal) |
| 242 | break; |
| 243 | s += quad ? "x4" : "x2"; |
| 244 | break; |
| 245 | case 'l': |
| 246 | s += "int64"; |
| 247 | if (scal) |
| 248 | break; |
| 249 | s += quad ? "x2" : "x1"; |
| 250 | break; |
| 251 | case 'h': |
| 252 | s += "float16"; |
| 253 | if (scal) |
| 254 | break; |
| 255 | s += quad ? "x8" : "x4"; |
| 256 | break; |
| 257 | case 'f': |
| 258 | s += "float32"; |
| 259 | if (scal) |
| 260 | break; |
| 261 | s += quad ? "x4" : "x2"; |
| 262 | break; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 263 | default: |
| 264 | throw "unhandled type!"; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | if (mod == '2') |
| 269 | s += "x2"; |
| 270 | if (mod == '3') |
| 271 | s += "x3"; |
| 272 | if (mod == '4') |
| 273 | s += "x4"; |
| 274 | |
| 275 | // Append _t, finishing the type string typedef type. |
| 276 | s += "_t"; |
| 277 | |
| 278 | if (cnst) |
| 279 | s += " const"; |
| 280 | |
| 281 | if (pntr) |
| 282 | s += " *"; |
| 283 | |
| 284 | return s.str(); |
| 285 | } |
| 286 | |
Bob Wilson | 1ac27cf | 2010-06-24 22:04:30 +0000 | [diff] [blame] | 287 | /// BuiltinTypeString - for a modifier and type, generate the clang |
| 288 | /// BuiltinsARM.def prototype code for the function. See the top of clang's |
| 289 | /// Builtins.def for a description of the type strings. |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 290 | static std::string BuiltinTypeString(const char mod, StringRef typestr, |
| 291 | ClassKind ck, bool ret) { |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 292 | bool quad = false; |
| 293 | bool poly = false; |
| 294 | bool usgn = false; |
| 295 | bool scal = false; |
| 296 | bool cnst = false; |
| 297 | bool pntr = false; |
| 298 | |
| 299 | if (mod == 'v') |
| 300 | return "v"; |
| 301 | if (mod == 'i') |
| 302 | return "i"; |
| 303 | |
| 304 | // base type to get the type string for. |
| 305 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 306 | |
| 307 | // Based on the modifying character, change the type and width if necessary. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 308 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
| 309 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 310 | if (pntr) { |
| 311 | usgn = false; |
| 312 | poly = false; |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 313 | type = 'v'; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 314 | } |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 315 | if (type == 'h') { |
| 316 | type = 's'; |
| 317 | usgn = true; |
| 318 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 319 | usgn = usgn | poly | ((ck == ClassI || ck == ClassW) && scal && type != 'f'); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 320 | |
| 321 | if (scal) { |
| 322 | SmallString<128> s; |
| 323 | |
| 324 | if (usgn) |
| 325 | s.push_back('U'); |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 326 | |
| 327 | if (type == 'l') |
| 328 | s += "LLi"; |
| 329 | else |
| 330 | s.push_back(type); |
| 331 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 332 | if (cnst) |
| 333 | s.push_back('C'); |
| 334 | if (pntr) |
| 335 | s.push_back('*'); |
| 336 | return s.str(); |
| 337 | } |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 338 | |
| 339 | // Since the return value must be one type, return a vector type of the |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 340 | // appropriate width which we will bitcast. An exception is made for |
| 341 | // returning structs of 2, 3, or 4 vectors which are returned in a sret-like |
| 342 | // fashion, storing them to a pointer arg. |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 343 | if (ret) { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 344 | if (mod == '2' || mod == '3' || mod == '4') |
| 345 | return "vv*"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 346 | if (mod == 'f' || (ck != ClassB && type == 'f')) |
Nate Begeman | 5638783 | 2010-06-08 06:01:16 +0000 | [diff] [blame] | 347 | return quad ? "V4f" : "V2f"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 348 | if (ck != ClassB && type == 's') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 349 | return quad ? "V8s" : "V4s"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 350 | if (ck != ClassB && type == 'i') |
Nate Begeman | 5638783 | 2010-06-08 06:01:16 +0000 | [diff] [blame] | 351 | return quad ? "V4i" : "V2i"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 352 | if (ck != ClassB && type == 'l') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 353 | return quad ? "V2LLi" : "V1LLi"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 354 | |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 355 | return quad ? "V16c" : "V8c"; |
| 356 | } |
| 357 | |
| 358 | // Non-return array types are passed as individual vectors. |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 359 | if (mod == '2') |
| 360 | return quad ? "V16cV16c" : "V8cV8c"; |
| 361 | if (mod == '3') |
| 362 | return quad ? "V16cV16cV16c" : "V8cV8cV8c"; |
| 363 | if (mod == '4') |
| 364 | return quad ? "V16cV16cV16cV16c" : "V8cV8cV8cV8c"; |
| 365 | |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 366 | if (mod == 'f' || (ck != ClassB && type == 'f')) |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 367 | return quad ? "V4f" : "V2f"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 368 | if (ck != ClassB && type == 's') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 369 | return quad ? "V8s" : "V4s"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 370 | if (ck != ClassB && type == 'i') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 371 | return quad ? "V4i" : "V2i"; |
Nate Begeman | f50551e | 2010-06-09 18:02:26 +0000 | [diff] [blame] | 372 | if (ck != ClassB && type == 'l') |
Nate Begeman | 007afe4 | 2010-06-09 05:11:55 +0000 | [diff] [blame] | 373 | return quad ? "V2LLi" : "V1LLi"; |
| 374 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 375 | return quad ? "V16c" : "V8c"; |
| 376 | } |
| 377 | |
Bob Wilson | 9969bc3 | 2010-06-24 22:03:41 +0000 | [diff] [blame] | 378 | /// StructTag - generate the name of the struct tag for a type. |
| 379 | /// These names are mandated by ARM's ABI. |
| 380 | static std::string StructTag(StringRef typestr) { |
| 381 | bool quad = false; |
| 382 | bool poly = false; |
| 383 | bool usgn = false; |
| 384 | |
| 385 | // base type to get the type string for. |
| 386 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 387 | |
| 388 | SmallString<128> s; |
| 389 | s += "__simd"; |
| 390 | s += quad ? "128_" : "64_"; |
| 391 | if (usgn) |
| 392 | s.push_back('u'); |
| 393 | |
| 394 | switch (type) { |
| 395 | case 'c': |
| 396 | s += poly ? "poly8" : "int8"; |
| 397 | break; |
| 398 | case 's': |
| 399 | s += poly ? "poly16" : "int16"; |
| 400 | break; |
| 401 | case 'i': |
| 402 | s += "int32"; |
| 403 | break; |
| 404 | case 'l': |
| 405 | s += "int64"; |
| 406 | break; |
| 407 | case 'h': |
| 408 | s += "float16"; |
| 409 | break; |
| 410 | case 'f': |
| 411 | s += "float32"; |
| 412 | break; |
| 413 | default: |
| 414 | throw "unhandled type!"; |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | // Append _t, finishing the struct tag name. |
| 419 | s += "_t"; |
| 420 | |
| 421 | return s.str(); |
| 422 | } |
| 423 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 424 | /// MangleName - Append a type or width suffix to a base neon function name, |
| 425 | /// and insert a 'q' in the appropriate location if the operation works on |
| 426 | /// 128b rather than 64b. E.g. turn "vst2_lane" into "vst2q_lane_f32", etc. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 427 | static std::string MangleName(const std::string &name, StringRef typestr, |
| 428 | ClassKind ck) { |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 429 | if (name == "vcvt_f32_f16") |
| 430 | return name; |
| 431 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 432 | bool quad = false; |
| 433 | bool poly = false; |
| 434 | bool usgn = false; |
| 435 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 436 | |
| 437 | std::string s = name; |
| 438 | |
| 439 | switch (type) { |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 440 | case 'c': |
| 441 | switch (ck) { |
| 442 | case ClassS: s += poly ? "_p8" : usgn ? "_u8" : "_s8"; break; |
| 443 | case ClassI: s += "_i8"; break; |
| 444 | case ClassW: s += "_8"; break; |
| 445 | default: break; |
| 446 | } |
| 447 | break; |
| 448 | case 's': |
| 449 | switch (ck) { |
| 450 | case ClassS: s += poly ? "_p16" : usgn ? "_u16" : "_s16"; break; |
| 451 | case ClassI: s += "_i16"; break; |
| 452 | case ClassW: s += "_16"; break; |
| 453 | default: break; |
| 454 | } |
| 455 | break; |
| 456 | case 'i': |
| 457 | switch (ck) { |
| 458 | case ClassS: s += usgn ? "_u32" : "_s32"; break; |
| 459 | case ClassI: s += "_i32"; break; |
| 460 | case ClassW: s += "_32"; break; |
| 461 | default: break; |
| 462 | } |
| 463 | break; |
| 464 | case 'l': |
| 465 | switch (ck) { |
| 466 | case ClassS: s += usgn ? "_u64" : "_s64"; break; |
| 467 | case ClassI: s += "_i64"; break; |
| 468 | case ClassW: s += "_64"; break; |
| 469 | default: break; |
| 470 | } |
| 471 | break; |
| 472 | case 'h': |
| 473 | switch (ck) { |
| 474 | case ClassS: |
| 475 | case ClassI: s += "_f16"; break; |
| 476 | case ClassW: s += "_16"; break; |
| 477 | default: break; |
| 478 | } |
| 479 | break; |
| 480 | case 'f': |
| 481 | switch (ck) { |
| 482 | case ClassS: |
| 483 | case ClassI: s += "_f32"; break; |
| 484 | case ClassW: s += "_32"; break; |
| 485 | default: break; |
| 486 | } |
| 487 | break; |
| 488 | default: |
| 489 | throw "unhandled type!"; |
| 490 | break; |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 491 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 492 | if (ck == ClassB) |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 493 | s += "_v"; |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 494 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 495 | // Insert a 'q' before the first '_' character so that it ends up before |
| 496 | // _lane or _n on vector-scalar operations. |
| 497 | if (quad) { |
| 498 | size_t pos = s.find('_'); |
| 499 | s = s.insert(pos, "q"); |
| 500 | } |
| 501 | return s; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 504 | // Generate the string "(argtype a, argtype b, ...)" |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 505 | static std::string GenArgs(const std::string &proto, StringRef typestr) { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 506 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 507 | char arg = 'a'; |
| 508 | |
| 509 | std::string s; |
| 510 | s += "("; |
| 511 | |
| 512 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 513 | if (!define) { |
| 514 | s += TypeString(proto[i], typestr); |
| 515 | s.push_back(' '); |
| 516 | } |
Nate Begeman | af905ef | 2010-06-02 06:17:19 +0000 | [diff] [blame] | 517 | s.push_back(arg); |
| 518 | if ((i + 1) < e) |
| 519 | s += ", "; |
| 520 | } |
| 521 | |
| 522 | s += ")"; |
| 523 | return s; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 526 | static std::string Duplicate(unsigned nElts, StringRef typestr, |
| 527 | const std::string &a) { |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 528 | std::string s; |
| 529 | |
| 530 | s = "(__neon_" + TypeString('d', typestr) + "){ "; |
| 531 | for (unsigned i = 0; i != nElts; ++i) { |
| 532 | s += a; |
| 533 | if ((i + 1) < nElts) |
| 534 | s += ", "; |
| 535 | } |
| 536 | s += " }"; |
| 537 | |
| 538 | return s; |
| 539 | } |
| 540 | |
| 541 | // Generate the definition for this intrinsic, e.g. "a + b" for OpAdd. |
| 542 | // If structTypes is true, the NEON types are structs of vector types rather |
| 543 | // than vector types, and the call becomes "a.val + b.val" |
| 544 | static std::string GenOpString(OpKind op, const std::string &proto, |
| 545 | StringRef typestr, bool structTypes = true) { |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 546 | bool dummy, quad = false; |
| 547 | char type = ClassifyType(typestr, quad, dummy, dummy); |
| 548 | unsigned nElts = 0; |
| 549 | switch (type) { |
| 550 | case 'c': nElts = 8; break; |
| 551 | case 's': nElts = 4; break; |
| 552 | case 'i': nElts = 2; break; |
| 553 | case 'l': nElts = 1; break; |
| 554 | case 'h': nElts = 4; break; |
| 555 | case 'f': nElts = 2; break; |
| 556 | } |
| 557 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 558 | std::string ts = TypeString(proto[0], typestr); |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 559 | std::string s; |
| 560 | if (op == OpHi || op == OpLo) { |
| 561 | s = "union { " + ts + " r; double d; } u; u.d"; |
| 562 | } else { |
| 563 | s = ts + " r; r"; |
| 564 | if (structTypes) |
| 565 | s += ".val"; |
| 566 | } |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 567 | |
| 568 | s += " = "; |
| 569 | |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 570 | std::string a, b, c; |
| 571 | if (proto.size() > 1) |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 572 | a = (structTypes && proto[1] != 'l' && proto[1] != 's') ? "a.val" : "a"; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 573 | b = structTypes ? "b.val" : "b"; |
| 574 | c = structTypes ? "c.val" : "c"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 575 | |
| 576 | switch(op) { |
| 577 | case OpAdd: |
| 578 | s += a + " + " + b; |
| 579 | break; |
| 580 | case OpSub: |
| 581 | s += a + " - " + b; |
| 582 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 583 | case OpMulN: |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 584 | b = Duplicate(nElts << (int)quad, typestr, "b"); |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 585 | case OpMul: |
| 586 | s += a + " * " + b; |
| 587 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 588 | case OpMlaN: |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 589 | c = Duplicate(nElts << (int)quad, typestr, "c"); |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 590 | case OpMla: |
| 591 | s += a + " + ( " + b + " * " + c + " )"; |
| 592 | break; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 593 | case OpMlsN: |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 594 | c = Duplicate(nElts << (int)quad, typestr, "c"); |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 595 | case OpMls: |
| 596 | s += a + " - ( " + b + " * " + c + " )"; |
| 597 | break; |
| 598 | case OpEq: |
| 599 | s += "(__neon_" + ts + ")(" + a + " == " + b + ")"; |
| 600 | break; |
| 601 | case OpGe: |
| 602 | s += "(__neon_" + ts + ")(" + a + " >= " + b + ")"; |
| 603 | break; |
| 604 | case OpLe: |
| 605 | s += "(__neon_" + ts + ")(" + a + " <= " + b + ")"; |
| 606 | break; |
| 607 | case OpGt: |
| 608 | s += "(__neon_" + ts + ")(" + a + " > " + b + ")"; |
| 609 | break; |
| 610 | case OpLt: |
| 611 | s += "(__neon_" + ts + ")(" + a + " < " + b + ")"; |
| 612 | break; |
| 613 | case OpNeg: |
| 614 | s += " -" + a; |
| 615 | break; |
| 616 | case OpNot: |
| 617 | s += " ~" + a; |
| 618 | break; |
| 619 | case OpAnd: |
| 620 | s += a + " & " + b; |
| 621 | break; |
| 622 | case OpOr: |
| 623 | s += a + " | " + b; |
| 624 | break; |
| 625 | case OpXor: |
| 626 | s += a + " ^ " + b; |
| 627 | break; |
| 628 | case OpAndNot: |
| 629 | s += a + " & ~" + b; |
| 630 | break; |
| 631 | case OpOrNot: |
| 632 | s += a + " | ~" + b; |
| 633 | break; |
Nate Begeman | 3861e74 | 2010-06-03 21:35:22 +0000 | [diff] [blame] | 634 | case OpCast: |
| 635 | s += "(__neon_" + ts + ")" + a; |
| 636 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 637 | case OpConcat: |
| 638 | s += "__builtin_shufflevector((__neon_int64x1_t)" + a; |
| 639 | s += ", (__neon_int64x1_t)" + b + ", 0, 1)"; |
| 640 | break; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 641 | case OpHi: |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 642 | s += "(((__neon_float64x2_t)" + a + ")[1])"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 643 | break; |
| 644 | case OpLo: |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 645 | s += "(((__neon_float64x2_t)" + a + ")[0])"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 646 | break; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 647 | case OpDup: |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 648 | s += Duplicate(nElts << (int)quad, typestr, a); |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 649 | break; |
| 650 | case OpSelect: |
| 651 | // ((0 & 1) | (~0 & 2)) |
| 652 | ts = TypeString(proto[1], typestr); |
| 653 | s += "( " + a + " & (__neon_" + ts + ")" + b + ") | "; |
| 654 | s += "(~" + a + " & (__neon_" + ts + ")" + c + ")"; |
| 655 | break; |
| 656 | case OpRev16: |
| 657 | s += "__builtin_shufflevector(" + a + ", " + a; |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 658 | for (unsigned i = 2; i <= nElts << (int)quad; i += 2) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 659 | for (unsigned j = 0; j != 2; ++j) |
| 660 | s += ", " + utostr(i - j - 1); |
| 661 | s += ")"; |
| 662 | break; |
| 663 | case OpRev32: |
| 664 | nElts >>= 1; |
| 665 | s += "__builtin_shufflevector(" + a + ", " + a; |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 666 | for (unsigned i = nElts; i <= nElts << (1 + (int)quad); i += nElts) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 667 | for (unsigned j = 0; j != nElts; ++j) |
| 668 | s += ", " + utostr(i - j - 1); |
| 669 | s += ")"; |
| 670 | break; |
| 671 | case OpRev64: |
| 672 | s += "__builtin_shufflevector(" + a + ", " + a; |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 673 | for (unsigned i = nElts; i <= nElts << (int)quad; i += nElts) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 674 | for (unsigned j = 0; j != nElts; ++j) |
| 675 | s += ", " + utostr(i - j - 1); |
| 676 | s += ")"; |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 677 | break; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 678 | default: |
| 679 | throw "unknown OpKind!"; |
| 680 | break; |
| 681 | } |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 682 | if (op == OpHi || op == OpLo) |
| 683 | s += "; return u.r;"; |
| 684 | else |
| 685 | s += "; return r;"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 686 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 689 | static unsigned GetNeonEnum(const std::string &proto, StringRef typestr) { |
| 690 | unsigned mod = proto[0]; |
| 691 | unsigned ret = 0; |
| 692 | |
Nate Begeman | 900f467 | 2010-06-08 00:14:42 +0000 | [diff] [blame] | 693 | if (mod == 'v' || mod == 'f') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 694 | mod = proto[1]; |
| 695 | |
| 696 | bool quad = false; |
| 697 | bool poly = false; |
| 698 | bool usgn = false; |
| 699 | bool scal = false; |
| 700 | bool cnst = false; |
| 701 | bool pntr = false; |
| 702 | |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 703 | // Base type to get the type string for. |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 704 | char type = ClassifyType(typestr, quad, poly, usgn); |
| 705 | |
| 706 | // Based on the modifying character, change the type and width if necessary. |
| 707 | type = ModType(mod, type, quad, poly, usgn, scal, cnst, pntr); |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 708 | |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 709 | if (usgn) |
| 710 | ret |= 0x08; |
Nate Begeman | 59d70cb | 2010-08-06 01:24:11 +0000 | [diff] [blame] | 711 | if (quad && proto[1] != 'g') |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 712 | ret |= 0x10; |
| 713 | |
| 714 | switch (type) { |
| 715 | case 'c': |
| 716 | ret |= poly ? 5 : 0; |
| 717 | break; |
| 718 | case 's': |
| 719 | ret |= poly ? 6 : 1; |
| 720 | break; |
| 721 | case 'i': |
| 722 | ret |= 2; |
| 723 | break; |
| 724 | case 'l': |
| 725 | ret |= 3; |
| 726 | break; |
| 727 | case 'h': |
| 728 | ret |= 7; |
| 729 | break; |
| 730 | case 'f': |
| 731 | ret |= 4; |
| 732 | break; |
| 733 | default: |
| 734 | throw "unhandled type!"; |
| 735 | break; |
| 736 | } |
| 737 | return ret; |
| 738 | } |
| 739 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 740 | // Generate the definition for this intrinsic, e.g. __builtin_neon_cls(a) |
| 741 | // If structTypes is true, the NEON types are structs of vector types rather |
| 742 | // than vector types, and the call becomes __builtin_neon_cls(a.val) |
| 743 | static std::string GenBuiltin(const std::string &name, const std::string &proto, |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 744 | StringRef typestr, ClassKind ck, |
| 745 | bool structTypes = true) { |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 746 | bool dummy, quad = false; |
| 747 | char type = ClassifyType(typestr, quad, dummy, dummy); |
| 748 | unsigned nElts = 0; |
| 749 | switch (type) { |
| 750 | case 'c': nElts = 8; break; |
| 751 | case 's': nElts = 4; break; |
| 752 | case 'i': nElts = 2; break; |
| 753 | case 'l': nElts = 1; break; |
| 754 | case 'h': nElts = 4; break; |
| 755 | case 'f': nElts = 2; break; |
| 756 | } |
Chris Lattner | 5ca9698 | 2010-06-12 15:46:56 +0000 | [diff] [blame] | 757 | if (quad) nElts <<= 1; |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 758 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 759 | char arg = 'a'; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 760 | std::string s; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 761 | |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 762 | // If this builtin returns a struct 2, 3, or 4 vectors, pass it as an implicit |
| 763 | // sret-like argument. |
| 764 | bool sret = (proto[0] == '2' || proto[0] == '3' || proto[0] == '4'); |
| 765 | |
| 766 | // If this builtin takes an immediate argument, we need to #define it rather |
| 767 | // than use a standard declaration, so that SemaChecking can range check |
| 768 | // the immediate passed by the user. |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 769 | bool define = proto.find('i') != std::string::npos; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 770 | |
| 771 | // If all types are the same size, bitcasting the args will take care |
| 772 | // of arg checking. The actual signedness etc. will be taken care of with |
| 773 | // special enums. |
| 774 | if (proto.find('s') == std::string::npos) |
| 775 | ck = ClassB; |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 776 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 777 | if (proto[0] != 'v') { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 778 | std::string ts = TypeString(proto[0], typestr); |
| 779 | |
| 780 | if (define) { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 781 | if (sret) |
| 782 | s += "({ " + ts + " r; "; |
| 783 | else if (proto[0] != 's') |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 784 | s += "(" + ts + "){(__neon_" + ts + ")"; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 785 | } else if (sret) { |
| 786 | s += ts + " r; "; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 787 | } else { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 788 | s += ts + " r; r"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 789 | if (structTypes && proto[0] != 's' && proto[0] != 'i' && proto[0] != 'l') |
| 790 | s += ".val"; |
| 791 | |
| 792 | s += " = "; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 793 | } |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | bool splat = proto.find('a') != std::string::npos; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 797 | |
| 798 | s += "__builtin_neon_"; |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 799 | if (splat) { |
| 800 | std::string vname(name, 0, name.size()-2); |
| 801 | s += MangleName(vname, typestr, ck); |
| 802 | } else { |
| 803 | s += MangleName(name, typestr, ck); |
| 804 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 805 | s += "("; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 806 | |
| 807 | // Pass the address of the return variable as the first argument to sret-like |
| 808 | // builtins. |
| 809 | if (sret) |
| 810 | s += "&r, "; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 811 | |
| 812 | for (unsigned i = 1, e = proto.size(); i != e; ++i, ++arg) { |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 813 | std::string args = std::string(&arg, 1); |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 814 | bool argquad = quad; |
| 815 | bool scal = false; |
| 816 | |
| 817 | (void) ModType(proto[i], type, argquad, dummy, dummy, scal, dummy, dummy); |
| 818 | bool explicitcast = define && !scal; |
| 819 | |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 820 | if (define) |
| 821 | args = "(" + args + ")"; |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 822 | if (explicitcast) { |
| 823 | unsigned builtinelts = quad ? 16 : 8; |
| 824 | args = "(__neon_int8x" + utostr(builtinelts) + "_t)(" + args; |
| 825 | } |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 826 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 827 | // Handle multiple-vector values specially, emitting each subvector as an |
| 828 | // argument to the __builtin. |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 829 | if (structTypes && (proto[i] >= '2') && (proto[i] <= '4')) { |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 830 | for (unsigned vi = 0, ve = proto[i] - '0'; vi != ve; ++vi) { |
Bob Wilson | e9c2615 | 2010-06-24 22:21:19 +0000 | [diff] [blame] | 831 | s += args + ".val[" + utostr(vi) + "].val"; |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 832 | if (explicitcast) |
| 833 | s += ")"; |
| 834 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 835 | if ((vi + 1) < ve) |
| 836 | s += ", "; |
| 837 | } |
| 838 | if ((i + 1) < e) |
| 839 | s += ", "; |
| 840 | |
| 841 | continue; |
| 842 | } |
| 843 | |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 844 | if (splat && (i + 1) == e) |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 845 | s += Duplicate(nElts, typestr, args); |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 846 | else |
Nate Begeman | cc3c41a | 2010-06-12 03:09:49 +0000 | [diff] [blame] | 847 | s += args; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 848 | |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 849 | if (structTypes && !scal) |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 850 | s += ".val"; |
Nate Begeman | e5cb26f | 2010-09-22 22:28:42 +0000 | [diff] [blame] | 851 | if (explicitcast) |
| 852 | s += ")"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 853 | if ((i + 1) < e) |
| 854 | s += ", "; |
| 855 | } |
| 856 | |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 857 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 858 | if (ck == ClassB) |
Nate Begeman | b0a4e45 | 2010-06-07 16:00:37 +0000 | [diff] [blame] | 859 | s += ", " + utostr(GetNeonEnum(proto, typestr)); |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 860 | |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 861 | if (define) |
| 862 | s += ")"; |
| 863 | else |
| 864 | s += ");"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 865 | |
| 866 | if (proto[0] != 'v') { |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 867 | if (define) { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 868 | if (sret) |
| 869 | s += "; r; })"; |
| 870 | else if (proto[0] != 's') |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 871 | s += "}"; |
| 872 | } else { |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 873 | s += " return r;"; |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 874 | } |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 875 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 876 | return s; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 879 | static std::string GenBuiltinDef(const std::string &name, |
| 880 | const std::string &proto, |
| 881 | StringRef typestr, ClassKind ck) { |
| 882 | std::string s("BUILTIN(__builtin_neon_"); |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 883 | |
| 884 | // If all types are the same size, bitcasting the args will take care |
| 885 | // of arg checking. The actual signedness etc. will be taken care of with |
| 886 | // special enums. |
| 887 | if (proto.find('s') == std::string::npos) |
| 888 | ck = ClassB; |
| 889 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 890 | s += MangleName(name, typestr, ck); |
| 891 | s += ", \""; |
| 892 | |
Nate Begeman | 92f98af | 2010-06-04 07:11:25 +0000 | [diff] [blame] | 893 | for (unsigned i = 0, e = proto.size(); i != e; ++i) |
Nate Begeman | 7c21f74 | 2010-06-04 21:36:00 +0000 | [diff] [blame] | 894 | s += BuiltinTypeString(proto[i], typestr, ck, i == 0); |
| 895 | |
| 896 | // Extra constant integer to hold type class enum for this function, e.g. s8 |
| 897 | if (ck == ClassB) |
| 898 | s += "i"; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 899 | |
| 900 | s += "\", \"n\")"; |
| 901 | return s; |
| 902 | } |
| 903 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 904 | /// run - Read the records in arm_neon.td and output arm_neon.h. arm_neon.h |
| 905 | /// is comprised of type definitions and function declarations. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 906 | void NeonEmitter::run(raw_ostream &OS) { |
| 907 | EmitSourceFileHeader("ARM NEON Header", OS); |
| 908 | |
| 909 | // FIXME: emit license into file? |
| 910 | |
| 911 | OS << "#ifndef __ARM_NEON_H\n"; |
| 912 | OS << "#define __ARM_NEON_H\n\n"; |
| 913 | |
| 914 | OS << "#ifndef __ARM_NEON__\n"; |
| 915 | OS << "#error \"NEON support not enabled\"\n"; |
| 916 | OS << "#endif\n\n"; |
| 917 | |
| 918 | OS << "#include <stdint.h>\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 919 | |
| 920 | // Emit NEON-specific scalar typedefs. |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 921 | OS << "typedef float float32_t;\n"; |
| 922 | OS << "typedef uint8_t poly8_t;\n"; |
| 923 | OS << "typedef uint16_t poly16_t;\n"; |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 924 | OS << "typedef uint16_t float16_t;\n"; |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 925 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 926 | // Emit Neon vector typedefs. |
| 927 | std::string TypedefTypes("cQcsQsiQilQlUcQUcUsQUsUiQUiUlQUlhQhfQfPcQPcPsQPs"); |
| 928 | SmallVector<StringRef, 24> TDTypeVec; |
| 929 | ParseTypes(0, TypedefTypes, TDTypeVec); |
| 930 | |
| 931 | // Emit vector typedefs. |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 932 | for (unsigned v = 1; v != 5; ++v) { |
| 933 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
| 934 | bool dummy, quad = false; |
| 935 | (void) ClassifyType(TDTypeVec[i], quad, dummy, dummy); |
| 936 | OS << "typedef __attribute__(( __vector_size__("; |
| 937 | |
| 938 | OS << utostr(8*v*(quad ? 2 : 1)) << ") )) "; |
Bob Wilson | 72774c9 | 2010-09-14 21:52:34 +0000 | [diff] [blame] | 939 | if (!quad && v == 1) |
Nate Begeman | 9e584b3 | 2010-06-04 22:53:30 +0000 | [diff] [blame] | 940 | OS << " "; |
| 941 | |
| 942 | OS << TypeString('s', TDTypeVec[i]); |
| 943 | OS << " __neon_"; |
| 944 | |
| 945 | char t = (v == 1) ? 'd' : '0' + v; |
| 946 | OS << TypeString(t, TDTypeVec[i]) << ";\n"; |
| 947 | } |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 948 | } |
| 949 | OS << "\n"; |
Bob Wilson | ee9ca07 | 2010-09-15 01:52:33 +0000 | [diff] [blame] | 950 | OS << "typedef __attribute__(( __vector_size__(8) )) " |
| 951 | "double __neon_float64x1_t;\n"; |
| 952 | OS << "typedef __attribute__(( __vector_size__(16) )) " |
| 953 | "double __neon_float64x2_t;\n"; |
| 954 | OS << "\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 955 | |
| 956 | // Emit struct typedefs. |
| 957 | for (unsigned vi = 1; vi != 5; ++vi) { |
| 958 | for (unsigned i = 0, e = TDTypeVec.size(); i != e; ++i) { |
Bob Wilson | 9969bc3 | 2010-06-24 22:03:41 +0000 | [diff] [blame] | 959 | std::string ts = TypeString('d', TDTypeVec[i], vi == 1); |
| 960 | std::string vs = TypeString((vi > 1) ? '0' + vi : 'd', TDTypeVec[i]); |
| 961 | std::string tag = (vi > 1) ? vs : StructTag(TDTypeVec[i]); |
| 962 | OS << "typedef struct " << tag << " {\n"; |
| 963 | OS << " " << ts << " val"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 964 | if (vi > 1) |
| 965 | OS << "[" << utostr(vi) << "]"; |
| 966 | OS << ";\n} " << vs << ";\n\n"; |
| 967 | } |
| 968 | } |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 969 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 970 | OS << "#define __ai static __attribute__((__always_inline__))\n\n"; |
| 971 | |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 972 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 973 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 974 | // Unique the return+pattern types, and assign them. |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 975 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 976 | Record *R = RV[i]; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 977 | std::string name = LowercaseString(R->getName()); |
| 978 | std::string Proto = R->getValueAsString("Prototype"); |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 979 | std::string Types = R->getValueAsString("Types"); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 980 | |
| 981 | SmallVector<StringRef, 16> TypeVec; |
| 982 | ParseTypes(R, Types, TypeVec); |
| 983 | |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 984 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 985 | |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 986 | bool define = Proto.find('i') != std::string::npos; |
| 987 | |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 988 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 989 | assert(!Proto.empty() && ""); |
| 990 | |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 991 | // static always inline + return type |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 992 | if (define) |
| 993 | OS << "#define"; |
| 994 | else |
| 995 | OS << "__ai " << TypeString(Proto[0], TypeVec[ti]); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 996 | |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 997 | // Function name with type suffix |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 998 | OS << " " << MangleName(name, TypeVec[ti], ClassS); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 999 | |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 1000 | // Function arguments |
| 1001 | OS << GenArgs(Proto, TypeVec[ti]); |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1002 | |
Nate Begeman | e66aab5 | 2010-06-02 07:14:28 +0000 | [diff] [blame] | 1003 | // Definition. |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 1004 | if (define) |
| 1005 | OS << " "; |
| 1006 | else |
| 1007 | OS << " { "; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1008 | |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1009 | if (k != OpNone) { |
Nate Begeman | 162d3ba | 2010-06-03 04:04:09 +0000 | [diff] [blame] | 1010 | OS << GenOpString(k, Proto, TypeVec[ti]); |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1011 | } else { |
| 1012 | if (R->getSuperClasses().size() < 2) |
| 1013 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1014 | |
| 1015 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
| 1016 | |
| 1017 | if (ck == ClassNone) |
| 1018 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1019 | OS << GenBuiltin(name, Proto, TypeVec[ti], ck); |
| 1020 | } |
Nate Begeman | 6c060db | 2010-06-09 01:09:00 +0000 | [diff] [blame] | 1021 | if (!define) |
| 1022 | OS << " }"; |
| 1023 | OS << "\n"; |
Nate Begeman | 2223777 | 2010-06-02 00:34:55 +0000 | [diff] [blame] | 1024 | } |
| 1025 | OS << "\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1026 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1027 | OS << "#undef __ai\n\n"; |
Nate Begeman | 7c8c883 | 2010-06-02 21:53:00 +0000 | [diff] [blame] | 1028 | OS << "#endif /* __ARM_NEON_H */\n"; |
Nate Begeman | 5ddb087 | 2010-05-28 01:08:32 +0000 | [diff] [blame] | 1029 | } |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1030 | |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1031 | static unsigned RangeFromType(StringRef typestr) { |
| 1032 | // base type to get the type string for. |
| 1033 | bool quad = false, dummy = false; |
| 1034 | char type = ClassifyType(typestr, quad, dummy, dummy); |
| 1035 | |
| 1036 | switch (type) { |
| 1037 | case 'c': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1038 | return (8 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1039 | case 'h': |
| 1040 | case 's': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1041 | return (4 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1042 | case 'f': |
| 1043 | case 'i': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1044 | return (2 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1045 | case 'l': |
Nate Begeman | 4da883a | 2010-06-15 22:10:31 +0000 | [diff] [blame] | 1046 | return (1 << (int)quad) - 1; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1047 | default: |
| 1048 | throw "unhandled type!"; |
| 1049 | break; |
| 1050 | } |
Bob Wilson | fdb530d | 2010-07-28 18:21:10 +0000 | [diff] [blame] | 1051 | assert(0 && "unreachable"); |
| 1052 | return 0; |
Nate Begeman | 918f8e4 | 2010-06-14 05:17:23 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1055 | /// runHeader - Emit a file with sections defining: |
| 1056 | /// 1. the NEON section of BuiltinsARM.def. |
| 1057 | /// 2. the SemaChecking code for the type overload checking. |
| 1058 | /// 3. the SemaChecking code for validation of intrinsic immedate arguments. |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1059 | void NeonEmitter::runHeader(raw_ostream &OS) { |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1060 | std::vector<Record*> RV = Records.getAllDerivedDefinitions("Inst"); |
| 1061 | |
| 1062 | StringMap<OpKind> EmittedMap; |
| 1063 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1064 | // Generate BuiltinsARM.def for NEON |
| 1065 | OS << "#ifdef GET_NEON_BUILTINS\n"; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1066 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1067 | Record *R = RV[i]; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1068 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1069 | if (k != OpNone) |
| 1070 | continue; |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1071 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1072 | std::string Proto = R->getValueAsString("Prototype"); |
| 1073 | |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1074 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1075 | // their own builtin as they use the non-splat variant. |
Nate Begeman | 4b425a8 | 2010-06-10 00:16:56 +0000 | [diff] [blame] | 1076 | if (Proto.find('a') != std::string::npos) |
| 1077 | continue; |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1078 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1079 | std::string Types = R->getValueAsString("Types"); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1080 | SmallVector<StringRef, 16> TypeVec; |
| 1081 | ParseTypes(R, Types, TypeVec); |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1082 | |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1083 | if (R->getSuperClasses().size() < 2) |
| 1084 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1085 | |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1086 | std::string name = LowercaseString(R->getName()); |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1087 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
| 1088 | |
| 1089 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1090 | // Generate the BuiltinsARM.def declaration for this builtin, ensuring |
| 1091 | // that each unique BUILTIN() macro appears only once in the output |
| 1092 | // stream. |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1093 | std::string bd = GenBuiltinDef(name, Proto, TypeVec[ti], ck); |
| 1094 | if (EmittedMap.count(bd)) |
| 1095 | continue; |
| 1096 | |
| 1097 | EmittedMap[bd] = OpNone; |
| 1098 | OS << bd << "\n"; |
| 1099 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1100 | } |
| 1101 | OS << "#endif\n\n"; |
| 1102 | |
| 1103 | // Generate the overloaded type checking code for SemaChecking.cpp |
| 1104 | OS << "#ifdef GET_NEON_OVERLOAD_CHECK\n"; |
| 1105 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1106 | Record *R = RV[i]; |
| 1107 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1108 | if (k != OpNone) |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1109 | continue; |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1110 | |
| 1111 | std::string Proto = R->getValueAsString("Prototype"); |
| 1112 | std::string Types = R->getValueAsString("Types"); |
| 1113 | std::string name = LowercaseString(R->getName()); |
| 1114 | |
| 1115 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1116 | // their own builtin as they use the non-splat variant. |
| 1117 | if (Proto.find('a') != std::string::npos) |
| 1118 | continue; |
| 1119 | |
| 1120 | // Functions which have a scalar argument cannot be overloaded, no need to |
| 1121 | // check them if we are emitting the type checking code. |
| 1122 | if (Proto.find('s') != std::string::npos) |
| 1123 | continue; |
| 1124 | |
| 1125 | SmallVector<StringRef, 16> TypeVec; |
| 1126 | ParseTypes(R, Types, TypeVec); |
| 1127 | |
| 1128 | if (R->getSuperClasses().size() < 2) |
| 1129 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1130 | |
| 1131 | int si = -1, qi = -1; |
| 1132 | unsigned mask = 0, qmask = 0; |
| 1133 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1134 | // Generate the switch case(s) for this builtin for the type validation. |
| 1135 | bool quad = false, poly = false, usgn = false; |
| 1136 | (void) ClassifyType(TypeVec[ti], quad, poly, usgn); |
| 1137 | |
| 1138 | if (quad) { |
| 1139 | qi = ti; |
| 1140 | qmask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1141 | } else { |
| 1142 | si = ti; |
| 1143 | mask |= 1 << GetNeonEnum(Proto, TypeVec[ti]); |
| 1144 | } |
| 1145 | } |
| 1146 | if (mask) |
| 1147 | OS << "case ARM::BI__builtin_neon_" |
| 1148 | << MangleName(name, TypeVec[si], ClassB) |
| 1149 | << ": mask = " << "0x" << utohexstr(mask) << "; break;\n"; |
| 1150 | if (qmask) |
| 1151 | OS << "case ARM::BI__builtin_neon_" |
| 1152 | << MangleName(name, TypeVec[qi], ClassB) |
| 1153 | << ": mask = " << "0x" << utohexstr(qmask) << "; break;\n"; |
| 1154 | } |
| 1155 | OS << "#endif\n\n"; |
| 1156 | |
| 1157 | // Generate the intrinsic range checking code for shift/lane immediates. |
| 1158 | OS << "#ifdef GET_NEON_IMMEDIATE_CHECK\n"; |
| 1159 | for (unsigned i = 0, e = RV.size(); i != e; ++i) { |
| 1160 | Record *R = RV[i]; |
| 1161 | |
| 1162 | OpKind k = OpMap[R->getValueAsDef("Operand")->getName()]; |
| 1163 | if (k != OpNone) |
| 1164 | continue; |
| 1165 | |
| 1166 | std::string name = LowercaseString(R->getName()); |
| 1167 | std::string Proto = R->getValueAsString("Prototype"); |
| 1168 | std::string Types = R->getValueAsString("Types"); |
| 1169 | |
| 1170 | // Functions with 'a' (the splat code) in the type prototype should not get |
| 1171 | // their own builtin as they use the non-splat variant. |
| 1172 | if (Proto.find('a') != std::string::npos) |
| 1173 | continue; |
| 1174 | |
| 1175 | // Functions which do not have an immediate do not need to have range |
| 1176 | // checking code emitted. |
| 1177 | if (Proto.find('i') == std::string::npos) |
| 1178 | continue; |
| 1179 | |
| 1180 | SmallVector<StringRef, 16> TypeVec; |
| 1181 | ParseTypes(R, Types, TypeVec); |
| 1182 | |
| 1183 | if (R->getSuperClasses().size() < 2) |
| 1184 | throw TGError(R->getLoc(), "Builtin has no class kind"); |
| 1185 | |
| 1186 | ClassKind ck = ClassMap[R->getSuperClasses()[1]]; |
| 1187 | |
| 1188 | for (unsigned ti = 0, te = TypeVec.size(); ti != te; ++ti) { |
| 1189 | std::string namestr, shiftstr, rangestr; |
| 1190 | |
| 1191 | // Builtins which are overloaded by type will need to have their upper |
| 1192 | // bound computed at Sema time based on the type constant. |
| 1193 | if (Proto.find('s') == std::string::npos) { |
| 1194 | ck = ClassB; |
| 1195 | if (R->getValueAsBit("isShift")) { |
| 1196 | shiftstr = ", true"; |
| 1197 | |
| 1198 | // Right shifts have an 'r' in the name, left shifts do not. |
| 1199 | if (name.find('r') != std::string::npos) |
| 1200 | rangestr = "l = 1; "; |
| 1201 | } |
| 1202 | rangestr += "u = RFT(TV" + shiftstr + ")"; |
| 1203 | } else { |
| 1204 | rangestr = "u = " + utostr(RangeFromType(TypeVec[ti])); |
| 1205 | } |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1206 | // Make sure cases appear only once by uniquing them in a string map. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1207 | namestr = MangleName(name, TypeVec[ti], ck); |
| 1208 | if (EmittedMap.count(namestr)) |
| 1209 | continue; |
| 1210 | EmittedMap[namestr] = OpNone; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1211 | |
| 1212 | // Calculate the index of the immediate that should be range checked. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1213 | unsigned immidx = 0; |
Nate Begeman | c4a1b65 | 2010-06-20 21:09:52 +0000 | [diff] [blame] | 1214 | |
| 1215 | // Builtins that return a struct of multiple vectors have an extra |
| 1216 | // leading arg for the struct return. |
| 1217 | if (Proto[0] == '2' || Proto[0] == '3' || Proto[0] == '4') |
| 1218 | ++immidx; |
| 1219 | |
| 1220 | // Add one to the index for each argument until we reach the immediate |
| 1221 | // to be checked. Structs of vectors are passed as multiple arguments. |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1222 | for (unsigned ii = 1, ie = Proto.size(); ii != ie; ++ii) { |
| 1223 | switch (Proto[ii]) { |
| 1224 | default: immidx += 1; break; |
| 1225 | case '2': immidx += 2; break; |
| 1226 | case '3': immidx += 3; break; |
| 1227 | case '4': immidx += 4; break; |
| 1228 | case 'i': ie = ii + 1; break; |
| 1229 | } |
| 1230 | } |
| 1231 | OS << "case ARM::BI__builtin_neon_" << MangleName(name, TypeVec[ti], ck) |
| 1232 | << ": i = " << immidx << "; " << rangestr << "; break;\n"; |
Nate Begeman | d72c900 | 2010-06-13 04:47:03 +0000 | [diff] [blame] | 1233 | } |
Nate Begeman | 73cef3e | 2010-06-04 01:26:15 +0000 | [diff] [blame] | 1234 | } |
Nate Begeman | f8c4c27 | 2010-06-17 04:15:13 +0000 | [diff] [blame] | 1235 | OS << "#endif\n\n"; |
Nate Begeman | a8979a0 | 2010-06-04 00:21:41 +0000 | [diff] [blame] | 1236 | } |