summaryrefslogtreecommitdiff
path: root/test/binaryen.js/sieve.js
blob: a7f0c993b8c848e25b75d0e3655191bd409eaf1b (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
// 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);

// Create a function type for  i32 (i32)  (i.e., return i32, get an i32 param)
var ii = module.addFunctionType('i', Binaryen.i32, [Binaryen.i32]);

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.current_memory(),
          module.i32.const(65536)
        ),
        module.get_local(0, Binaryen.i32)
      ),
      module.grow_memory(
        module.i32.sub(
          module.i32.div_u(
            module.i32.add(
              module.get_local(0, Binaryen.i32),
              module.i32.const(65535)
            ),
            module.i32.const(65536)
          ),
          module.current_memory()
        )
      )
    ),
    // first, clear memory
    module.set_local(1, module.i32.const(0)),
    module.loop('clear', module.block(null, [
      module.i32.store8(0, 1,
        module.get_local(1, Binaryen.i32),
        module.i32.const(0)
      ),
      module.set_local(1, module.i32.add(
        module.get_local(1, Binaryen.i32),
        module.i32.const(1)
      )),
      module.br_if('clear', module.i32.eq(
        module.get_local(1),
        module.get_local(0)
      ))
    ])),
    // perform the sieve TODO
    // calculate how many primes there are
    module.return(module.get_local(0, Binaryen.i32))
  ],
  Binaryen.none
);

// Create the add function
// Note: no additional local variables (that's the [])
module.addFunction('sieve', ii, [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');

// 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());