Continuum Analytics: Documentation

# 4.4. Modules

While they conceptually represent the same thing, modules in the IR layer and modules in the binding layer don’t have the same roles and don’t expose the same API.

While modules in the IR layer allow to build and group functions together, modules in the binding layer give access to compilation, linking and execution of code. To distinguish between the two, the module class in the binding layer is called `ModuleRef` as opposed to `llvmlite.ir.Module`.

To go from one realm to the other, you must use the `parse_assembly()` function.

## 4.4.1. Factory functions

A module can be created from the following factory functions:

`llvmlite.binding.``parse_assembly`(llvmir)

  Parse the given llvmir, a string containing some LLVM IR code. If parsing is successful, a new `ModuleRef` instance is returned.

  llvmir can be obtained, for example, by calling `str()` on a `llvmlite.ir.Module` object.

`llvmlite.binding.``parse_bitcode`(bitcode)

  Parse the given bitcode, a bytestring containing the LLVM bitcode of a module. If parsing is successful, a new `ModuleRef` instance is returned.

  bitcode can be obtained, for example, by calling `ModuleRef.as_bitcode()`.

## 4.4.2. The ModuleRef class

class `llvmlite.binding.``ModuleRef`

  A wrapper around a LLVM module object. The following methods and properties are available:

  `as_bitcode`()

    Return the bitcode of this module as a bytes object.

  `get_function`(name)

    Get the function with the given name in this module. If found, a `ValueRef` is returned. Otherwise, `NameError` is raised.

  `get_global_variable`(name)

    Get the global variable with the given name in this module. If found, a `ValueRef` is returned. Otherwise, `NameError` is raised.

  `link_in`(other, preserve=False)

    Link the other module into this module, resolving references wherever possible. If preserve is true, the other module is first copied in order to preserve its contents; if preserve is false, the other module is not usable after this call anymore.

  `verify`()

    Verify the module’s correctness. `RuntimeError` is raised on error.

  `data_layout`

    The data layout string for this module. This attribute is settable.

  `functions`

    An iterator over the functions defined in this module. Each function is a `ValueRef` instance.

  `global_variables`

    An iterator over the global variables defined in this module. Each global variable is a `ValueRef` instance.

  `name`

    The module’s identifier, as a string. This attribute is settable.

  `triple`

    The platform “triple” string for this module. This attribute is settable.