# Modules

Although they conceptually represent the same thing, modules in the IR layer and modules in the binding layer do not have the same roles and do not expose the same API.

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

To go from the IR layer to the binding layer, use the `parse_assembly()` function.

## Factory functions

You can create a module from the following factory functions:

- llvmlite.binding.parse_assembly(llvmir, context=None)

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

    - context: an instance of `LLVMContextRef`.

      > Defaults to the global context.

    EXAMPLE: You can obtain llvmir by calling `str()` on an `llvmlite.ir.Module` object.

- llvmlite.binding.parse_bitcode(bitcode, context=None)

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

    - context: an instance of `LLVMContextRef`.

      > Defaults to the global context.

    EXAMPLE: You can obtain the bitcode by calling `ModuleRef.as_bitcode()`.

## The ModuleRef class

class llvmlite.binding.ModuleRef

  A wrapper around an 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.

  > - get_struct_type(name)

  >     Get the struct type with the given name in this module.

  >     If found, a `TypeRef` 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.

  - verify()

      Verify the module’s correctness. On error, raise `RuntimeError`.

  - data_layout

      The data layout string for this module. This attribute can be set.

  - 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.

  - struct_types

      An iterator over the struct types defined in this module. Each type is a `TypeRef` instance.

  - name

      The module’s identifier, as a string. This attribute can be set.

  - source_file

      The module’s reported source file, as a string. This attribute can not be set.

  - triple

      The platform “triple” string for this module. This attribute can be set.