示例#1
0
def c3_to_wasm(filename, verbose=False):
    """ Take c3 to wasm """
    bsp = io.StringIO("""
       module bsp;
       public function void putc(byte c);
       """)
    ir_module = c3_to_ir([bsp, libio_filename, filename], [], arch)

    # ir_modules.insert(0, ir_modules.pop(-1))  # Shuffle bsp forwards
    if verbose:
        print(str(ir_module))
    # optimize(x, level='2')
    # print(x, x.stats())

    # x.display()

    wasm_module = ir_to_wasm(ir_module)

    print('Completed generating wasm module', wasm_module)
    if verbose:
        wasm_module.show()
        print(wasm_module.to_bytes())
    # with open(wasm_filename, 'wb') as f:
    #    wasm_module.to_file(f)
    return wasm_module
示例#2
0
def c_to_wasm(filename, verbose=False):
    # Compile c source:
    with open(libc_filename, 'r') as f:
        ir_libc = c_to_ir(f, arch, coptions=coptions)

    print(ir_libc, ir_libc.stats())
    optimize(ir_libc, level='2')

    with open(filename, 'r') as f:
        x = c_to_ir(f, arch, coptions=coptions)

    print(x, x.stats())
    optimize(x, level='2')
    print(x, x.stats())

    if verbose:
        x.display()

    wasm_module = ir_to_wasm(ir_link([ir_libc, x]))

    print('Completed generating wasm module', wasm_module)
    if verbose:
        wasm_module.show()
        print(wasm_module.to_bytes())
    # with open(wasm_filename, 'wb') as f:
    #    wasm_module.to_file(f)
    return wasm_module
示例#3
0
    def test_single_sample(self):
        src = io.StringIO("""
        int add(int a, int b) {
          int g = a+b+55+1-2;
          return g + a+8*b;
        }
        """)
        mod = api.c_to_ir(src, 'x86_64')
        # For now optimize to the allocation of a variable on heap:
        api.optimize(mod, level='2')
        wasm_module = ir_to_wasm(mod)

        # W00t! Convert back to ir again! (because it is possible)
        mod2 = wasm_to_ir(
            wasm_module, api.get_arch('x86_64').info.get_type_info('ptr'))
        # TODO: find a way to execute this wasm code.
        ir_to_wasm(mod2)
示例#4
0
    def do(self, src, expected_output, lang="c3"):
        base_filename = make_filename(self.id())
        list_filename = base_filename + ".html"

        bsp_c3 = io.StringIO("""
           module bsp;
           public function void putc(byte c);
           """)
        march = "arm"  # TODO: this must be wasm!
        with html_reporter(list_filename) as reporter:
            ir_modules = build_sample_to_ir(src, lang, bsp_c3, march, reporter)

            for ir_module in ir_modules:
                api.optimize(ir_module,
                             level=self.opt_level,
                             reporter=reporter)

            wasm_module = ir_to_wasm(ir_link(ir_modules), reporter=reporter)

        # Output wasm file:
        wasm_filename = base_filename + ".wasm"
        with open(wasm_filename, "wb") as f:
            wasm_module.to_file(f)

        # Dat was 'm:
        wasm = wasm_module.to_bytes()
        wasm_text = str(list(wasm))
        wasm_data = "var wasm_data = new Uint8Array(" + wasm_text + ");"

        # Output javascript file:
        js = NODE_JS_TEMPLATE.replace("JS_PLACEHOLDER", wasm_data)
        js_filename = base_filename + ".js"
        with open(js_filename, "w") as f:
            f.write(js)

        # run node.js and compare output:
        res = run_nodejs(js_filename)
        self.assertEqual(expected_output, res)
    def do(self, src, expected_output, lang='c3'):
        base_filename = make_filename(self.id())
        list_filename = base_filename + '.html'

        bsp = io.StringIO("""
           module bsp;
           public function void putc(byte c);
           """)
        march = 'arm'  # TODO: this must be wasm!
        with HtmlReportGenerator(open(list_filename, 'w')) as reporter:
            if lang == 'c3':
                ir_modules = [
                    c3_to_ir([
                        bsp,
                        relpath('..', 'librt', 'io.c3'),
                        io.StringIO(src)
                    ], [],
                             march,
                             reporter=reporter)
                ]
            elif lang == 'bf':
                ir_modules = [bf_to_ir(src, march)]
            elif lang == 'c':
                coptions = COptions()
                include_path1 = relpath('..', 'librt', 'libc')
                lib = relpath('..', 'librt', 'libc', 'lib.c')
                coptions.add_include_path(include_path1)
                with open(lib, 'r') as f:
                    mod1 = c_to_ir(f,
                                   march,
                                   coptions=coptions,
                                   reporter=reporter)
                mod2 = c_to_ir(io.StringIO(src),
                               march,
                               coptions=coptions,
                               reporter=reporter)
                ir_modules = [mod1, mod2]
            else:  # pragma: no cover
                raise NotImplementedError(
                    'Language {} not implemented'.format(lang))

            for ir_module in ir_modules:
                optimize(ir_module, level=self.opt_level, reporter=reporter)

            wasm_module = ir_to_wasm(ir_link(ir_modules), reporter=reporter)

        # Output wasm file:
        wasm_filename = base_filename + '.wasm'
        with open(wasm_filename, 'wb') as f:
            wasm_module.to_file(f)

        # Dat was 'm:
        wasm = wasm_module.to_bytes()
        wasm_text = str(list(wasm))
        wasm_data = 'var wasm_data = new Uint8Array(' + wasm_text + ');'

        # Output javascript file:
        js = NODE_JS_TEMPLATE.replace('JS_PLACEHOLDER', wasm_data)
        js_filename = base_filename + '.js'
        with open(js_filename, 'w') as f:
            f.write(js)

        # run node.js and compare output:
        res = run_nodejs(js_filename)
        self.assertEqual(expected_output, res)
示例#6
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)