summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/wasm/bigint/stubs.js
blob: 89f7d7841342a4b51e9d8b73d71507b6c72d811a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// This is similar to the corresponding JIT exit stub test in the
// wasm/import-export.js file, but it also tests BigInt cases.
(function testImportJitExit() {
  let options = getJitCompilerOptions();
  if (!options["baseline.enable"]) return;

  let baselineTrigger = options["baseline.warmup.trigger"];

  let valueToConvert = 0;
  function ffi(n) {
    if (n == 1337n) {
      return BigInt(valueToConvert);
    }
    return 42n;
  }

  // This case is intended to test that the I64 to BigInt argument
  // filling for the JIT exit stub does not interfere with the other
  // argument registers. It's important that the BigInt argument comes
  // first (to test that it doesn't clobber the subsequent ones).
  function ffi2(n, x1, x2, x3, x4, x5, x6) {
    return (
      42n +
      BigInt(x1) +
      BigInt(x2) +
      BigInt(x3) +
      BigInt(x4) +
      BigInt(x5) +
      BigInt(x6)
    );
  }

  // This case is for testing issues with potential GC.
  function ffi3(n1, n2, n3, n4) {
    return n3;
  }

  // Baseline compile ffis.
  for (let i = baselineTrigger + 1; i-- > 0; ) {
    ffi(BigInt(i));
    ffi2(BigInt(i), i, i, i, i, i, i);
    ffi3(BigInt(i), BigInt(i), BigInt(i), BigInt(i));
  }

  let imports = {
    a: {
      ffi,
      ffi2,
      ffi3,
    },
  };

  ex = wasmEvalText(
    `(module
        (import "a" "ffi" (func $ffi (param i64) (result i64)))
        (import "a" "ffi2" (func $ffi2
          (param i64 i32 i32 i32 i32 i32 i32)
          (result i64)))
        (import "a" "ffi3" (func $ffi3 (param i64 i64 i64 i64) (result i64)))

        (func (export "callffi") (param i64) (result i64)
         local.get 0
         call $ffi
        )

        (func (export "callffi2") (param i32 i64) (result i64)
         local.get 1
         local.get 0
         local.get 0
         local.get 0
         local.get 0
         local.get 0
         local.get 0
         call $ffi2
        )

        (func (export "callffi3") (param i64 i64 i64 i64) (result i64)
          local.get 0
          local.get 1
          local.get 2
          local.get 3
          call $ffi3
        )
    )`,
    imports
  ).exports;

  // Enable the jit exit for each JS callee.
  assertEq(ex.callffi(0n), 42n);
  assertEq(ex.callffi2(2, 0n), 54n);
  assertEq(ex.callffi3(0n, 1n, 2n, 3n), 2n);

  // Test the jit exit under normal conditions.
  assertEq(ex.callffi(0n), 42n);
  assertEq(ex.callffi(1337n), 0n);
  assertEq(ex.callffi2(2, 0n), 54n);
  assertEq(ex.callffi3(0n, 1n, 2n, 3n), 2n);

  // Test the jit exit with GC stress in order to ensure that
  // any trigger of GC in the stub do not cause errors.
  if (this.gczeal) {
    this.gczeal(2, 1); // Collect on every allocation
  }
  for (let i = 0; i < 1000; i++) {
    assertEq(ex.callffi3(0n, 1n, 2n, 3n), 2n);
  }
})();

// Test JIT entry stub
(function testJitEntry() {
  let options = getJitCompilerOptions();
  if (!options["baseline.enable"]) return;

  let baselineTrigger = options["baseline.warmup.trigger"];

  i = wasmEvalText(
    `(module
      (func (export "foo") (param i64) (result i64)
       local.get 0
       i64.const 1
       i64.add
       return
      )
  )`,
    {}
  ).exports;

  function caller(n) {
    return i.foo(42n);
  }

  // Baseline compile ffis.
  for (let i = baselineTrigger + 1; i-- > 0; ) {
    caller(i);
  }

  // Enable the jit entry.
  assertEq(caller(0), 43n);

  // Test the jit exit under normal conditions.
  assertEq(caller(0), 43n);
})();