blob: 976e4736bca371b7a01068d2857248a83dca0d7c [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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// Flags: --expose-wasm --expose-gc --allow-natives-syntax
6
7load("test/mjsunit/wasm/wasm-constants.js");
Ben Murdochda12d292016-06-02 14:46:10 +01008load("test/mjsunit/wasm/wasm-module-builder.js");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009
10function assertTraps(code, msg) {
11 var threwException = true;
12 try {
13 if (typeof code === 'function') {
14 code();
15 } else {
16 eval(code);
17 }
18 threwException = false;
19 } catch (e) {
20 if (typeof type_opt === 'function') {
21 assertInstanceof(e, type_opt);
22 }
23 if (arguments.length >= 3) {
24 assertEquals(e.type, cause_opt);
25 }
26 // Success.
27 return;
28 }
29 throw new MjsUnitAssertionError("Did not throw exception");
30}
31
32
Ben Murdochda12d292016-06-02 14:46:10 +010033function makeBinop(opcode) {
34 var builder = new WasmModuleBuilder();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035
Ben Murdochda12d292016-06-02 14:46:10 +010036 builder.addFunction("main", [kAstI32, kAstI32, kAstI32])
37 .addBody([opcode, kExprGetLocal, 0, kExprGetLocal, 1])
38 .exportFunc();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039
Ben Murdochda12d292016-06-02 14:46:10 +010040 return builder.instantiate().exports.main;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041}
42
Ben Murdochda12d292016-06-02 14:46:10 +010043var divs = makeBinop(kExprI32DivS);
44var divu = makeBinop(kExprI32DivU);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045
46assertEquals( 33, divs( 333, 10));
47assertEquals(-33, divs(-336, 10));
48
49assertEquals( 44, divu( 445, 10));
50assertEquals(429496685, divu(-446, 10));
51
52assertTraps(kTrapDivByZero, "divs(100, 0);");
53assertTraps(kTrapDivByZero, "divs(-1009, 0);");
54
55assertTraps(kTrapDivByZero, "divu(200, 0);");
56assertTraps(kTrapDivByZero, "divu(-2009, 0);");
57
58assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)");
59assertEquals(0, divu(0x80000000, -1));
60
61
Ben Murdochda12d292016-06-02 14:46:10 +010062var rems = makeBinop(kExprI32RemS);
63var remu = makeBinop(kExprI32RemU);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064
65assertEquals( 3, rems( 333, 10));
66assertEquals(-6, rems(-336, 10));
67
68assertEquals( 5, remu( 445, 10));
69assertEquals( 3, remu(-443, 10));
70
71assertTraps(kTrapRemByZero, "rems(100, 0);");
72assertTraps(kTrapRemByZero, "rems(-1009, 0);");
73
74assertTraps(kTrapRemByZero, "remu(200, 0);");
75assertTraps(kTrapRemByZero, "remu(-2009, 0);");
76
77assertEquals(-2147483648, remu(0x80000000, -1));
78assertEquals(0, rems(0x80000000, -1));