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)
def xx_test_compiling(): # Convert Python to wasm wasm_module = python_to_wasm(py3) # Convert wasm to ppci ppci_module = wasm.wasm_to_ir(wasm_module) # Optimizer fails, or makes it slower ;) # optimize(ppci_module, 2) # Compile to native object arch = get_current_arch() ob = ir_to_object([ppci_module], arch, debug=True) # Run in memory native_module = codepage.load_obj(ob) result = native_module.main() assert result == 2741
""" import logging from ppci.common import logformat from ppci.wasm import read_wasm, wasm_to_ir from ppci.api import get_arch logging.basicConfig(level=logging.DEBUG, format=logformat) with open('program.wasm', 'rb') as f: module = read_wasm(f) # Save as binary: with open('copy.wasm', 'wb') as f: module.to_file(f) assert open('program.wasm', 'rb').read() == open('copy.wasm', 'rb').read() # Save as text: with open('wat.wat', 'w') as f: f.write(module.to_string()) print(module) ptr_info = get_arch('arm').info.get_type_info('ptr') m2 = wasm_to_ir(module, ptr_info) print(m2) print(m2.stats())
# print(perf_counter() - t0) # print(i) return i """ ## Run in memory arch = get_current_arch() # Convert Python to wasm wasm_module = python_to_wasm(py3) print(wasm_module.to_string()) wasm_module.show_interface() # Convert wasm to ppci ppci_module = wasm_to_ir(wasm_module, arch.info.get_type_info('ptr')) # Optimizer fails, or makes it slower ;) # optimize(ppci_module, 2) this_dir = os.path.dirname(os.path.abspath(__file__)) # Generate a report: html_report = os.path.join(this_dir, 'compilation_report.html') with reporting.html_reporter(html_report) as reporter: # Write IR code f = StringIO() irutils.print_module(ppci_module, file=f, verify=False) print(f.getvalue()) # Compile to native object ob = ir_to_object([ppci_module], arch, debug=True, reporter=reporter)
f.write(data) files = [ 'clang.wasm', 'clang-format.wasm', 'runtime.wasm', ] for local_filename in files: url = 'https://tbfleming.github.io/cib/{}'.format(local_filename) if not os.path.exists(local_filename): download_file(url, local_filename) with open('runtime.wasm', 'rb') as f: # with open('clang-format.wasm', 'rb') as f: wasm_module = Module(f.read()) print(wasm_module) wasm_module.show_interface() arch = get_arch('x86_64') ptr_info = arch.info.get_type_info('ptr') ir_module = wasm_to_ir(wasm_module, ptr_info) print(ir_module) print(ir_module.stats()) obj = ir_to_object([ir_module], arch)