def test_numpy(self): source_file = io.StringIO(""" module main; function int find_first(int *a, int b, int size, int stride) { var int i = 0; var int ptr = 0; for ( i=0; i<size; i+=1 ) { if ( *(a + ptr) == b ) { return i; } ptr += stride; } return 0xff; } """) html_filename = make_filename(self.id()) + '.html' with html_reporter(html_filename) as reporter: m = load_code_as_module(source_file, reporter=reporter) import numpy as np a = np.array([12, 7, 3, 5, 42, 8, 3, 5, 8, 1, 4, 6, 2], dtype=int) addr = ctypes.cast(a.ctypes.data, ctypes.POINTER(ctypes.c_int)) # Cross fingers pos = m.find_first(addr, 42, len(a), a.itemsize) self.assertEqual(4, pos) pos = m.find_first(addr, 200, len(a), a.itemsize) self.assertEqual(0xff, pos)
def main(): t1 = time.time() failed = 0 passed = 0 with html_reporter(report_filename) as reporter: for filename in glob.iglob(os.path.join(nos_src_folder, '*.c')): print('==> Compiling', filename) try: do_compile(filename, reporter) except CompilerError as ex: print('Error:', ex.msg, ex.loc) ex.print() print_exc() failed += 1 except Exception as ex: print('General exception:', ex) print_exc() failed += 1 else: print('Great success!') passed += 1 t2 = time.time() elapsed = t2 - t1 print(passed, 'passed,', failed, 'failed in', elapsed, 'seconds')
def test_jit_example(self): """ Test loading of C code from jit example """ source = io.StringIO(""" int mega_complex_stuff(int* a, int* b, int count) { int sum = 0; int i; for (i=0; i < count; i++) sum += a[i] * b[i]; return sum; } """) arch = get_current_arch() html_filename = make_filename(self.id()) + '.html' with html_reporter(html_filename) as reporter: obj = cc(source, arch, debug=True, reporter=reporter) m = load_obj(obj) # print(m.x.argtypes) count = 6 T = ctypes.c_int * count a = T() a[:] = 1, 0, 2, 0, 3, 0 b = T() b[:] = 5, 0, 4, 0, 9, 0 y = m.mega_complex_stuff(a, b, count) self.assertEqual(40, y)
def test_numpy_floated(self): # TODO: add '10.2' instead of '10'. Somehow, adding 10.2 does not work source_file = io.StringIO(""" module main; function void mlt(double* a, double *b, int size, int stride) { var int i = 0; var int ptr = 0; for ( i=0; i<size; i+=1 ) { *(a + ptr) = *(a + ptr) + *(b + ptr) * 2 + 10; ptr += stride; } } """) html_filename = make_filename(self.id()) + '.html' with html_reporter(html_filename) as r: m = load_code_as_module(source_file, reporter=r) import numpy as np a = np.array([12, 7, 3, 5, 42, 100], dtype=float) b = np.array([82, 2, 5, 8, 13, 600], dtype=float) c = np.array([186, 21, 23, 31, 78, 1310], dtype=float) ap = ctypes.cast(a.ctypes.data, ctypes.POINTER(ctypes.c_double)) bp = ctypes.cast(b.ctypes.data, ctypes.POINTER(ctypes.c_double)) m.mlt(ap, bp, len(a), a.itemsize) # print(a) # print(c) self.assertTrue(np.allclose(c, a))
def do(self, src, expected_output, lang="c3"): # Compile: bsp_c3_src = """ module bsp; public function void putc(byte c); """ bsp_c3 = io.StringIO(bsp_c3_src) march = get_current_arch() base_filename = make_filename(self.id()) report_filename = base_filename + ".html" with html_reporter(report_filename) as reporter: obj = partial_build(src, lang, bsp_c3, 0, march, reporter) actual_output = [] def bsp_putc(c: int) -> None: # print('bsp_putc:', chr(c)) actual_output.append(chr(c)) # Dynamically load: imports = { "bsp_putc": bsp_putc, } mod = load_obj(obj, imports=imports) # print(dir(mod)) # Invoke! if hasattr(mod, "main"): mod.main() else: mod.main_main() # Check output: actual_output = "".join(actual_output) self.assertEqual(expected_output, actual_output)
def test_load_py(self): d = {} exec(src1, d) a = d['a'] with html_reporter('p2p_report.html') as reporter: m2 = load_py(io.StringIO(src1), reporter=reporter) for x in range(20): v1 = a(x, 2) # Python variant v2 = m2.a(x, 2) # Compiled variant! self.assertEqual(v1, v2)
def perform_test(filename): """ Try to compile the given snippet. """ logger.info("Step 1: Compile %s!", filename) march = "x86_64" html_report = os.path.splitext(filename)[0] + "_report.html" coptions = COptions() root_folder = os.path.join(this_dir, "..", "..", "..") libc_folder = os.path.join(root_folder, "librt", "libc") libc_include = os.path.join(libc_folder, "include") coptions.add_include_path(libc_include) # TODO: this should be injected elsewhere? coptions.add_define('__LP64__', '1') # coptions.enable('freestanding') with html_reporter(html_report) as reporter: with open(filename, "r") as f: try: obj1 = api.cc(f, march, coptions=coptions, reporter=reporter) except CompilerError as ex: ex.print() raise logger.info("Compilation complete, %s", obj1) obj0 = api.asm(io.StringIO(STARTERCODE), march) obj2 = api.c3c([io.StringIO(BSP_C3_SRC)], [], march) with open(os.path.join(libc_include, "lib.c"), "r") as f: obj3 = api.cc(f, march, coptions=coptions) obj = api.link([obj0, obj1, obj2, obj3], layout=io.StringIO(ARCH_MMAP)) logger.info("Step 2: Run it!") exe_filename = os.path.splitext(filename)[0] + "_executable.elf" with open(exe_filename, "wb") as f: write_elf(obj, f, type="executable") api.chmod_x(exe_filename) logger.info("Running %s", exe_filename) test_prog = subprocess.Popen(exe_filename, stdout=subprocess.PIPE) exit_code = test_prog.wait() assert exit_code == 0 captured_stdout = test_prog.stdout.read().decode("ascii") with open(filename + ".expected", "r") as f: expected_stdout = f.read() # Compare stdout: assert captured_stdout == expected_stdout
def main(): environment_variable = "LCC_FOLDER" if environment_variable in os.environ: lcc_folder = os.environ[environment_variable] else: logging.error( "Please define %s to point to the lcc source folder", environment_variable, ) return this_dir = os.path.abspath(os.path.dirname(__file__)) report_filename = os.path.join(this_dir, "report_lcc.html") libc_includes = os.path.join(this_dir, "..", "librt", "libc") include_paths = [libc_includes] arch = "x86_64" t1 = time.time() failed = 0 passed = 0 sources = glob.glob(os.path.join(lcc_folder, "src", "*.c")) objs = [] with html_reporter(report_filename) as reporter: for filename in sources: print(" ======================") print(" ========================") print(" ==> Compiling", filename) try: obj = do_compile(filename, include_paths, arch, reporter) objs.append(obj) except CompilerError as ex: print("Error:", ex.msg, ex.loc) ex.print() # print_exc() failed += 1 except Exception as ex: print("General exception:", ex) # ex.print() print_exc() failed += 1 else: print("Great success!") passed += 1 t2 = time.time() elapsed = t2 - t1 print("Passed:", passed, "failed:", failed, "in", elapsed, "seconds") obj = link(objs) print(obj)
def test_callback(self): mock = Mock() def myprint(x: int) -> None: mock(x) imports = { 'myprint': myprint, } with html_reporter('p2p_callback_report.html') as reporter: m2 = load_py( io.StringIO(src2), imports=imports, reporter=reporter) # Segfaults: m2.a(2) mock.assert_called_with(15)
def main(): t1 = time.time() failed = 0 passed = 0 sources = [ 'cpp.c', 'debug.c', 'dict.c', 'gen.c', 'lex.c', 'vector.c', 'parse.c', 'buffer.c', 'map.c', 'error.c', 'path.c', 'file.c', 'set.c', 'encoding.c', ] objs = [] with html_reporter(report_filename) as reporter: for filename in sources: filename = os.path.join(_8cc_folder, filename) print('==> Compiling', filename) try: obj = do_compile(filename, reporter) except CompilerError as ex: print('Error:', ex.msg, ex.loc) ex.print() print_exc() failed += 1 except Exception as ex: print('General exception:', ex) print_exc() failed += 1 else: objs.append(obj) print('Great success!') passed += 1 t2 = time.time() elapsed = t2 - t1 print(passed, 'passed,', failed, 'failed in', elapsed, 'seconds') obj = api.link(objs) with open('8cc.exe', 'wb') as f: write_elf(obj, f, type='executable')
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_c3 = io.StringIO(""" module bsp; public function void putc(byte c); """) march = "arm" with html_reporter(list_filename) as reporter: ir_modules = build_sample_to_ir(src, lang, bsp_c3, march, reporter) # Test roundtrip of ir_modules for ir_module in ir_modules: verify_module(ir_module) serialization_roundtrip(ir_module) api.optimize(ir_module, level=self.opt_level, reporter=reporter) with open(sample_filename, "w") as f: api.ir_to_python(ir_modules, f, reporter=reporter) # Expose all functions as external symbols: for ir_module in ir_modules: for routine in ir_module.functions: print('_irpy_externals["{0}"] = {0}'.format( routine.name), file=f) # Add glue: print("", file=f) print("def bsp_putc(c):", file=f) print(' print(chr(c), end="")', file=f) print("", file=f) # print('_irpy_externals["printf"] = printf', file=f) print('_irpy_externals["bsp_putc"] = bsp_putc', file=f) print("", file=f) print("main_main()", file=f) print("", file=f) res = run_python(sample_filename) self.assertEqual(expected_output, res)
def build( base_filename, src, bsp_c3, crt0_asm, march, opt_level, mmap, lang="c3", bin_format=None, elf_format=None, code_image="code", ): """ Construct object file from source snippet """ list_filename = base_filename + ".html" with html_reporter(list_filename) as reporter: objs = build_sample_to_code(src, lang, bsp_c3, opt_level, march, True, reporter) o1 = api.asm(crt0_asm, march) objs.append(o1) obj = api.link(objs, layout=mmap, use_runtime=True, reporter=reporter, debug=True) # Save object: obj_file = base_filename + ".oj" with open(obj_file, "w") as f: obj.save(f) if elf_format: elf_filename = base_filename + "." + elf_format api.objcopy(obj, code_image, elf_format, elf_filename) # Export code image to some format: if bin_format: sample_filename = base_filename + "." + bin_format api.objcopy(obj, code_image, bin_format, sample_filename) return obj
def main(): t1 = time.time() failed = 0 passed = 0 sources = glob.iglob(os.path.join(links_folder, '*.c')) objs = [] coptions = COptions() include_paths = [ libc_includes, links_folder, '/usr/include', ] coptions.add_include_paths(include_paths) with html_reporter(report_filename) as reporter: for filename in sources: filename = os.path.join(links_folder, filename) print(' ======================') print(' ========================') print(' ==> Compiling', filename) try: obj = do_compile(filename, coptions, reporter) objs.append(obj) except CompilerError as ex: print('Error:', ex.msg, ex.loc) ex.print() print_exc() failed += 1 except Exception as ex: print('General exception:', ex) print_exc() failed += 1 else: print('Great success!') passed += 1 t2 = time.time() elapsed = t2 - t1 print('Passed:', passed, 'failed:', failed, 'in', elapsed, 'seconds') obj = link(objs) print(obj)
def perform_test(filename, target): logger = logging.getLogger() logger.info('Loading %s', filename) base_name = os.path.splitext(os.path.split(filename)[1])[0] with open(filename, 'rt', encoding='utf-8') as f: source_text = f.read() html_report = os.path.splitext(filename)[0] + '_' + target + '.html' with html_reporter(html_report) as reporter: reporter.message('Test spec file {}'.format(filename)) try: s_expressions = parse_multiple_sexpr(source_text) expressions2ignore = black_list_expr.get(base_name, []) executor = WastExecutor(target, reporter, expressions2ignore) executor.execute(s_expressions) except CompilerError as ex: print('Exception:', ex) if ex.loc: lines = list(io.StringIO(source_text)) ex.render(lines) raise
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)
parser = argparse.ArgumentParser() parser.add_argument('--rom', default='cpu_instrs.gb') args = parser.parse_args() logging.basicConfig(level=logging.INFO) with open('wasmboy.wasm', 'rb') as f: wasm_module = Module(f) def log(a: int, b: int, c: int, d: int, e: int, f: int, g: int) -> None: print('Log:', a, b, c, d, e, f, g) this_dir = os.path.dirname(os.path.abspath(__file__)) html_report = os.path.join(this_dir, 'wasmboy_report.html') with reporting.html_reporter(html_report) as reporter: wasm_boy = instantiate(wasm_module, imports={'env': { 'log': log }}, target='native', reporter=reporter) # Following this explanation: # https://github.com/torch2424/wasmBoy/wiki/%5BWIP%5D-Core-API rom_filename = args.rom logging.info('Loading %s', rom_filename) # Load in a game to CARTRIDGE_ROM_LOCATION rom_location = wasm_boy.exports.CARTRIDGE_ROM_LOCATION.read() with open(rom_filename, 'rb') as f:
from ppci.binutils.objectfile import merge_memories from ppci.lang.c import COptions from ppci.utils.reporting import html_reporter def get_sources(folder, extension): resfiles = [] resdirs = [] for x in os.walk(folder): for y in glob(os.path.join(x[0], extension)): resfiles.append(y) resdirs.append(x[0]) return ((resdirs, resfiles)) with html_reporter('report.html') as reporter: arch = get_arch('riscv') o1 = asm("start.s", arch) o2 = asm("nOSPortasm.s", arch) path = os.path.join('.', 'csrc', argv[1]) dirs, srcs = get_sources(path, '*.c') #srcs += [os.path.join('.','csrc','bsp.c')] + [os.path.join('.','csrc','lib.c')] dirs += [os.path.join('.', 'csrc')] obj = [] coptions = COptions() for dir in dirs: coptions.add_include_path(dir) for src in srcs: with open(src) as f: obj.append( cc(f,
parser = argparse.ArgumentParser() parser.add_argument("-v", help="Increase verbosity", action="count", default=0) parser.add_argument("example", help="example name from the c3src directory") parser.add_argument( "-g", "--debug", action="store_true", default=False, help="Enable debug code", ) args = parser.parse_args() loglevel = logging.DEBUG if args.v else logging.INFO logging.basicConfig(level=loglevel, filename="debug.log") with html_reporter("report.html") as reporter: arch = get_arch("riscv") if args.debug: obj1 = asm("startdbg.s", arch) else: obj1 = asm("start.s", arch) c3_sources = [ os.path.join("c3src", "bsp.c3"), os.path.join("c3src", "io.c3"), os.path.join("c3src", args.example, "main.c3"), ] if args.debug: c3_sources.append(os.path.join("c3src", "gdbstub.c3")) c3_sources.append(os.path.join("c3src", "irq.c3"))
import io import logging from ppci.lang.python import python_to_ir from ppci.irutils import verify_module, print_module from ppci.api import ir_to_object, link, cc, objcopy from ppci.utils.reporting import html_reporter # logging.basicConfig(level=logging.INFO) def puts(txt: str): pass with html_reporter('mandelbrot_compilation_report.html') as reporter: with open('mandelbrot.py', 'r') as f: mod = python_to_ir(f, imports={'puts': puts}) with open('mandelbrot.py', 'r') as f: src = list(f) reporter.annotate_source(src, mod) verify_module(mod) # print_module(mod) obj_py = ir_to_object([mod], 'x86_64') # print(obj_py) c_glue = """
parser = argparse.ArgumentParser(parents=[coptions_parser]) parser.add_argument('-v', action='count', default=0) args = parser.parse_args() coptions = COptions.from_args(args) coptions.add_include_path(libc_includes) report_html = os.path.join(this_dir, 'compilation_report.html') if args.v > 0: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) arch = api.get_arch('riscv') with html_reporter(report_html) as reporter: def cc(filename): logging.info('Compiling %s', filename) with open(os.path.join(this_dir, filename)) as f: try: obj = api.cc(f, arch, reporter=reporter, coptions=coptions) logging.info('Compiled %s into %s bytes', filename, obj.byte_size) except CompilerError as e: print(e) e.print() obj = None return obj file_list = ['xprintf.c', 'loader.c', 'ff.c', 'sdmm.c']
def test_table1(): # The canonical form CODE0 = dedent(r""" (module (type $print (func (param i32))) (type $2 (func)) (import "js" "print_ln" (func $print (type $print))) (table $0 2 2 funcref) (start $main) (elem i32.const 0 $f1 $f2) (func $f1 (type $2) i32.const 101 call $print) (func $f2 (type $2) i32.const 102 call $print) (func $main (type $2) i32.const 0 call_indirect (type $2) i32.const 1 call_indirect (type $2)) ) """) # Test main code m0 = Module(CODE0) assert m0.to_string() == CODE0 b0 = m0.to_bytes() assert Module(b0).to_bytes() == b0 html_report = 'table_and_element_compilation_report.html' with html_reporter(html_report) as reporter: printed_numbers = [] def print_ln(x: int) -> None: printed_numbers.append(x) imports = { 'js': { 'print_ln': print_ln, }, } instantiate(m0, imports=imports, target='python', reporter=reporter) assert [101, 102] == printed_numbers if is_platform_supported(): printed_numbers = [] def print_ln(x: int) -> None: printed_numbers.append(x) imports = { 'js': { 'print_ln': print_ln, }, } instantiate(m0, imports=imports, target='native', reporter=reporter) assert [101, 102] == printed_numbers if has_node(): assert run_wasm_in_node(m0, True) == '101\n102' # Abbreviation: imported table m3 = Module('(module (table $t1 (import "foo" "bar_table1") funcref) )') assert m3.to_string() == dedent(""" (module (import "foo" "bar_table1" (table $t1 funcref)) ) """) m3 = Module('(module (table (import "foo" "bar_table1") 2 3 funcref) )') assert m3.to_string() == dedent(""" (module (import "foo" "bar_table1" (table 2 3 funcref)) ) """) # Abbeviation: inline data and unspecified (default) alignment CODE1 = r""" (module (type $print (func (param i32))) (type $2 (func)) (import "js" "print_ln" (func $print (type $print))) (table funcref (elem $f1 $f2)) (start $main) (func $f1 (type $2) (i32.const 101) (call $print) ) (func $f2 (type $2) (i32.const 102) (call $print) ) (func $main (type $2) (i32.const 0) (call_indirect (type $2)) (i32.const 1) (call_indirect (type $2)) ) ) """ m1 = Module(CODE1) assert m1.to_string() == CODE0 assert m1.to_bytes() == b0
def main(): environment_variable = "LIBMAD_FOLDER" if environment_variable in os.environ: libmad_folder = os.environ[environment_variable] else: logging.error( "Please define %s to point to the libmad source folder", environment_variable, ) return this_dir = os.path.abspath(os.path.dirname(__file__)) report_filename = os.path.join(this_dir, "report_libmad.html") libc_folder = os.path.join(this_dir, "..", "librt", "libc") libc_includes = os.path.join(libc_folder, "include") include_paths = [libc_includes, libmad_folder] arch = "x86_64" t1 = time.time() failed = 0 passed = 0 sources = [ "layer3.c", "version.c", "fixed.c", "bit.c", "timer.c", "stream.c", "frame.c", "synth.c", "decoder.c", "layer12.c", "huffman.c", ] objs = [] with html_reporter(report_filename) as reporter: for filename in sources: filename = os.path.join(libmad_folder, filename) print(" ======================") print(" ========================") print(" ==> Compiling", filename) try: obj = do_compile(filename, include_paths, arch, reporter) objs.append(obj) except CompilerError as ex: print("Error:", ex.msg, ex.loc) ex.print() print_exc() failed += 1 # except Exception as ex: # print("General exception:", ex) # print_exc() # failed += 1 else: print("Great success!") passed += 1 t2 = time.time() elapsed = t2 - t1 print("Passed:", passed, "failed:", failed, "in", elapsed, "seconds") obj = link(objs) print(obj)