OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --expose-wasm |
| 6 |
| 7 (function TestSwitch() { |
| 8 function asmModule() { |
| 9 "use asm" |
| 10 |
| 11 function caller() { |
| 12 var ret = 0; |
| 13 var x = 7; |
| 14 switch (x) { |
| 15 case 1: return 0; |
| 16 case 7: { |
| 17 ret = 12; |
| 18 break; |
| 19 } |
| 20 default: return 0; |
| 21 } |
| 22 switch (x) { |
| 23 case 1: return 0; |
| 24 case 8: return 0; |
| 25 default: ret = (ret + 11)|0; |
| 26 } |
| 27 return ret|0; |
| 28 } |
| 29 |
| 30 return {caller:caller}; |
| 31 } |
| 32 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 33 assertEquals(23, wasm.caller()); |
| 34 })(); |
| 35 |
| 36 (function TestSwitchFallthrough() { |
| 37 function asmModule() { |
| 38 "use asm" |
| 39 |
| 40 function caller() { |
| 41 var x = 17; |
| 42 var ret = 0; |
| 43 switch (x) { |
| 44 case 17: |
| 45 case 14: ret = 39; |
| 46 case 1: ret = (ret + 3)|0; |
| 47 case 4: break; |
| 48 default: ret = (ret + 1)|0; |
| 49 } |
| 50 return ret|0; |
| 51 } |
| 52 |
| 53 return {caller:caller}; |
| 54 } |
| 55 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 56 assertEquals(42, wasm.caller()); |
| 57 })(); |
| 58 |
| 59 (function TestNestedSwitch() { |
| 60 function asmModule() { |
| 61 "use asm" |
| 62 |
| 63 function caller() { |
| 64 var x = 3; |
| 65 var y = -13; |
| 66 switch (x) { |
| 67 case 1: return 0; |
| 68 case 3: { |
| 69 switch (y) { |
| 70 case 2: return 0; |
| 71 case -13: return 43; |
| 72 default: return 0; |
| 73 } |
| 74 } |
| 75 default: return 0; |
| 76 } |
| 77 return 0; |
| 78 } |
| 79 |
| 80 return {caller:caller}; |
| 81 } |
| 82 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 83 assertEquals(43, wasm.caller()); |
| 84 })(); |
| 85 |
| 86 (function TestSwitchWithDefaultOnly() { |
| 87 function asmModule() { |
| 88 "use asm"; |
| 89 function main(x) { |
| 90 x = x|0; |
| 91 switch(x|0) { |
| 92 default: return -10; |
| 93 } |
| 94 return 0; |
| 95 } |
| 96 return { |
| 97 main: main, |
| 98 }; |
| 99 } |
| 100 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 101 assertEquals(-10, wasm.main(2)); |
| 102 assertEquals(-10, wasm.main(54)); |
| 103 })(); |
| 104 |
| 105 (function TestEmptySwitch() { |
| 106 function asmModule() { |
| 107 "use asm"; |
| 108 function main(x) { |
| 109 x = x|0; |
| 110 switch(x|0) { |
| 111 } |
| 112 return 73; |
| 113 } |
| 114 return { |
| 115 main: main, |
| 116 }; |
| 117 } |
| 118 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 119 assertEquals(73, wasm.main(7)); |
| 120 })(); |
| 121 |
| 122 (function TestSwitchWithBrTable() { |
| 123 function asmModule() { |
| 124 "use asm"; |
| 125 function main(x) { |
| 126 x = x|0; |
| 127 switch(x|0) { |
| 128 case 14: return 23; |
| 129 case 12: return 25; |
| 130 case 15: return 29; |
| 131 case 19: return 34; |
| 132 case 18: return 17; |
| 133 case 16: return 16; |
| 134 default: return -1; |
| 135 } |
| 136 return 0; |
| 137 } |
| 138 return { |
| 139 main: main, |
| 140 }; |
| 141 } |
| 142 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 143 assertEquals(25, wasm.main(12)); |
| 144 assertEquals(23, wasm.main(14)); |
| 145 assertEquals(29, wasm.main(15)); |
| 146 assertEquals(16, wasm.main(16)); |
| 147 assertEquals(17, wasm.main(18)); |
| 148 assertEquals(34, wasm.main(19)); |
| 149 assertEquals(-1, wasm.main(-1)); |
| 150 })(); |
| 151 |
| 152 (function TestSwitchWithBalancedTree() { |
| 153 function asmModule() { |
| 154 "use asm"; |
| 155 function main(x) { |
| 156 x = x|0; |
| 157 switch(x|0) { |
| 158 case 5: return 52; |
| 159 case 1: return 11; |
| 160 case 6: return 63; |
| 161 case 9: return 19; |
| 162 case -4: return -4; |
| 163 } |
| 164 return 0; |
| 165 } |
| 166 return { |
| 167 main: main, |
| 168 }; |
| 169 } |
| 170 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 171 assertEquals(-4, wasm.main(-4)); |
| 172 assertEquals(11, wasm.main(1)); |
| 173 assertEquals(52, wasm.main(5)); |
| 174 assertEquals(63, wasm.main(6)); |
| 175 assertEquals(19, wasm.main(9)); |
| 176 assertEquals(0, wasm.main(11)); |
| 177 })(); |
| 178 |
| 179 (function TestSwitchHybrid() { |
| 180 function asmModule() { |
| 181 "use asm"; |
| 182 function main(x) { |
| 183 x = x|0; |
| 184 switch(x|0) { |
| 185 case 1: return -4; |
| 186 case 2: return 23; |
| 187 case 3: return 32; |
| 188 case 4: return 14; |
| 189 case 7: return 17; |
| 190 case 10: return 10; |
| 191 case 11: return 121; |
| 192 case 12: return 112; |
| 193 case 13: return 31; |
| 194 case 16: return 16; |
| 195 default: return -1; |
| 196 } |
| 197 return 0; |
| 198 } |
| 199 return { |
| 200 main: main, |
| 201 }; |
| 202 } |
| 203 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 204 assertEquals(-4, wasm.main(1)); |
| 205 assertEquals(23, wasm.main(2)); |
| 206 assertEquals(32, wasm.main(3)); |
| 207 assertEquals(14, wasm.main(4)); |
| 208 assertEquals(17, wasm.main(7)); |
| 209 assertEquals(10, wasm.main(10)); |
| 210 assertEquals(121, wasm.main(11)); |
| 211 assertEquals(112, wasm.main(12)); |
| 212 assertEquals(31, wasm.main(13)); |
| 213 assertEquals(16, wasm.main(16)); |
| 214 assertEquals(-1, wasm.main(20)); |
| 215 })(); |
| 216 |
| 217 (function TestSwitchFallthroughWithBrTable() { |
| 218 function asmModule() { |
| 219 "use asm"; |
| 220 function main(x) { |
| 221 x = x|0; |
| 222 var ret = 0; |
| 223 switch(x|0) { |
| 224 case 1: { |
| 225 ret = 21; |
| 226 break; |
| 227 } |
| 228 case 2: { |
| 229 ret = 12; |
| 230 break; |
| 231 } |
| 232 case 3: { |
| 233 ret = 43; |
| 234 } |
| 235 case 4: { |
| 236 ret = 54; |
| 237 break; |
| 238 } |
| 239 default: { |
| 240 ret = 10; |
| 241 break; |
| 242 } |
| 243 } |
| 244 return ret|0; |
| 245 } |
| 246 return { |
| 247 main: main, |
| 248 }; |
| 249 } |
| 250 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 251 assertEquals(12, wasm.main(2)); |
| 252 assertEquals(10, wasm.main(10)); |
| 253 assertEquals(54, wasm.main(3)); |
| 254 })(); |
| 255 |
| 256 (function TestSwitchFallthroughHybrid() { |
| 257 function asmModule() { |
| 258 "use asm"; |
| 259 function main(x) { |
| 260 x = x|0; |
| 261 var ret = 0; |
| 262 switch(x|0) { |
| 263 case 1: { |
| 264 ret = 1; |
| 265 break; |
| 266 } |
| 267 case 2: { |
| 268 ret = 2; |
| 269 break; |
| 270 } |
| 271 case 3: { |
| 272 ret = 3; |
| 273 break; |
| 274 } |
| 275 case 4: { |
| 276 ret = 4; |
| 277 } |
| 278 case 7: { |
| 279 ret = 7; |
| 280 break; |
| 281 } |
| 282 case 10: { |
| 283 ret = 10; |
| 284 } |
| 285 case 16: { |
| 286 ret = 16; |
| 287 break; |
| 288 } |
| 289 case 17: { |
| 290 ret = 17; |
| 291 break; |
| 292 } |
| 293 case 18: { |
| 294 ret = 18; |
| 295 break; |
| 296 } |
| 297 case 19: { |
| 298 ret = 19; |
| 299 } |
| 300 default: { |
| 301 ret = -1; |
| 302 break; |
| 303 } |
| 304 } |
| 305 return ret|0; |
| 306 } |
| 307 return { |
| 308 main: main, |
| 309 }; |
| 310 } |
| 311 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 312 assertEquals(7, wasm.main(4)); |
| 313 assertEquals(16, wasm.main(10)); |
| 314 assertEquals(-1, wasm.main(19)); |
| 315 assertEquals(-1, wasm.main(23)); |
| 316 })(); |
| 317 |
| 318 (function TestSwitchHybridWithNoDefault() { |
| 319 function asmModule() { |
| 320 "use asm"; |
| 321 function main(x) { |
| 322 x = x|0; |
| 323 var ret = 19; |
| 324 switch(x|0) { |
| 325 case 1: { |
| 326 ret = 1; |
| 327 break; |
| 328 } |
| 329 case 2: { |
| 330 ret = 2; |
| 331 break; |
| 332 } |
| 333 case 3: { |
| 334 ret = 3; |
| 335 break; |
| 336 } |
| 337 case 4: { |
| 338 ret = 4; |
| 339 break; |
| 340 } |
| 341 case 7: { |
| 342 ret = 7; |
| 343 break; |
| 344 } |
| 345 } |
| 346 return ret|0; |
| 347 } |
| 348 return { |
| 349 main: main, |
| 350 }; |
| 351 } |
| 352 var wasm = Wasm.instantiateModuleFromAsm(asmModule.toString()); |
| 353 assertEquals(2, wasm.main(2)); |
| 354 assertEquals(7, wasm.main(7)); |
| 355 assertEquals(19, wasm.main(-1)); |
| 356 })(); |
| 357 |
| 358 (function TestLargeSwitch() { |
| 359 function LargeSwitchGenerator(begin, end, gap, handle_case) { |
| 360 var str = "function asmModule() {\ |
| 361 \"use asm\";\ |
| 362 function main(x) {\ |
| 363 x = x|0;\ |
| 364 switch(x|0) {"; |
| 365 for (var i = begin; i <= end; i = i + gap) { |
| 366 str = str.concat("case ", i.toString(), ": ", handle_case(i)); |
| 367 } |
| 368 str = str.concat("default: return -1;\ |
| 369 }\ |
| 370 return -2;\ |
| 371 }\ |
| 372 return {main: main}; }"); |
| 373 |
| 374 var wasm = Wasm.instantiateModuleFromAsm(str); |
| 375 return wasm; |
| 376 } |
| 377 |
| 378 var handle_case = function(k) { |
| 379 return "return ".concat(k, ";"); |
| 380 } |
| 381 var wasm = LargeSwitchGenerator(0, 513, 1, handle_case); |
| 382 for (var i = 0; i <= 513; i++) { |
| 383 assertEquals(i, wasm.main(i)); |
| 384 } |
| 385 assertEquals(-1, wasm.main(-1)); |
| 386 |
| 387 wasm = LargeSwitchGenerator(0, 1024, 3, handle_case); |
| 388 for (var i = 0; i <= 1024; i = i + 3) { |
| 389 assertEquals(i, wasm.main(i)); |
| 390 } |
| 391 assertEquals(-1, wasm.main(-1)); |
| 392 |
| 393 wasm = LargeSwitchGenerator(-2147483648, -2147483000, 1, handle_case); |
| 394 for (var i = -2147483648; i <= -2147483000; i++) { |
| 395 assertEquals(i, wasm.main(i)); |
| 396 } |
| 397 assertEquals(-1, wasm.main(-1)); |
| 398 assertEquals(-1, wasm.main(214748647)); |
| 399 |
| 400 wasm = LargeSwitchGenerator(-2147483648, -2147483000, 3, handle_case); |
| 401 for (var i = -2147483648; i <= -2147483000; i = i + 3) { |
| 402 assertEquals(i, wasm.main(i)); |
| 403 } |
| 404 assertEquals(-1, wasm.main(-1)); |
| 405 assertEquals(-1, wasm.main(214748647)); |
| 406 |
| 407 wasm = LargeSwitchGenerator(2147483000, 2147483647, 1, handle_case); |
| 408 for (var i = 2147483000; i <= 2147483647; i++) { |
| 409 assertEquals(i, wasm.main(i)); |
| 410 } |
| 411 assertEquals(-1, wasm.main(-1)); |
| 412 assertEquals(-1, wasm.main(-214748647)); |
| 413 |
| 414 wasm = LargeSwitchGenerator(2147483000, 2147483647, 4, handle_case); |
| 415 for (var i = 2147483000; i <= 2147483647; i = i + 4) { |
| 416 assertEquals(i, wasm.main(i)); |
| 417 } |
| 418 assertEquals(-1, wasm.main(-1)); |
| 419 assertEquals(-1, wasm.main(-214748647)); |
| 420 |
| 421 handle_case = function(k) { |
| 422 if (k != 7) return "return ".concat(k, ";"); |
| 423 else return "break;"; |
| 424 } |
| 425 wasm = LargeSwitchGenerator(0, 1499, 7, handle_case); |
| 426 for (var i = 0; i <= 1499; i = i + 7) { |
| 427 if (i == 7) assertEquals(-2, wasm.main(i)); |
| 428 else assertEquals(i, wasm.main(i)); |
| 429 } |
| 430 assertEquals(-1, wasm.main(-1)); |
| 431 |
| 432 handle_case = function(k) { |
| 433 if (k != 56) return "break;"; |
| 434 else return "return 23;"; |
| 435 } |
| 436 wasm = LargeSwitchGenerator(0, 638, 2, handle_case); |
| 437 for (var i = 0; i <= 638; i = i + 2) { |
| 438 if (i == 56) assertEquals(23, wasm.main(i)); |
| 439 else assertEquals(-2, wasm.main(i)); |
| 440 } |
| 441 assertEquals(-1, wasm.main(-1)); |
| 442 })(); |
OLD | NEW |