OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 function IntTest() { |
| 6 "use asm"; |
| 7 function sum(a, b) { |
| 8 a = a|0; |
| 9 b = b|0; |
| 10 var c = (b + 1)|0 |
| 11 return (a + c + 1)|0; |
| 12 } |
| 13 |
| 14 function caller() { |
| 15 return sum(77,22) | 0; |
| 16 } |
| 17 |
| 18 return {caller: caller}; |
| 19 } |
| 20 |
| 21 assertEquals(101, WASM.asmCompileRun(IntTest.toString())); |
| 22 |
| 23 function Float64Test() { |
| 24 "use asm"; |
| 25 function sum(a, b) { |
| 26 a = +a; |
| 27 b = +b; |
| 28 return +(a + b); |
| 29 } |
| 30 |
| 31 function caller() { |
| 32 var a = +sum(70.1,10.2); |
| 33 var ret = 0|0; |
| 34 if (a == 80.3) { |
| 35 ret = 1|0; |
| 36 } else { |
| 37 ret = 0|0; |
| 38 } |
| 39 return ret|0; |
| 40 } |
| 41 |
| 42 return {caller: caller}; |
| 43 } |
| 44 |
| 45 assertEquals(1, WASM.asmCompileRun(Float64Test.toString())); |
| 46 |
| 47 function BadModule() { |
| 48 "use asm"; |
| 49 function caller(a, b) { |
| 50 a = a|0; |
| 51 b = b+0; |
| 52 var c = (b + 1)|0 |
| 53 return (a + c + 1)|0; |
| 54 } |
| 55 |
| 56 function caller() { |
| 57 return call(1, 2)|0; |
| 58 } |
| 59 |
| 60 return {caller: caller}; |
| 61 } |
| 62 |
| 63 assertThrows(function() { |
| 64 WASM.asmCompileRun(BadModule.toString()) |
| 65 }); |
| 66 |
| 67 function TestReturnInBlock() { |
| 68 "use asm"; |
| 69 |
| 70 function caller() { |
| 71 if(1) { |
| 72 { |
| 73 { |
| 74 return 1; |
| 75 } |
| 76 } |
| 77 } |
| 78 return 0; |
| 79 } |
| 80 |
| 81 return {caller: caller}; |
| 82 } |
| 83 |
| 84 assertEquals(1, WASM.asmCompileRun(TestReturnInBlock.toString())); |
| 85 |
| 86 function TestWhileSimple() { |
| 87 "use asm"; |
| 88 |
| 89 function caller() { |
| 90 var x = 0; |
| 91 while(x < 5) { |
| 92 x = (x + 1)|0; |
| 93 } |
| 94 return x|0; |
| 95 } |
| 96 |
| 97 return {caller: caller}; |
| 98 } |
| 99 |
| 100 assertEquals(5, WASM.asmCompileRun(TestWhileSimple.toString())); |
| 101 |
| 102 function TestWhileWithoutBraces() { |
| 103 "use asm"; |
| 104 |
| 105 function caller() { |
| 106 var x = 0; |
| 107 while(x <= 3) |
| 108 x = (x + 1)|0; |
| 109 return x|0; |
| 110 } |
| 111 |
| 112 return {caller: caller}; |
| 113 } |
| 114 |
| 115 assertEquals(4, WASM.asmCompileRun(TestWhileWithoutBraces.toString())); |
| 116 |
| 117 function TestReturnInWhile() { |
| 118 "use asm"; |
| 119 |
| 120 function caller() { |
| 121 var x = 0; |
| 122 while(x < 10) { |
| 123 x = (x + 6)|0; |
| 124 return x|0; |
| 125 } |
| 126 return x|0; |
| 127 } |
| 128 |
| 129 return {caller: caller}; |
| 130 } |
| 131 |
| 132 assertEquals(6, WASM.asmCompileRun(TestReturnInWhile.toString())); |
| 133 |
| 134 function TestReturnInWhileWithoutBraces() { |
| 135 "use asm"; |
| 136 |
| 137 function caller() { |
| 138 var x = 0; |
| 139 while(x < 5) |
| 140 return 7; |
| 141 return x|0; |
| 142 } |
| 143 |
| 144 return {caller: caller}; |
| 145 } |
| 146 |
| 147 assertEquals(7, WASM.asmCompileRun(TestReturnInWhileWithoutBraces.toString())); |
| 148 |
| 149 function TestBreakInWhile() { |
| 150 "use asm"; |
| 151 |
| 152 function caller() { |
| 153 while(1) { |
| 154 break; |
| 155 } |
| 156 return 8; |
| 157 } |
| 158 |
| 159 return {caller: caller}; |
| 160 } |
| 161 |
| 162 assertEquals(8, WASM.asmCompileRun(TestBreakInWhile.toString())); |
| 163 |
| 164 function TestBreakInNestedWhile() { |
| 165 "use asm"; |
| 166 |
| 167 function caller() { |
| 168 var x = 1.0; |
| 169 while(x < 1.5) { |
| 170 while(1) |
| 171 break; |
| 172 x = +(x + 0.25); |
| 173 } |
| 174 var ret = 0; |
| 175 if (x == 1.5) { |
| 176 ret = 9; |
| 177 } |
| 178 return ret|0; |
| 179 } |
| 180 |
| 181 return {caller: caller}; |
| 182 } |
| 183 |
| 184 assertEquals(9, WASM.asmCompileRun(TestBreakInNestedWhile.toString())); |
| 185 |
| 186 function TestBreakInBlock() { |
| 187 "use asm"; |
| 188 |
| 189 function caller() { |
| 190 var x = 0; |
| 191 abc: { |
| 192 x = 10; |
| 193 if (x == 10) { |
| 194 break abc; |
| 195 } |
| 196 x = 20; |
| 197 } |
| 198 return x|0; |
| 199 } |
| 200 |
| 201 return {caller: caller}; |
| 202 } |
| 203 |
| 204 assertEquals(10, WASM.asmCompileRun(TestBreakInBlock.toString())); |
| 205 |
| 206 function TestBreakInNamedWhile() { |
| 207 "use asm"; |
| 208 |
| 209 function caller() { |
| 210 var x = 0; |
| 211 outer: while (1) { |
| 212 x = (x + 1)|0; |
| 213 while (x == 11) { |
| 214 break outer; |
| 215 } |
| 216 } |
| 217 return x|0; |
| 218 } |
| 219 |
| 220 return {caller: caller}; |
| 221 } |
| 222 |
| 223 assertEquals(11, WASM.asmCompileRun(TestBreakInNamedWhile.toString())); |
| 224 |
| 225 function TestContinue() { |
| 226 "use asm"; |
| 227 |
| 228 function caller() { |
| 229 var x = 5; |
| 230 var ret = 0; |
| 231 while (x >= 0) { |
| 232 x = (x - 1)|0; |
| 233 if (x == 2) { |
| 234 continue; |
| 235 } |
| 236 ret = (ret - 1)|0; |
| 237 } |
| 238 return ret|0; |
| 239 } |
| 240 |
| 241 return {caller: caller}; |
| 242 } |
| 243 |
| 244 assertEquals(-5, WASM.asmCompileRun(TestContinue.toString())); |
| 245 |
| 246 function TestContinueInNamedWhile() { |
| 247 "use asm"; |
| 248 |
| 249 function caller() { |
| 250 var x = 5; |
| 251 var y = 0; |
| 252 var ret = 0; |
| 253 outer: while (x > 0) { |
| 254 x = (x - 1)|0; |
| 255 y = 0; |
| 256 while (y < 5) { |
| 257 if (x == 3) { |
| 258 continue outer; |
| 259 } |
| 260 ret = (ret + 1)|0; |
| 261 y = (y + 1)|0; |
| 262 } |
| 263 } |
| 264 return ret|0; |
| 265 } |
| 266 |
| 267 return {caller: caller}; |
| 268 } |
| 269 |
| 270 assertEquals(20, WASM.asmCompileRun(TestContinueInNamedWhile.toString())); |
| 271 |
| 272 function TestNot() { |
| 273 "use asm"; |
| 274 |
| 275 function caller() { |
| 276 var a = !(2 > 3); |
| 277 return a | 0; |
| 278 } |
| 279 |
| 280 return {caller:caller}; |
| 281 } |
| 282 |
| 283 assertEquals(1, WASM.asmCompileRun(TestNot.toString())); |
| 284 |
| 285 function TestNotEquals() { |
| 286 "use asm"; |
| 287 |
| 288 function caller() { |
| 289 var a = 3; |
| 290 if (a != 2) { |
| 291 return 21; |
| 292 } |
| 293 return 0; |
| 294 } |
| 295 |
| 296 return {caller:caller}; |
| 297 } |
| 298 |
| 299 assertEquals(21, WASM.asmCompileRun(TestNotEquals.toString())); |
| 300 |
| 301 function TestUnsignedComparison() { |
| 302 "use asm"; |
| 303 |
| 304 function caller() { |
| 305 var a = 0xffffffff; |
| 306 if ((a>>>0) > (0>>>0)) { |
| 307 return 22; |
| 308 } |
| 309 return 0; |
| 310 } |
| 311 |
| 312 return {caller:caller}; |
| 313 } |
| 314 |
| 315 assertEquals(22, WASM.asmCompileRun(TestUnsignedComparison.toString())); |
| 316 |
| 317 function TestMixedAdd() { |
| 318 "use asm"; |
| 319 |
| 320 function caller() { |
| 321 var a = 0x80000000; |
| 322 var b = 0x7fffffff; |
| 323 var c = 0; |
| 324 c = ((a>>>0) + b)|0; |
| 325 if ((c >>> 0) > (0>>>0)) { |
| 326 if (c < 0) { |
| 327 return 23; |
| 328 } |
| 329 } |
| 330 return 0; |
| 331 } |
| 332 |
| 333 return {caller:caller}; |
| 334 } |
| 335 |
| 336 assertEquals(23, WASM.asmCompileRun(TestMixedAdd.toString())); |
| 337 |
| 338 function TestInt32HeapAccess(stdlib, foreign, buffer) { |
| 339 "use asm"; |
| 340 |
| 341 var m = new stdlib.Int32Array(buffer); |
| 342 function caller() { |
| 343 var i = 4; |
| 344 |
| 345 m[0] = (i + 1) | 0; |
| 346 m[i >> 2] = ((m[0]|0) + 1) | 0; |
| 347 m[2] = ((m[i >> 2]|0) + 1) | 0; |
| 348 return m[2] | 0; |
| 349 } |
| 350 |
| 351 return {caller: caller}; |
| 352 } |
| 353 |
| 354 assertEquals(7, WASM.asmCompileRun(TestInt32HeapAccess.toString())); |
| 355 |
| 356 function TestHeapAccessIntTypes() { |
| 357 var types = [ |
| 358 ['Int8Array', '>> 0'], |
| 359 ['Uint8Array', '>> 0'], |
| 360 ['Int16Array', '>> 1'], |
| 361 ['Uint16Array', '>> 1'], |
| 362 ['Int32Array', '>> 2'], |
| 363 ['Uint32Array', '>> 2'], |
| 364 ]; |
| 365 for (var i = 0; i < types.length; i++) { |
| 366 var code = TestInt32HeapAccess.toString(); |
| 367 code = code.replace('Int32Array', types[i][0]); |
| 368 code = code.replace(/>> 2/g, types[i][1]); |
| 369 assertEquals(7, WASM.asmCompileRun(code)); |
| 370 } |
| 371 } |
| 372 |
| 373 TestHeapAccessIntTypes(); |
| 374 |
| 375 function TestFloatHeapAccess(stdlib, foreign, buffer) { |
| 376 "use asm"; |
| 377 |
| 378 var f32 = new stdlib.Float32Array(buffer); |
| 379 var f64 = new stdlib.Float64Array(buffer); |
| 380 var fround = stdlib.Math.fround; |
| 381 function caller() { |
| 382 var i = 8; |
| 383 var j = 8; |
| 384 var v = 6.0; |
| 385 |
| 386 // TODO(bradnelson): Add float32 when asm-wasm supports it. |
| 387 f64[2] = v + 1.0; |
| 388 f64[i >> 3] = +f64[2] + 1.0; |
| 389 f64[j >> 3] = +f64[j >> 3] + 1.0; |
| 390 i = +f64[i >> 3] == 9.0; |
| 391 return i|0; |
| 392 } |
| 393 |
| 394 return {caller: caller}; |
| 395 } |
| 396 |
| 397 assertEquals(1, WASM.asmCompileRun(TestFloatHeapAccess.toString())); |
| 398 |
| 399 function TestConvertI32() { |
| 400 "use asm"; |
| 401 |
| 402 function caller() { |
| 403 var a = 1.5; |
| 404 if ((~~(a + a)) == 3) { |
| 405 return 24; |
| 406 } |
| 407 return 0; |
| 408 } |
| 409 |
| 410 return {caller:caller}; |
| 411 } |
| 412 |
| 413 assertEquals(24, WASM.asmCompileRun(TestConvertI32.toString())); |
| 414 |
| 415 function TestConvertF64FromInt() { |
| 416 "use asm"; |
| 417 |
| 418 function caller() { |
| 419 var a = 1; |
| 420 if ((+(a + a)) > 1.5) { |
| 421 return 25; |
| 422 } |
| 423 return 0; |
| 424 } |
| 425 |
| 426 return {caller:caller}; |
| 427 } |
| 428 |
| 429 assertEquals(25, WASM.asmCompileRun(TestConvertF64FromInt.toString())); |
| 430 |
| 431 function TestConvertF64FromUnsigned() { |
| 432 "use asm"; |
| 433 |
| 434 function caller() { |
| 435 var a = 0xffffffff; |
| 436 if ((+(a>>>0)) > 0.0) { |
| 437 if((+a) < 0.0) { |
| 438 return 26; |
| 439 } |
| 440 } |
| 441 return 0; |
| 442 } |
| 443 |
| 444 return {caller:caller}; |
| 445 } |
| 446 |
| 447 assertEquals(26, WASM.asmCompileRun(TestConvertF64FromUnsigned.toString())); |
| 448 |
| 449 function TestModInt() { |
| 450 "use asm"; |
| 451 |
| 452 function caller() { |
| 453 var a = -83; |
| 454 var b = 28; |
| 455 return ((a|0)%(b|0))|0; |
| 456 } |
| 457 |
| 458 return {caller:caller}; |
| 459 } |
| 460 |
| 461 assertEquals(-27, WASM.asmCompileRun(TestModInt.toString())); |
| 462 |
| 463 function TestModUnsignedInt() { |
| 464 "use asm"; |
| 465 |
| 466 function caller() { |
| 467 var a = 0x80000000; //2147483648 |
| 468 var b = 10; |
| 469 return ((a>>>0)%(b>>>0))|0; |
| 470 } |
| 471 |
| 472 return {caller:caller}; |
| 473 } |
| 474 |
| 475 assertEquals(8, WASM.asmCompileRun(TestModUnsignedInt.toString())); |
| 476 |
| 477 function TestModDouble() { |
| 478 "use asm"; |
| 479 |
| 480 function caller() { |
| 481 var a = 5.25; |
| 482 var b = 2.5; |
| 483 if (a%b == 0.25) { |
| 484 return 28; |
| 485 } |
| 486 return 0; |
| 487 } |
| 488 |
| 489 return {caller:caller}; |
| 490 } |
| 491 |
| 492 assertEquals(28, WASM.asmCompileRun(TestModDouble.toString())); |
| 493 |
| 494 /* |
| 495 TODO: Fix parsing of negative doubles |
| 496 Fix code to use trunc instead of casts |
| 497 function TestModDoubleNegative() { |
| 498 "use asm"; |
| 499 |
| 500 function caller() { |
| 501 var a = -34359738368.25; |
| 502 var b = 2.5; |
| 503 if (a%b == -0.75) { |
| 504 return 28; |
| 505 } |
| 506 return 0; |
| 507 } |
| 508 |
| 509 return {caller:caller}; |
| 510 } |
| 511 |
| 512 assertEquals(28, WASM.asmCompileRun(TestModDoubleNegative.toString())); |
| 513 */ |
| 514 |
| 515 function TestGlobals() { |
| 516 "use asm"; |
| 517 |
| 518 var a = 0.0; |
| 519 var b = 0.0; |
| 520 var c = 0.0; |
| 521 |
| 522 function add() { |
| 523 c = a + b; |
| 524 } |
| 525 |
| 526 function caller() { |
| 527 a = 23.75; |
| 528 b = 7.75; |
| 529 add(); |
| 530 return (~~c)|0; |
| 531 } |
| 532 |
| 533 return {caller:caller}; |
| 534 } |
| 535 |
| 536 assertEquals(31, WASM.asmCompileRun(TestGlobals.toString())); |
| 537 |
| 538 function TestGlobalsWithInit() { |
| 539 "use asm"; |
| 540 |
| 541 var a = 0.0; |
| 542 var b = 0.0; |
| 543 |
| 544 function add() { |
| 545 return +(a + b); |
| 546 } |
| 547 |
| 548 function init() { |
| 549 a = 43.25; |
| 550 b = 34.25; |
| 551 } |
| 552 |
| 553 return {init:init, |
| 554 add:add}; |
| 555 } |
| 556 |
| 557 var module = WASM.instantiateModuleFromAsm(TestGlobalsWithInit.toString()); |
| 558 module.init(); |
| 559 assertEquals(77.5, module.add()); |
OLD | NEW |