Python compilation

The ppci.lang.python module can be used to translate python code into wasm, ir or machine code.

ppci.lang.python.python_to_ir(f, imports=None)

Compile a piece of python code to an ir module.

Parameters:
  • f (file-like-object) – a file like object containing the python code
  • imports – Dictionary with symbols that are present.
Returns:

A ppci.ir.Module module

>>> import io
>>> from ppci.lang.python import python_to_ir
>>> f = io.StringIO("def calc(x: int) -> int: return x + 133")
>>> python_to_ir(f)
<ppci.ir.Module object at ...>
ppci.lang.python.ir_to_python(ir_modules, f, reporter=None)

Convert ir-code to python code

ppci.lang.python.jit(function)

Jitting function decorator.

Can be used to just-in-time (jit) compile and load a function. When a function is decorated with this decorator, the python code is translated into machine code and this code is loaded in the current process.

For example:

from ppci.lang.python import jit

@jit
def heavymath(a: int, b: int) -> int:
    return a + b

Now the function can be used as before:

>>> heavymath(2, 7)
9
ppci.lang.python.load_py(f, imports=None, reporter=None)

Load a type annotated python file.

Parameters:f – a file like object containing the python source code.
ppci.lang.python.python_to_wasm(*sources)

Compile Python functions to wasm, by using Python’s ast parser and compiling a very specific subset to WASM instructions. All values are float64. Each source can be a string, a function, or AST.