Ejemplo n.º 1
0
    {
     a -= 2;
    }
  }
  return add(a, b) - 133;
}

""")
x = c_to_ir(f, arch, coptions=coptions)
print(x, x.stats())
# optimize(x, level='2')
print(x, x.stats())

x.display()

wasm_module = ir_to_wasm(x)

print(wasm_module)
wasm_module.show()
print(wasm_module.to_bytes())

html_filename = os.path.join(this_dir, 'wasm_demo.html')
src = 'source'
main_js = """
 module.exports.w00t(4000);
 print_ln(module.exports.add(2,5));
 print_ln(module.exports.sub(2,5));
 print_ln(module.exports.sub(18,5));
"""
export_wasm_example(html_filename, src, wasm_module, main_js=main_js)
Ejemplo n.º 2
0
    print(f.getvalue())

    # Compile to native object
    ob = ir_to_object([ppci_module], arch, debug=True, reporter=reporter)

# Hack: fix the return type
ob.debug_info.functions[0].return_type = float

# Run in memory
native_module = codepage.load_obj(ob)
t0 = perf_counter()
result = native_module.main()
etime = perf_counter() - t0
print(f'native says {result} in {etime} s')

# Generate html page:
export_wasm_example(os.path.join(this_dir, 'prime_demo_page.html'), py3,
                    wasm_module)

# Convert PPCI IR to (ugly) Python to skip codegen
if True:
    f = StringIO()
    ir_to_python([ppci_module], f)
    py_code = 'def main():\n' + '\n'.join(
        ['    ' + line for line in py3.splitlines()])
    exec(py_code)
    t0 = perf_counter()
    result = main()
    etime = perf_counter() - t0
    print(f'python says {result} in {etime}')
Ejemplo n.º 3
0
    wasm.components.Start(wasm.components.Ref('func', name='$main', index=1)),
    wasm.components.Func(
        '$main',
        wasm.components.Ref('type', name='$main_sig', index=1),
        [(None, 'f64')], instructions),
)

# Result is exactly the same!
assert wa3.to_bytes() == wa1.to_bytes()


## Recursion ...

# The text representation of a module produces an exact copy
assert wasm.Module(wa1.to_string()).to_bytes() == wa1.to_bytes()

# Using the elements (definitions) of a module to create a new module too
assert wasm.Module(*[d for d in wa1]).to_bytes() == wa1.to_bytes()

# We can reconstruct the module from its binary form
assert wasm.Module(wa1.to_bytes()).to_bytes() == wa1.to_bytes()


## Let's run it

wasm.run_wasm_in_node(wa1)

import webbrowser
wasm.export_wasm_example(__file__[:-3] + '.html', wa1.to_string(), wa1)
webbrowser.open(__file__[:-3] + '.html')