def build_sample_to_ir(src, lang, bsp_c3, march, reporter): """ Compile the given sample into ir-modules """ if lang == "c3": ir_modules = [ api.c3_to_ir( [bsp_c3, relpath("..", "librt", "io.c3"), io.StringIO(src)], [], march, reporter=reporter, ) ] elif lang == "bf": ir_modules = [api.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 = api.c_to_ir(f, march, coptions=coptions, reporter=reporter) mod2 = api.c_to_ir( io.StringIO(src), march, coptions=coptions, reporter=reporter ) ir_modules = [mod1, mod2] elif lang == "pas": pascal_ir_modules = api.pascal_to_ir( [io.StringIO(src)], api.get_arch(march) ) ir_modules = pascal_ir_modules else: # pragma: no cover raise NotImplementedError("Language {} not implemented".format(lang)) return ir_modules
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
def run_it(): arch = get_arch("example") bsp = io.StringIO( """ module bsp; public function void sleep(int ms); public function void putc(byte c); public function bool get_key(int* key); """ ) ircode = c3_to_ir( ["../src/snake/game.c3", "../src/snake/main.c3", "../../librt/io.c3"], [bsp], arch, ) with open("python_snake2.py", "w") as f: print("import time", file=f) print("import sys", file=f) print("import threading", file=f) ir_to_python([ircode], f) print("", file=f) print("def bsp_putc(c):", file=f) print(' print(chr(c), end="")', file=f) print("def bsp_get_key(x):", file=f) print(" return 0", file=f) print("def bsp_sleep(x):", file=f) print(" time.sleep(x*0.001)", file=f) print("main_main()", file=f) print("Now run python_snake2.py !")
def do(self, src, expected_output, lang='c3'): base_filename = make_filename(self.id()) sample_filename = base_filename + '.py' list_filename = base_filename + '.html' bsp = io.StringIO(""" module bsp; public function void putc(byte c); """) march = 'arm' with HtmlReportGenerator(open(list_filename, 'w')) as reporter: if lang == 'c3': ir_modules = [ c3_to_ir([ relpath('..', 'librt', 'io.c3'), bsp, 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)) # Test roundtrip of ir_modules for ir_module in ir_modules: serialization_roundtrip(ir_module) optimize(ir_module, level=self.opt_level, reporter=reporter) with open(sample_filename, 'w') as f: ir_to_python(ir_modules, f, reporter=reporter) # Add glue: print('', file=f) print('def bsp_putc(c):', file=f) print(' print(chr(c), end="")', file=f) print('main_main()', file=f) res = run_python(sample_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)
import os from ppci import api from ppci.graph import cyclo, callgraph from ppci.graph.cfg import ir_function_to_graph sources = [ os.path.join('src', 'hello', 'hello.c3'), os.path.join('..', 'librt', 'io.c3'), os.path.join('linux64', 'bsp.c3'), ] m = api.c3_to_ir(sources, [], 'arm') print(m) print(m.stats()) cg = callgraph.mod_to_call_graph(m) print(cg) # Print callgraph for func in m.functions: cfg, _ = ir_function_to_graph(func) complexity = cyclo.cyclomatic_complexity(cfg) print('Function: {}, Complexity: {}'.format(func.name, complexity))