当前位置: 首页>>代码示例>>Python>>正文


Python core.Function类代码示例

本文整理汇总了Python中llvm.core.Function的典型用法代码示例。如果您正苦于以下问题:Python Function类的具体用法?Python Function怎么用?Python Function使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Function类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_mysin

    def test_mysin(self):
        if sys.platform == 'win32' and BITS == 32:
            # float32 support is known to fail on 32-bit Windows
            return

        # mysin(x) = sqrt(1.0 - pow(cos(x), 2))
        mod     = Module.new('test')

        float   = Type.float()
        mysinty = Type.function( float, [float] )
        mysin   = mod.add_function(mysinty, "mysin")
        block   = mysin.append_basic_block("entry")
        b       = Builder.new(block)

        sqrt = Function.intrinsic(mod, lc.INTR_SQRT, [float])
        pow  = Function.intrinsic(mod, lc.INTR_POWI, [float])
        cos  = Function.intrinsic(mod, lc.INTR_COS,  [float])

        mysin.args[0].name = "x"
        x    = mysin.args[0]
        one  = Constant.real(float, "1")
        cosx = b.call(cos, [x], "cosx")
        cos2 = b.call(pow, [cosx, Constant.int(Type.int(), 2)], "cos2")
        onemc2 = b.fsub(one, cos2, "onemc2") # Should use fsub
        sin  = b.call(sqrt, [onemc2], "sin")
        b.ret(sin)
        #logging.debug(mod)

#   ; ModuleID = 'test'
#
#   define void @showme() {
#   entry:
#       call i32 @llvm.bswap.i32( i32 42 )              ; <i32>:0 [#uses
#   }
#
#   declare i32 @llvm.bswap.i32(i32) nounwind readnone
#
#   define float @mysin(float %x) {
#   entry:
#       %cosx = call float @llvm.cos.f32( float %x )            ; <float
#       %cos2 = call float @llvm.powi.f32( float %cosx, i32 2 )
#       %onemc2 = sub float 1.000000e+00, %cos2         ; <float> [#uses
#       %sin = call float @llvm.sqrt.f32( float %onemc2 )
#       ret float %sin
#   }
#
#   declare float @llvm.sqrt.f32(float) nounwind readnone
#
#   declare float @llvm.powi.f32(float, i32) nounwind readnone
#
#   declare float @llvm.cos.f32(float) nounwind readnone

        # let's run the function
        ee = le.ExecutionEngine.new(mod)
        arg = le.GenericValue.real(Type.float(), 1.234)
        retval = ee.run_function(mysin, [arg])

        golden = math.sin(1.234)
        answer = retval.as_real(Type.float())
        self.assertTrue(abs(answer-golden)/golden < 1e-5)
开发者ID:chiehwen,项目名称:llvmpy,代码行数:60,代码来源:test_llvmpy.py

示例2: declare_runtime_library

    def declare_runtime_library(self):
        self.runtime = {}

        self.runtime['_print_int'] = Function.new(self.module,
                                                  Type.function(Type.void(), [int_type], False),
                                                  "_print_int")
        self.runtime['_print_float'] = Function.new(self.module,
                                                    Type.function(Type.void(), [float_type], False),
                                                    "_print_float")
        self.runtime['_print_bool'] = Function.new(self.module,
                                                   Type.function(Type.void(), [bool_type], False),
                                                   "_print_bool")
开发者ID:emmett9001,项目名称:gone-compiler,代码行数:12,代码来源:gonellvm.py

示例3: __init__

 def __init__(self, env, node, restype, argtypes):
     self.env = env
     self.node = node
     self.restype = ctypes[restype]
     self.argtypes = [ctypes[t] for t in argtypes]
     self.fntype = Type.function(self.restype, self.argtypes)
     self.fn = Function.new(env.options['module'], self.fntype, node.value)
开发者ID:cheery,项目名称:0137-compiler,代码行数:7,代码来源:cffi.py

示例4: CodeGen

 def CodeGen(self):
     # Make the function type, eg. double(double,double).
     funct_type = Type.function(Type.double(), [Type.double()] * len(self.args), False)
                                                 
     function = Function.new(g_llvm_module, funct_type, self.name)
                                                     
     # If the name conflicted, there was already something with the same name.
     # If it has a body, don't allow redefinition or reextern.
     if function.name != self.name:
         function.delete()
         function = g_llvm_module.get_function_named(self.name)
                                                                 
         # If the function already has a body, reject this.
         if not function.is_declaration:
             raise RuntimeError('Redefinition of function.')
                                                                         
         # If the function took a different number of args, reject.
         if len(function.args) != len(self.args):
             raise RuntimeError('Redeclaration of a function with different number of args.')
                                                                                 
     # Set names for all arguments and add them to the variables symbol table.
     for arg, arg_name in zip(function.args, self.args):
         arg.name = arg_name
                                                                                         
     return function
开发者ID:dongchen-coder,项目名称:dongchen-coder.github.io,代码行数:25,代码来源:kaleidoscope.py

示例5: test_bswap

    def test_bswap(self):
        # setup a function and a builder
        mod = Module.new("test")
        functy = Type.function(Type.int(), [])
        func = mod.add_function(functy, "showme")
        block = func.append_basic_block("entry")
        b = Builder.new(block)

        # let's do bswap on a 32-bit integer using llvm.bswap
        val = Constant.int(Type.int(), 0x42)
        bswap = Function.intrinsic(mod, lc.INTR_BSWAP, [Type.int()])

        bswap_res = b.call(bswap, [val])
        b.ret(bswap_res)

        # logging.debug(mod)

        # the output is:
        #
        #    ; ModuleID = 'test'
        #
        #    define void @showme() {
        #    entry:
        #      %0 = call i32 @llvm.bswap.i32(i32 42)
        #      ret i32 %0
        #    }

        # let's run the function
        ee = le.ExecutionEngine.new(mod)
        retval = ee.run_function(func, [])
        self.assertEqual(retval.as_int(), 0x42000000)
开发者ID:meteogrid,项目名称:llvmpy,代码行数:31,代码来源:test_llvmpy.py

示例6: add_prelude

 def add_prelude(self):
     for name, function in prelude.iteritems():
         self.intrinsics[name] = Function.new(
             self.module,
             function,
             name
         )
开发者ID:rfaulkner,项目名称:blaze-core,代码行数:7,代码来源:codegen.py

示例7: codegen_proto

def codegen_proto(proto):
    name, args = proto[1], proto[2]
    double_types = [double_type] * len(args)
    func_type = Type.function(double_type, double_types)
    try:
        func = Function.get(the_module, name)
        if func.basic_block_count:
            raise CodegenError("redefinition of function")
        if len(func.args) != len(args):
            raise CodegenError("redefinition of function with different # args")
    except LLVMException:
        func = Function.new(the_module, func_type, name)
    for arg, name in zip(func.args, args):
        arg.name = name
        named_values[name] = arg
    return func
开发者ID:simudream,项目名称:kaleidoscope,代码行数:16,代码来源:kaleidoscope.py

示例8: emit_extern_func

 def emit_extern_func(self, extnode):
     assert extnode.name not in self.functions
     fntype = Type.function(self.type_map[extnode.rettype],
                            [self.type_map[t] for _, t in funcnode],
                            extnode.varargs)
     self.functions[extnode.name] = Function.new(self.module, fntype,
                                                 extnode.name)
     return self.funcnode[extnode.name]
开发者ID:bryant,项目名称:gravel,代码行数:8,代码来源:gravel.py

示例9: test_sin_f32

 def test_sin_f32(self):
     if sys.platform == 'win32' and BITS == 32:
         # float32 support is known to fail on 32-bit Windows
         return
     float = Type.float()
     mod, func, b = self._build_module(float)
     intr = Function.intrinsic(mod, lc.INTR_SIN, [float])
     b.ret(b.call(intr, func.args))
     self._template(mod, func, math.sin)
开发者ID:B-Rich,项目名称:llvmpy,代码行数:9,代码来源:test_intrinsic_basic.py

示例10: _build_test_module

    def _build_test_module(self):
        mod = Module.new("test")

        float = Type.double()
        mysinty = Type.function(float, [float])
        mysin = mod.add_function(mysinty, "mysin")
        block = mysin.append_basic_block("entry")
        b = Builder.new(block)

        sqrt = Function.intrinsic(mod, lc.INTR_SQRT, [float])
        pow = Function.intrinsic(mod, lc.INTR_POWI, [float])
        cos = Function.intrinsic(mod, lc.INTR_COS, [float])

        mysin.args[0].name = "x"
        x = mysin.args[0]
        one = Constant.real(float, "1")
        cosx = b.call(cos, [x], "cosx")
        cos2 = b.call(pow, [cosx, Constant.int(Type.int(), 2)], "cos2")
        onemc2 = b.fsub(one, cos2, "onemc2")  # Should use fsub
        sin = b.call(sqrt, [onemc2], "sin")
        b.ret(sin)
        return mod, mysin
开发者ID:meteogrid,项目名称:llvmpy,代码行数:22,代码来源:test_llvmpy.py

示例11: init_llvm

    def init_llvm(self):
        mod = Module.new("exprllvm")
        self.engine = ExecutionEngine.new(mod)

        # functions
        self.llvm_functions = {}
        func = Function.new(mod, Type.function(
            Type.void(), [], False), "main")
        self.llvm_functions['main'] = func
        block = func.append_basic_block("entry")

        # builder
        builder = Builder.new(block)
        self.builder = builder

        # add some pre-defined functions
        print_int = Function.new(mod, Type.function(
            Type.void(), [Type.int()], False), "print_int")
        self.llvm_functions['print_int'] = print_int

        self.builder.call(print_int, [Constant.int(Type.int(),3)])
        self.builder.ret_void()
开发者ID:DrOriam,项目名称:pycompile12,代码行数:22,代码来源:exprllvm.py

示例12: build_intrinsics

def build_intrinsics(mod):
    # XXX define in seperate module and then link in
    ins = {}

    for name, intr in intrinsics.llvm_intrinsics.items():
         # get the function signature
        name, retty, argtys = getattr(intrinsics, name)

        largtys = list(map(arg_typemap, argtys))
        lretty  = arg_typemap(retty)

        lfunc = Function.intrinsic(mod, intr, largtys)
        ins[name] = lfunc

    return mod, ins
开发者ID:aashish24,项目名称:blaze,代码行数:15,代码来源:codegen.py

示例13: declare_function

 def declare_function(self, func_block):
     """Declare a Function and add it to self.vars"""
     _, name, ret_type, *args = func_block.instructions[0]
     args = [] if args == [[]] else args
     if not ret_type:
         ret_type = args[0]
         arg_types = []
     else:
         arg_types = [typemap[t] for t in args[1::2]]
     ret_type = typemap[ret_type]
     function = Function.new(
         self.module, Type.function(ret_type, arg_types, False), name
     )
     for i, argname in enumerate(args[0::2]):
         function.args[i].name = argname
     self.vars[name] = function
开发者ID:geraldstanje,项目名称:gone,代码行数:16,代码来源:gonellvm.py

示例14: buildLoad

def buildLoad(module, memory):
    '''Build function load value from memory at address'''

    # Declare load
    loadType = Type.function(Type.int(), [Type.int()], False)
    load = Function.new(module, loadType, 'load')

    # Build body
    body = load.append_basic_block('body')
    builder = Builder.new(body)

    # Get pointer to memory at address
    addr = builder.sext(load.args[0], Type.int(bits=64))
    value = builder.load(builder.gep(memory, [num(0), addr]))

    # Return value
    builder.ret(value)
    load.verify()
开发者ID:cdparks,项目名称:hugo,代码行数:18,代码来源:genllvm.py

示例15: start_function

    def start_function(self, name, retty, argtys):
        rettype = arg_typemap(retty)
        argtypes = [arg_typemap(arg) for arg in argtys]
        func_type = Type.function(rettype, argtypes, False)

        self.function = Function.new(self.module, func_type, name)

        self.block = self.function.append_basic_block("entry")
        self.builder = Builder.new(self.block)
        self.exit_block = self.function.append_basic_block("exit")

        self.locals = {}
        self.stack = {}

        if rettype is not void_type:
            self.locals['retval'] = self.builder.alloca(rettype, "retval")

        self.globals[name] = self.function
开发者ID:rfaulkner,项目名称:blaze-core,代码行数:18,代码来源:codegen.py


注:本文中的llvm.core.Function类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。