Beispiel #1
0
    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 open(html_filename, 'w') as f:
            with HtmlReportGenerator(f) 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)
Beispiel #2
0
    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 open(html_filename, 'w') as f, HtmlReportGenerator(f) 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))
Beispiel #3
0
def perform_test(filename, target):
    # if not os.path.basename(filename).startswith('z'):
    #     return
    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 open(html_report, 'wt', encoding='utf8') as f, HtmlReportGenerator(f) as reporter:
        reporter.message('Test spec file {}'.format(filename))
        try:
            s_expressions = parse_multiple_sexpr(source_text)
            expressions2ignore = black_list_expr.get(base_name, [])
            # s_expressions = [s_expr for s_expr in s_expressions if len(s_expr) != 3 or s_expr[1] not in expressions2ignore]
            # if base_name == 'i64':
            #    does_big_edges = lambda x: '9223372036854775808' in x or '9223372036854775807' in x or '9223372036854775809' in x
            #    s_expressions = [s_expr for s_expr in s_expressions if not does_big_edges(str(s_expr))]
            executor = WastExecutor(target, reporter, expressions2ignore)
            executor.execute(s_expressions)

        except CompilerError as ex:
            print('Exception:', ex)
            lines = list(io.StringIO(source_text))
            ex.render(lines)
            raise
Beispiel #4
0
 def test_jit_example(self):
     """ Test loading of C code from jit example """
     source = io.StringIO("""
     int x(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 open(html_filename, 'w') as f, HtmlReportGenerator(f) as reporter:
         obj = cc(source, arch, debug=True, reporter=reporter)
     m = load_obj(obj)
     # print(m.x.argtypes)
     T = ctypes.c_int * 6
     a = T()
     # TODO: integers in ctypes are 32 bit, they are 64 bit in ppci?
     a[:] = 1, 0, 2, 0, 3, 0
     # ap = ctypes.cast(a, ctypes.POINTER(ctypes.c_long))
     b = T()
     b[:] = 5, 0, 4, 0, 9, 0
     # bp = ctypes.cast(b, ctypes.POINTER(ctypes.c_long))
     y = m.x(a, b, 3)
     self.assertEqual(40, y)
Beispiel #5
0
 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 open(html_filename, 'w') as f, HtmlReportGenerator(f) 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)
Beispiel #6
0
def main():
    t1 = time.time()
    failed = 0
    passed = 0
    with open(report_filename, 'w') as f, HtmlReportGenerator(f) 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 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)
Beispiel #8
0
    def test_load_py(self):
        d = {}
        exec(src1, d)
        a = d['a']
        with open('p2p_report.html', 'w') as f, \
                HtmlReportGenerator(f) 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)
Beispiel #9
0
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 open(report_filename, "w") as f, HtmlReportGenerator(f) 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)
Beispiel #10
0
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()
    libc_include = os.path.join(this_dir, "..", "..", "..", "librt", "libc")
    coptions.add_include_path(libc_include)

    # TODO: this should be injected elsewhere?
    coptions.add_define('__LP64__', '1')
    # coptions.enable('freestanding')

    with open(html_report, "w") as rf, HtmlReportGenerator(rf) 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
Beispiel #11
0
    def test_callback(self):
        mock = Mock()

        def myprint(x: int) -> None:
            mock(x)

        imports = {
            'myprint': myprint,
        }
        with open('p2p_callback_report.html', 'w') as f, \
                HtmlReportGenerator(f) as reporter:
            m2 = load_py(io.StringIO(src2), imports=imports, reporter=reporter)
        # Segfaults:
        m2.a(2)
        mock.assert_called_with(15)
Beispiel #12
0
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 open(report_filename, 'w') as f, HtmlReportGenerator(f) 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')
    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 HtmlReportGenerator(open(list_filename, "w")) 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)
Beispiel #14
0
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 HtmlReportGenerator(open(list_filename, "w")) 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
Beispiel #15
0
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 open(report_filename, 'w') as f, HtmlReportGenerator(f) 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)
Beispiel #16
0
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 open(html_report, 'wt',
              encoding='utf8') as f, HtmlReportGenerator(f) 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"):
        # 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 open(report_filename,
                  "w") as f, HtmlReportGenerator(f) 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)
Beispiel #18
0
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 open("report.html", "w") as f, HtmlReportGenerator(f) 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"))
Beispiel #19
0
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 open('report.html', 'w') as f:
    arch = get_arch('riscv')
    o1 = asm("start.s", arch)
    o2 = asm("nOSPortasm.s", arch)
    reporter = HtmlReportGenerator(f)
    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, "riscv", coptions=coptions, debug=True, reporter=reporter))
    obj = link([o1,o2] + obj, "firmware.mmap", use_runtime=True, reporter=reporter, debug=True)

    with open("firmware.oj", "w") as of:
        obj.save(of)
Beispiel #20
0
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 open(html_report, 'w') as f, HtmlReportGenerator(f) as reporter:
        printed_numbers = []

        def print_ln(x: int) -> None:
            printed_numbers.append(x)

        imports = {
            'js': {
                'print_ln': print_ln,
            },
        }
        instantiate(m0, 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, 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 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)
Beispiel #22
0
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 HtmlReportGenerator(open(list_filename, 'w')) as reporter:
        o1 = asm(crt0_asm, march)
        if lang == 'c3':
            srcs = [relpath('..', 'librt', 'io.c3'), bsp_c3, io.StringIO(src)]
            o2 = c3c(srcs, [],
                     march,
                     opt_level=opt_level,
                     reporter=reporter,
                     debug=True)
            objs = [o1, o2]
        elif lang == 'bf':
            o3 = bfcompile(src, march, reporter=reporter)
            o2 = c3c([bsp_c3], [], march, reporter=reporter)
            objs = [o1, o2, o3]
        elif lang == 'c':
            o2 = c3c([bsp_c3], [], march, reporter=reporter)
            coptions = COptions()
            include_path1 = relpath('..', 'librt', 'libc')
            coptions.add_include_path(include_path1)
            with open(relpath('..', 'librt', 'libc', 'lib.c'), 'r') as f:
                o3 = cc(f, march, coptions=coptions, reporter=reporter)
            o4 = cc(io.StringIO(src),
                    march,
                    coptions=coptions,
                    reporter=reporter)
            objs = [o1, o2, o3, o4]
        else:
            raise NotImplementedError('language not implemented')
        obj = 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
        objcopy(obj, code_image, elf_format, elf_filename)

    # Export code image to some format:
    if bin_format:
        sample_filename = base_filename + '.' + bin_format
        objcopy(obj, code_image, bin_format, sample_filename)

    return obj
Beispiel #23
0
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_includes = os.path.join(this_dir, "..", "librt", "libc")
    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 open(report_filename, "w") as f, HtmlReportGenerator(f) 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)
Beispiel #24
0
"""
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 HtmlReportGenerator

# logging.basicConfig(level=logging.INFO)

def puts(txt: str):
	pass

with open('mandelbrot_compilation_report.html', 'w') as f, HtmlReportGenerator(f) 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)
Beispiel #25
0
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 HtmlReportGenerator

# logging.basicConfig(level=logging.INFO)


def puts(txt: str):
    pass


with open('mandelbrot_compilation_report.html',
          'w') as f, HtmlReportGenerator(f) 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 = """
Beispiel #26
0
""" Takes a web assembly module and turns it into riscv code """
import logging
from ppci.api import asm, c3c, link, get_arch, wasmcompile
from ppci.binutils.objectfile import merge_memories
from ppci.utils.reporting import HtmlReportGenerator

logging.basicConfig(level=logging.INFO)
arch = get_arch('riscv')
obj1 = asm("start.s", arch)

with open('report.html', 'w') as f, HtmlReportGenerator(f) as reporter:
    srcs = ['../src/wasm_fac/main.c3', '../../librt/io.c3', 'c3src/bsp.c3']
    obj2 = c3c(srcs, [], arch, reporter=reporter)
    with open('../src/wasm_fac/fact.wasm', 'rb') as f2:
        obj3 = wasmcompile(f2, arch)

    obj = link([obj1, obj2, obj3],
               "firmware.mmap",
               use_runtime=True,
               reporter=reporter,
               debug=True)

size = 0x2000
cimg = obj.get_image('flash')
dimg = obj.get_image('ram')
img = merge_memories(cimg, dimg, 'img')
imgdata = img.data

with open("firmware.hex", "w") as f:
    for i in range(size):
        if i < len(imgdata) // 4: