blob: 6f3ff5db7369733a7aa8bf667c01ec183c579173 [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 Murdochc5610432016-08-08 18:44:38 +010036 builder.addFunction("main", kSig_i_ii)
37 .addBody([
38 kExprGetLocal, 0, // --
39 kExprGetLocal, 1, // --
40 opcode, // --
41 ])
Ben Murdochda12d292016-06-02 14:46:10 +010042 .exportFunc();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043
Ben Murdochda12d292016-06-02 14:46:10 +010044 return builder.instantiate().exports.main;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000045}
46
Ben Murdochda12d292016-06-02 14:46:10 +010047var divs = makeBinop(kExprI32DivS);
48var divu = makeBinop(kExprI32DivU);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049
50assertEquals( 33, divs( 333, 10));
51assertEquals(-33, divs(-336, 10));
52
53assertEquals( 44, divu( 445, 10));
54assertEquals(429496685, divu(-446, 10));
55
56assertTraps(kTrapDivByZero, "divs(100, 0);");
57assertTraps(kTrapDivByZero, "divs(-1009, 0);");
58
59assertTraps(kTrapDivByZero, "divu(200, 0);");
60assertTraps(kTrapDivByZero, "divu(-2009, 0);");
61
62assertTraps(kTrapDivUnrepresentable, "divs(0x80000000, -1)");
63assertEquals(0, divu(0x80000000, -1));
64
65
Ben Murdochda12d292016-06-02 14:46:10 +010066var rems = makeBinop(kExprI32RemS);
67var remu = makeBinop(kExprI32RemU);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068
69assertEquals( 3, rems( 333, 10));
70assertEquals(-6, rems(-336, 10));
71
72assertEquals( 5, remu( 445, 10));
73assertEquals( 3, remu(-443, 10));
74
75assertTraps(kTrapRemByZero, "rems(100, 0);");
76assertTraps(kTrapRemByZero, "rems(-1009, 0);");
77
78assertTraps(kTrapRemByZero, "remu(200, 0);");
79assertTraps(kTrapRemByZero, "remu(-2009, 0);");
80
81assertEquals(-2147483648, remu(0x80000000, -1));
82assertEquals(0, rems(0x80000000, -1));