예제 #1
0
    def do(self, src, imports=None):
        mod = python_to_ir(io.StringIO(src), imports=imports)
        f = io.StringIO()
        irutils.print_module(mod, file=f)

        # Round trip test:
        irutils.read_module(io.StringIO(f.getvalue()))
예제 #2
0
    def compile(self, source):
        """ Compile the given source with current settings. """
        srcfile = io.StringIO(source)
        outfile = io.StringIO()
        if self.stage == "ast":
            src_ast = create_ast(srcfile, api.get_arch(self.arch).info)
            print_ast(src_ast, file=outfile)
        else:
            ir_module = api.c_to_ir(srcfile, self.arch)

            if self.optimize:
                api.optimize(ir_module, level=2)

            if self.stage == "ir":
                print_module(ir_module, file=outfile)
            else:
                text_stream = TextOutputStream(f=outfile, add_binary=True)
                api.ir_to_stream(ir_module, self.arch, text_stream)
        return outfile.getvalue()
예제 #3
0
def serialization_roundtrip(ir_module):
    f = io.StringIO()
    print_module(ir_module, file=f)
    txt1 = f.getvalue()
    print(txt1)

    # Round trip via json:
    d = to_json(ir_module)
    ir_module2 = from_json(d)

    f = io.StringIO()
    print_module(ir_module2, file=f)
    txt2 = f.getvalue()
    assert txt1 == txt2

    # Round trip via textual representation:
    f = io.StringIO(txt1)
    ir_module3 = read_module(f)

    f = io.StringIO()
    print_module(ir_module3, file=f)
    txt3 = f.getvalue()
    assert txt1 == txt3
예제 #4
0
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)

# 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')
예제 #5
0
 def test_snippet2(self):
     imports = {'myprint': (None, (int, ))}
     mod = python_to_ir(io.StringIO(src2), imports=imports)
     f = io.StringIO()
     irutils.print_module(mod, file=f)