Api

The ppci library provides provides an intuitive api to the compiler, assembler and other tools. For example to assemble, compile, link and objcopy the msp430 blinky example project, the api can be used as follows:

>>> from ppci.api import asm, c3c, link, objcopy
>>> march = "msp430"
>>> o1 = asm('examples/msp430/blinky/boot.asm', march)
>>> o2 = c3c(['examples/msp430/blinky/blinky.c3'], [], march)
>>> o3 = link([o2, o1], 'examples/msp430/blinky/msp430.mmap', march)
>>> objcopy(o3, 'flash', 'hex', 'blinky_msp430.hex')

Instead of using the api, a set of commandline tools are also prodived.

The api module contains a set of handy functions to invoke compilation, linking and assembling.

ppci.api.asm(source, march, debug=False)

Assemble the given source for machine march.

Parameters:
  • source (str) – can be a filename or a file like object.
  • march (str) – march can be a ppci.arch.arch.Architecture instance or a string indicating the machine architecture.
  • debug – generate debugging information
Returns:

A ppci.binutils.objectfile.ObjectFile object

>>> import io
>>> from ppci.api import asm
>>> source_file = io.StringIO("db 0x77")
>>> obj = asm(source_file, 'arm')
>>> print(obj)
CodeObject of 1 bytes
ppci.api.c3c(sources, includes, march, opt_level=0, reporter=None, debug=False, outstream=None)

Compile a set of sources into binary format for the given target.

Parameters:
  • sources – a collection of sources that will be compiled.
  • includes – a collection of sources that will be used for type and function information.
  • march – the architecture for which to compile.
  • reporter – reporter to write compilation report to
  • debug – include debugging information
Returns:

An object file

>>> import io
>>> from ppci.api import c3c
>>> source_file = io.StringIO("module main; var int a;")
>>> obj = c3c([source_file], [], 'arm')
>>> print(obj)
CodeObject of 4 bytes
ppci.api.cc(source: io.TextIOBase, march, coptions=None, debug=False, reporter=None)

C compiler. compiles a single source file into an object file.

Parameters:
  • source – file like object from which text can be read
  • march – The architecture for which to compile
  • coptions – options for the C frontend
  • debug – Create debug info when set to True
Returns:

an object file

>>> import io
>>> from ppci.api import cc
>>> source_file = io.StringIO("void main() { int a; }")
>>> obj = cc(source_file, 'x86_64')
>>> print(obj)
CodeObject of 25 bytes

Links the iterable of objects into one using the given layout.

Parameters:
  • objects – a collection of objects to be linked together.
  • use_runtime (bool) – also link compiler runtime functions
  • debug (bool) – when true, keep debug information. Otherwise remove this debug information from the result.
Returns:

The linked object file

>>> import io
>>> from ppci.api import asm, c3c, link
>>> asm_source = io.StringIO("db 0x77")
>>> obj1 = asm(asm_source, 'arm')
>>> c3_source = io.StringIO("module main; var int a;")
>>> obj2 = c3c([c3_source], [], 'arm')
>>> obj = link([obj1, obj2])
>>> print(obj)
CodeObject of 8 bytes
ppci.api.objcopy(obj: ppci.binutils.objectfile.ObjectFile, image_name: str, fmt: str, output_filename)

Copy some parts of an object file to an output

ppci.api.bfcompile(source, target, reporter=None)

Compile brainfuck source into binary format for the given target

Parameters:
  • source – a filename or a file like object.
  • march – a architecture instance or a string indicating the target.
Returns:

A new object.

>>> import io
>>> from ppci.api import bfcompile
>>> source_file = io.StringIO(">>[-]<<[->>+<<]")
>>> obj = bfcompile(source_file, 'arm')
>>> print(obj) 
CodeObject of ... bytes
ppci.api.construct(buildfile, targets=())

Construct the given buildfile.

Raise task error if something goes wrong.

ppci.api.optimize(ir_module, level=0, reporter=None)

Run a bag of tricks against the ir-code.

This is an in-place operation!

Parameters:
  • ir_module (ppci.ir.Module) – The ir module to optimize.
  • level – The optimization level, 0 is default. Can be 0,1,2 or s 0: No optimization 1: some optimization 2: more optimization s: optimize for size
  • reporter – Report detailed log to this reporter
ppci.api.preprocess(f, output_file, coptions=None)

Pre-process a file into the other file.

ppci.api.get_arch(arch)

Try to return an architecture instance.

Parameters:arch – can be a string in the form of arch:option1:option2
>>> from ppci.api import get_arch
>>> arch = get_arch('msp430')
>>> arch
msp430-arch
>>> type(arch)
<class 'ppci.arch.msp430.arch.Msp430Arch'>
ppci.api.ir_to_object(ir_modules, march, reporter=None, debug=False, opt='speed', outstream=None)

Translate IR-modules into code for the given architecture.

Parameters:
  • ir_modules – a collection of ir-modules that will be transformed into machine code.
  • march – the architecture for which to compile.
  • reporter – reporter to write compilation report to
  • debug (bool) – include debugging information
  • opt (str) – optimization goal. Can be ‘speed’, ‘size’ or ‘co2’.
  • outstream – instruction stream to write instructions to
Returns:

An object file

Return type:

ObjectFile

ppci.api.ir_to_python(ir_modules, f, reporter=None)

Convert ir-code to python code