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