JSON serialization

Module to serialize and deserialize IR-code.

Take an IR-module, and turn it into a dict or json. Then, this item can be persistet or send via e-mail. Next up, take this dict / json and reconstruct the identical IR-module from it.

This can be useful in these scenario’s:

  • Compilation caching: store the IR-code and load from disk when required later on.
  • Distributed compilation: transfer IR-code across processes.
>>> import io
>>> from ppci.api import c_to_ir
>>> from ppci.irutils import to_json, from_json
>>> c_src = "int add(int a, int b) { return a + b; }"   # Define some C-code
>>> mod = c_to_ir(io.StringIO(c_src), "x86_64")         # turn C-code into IR-code
>>> mod.stats()
'functions: 1, blocks: 2, instructions: 11'
>>> json_txt = to_json(mod)                             # Turn module into JSON
>>> mod2 = from_json(json_txt)                          # Load module from JSON.
>>> mod2.stats()
'functions: 1, blocks: 2, instructions: 11'
class ppci.irutils.io.DictReader

Construct IR-module from given json.

get_value_ref(name, ty=ir-typ ptr)

Retrieve reference to a value.

class ppci.irutils.io.DictWriter

Serialize an IR-module as a dict.

ppci.irutils.io.from_json(json_txt)

Construct a module from valid json.

Parameters:json_txt – A string with valid JSON.
Returns:The IR-module as represented by JSON.
ppci.irutils.io.to_json(module)

Convert an IR-module to json format.

Parameters:module – the IR-module intended for serialization.
Returns:A JSON string representing the module.