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