summaryrefslogtreecommitdiff
path: root/test/binaryen.js/sieve.js
blob: ef7c6cca8f47c16a0c1081cdca3e7c742e8197bc (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
function test() {
  // Create a module to work on
  var module = new Binaryen.Module();

  // Set a memory of initially one page, maximum 100 pages
  module.setMemory(1, 100);

  var body = module.block(
    null,
    [
      // if the current memory size is too small, grow it
      module.if(
        module.i32.lt_u(
          module.i32.mul(
            module.memory.size(),
            module.i32.const(65536)
          ),
          module.local.get(0, Binaryen.i32)
        ),
        module.drop(
          module.memory.grow(
            module.i32.sub(
              module.i32.div_u(
                module.i32.add(
                  module.local.get(0, Binaryen.i32),
                  module.i32.const(65535)
                ),
                module.i32.const(65536)
              ),
              module.memory.size()
            )
          )
        )
      ),
      // first, clear memory
      module.local.set(1, module.i32.const(0)),
      module.loop('clear', module.block(null, [
        module.i32.store8(0, 1,
          module.local.get(1, Binaryen.i32),
          module.i32.const(0)
        ),
        module.local.set(1, module.i32.add(
          module.local.get(1, Binaryen.i32),
          module.i32.const(1)
        )),
        module.br_if('clear', module.i32.eq(
          module.local.get(1, Binaryen.i32),
          module.local.get(0, Binaryen.i32)
        ))
      ])),
      // perform the sieve TODO
      // calculate how many primes there are
      module.return(module.local.get(0, Binaryen.i32))
    ],
    Binaryen.none
  );

  // Create the add function
  // Note: no additional local variables (that's the [])
  module.addFunction('sieve', Binaryen.i32, Binaryen.i32, [Binaryen.i32], body);

  // Export the function, so we can call it later (for simplicity we
  // export it as the same name as it has internally)
  module.addFunctionExport('sieve', 'sieve');

  if (!module.validate()) throw 'did not validate :(';

  // Print out the text
  console.log(module.emitText());

  // Optimize the module! This removes the 'return', since the
  // output of the add can just fall through
  module.optimize();

  // Print out the optimized module's text
  console.log('optimized:\n\n' + module.emitText());
}

Binaryen.ready.then(test);