Exemple #1
0
def generate_code(module):
    """ Generate code """
    march = 'x86_64'
    obj1 = api.ir_to_object([module], march)
    obj2 = api.asm('crt0.asm', march)
    obj3 = api.c3c(['crt0.c3'], [], march)
    obj = api.link([obj1, obj2, obj3])

    with open('test.oj', 'w') as f:
        obj.save(f)
Exemple #2
0
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
Exemple #3
0
def go():
    generator = Generator()
    module = generator.gen_module()

    obj = ir_to_object([module], 'arm')
    print(obj)
            lhs = self.emit(ir.Binop(lhs, '*', rhs, 'prod', self.int_type))
        product.ir_value = lhs

    def get_value(self, value):
        if isinstance(value, int):
            ir_value = self.emit(ir.Const(value, 'constant', self.int_type))
        elif isinstance(value, str):
            ir_value = self.load_var(value)
        else:
            ir_value = value.ir_value
        return ir_value

    def load_var(self, var_name):
        mem_loc = self.variables[var_name]
        return self.emit(ir.Load(mem_loc, var_name, self.int_type))


tcfCompiler = TcfCompiler()
ir_module = tcfCompiler.compile('example.tcf')

obj1 = api.ir_to_object([ir_module], 'x86_64')
obj2 = api.c3c(['bsp.c3', 'io.c3'], [], 'x86_64')
obj3 = api.asm('linux.asm', 'x86_64')
obj = api.link([obj1, obj2, obj3], layout='layout.mmap')

print(obj)
with open('example.oj', 'w') as f:
    obj.save(f)

api.objcopy(obj, 'code', 'elf', 'example')
Exemple #5
0
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)

# Hack: fix the return type
ob.debug_info.functions[0].return_type = float

# Run in memory
native_module = codepage.load_obj(ob)
t0 = perf_counter()
result = native_module.main()
etime = perf_counter() - t0
print(f'native says {result} in {etime} s')

# Generate html page:
export_wasm_example(os.path.join(this_dir, 'prime_demo_page.html'), py3,
                    wasm_module)
Exemple #6
0
            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)
Exemple #7
0

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 = """

void mandelbrot();
void syscall(int nr, int a, int b, int c);
void exit(int status);
void putc(char c);
void puts(char *s);

int main()
{
    mandelbrot();
    /* As we don't have any C startup code in this example, and main is
       the executable entrypoint, we can't just return, but have to
Exemple #8
0
    debug_db = debuginfo.DebugDb()
    converter = PPCIGen()
    ppci_module = converter.generate(wasm, debug_db=debug_db)

    # Optimizer fails, or makes it slower ;)
    # optimize(ppci_module, 2)

    f = StringIO()
    irutils.Writer(f).write(ppci_module, verify=False)
    print(f.getvalue())

    with open('c:/dev/report.html',
              'w') as f, reporting.HtmlReportGenerator(f) as reporter:
        ob = ir_to_object([ppci_module],
                          get_arch('x86_64:wincc'),
                          debug=True,
                          debug_db=debug_db,
                          reporter=reporter)
    native_module = codepage.load_obj(ob)

    t0 = perf_counter()
    result = native_module.main()
    etime = perf_counter() - t0
    print(f'native says {result} in {etime} s')

    ##
    f = StringIO()
    pythonizer = ir2py.IrToPython(f)
    pythonizer.header()
    pythonizer.generate(ppci_module)
    py_code = f.getvalue()
Exemple #9
0
        'memory',
        ('export', 'mem0ry'),
        ('data', 'abcd'),
    ),
)

print(wasm_module.to_string())

arch = get_current_arch()
ppci_module = wasm_to_ir(wasm_module, arch.info.get_type_info('ptr'))
ppci_module.display()

print('using this arch: ', arch)
f = io.StringIO()
txt_stream = TextOutputStream(f=f, add_binary=True)
obj = ir_to_object([ppci_module], arch, debug=True, outstream=txt_stream)
print(f.getvalue())


def my_add(x: int, y: int) -> int:
    print('my add called', x, y)
    return x + y + 1


# Run in memory
imports = {'py_add': my_add}

native_module = load_obj(obj, imports=imports)
print(dir(native_module))
result = getattr(native_module, 'add')(42, 42)
print(result, '(should be 85)')