Example #1
0
    def testTypeCompare(self):
        a = Type.int8()
        b = Type.int8()

        self.assertEqual(a, b)

        c = Type.int32()
        self.assertTrue(c != a)
Example #2
0
    def testTypeCompare(self):
        a = Type.int8()
        b = Type.int8()

        self.assertEqual(a, b)

        c = Type.int32()
        self.assertTrue(c != a)
Example #3
0
    def testCreateInt8(self):
        ty = Type.int8(self.global_context)
        self.assertEqual('i8', ty.name)

        ty = Type.int8()
        self.assertEqual('i8', ty.name)

        context = ty.context
        self.assertEqual(context, self.global_context)

        kind = ty.kind
        self.assertEqual(TypeKind.Integer, kind)
        self.assertTrue(ty.is_sized())
Example #4
0
    def testCreateInt8(self):
        ty = Type.int8(self.global_context)
        self.assertEqual('i8', ty.name)

        ty = Type.int8()
        self.assertEqual('i8', ty.name)

        context = ty.context
        self.assertEqual(context, self.global_context)

        kind = ty.kind
        self.assertEqual(TypeKind.Integer, kind)
        self.assertTrue(ty.is_sized())
Example #5
0
    def testNullInt8(self):
        ty = Type.int8()
        v = Value.null(ty)

        self.assertTrue(v.is_null())
        self.assertTrue(v.is_const_int())
        v.dump()
Example #6
0
def create_global_load_save_module():
    mod = Module.CreateWithName('module')
    ty = Type.int8(mod.context)
    x = Global.add(mod, ty, 'x')
    x.initializer = Value.const_int(ty, 0, True)

    def create_store():
        ft = Type.function(Type.void(), [ty], False)
        f = mod.add_function('store', ft)
        bb = f.append_basic_block('body')
        bldr = Builder.create(mod.context)
        bldr.position_at_end(bb)

        xt = f.get_param(0)
        bldr.store(xt, x)
        bldr.ret_void()

    def create_load():
        ft = Type.function(ty, [], False)
        f = mod.add_function('load', ft)
        bb = f.append_basic_block('body')
        bldr = Builder.create(mod.context)
        bldr.position_at_end(bb)

        xt = bldr.load(x, "xt")
        bldr.ret(xt)
        
    create_store()
    create_load()
    return mod
Example #7
0
def create_cumsum_module():
    mod = Module.CreateWithName('module')

    ty = Type.int8(context=mod.context)
    ft = Type.function(ty, [ty], False)
    
    f = mod.add_function('cumsum', ft)
    bb1 = f.append_basic_block('body')
    bb_hdr = f.append_basic_block('hdr')
    bb_loop = f.append_basic_block('loop')
    bb_exit = f.append_basic_block('exit')

    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb1)
    bldr.branch(bb_hdr)

    bldr.position_at_end(bb_hdr)
    i = bldr.phi(ty, 'i')
    s = bldr.phi(ty, 's')
    zero = Value.const_int(ty, 0, True)
    c = bldr.int_signed_lt(zero, i, 'comp')
    bldr.conditional_branch(c, bb_loop, bb_exit)

    bldr.position_at_end(bb_loop)
    s1 = bldr.add(s, i, 's1')
    i1 = bldr.sub(i, Value.const_int(ty, 1, True), 'i1')
    bldr.branch(bb_hdr)

    i.add_incoming([f.get_param(0), i1], [bb1, bb_loop])
    s.add_incoming([Value.const_int(ty, 0, True), s1], [bb1, bb_loop])
    
    bldr.position_at_end(bb_exit)
    bldr.ret(s)
    return (mod, f)
Example #8
0
def create_abs_module():
    mod = Module.CreateWithName('module')

    ty = Type.int8(context=mod.context)
    ft = Type.function(ty, [ty], False)
    
    f = mod.add_function('abs', ft)
    bb1 = f.append_basic_block('body')
    bbt = f.append_basic_block('true')
    bbf = f.append_basic_block('false')
    bbm = f.append_basic_block('merge')

    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb1)
    x = f.get_param(0)
    zero = Value.const_int(ty, 0, True)
    c = bldr.int_signed_lt(x, zero, 'comp')
    bldr.conditional_branch(c, bbt, bbf)

    # True branch
    bldr.position_at_end(bbt)
    y_t = bldr.neg(x, 'neg_x')
    bldr.branch(bbm)
    
    # False branch
    bldr.position_at_end(bbf)
    bldr.branch(bbm)
    
    bldr.position_at_end(bbm)
    y = bldr.phi(ty, 'y')
    y.add_incoming([y_t, x], [bbt, bbf])
    bldr.ret(y)
    return (mod, f)
Example #9
0
def create_global_load_save_module():
    mod = Module.CreateWithName('module')
    ty = Type.int8(mod.context)
    x = Global.add(mod, ty, 'x')
    x.initializer = Value.const_int(ty, 0, True)

    def create_store():
        ft = Type.function(Type.void(), [ty], False)
        f = mod.add_function('store', ft)
        bb = f.append_basic_block('body')
        bldr = Builder.create(mod.context)
        bldr.position_at_end(bb)

        xt = f.get_param(0)
        bldr.store(xt, x)
        bldr.ret_void()

    def create_load():
        ft = Type.function(ty, [], False)
        f = mod.add_function('load', ft)
        bb = f.append_basic_block('body')
        bldr = Builder.create(mod.context)
        bldr.position_at_end(bb)

        xt = bldr.load(x, "xt")
        bldr.ret(xt)

    create_store()
    create_load()
    return mod
Example #10
0
 def testAppendBasicBlock(self):
     mod = Module.CreateWithName('module')
     ty = Type.int8(context=mod.context)
     ft = Type.function(ty, [ty], False)
     f = mod.add_function('timestwo', ft)
     bb = f.append_basic_block('body')
     self.assertEqual('body', bb.name)
Example #11
0
def create_abs_module():
    mod = Module.CreateWithName('module')

    ty = Type.int8(context=mod.context)
    ft = Type.function(ty, [ty], False)

    f = mod.add_function('abs', ft)
    bb1 = f.append_basic_block('body')
    bbt = f.append_basic_block('true')
    bbf = f.append_basic_block('false')
    bbm = f.append_basic_block('merge')

    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb1)
    x = f.get_param(0)
    zero = Value.const_int(ty, 0, True)
    c = bldr.int_signed_lt(x, zero, 'comp')
    bldr.conditional_branch(c, bbt, bbf)

    # True branch
    bldr.position_at_end(bbt)
    y_t = bldr.neg(x, 'neg_x')
    bldr.branch(bbm)

    # False branch
    bldr.position_at_end(bbf)
    bldr.branch(bbm)

    bldr.position_at_end(bbm)
    y = bldr.phi(ty, 'y')
    y.add_incoming([y_t, x], [bbt, bbf])
    bldr.ret(y)
    return (mod, f)
Example #12
0
 def testTimesTwo_execution(self):
     mod = create_two_module()
     ee = ExecutionEngine.create_execution_engine(mod)
     ty = Type.int8(context=mod.context)
     f = mod.get_function('two')
     y = ee.run_function(f, [])
     self.assertEqual(2, y.to_int(True))
Example #13
0
    def testCreateGenericIntValue(self):
        ty = Type.int8()
        gv = GenericValue.of_int(ty, 4, True)
        self.assertEqual(4, gv.to_int(True))

        gv = GenericValue.of_int(ty, 128 + 4, False)
        self.assertEqual(132, gv.to_int(False))
Example #14
0
    def testCreateVectorType(self):
        ty = Type.int8()
        a = Type.vector(ty, 2)
        self.assertEqual(2, a.vector_size())

        t = a.element_type()
        self.assertEqual(ty.name, t.name)
Example #15
0
def create_cumsum_module():
    mod = Module.CreateWithName('module')

    ty = Type.int8(context=mod.context)
    ft = Type.function(ty, [ty], False)

    f = mod.add_function('cumsum', ft)
    bb1 = f.append_basic_block('body')
    bb_hdr = f.append_basic_block('hdr')
    bb_loop = f.append_basic_block('loop')
    bb_exit = f.append_basic_block('exit')

    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb1)
    bldr.branch(bb_hdr)

    bldr.position_at_end(bb_hdr)
    i = bldr.phi(ty, 'i')
    s = bldr.phi(ty, 's')
    zero = Value.const_int(ty, 0, True)
    c = bldr.int_signed_lt(zero, i, 'comp')
    bldr.conditional_branch(c, bb_loop, bb_exit)

    bldr.position_at_end(bb_loop)
    s1 = bldr.add(s, i, 's1')
    i1 = bldr.sub(i, Value.const_int(ty, 1, True), 'i1')
    bldr.branch(bb_hdr)

    i.add_incoming([f.get_param(0), i1], [bb1, bb_loop])
    s.add_incoming([Value.const_int(ty, 0, True), s1], [bb1, bb_loop])

    bldr.position_at_end(bb_exit)
    bldr.ret(s)
    return (mod, f)
Example #16
0
 def testTimesTwo_execution(self):
     mod = create_two_module()
     ee = ExecutionEngine.create_execution_engine(mod)
     ty = Type.int8(context=mod.context)
     f = mod.get_function('two')
     y = ee.run_function(f, [])
     self.assertEqual(2, y.to_int(True))
Example #17
0
    def testNullInt8(self):
        ty = Type.int8()
        v = Value.null(ty)

        self.assertTrue(v.is_null())
        self.assertTrue(v.is_const_int())
        v.dump()
Example #18
0
    def testCreateGenericIntValue(self):
        ty = Type.int8()
        gv = GenericValue.of_int(ty, 4, True)
        self.assertEqual(4, gv.to_int(True))

        gv = GenericValue.of_int(ty, 128 + 4, False)
        self.assertEqual(132, gv.to_int(False))
Example #19
0
    def testCreateArrayType(self):
        ty = Type.int8()
        a = Type.array(ty, 2)
        self.assertEqual(2, a.array_length())

        t = a.element_type()
        self.assertEqual(ty.name, t.name)
Example #20
0
 def testAppendBasicBlock(self):
     mod = Module.CreateWithName('module')
     ty = Type.int8(context=mod.context)
     ft = Type.function(ty, [ty], False)
     f = mod.add_function('timestwo', ft)
     bb = f.append_basic_block('body')
     self.assertEqual('body', bb.name)
Example #21
0
    def testCreateArrayType(self):
        ty = Type.int8()
        a = Type.array(ty, 2)
        self.assertEqual(2, a.array_length())

        t = a.element_type()
        self.assertEqual(ty.name, t.name)
Example #22
0
    def testCreateVectorType(self):
        ty = Type.int8()
        a = Type.vector(ty, 2)
        self.assertEqual(2, a.vector_size())

        t = a.element_type()
        self.assertEqual(ty.name, t.name)
Example #23
0
    def testLoad(self):
        ty = Type.int8()
        pt = Type.pointer(ty)
        bldr = Builder.create()

        a = bldr.alloca(ty, 'a')
        b = bldr.load(a, 'b')
        self.assertEqual('  %b = load i8* %a', str(b))
Example #24
0
    def testAdd(self):
        ty = Type.int8()
        a = Value.const_int(ty, 1, True)
        b = Value.const_int(ty, 1, True)
        bldr = Builder.create()
        c = bldr.add(a, b, "tmp1")

        self.assertEqual(2, c.get_signext_value())
Example #25
0
    def testConstInt8(self):
        ty = Type.int8()
        v = Value.const_int(ty, 3, True)

        self.assertEqual(3, v.get_signext_value())
        self.assertEqual('i8 3', str(v))
        self.assertEqual('i8', v.type.name)
        self.assertFalse(v.is_undef())
Example #26
0
    def testAdd(self):
        ty = Type.int8()
        mod = self.module
        g = Global.add(mod, ty, 'x')
        self.assertEqual('x', g.name)

        g2 = Global.get(mod, 'x')
        self.assertEqual('x', g2.name)
Example #27
0
    def testCreateNamedStruct(self):
        ty = Type.create_named_structure(self.global_context, "mystruct")
        self.assertEqual('mystruct', ty.struct_name())

        el = Type.int8()
        ty.set_body([el, el], True)
        self.assertEqual(2, ty.num_elements())
        self.assertTrue(ty.is_packed())
Example #28
0
    def testCreatePointer(self):
        ty = Type.int8()
        p = Type.pointer(ty)
        self.assertEqual(0, p.pointer_address_space())
        self.assertEqual('i8*', p.name)

        t = p.element_type()
        self.assertEqual('i8', t.name)
Example #29
0
 def testTimesTwoWithFunction(self):
     mod = create_timestwo_module_with_function()
     ee = ExecutionEngine.create_interpreter(mod)
     ty = Type.int8(context=mod.context)
     f = mod.get_function('caller')
     x = GenericValue.of_int(ty, 3, True)
     y = ee.run_function(f, [x])
     self.assertEqual(6, y.to_int(True))
Example #30
0
    def testInit(self):
        ty = Type.int8()
        mod = self.module
        g = Global.add(mod, ty, 'x')
        g.initializer = Value.const_int(ty, 4, True)
        v = g.initializer

        self.assertEqual(4, v.get_signext_value())
Example #31
0
    def testConstInt8(self):
        ty = Type.int8()
        v = Value.const_int(ty, 3, True)

        self.assertEqual(3, v.get_signext_value())
        self.assertEqual('i8 3', str(v))
        self.assertEqual('i8', v.type.name)
        self.assertFalse(v.is_undef())
Example #32
0
    def testLoad(self):
        ty = Type.int8()
        pt = Type.pointer(ty)
        bldr = Builder.create()

        a = bldr.alloca(ty, 'a')
        b = bldr.load(a, 'b')
        self.assertEqual('  %b = load i8* %a', str(b))
Example #33
0
    def testInit(self):
        ty = Type.int8()
        mod = self.module
        g = Global.add(mod, ty, 'x')
        g.initializer = Value.const_int(ty, 4, True)
        v = g.initializer

        self.assertEqual(4, v.get_signext_value())
Example #34
0
    def testCreatePointer(self):
        ty = Type.int8()
        p = Type.pointer(ty)
        self.assertEqual(0, p.pointer_address_space())
        self.assertEqual('i8*', p.name)

        t = p.element_type()
        self.assertEqual('i8', t.name)
Example #35
0
 def testCumsum(self):
     mod, _ = create_cumsum_module()
     ee = ExecutionEngine.create_interpreter(mod)
     ty = Type.int8(context=mod.context)
     f = mod.get_function('cumsum')
     x = GenericValue.of_int(ty, 10, True)
     y = ee.run_function(f, [x])
     self.assertEqual(55, y.to_int(True))
Example #36
0
 def testTimesTwoWithFunction(self):
     mod = create_timestwo_module_with_function()
     ee = ExecutionEngine.create_interpreter(mod)
     ty = Type.int8(context=mod.context)
     f = mod.get_function('caller')
     x = GenericValue.of_int(ty, 3, True)
     y = ee.run_function(f, [x])
     self.assertEqual(6, y.to_int(True))
Example #37
0
    def testCreateNamedStruct(self):
        ty = Type.create_named_structure(self.global_context, "mystruct")
        self.assertEqual('mystruct', ty.struct_name())

        el = Type.int8()
        ty.set_body([el, el], True)
        self.assertEqual(2, ty.num_elements())
        self.assertTrue(ty.is_packed())
Example #38
0
 def testCumsum(self):
     mod, _ = create_cumsum_module()
     ee = ExecutionEngine.create_interpreter(mod)
     ty = Type.int8(context=mod.context)
     f = mod.get_function('cumsum')
     x = GenericValue.of_int(ty, 10, True)
     y = ee.run_function(f, [x])
     self.assertEqual(55, y.to_int(True))
Example #39
0
    def testAdd(self):
        ty = Type.int8()
        mod = self.module
        g = Global.add(mod, ty, 'x')
        self.assertEqual('x', g.name)

        g2 = Global.get(mod, 'x')
        self.assertEqual('x', g2.name)
Example #40
0
    def testAdd(self):
        ty = Type.int8()
        a = Value.const_int(ty, 1, True)
        b = Value.const_int(ty, 1, True)
        bldr = Builder.create()
        c = bldr.add(a, b, "tmp1")

        self.assertEqual(2, c.get_signext_value())
Example #41
0
    def testCreateNamedStructInMod(self):
        ctx = Context()
        mod = Module.CreateWithName('mod', ctx)
        ty = Type.create_named_structure(mod.context, 'mystruct')
        el = Type.int8(context=mod.context)
        ty.set_body([el, el], True)
        t = mod.get_type('mystruct')

        self.assertEqual(ty, t)
Example #42
0
 def testAddGlobal(self):
     mod = Module.CreateWithName('module')
     ty = Type.int8(context=mod.context)
     a = Value.const_int(ty, 1, True)
     g = Global.add(mod, ty, 'x')
     g.initializer = Value.const_int(ty, 4, True)
     bldr = Builder.create()
     c = bldr.add(g.initializer, a, 'tmp1')
     self.assertEqual(5, c.get_signext_value())
Example #43
0
    def testAddAlias(self):
        ty = Type.int8()
        x = Global.add(self.module, ty, 'x')
        x.initializer = Value.const_int(ty, 1, True)
        pty = Type.pointer(ty)
        y = Global.add_alias(self.module, pty, x, 'y')

        g = list(GlobalIterator(self.module))
        self.assertEqual([x], g)
Example #44
0
 def testAddGlobal(self):
     mod = Module.CreateWithName('module')
     ty = Type.int8(context=mod.context)
     a = Value.const_int(ty, 1, True)
     g = Global.add(mod, ty, 'x')
     g.initializer = Value.const_int(ty, 4, True)
     bldr = Builder.create()
     c = bldr.add(g.initializer, a, 'tmp1')
     self.assertEqual(5, c.get_signext_value())
Example #45
0
    def testAddAlias(self):
        ty = Type.int8()
        x = Global.add(self.module, ty, 'x')
        x.initializer = Value.const_int(ty, 1, True)
        pty = Type.pointer(ty)
        y = Global.add_alias(self.module, pty, x, 'y')

        g = list(GlobalIterator(self.module))
        self.assertEqual([x], g)
Example #46
0
    def testConstInt8Array(self):
        ty = Type.int8()
        v = Value.const_int(ty, 3, True)
        arr = Value.const_array(ty, [v, v])

        arr_ty = arr.type
        self.assertEqual(2, arr_ty.array_length())
        self.assertEqual([v, v], arr.elements())
        self.assertFalse(v.is_const_array())
        self.assertTrue(arr.is_const_array())
Example #47
0
 def testCreateNamedStructInMod(self):
     ctx = Context()
     mod = Module.CreateWithName('mod', ctx)
     ty = Type.create_named_structure(mod.context,
                                      'mystruct')
     el = Type.int8(context=mod.context)
     ty.set_body([el, el], True)
     t = mod.get_type('mystruct')
     
     self.assertEqual(ty, t)
Example #48
0
    def testAlloca(self):
        ty = Type.int8()
        bldr = Builder.create()

        a = bldr.alloca(ty, 'a')
        self.assertEqual('  %a = alloca i8', str(a))

        arr = Type.array(ty, 2)
        b = bldr.alloca(arr, 'b')
        self.assertEqual('  %b = alloca [2 x i8]', str(b))
Example #49
0
    def testName(self):
        # constants cannot be named.
        ty = Type.int8(context=self.module.context)
        v = Global.add(self.module, ty, 'x')

        self.assertEqual('x', v.name)
        v.name = 'one'
        self.assertEqual('one', v.name)
        self.assertTrue(v.is_constant())
        self.assertFalse(v.is_undef())
Example #50
0
    def testName(self):
        # constants cannot be named. 
        ty = Type.int8(context=self.module.context)
        v = Global.add(self.module, ty, 'x')

        self.assertEqual('x', v.name)
        v.name = 'one'
        self.assertEqual('one', v.name)
        self.assertTrue(v.is_constant())
        self.assertFalse(v.is_undef())
Example #51
0
    def testDelete(self):
        ty = Type.int8()
        x = Global.add(self.module, ty, 'x')
        x.initializer = Value.const_int(ty, 1, True)
        y = Global.add(self.module, ty, 'y')
        y.initializer = Value.const_int(ty, 2, True)

        y.delete()
        g = list(GlobalIterator(self.module))
        self.assertEqual([x], g)
Example #52
0
    def testConstInt8Array(self):
        ty = Type.int8()
        v = Value.const_int(ty, 3, True)
        arr = Value.const_array(ty, [v, v])

        arr_ty = arr.type
        self.assertEqual(2, arr_ty.array_length())
        self.assertEqual([v, v], arr.elements())
        self.assertFalse(v.is_const_array())
        self.assertTrue(arr.is_const_array())
Example #53
0
    def testDelete(self):
        ty = Type.int8()
        x = Global.add(self.module, ty, 'x')
        x.initializer = Value.const_int(ty, 1, True)
        y = Global.add(self.module, ty, 'y')
        y.initializer = Value.const_int(ty, 2, True)   

        y.delete()
        g = list(GlobalIterator(self.module))
        self.assertEqual([x], g)
Example #54
0
    def testAlloca(self):
        ty = Type.int8()
        bldr = Builder.create()

        a = bldr.alloca(ty, 'a')
        self.assertEqual('  %a = alloca i8', str(a))

        arr = Type.array(ty, 2)
        b = bldr.alloca(arr, 'b')
        self.assertEqual('  %b = alloca [2 x i8]', str(b))
Example #55
0
    def testStruct(self):
        ty = Type.int8()
        sty = Type.structure([ty, ty], False)
        x = Value.const_int(ty, 1, True)
        y = Value.const_int(ty, 2, True)
        xy = Value.const_struct([x, y])

        self.assertTrue(xy.is_const_struct())
        [x_, y_] = xy.operands
        self.assertEqual(x, x_)
        self.assertEqual(y, y_)
Example #56
0
    def testIntNE(self):
        ty = Type.int8()
        a = Value.const_int(ty, 1, True)
        b = Value.const_int(ty, 2, True)
        bldr = Builder.create()

        c = bldr.int_ne(a, b, "tmp1")
        self.assertEqual(1, c.get_zeroext_value())

        d = bldr.int_ne(a, a, "tmp3")
        self.assertEqual(0, d.get_zeroext_value())
Example #57
0
    def testStruct(self):
        ty = Type.int8()
        sty = Type.structure([ty, ty], False)
        x = Value.const_int(ty, 1, True)
        y = Value.const_int(ty, 2, True)
        xy = Value.const_struct([x, y])

        self.assertTrue(xy.is_const_struct())
        [x_, y_] = xy.operands
        self.assertEqual(x, x_)
        self.assertEqual(y, y_)
Example #58
0
    def testEqual(self):
        a = LLVMObject(c_object_p())
        self.assertTrue(a.is_null())

        b = LLVMObject(c_object_p())
        self.assertEqual(a, b)

        c = Type.int8()
        self.assertNotEqual(c, b)
        self.assertFalse(c.__eq__(b))
        self.assertNotEqual(b, c)
Example #59
0
    def testIntNE(self):
        ty = Type.int8()
        a = Value.const_int(ty, 1, True)
        b = Value.const_int(ty, 2, True)
        bldr = Builder.create()

        c = bldr.int_ne(a, b, "tmp1")
        self.assertEqual(1, c.get_zeroext_value())

        d = bldr.int_ne(a, a, "tmp3")
        self.assertEqual(0, d.get_zeroext_value())
Example #60
0
    def testEqual(self):
        a = LLVMObject(c_object_p())
        self.assertTrue(a.is_null())

        b = LLVMObject(c_object_p())
        self.assertEqual(a, b)

        c = Type.int8()
        self.assertNotEqual(c, b)
        self.assertFalse(c.__eq__(b))
        self.assertNotEqual(b, c)