Ejemplo n.º 1
0
    def create_target_machine(self, cpu='', features='',
                              opt=2, reloc='default', codemodel='jitdefault',
                              jitdebug=False, printmc=False):
        """
        Create a new TargetMachine for this target and the given options.

        Specifying codemodel='default' will result in the use of the "small"
        code model. Specifying codemodel='jitdefault' will result in the code
        model being picked based on platform bitness (32="small", 64="large").
        """
        assert 0 <= opt <= 3
        assert reloc in RELOC
        assert codemodel in CODEMODEL
        triple = self._triple
        # MCJIT under Windows only supports ELF objects, see
        # http://lists.llvm.org/pipermail/llvm-dev/2013-December/068341.html
        # Note we still want to produce regular COFF files in AOT mode.
        if os.name == 'nt' and codemodel == 'jitdefault':
            triple += '-elf'
        tm = ffi.lib.LLVMPY_CreateTargetMachine(self,
                                                _encode_string(triple),
                                                _encode_string(cpu),
                                                _encode_string(features),
                                                opt,
                                                _encode_string(reloc),
                                                _encode_string(codemodel),
                                                int(jitdebug),
                                                int(printmc),
                                                )
        if tm:
            return TargetMachine(tm)
        else:
            raise RuntimeError("Cannot create target machine")
Ejemplo n.º 2
0
def set_option(name, option):
    """
    Set the given LLVM "command-line" option.

    For example set_option("test", "-debug-pass=Structure") would display
    all optimization passes when generating code.
    """
    ffi.lib.LLVMPY_SetCommandLine(_encode_string(name), _encode_string(option))
Ejemplo n.º 3
0
def load_library_permanently(filename):
    """
    Load an external library
    """
    with ffi.OutputString() as outerr:
        if ffi.lib.LLVMPY_LoadLibraryPermanently(
                _encode_string(filename), outerr):
            raise RuntimeError(str(outerr))
Ejemplo n.º 4
0
 def get_struct_type(self, name):
     """
     Get a TypeRef pointing to a structure type named *name*.
     NameError is raised if the struct type isn't found.
     """
     p = ffi.lib.LLVMPY_GetNamedStructType(self, _encode_string(name))
     if not p:
         raise NameError(name)
     return TypeRef(p)
Ejemplo n.º 5
0
 def get_global_variable(self, name):
     """
     Get a ValueRef pointing to the global variable named *name*.
     NameError is raised if the symbol isn't found.
     """
     p = ffi.lib.LLVMPY_GetNamedGlobalVariable(self, _encode_string(name))
     if not p:
         raise NameError(name)
     return ValueRef(p, 'global', dict(module=self))
Ejemplo n.º 6
0
 def get_function(self, name):
     """
     Get a ValueRef pointing to the function named *name*.
     NameError is raised if the symbol isn't found.
     """
     p = ffi.lib.LLVMPY_GetNamedFunction(self, _encode_string(name))
     if not p:
         raise NameError(name)
     return ValueRef(p, 'function', dict(module=self))
Ejemplo n.º 7
0
def get_object_format(triple=None):
    """
    Get the object format for the given *triple* string (or the default
    triple if omitted).
    A string is returned
    """
    if triple is None:
        triple = get_default_triple()
    res = ffi.lib.LLVMPY_GetTripleObjectFormat(_encode_string(triple))
    return _object_formats[res]
Ejemplo n.º 8
0
def parse_assembly(llvmir, context=None):
    """
    Create Module from a LLVM IR string
    """
    if context is None:
        context = get_global_context()
    llvmir = _encode_string(llvmir)
    strbuf = c_char_p(llvmir)
    with ffi.OutputString() as errmsg:
        mod = ModuleRef(ffi.lib.LLVMPY_ParseAssembly(context, strbuf, errmsg),
                        context)
        if errmsg:
            mod.close()
            raise RuntimeError("LLVM IR parsing error\n{0}".format(errmsg))
    return mod
Ejemplo n.º 9
0
    def add_function_attribute(self, attr):
        """Only works on function value

        Parameters
        -----------
        attr : str
            attribute name
        """
        if not self.is_function:
            raise ValueError("expected function value, got %s" % (self._kind,))
        attrname = str(attr)
        attrval = ffi.lib.LLVMPY_GetEnumAttributeKindForName(
            _encode_string(attrname), len(attrname)
        )
        if attrval == 0:
            raise ValueError("no such attribute {!r}".format(attrname))
        ffi.lib.LLVMPY_AddFunctionAttr(self, attrval)
Ejemplo n.º 10
0
    def create_target_machine(self,
                              cpu='',
                              features='',
                              opt=2,
                              reloc='default',
                              codemodel='jitdefault',
                              printmc=False,
                              jit=False,
                              abiname=''):
        """
        Create a new TargetMachine for this target and the given options.

        Specifying codemodel='default' will result in the use of the "small"
        code model. Specifying codemodel='jitdefault' will result in the code
        model being picked based on platform bitness (32="small", 64="large").

        The `printmc` option corresponds to llvm's `-print-machineinstrs`.

        The `jit` option should be set when the target-machine is to be used
        in a JIT engine.

        The `abiname` option specifies the ABI. RISC-V targets with hard-float
        needs to pass the ABI name to LLVM.
        """
        assert 0 <= opt <= 3
        assert reloc in RELOC
        assert codemodel in CODEMODEL
        triple = self._triple
        # MCJIT under Windows only supports ELF objects, see
        # http://lists.llvm.org/pipermail/llvm-dev/2013-December/068341.html
        # Note we still want to produce regular COFF files in AOT mode.
        if os.name == 'nt' and codemodel == 'jitdefault':
            triple += '-elf'
        tm = ffi.lib.LLVMPY_CreateTargetMachine(
            self,
            _encode_string(triple),
            _encode_string(cpu),
            _encode_string(features),
            opt,
            _encode_string(reloc),
            _encode_string(codemodel),
            int(printmc),
            int(jit),
            _encode_string(abiname),
        )
        if tm:
            return TargetMachine(tm)
        else:
            raise RuntimeError("Cannot create target machine")
Ejemplo n.º 11
0
def load_library_permanently(filename):
    """
    Load an external library
    """
    print("load_library_permanently({})".format(filename))
    from logging import getLogger
    import os
    log = getLogger(__name__)
    log.warning("load_library_permanently({})".format(filename))
    log.warning("load_library_permanently .. so.environ[PATH] is: {}".format(
        os.environ['PATH']))
    try:
        with ffi.OutputString() as outerr:
            if ffi.lib.LLVMPY_LoadLibraryPermanently(_encode_string(filename),
                                                     outerr):
                raise RuntimeError(
                    str(outerr) +
                    '\nLIZZY PATH is: {}'.format(os.environ['PATH']))
    except Exception as e:
        raise RuntimeError(
            str(outerr) + '\nLIZZY2 PATH is: {}'.format(os.environ['PATH']))
Ejemplo n.º 12
0
 def name(self, val):
     ffi.lib.LLVMPY_SetValueName(self, _encode_string(val))
Ejemplo n.º 13
0
 def name(self, value):
     ffi.lib.LLVMPY_SetModuleName(self, _encode_string(value))
Ejemplo n.º 14
0
def address_of_symbol(name):
    """
    Get the in-process address of symbol named *name*.
    An integer is returned, or None if the symbol isn't found.
    """
    return ffi.lib.LLVMPY_SearchAddressOfSymbol(_encode_string(name))
Ejemplo n.º 15
0
def add_symbol(name, address):
    """
    Register the *address* of global symbol *name*.  This will make
    it usable (e.g. callable) from LLVM-compiled functions.
    """
    ffi.lib.LLVMPY_AddSymbol(_encode_string(name), c_void_p(address))
Ejemplo n.º 16
0
def create_target_data(layout):
    """
    Create a TargetData instance for the given *layout* string.
    """
    return TargetData(ffi.lib.LLVMPY_CreateTargetData(_encode_string(layout)))