예제 #1
0
파일: testing.py 프로젝트: zhihan/llvm
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)
예제 #2
0
파일: testing.py 프로젝트: zhihan/llvm
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)
예제 #3
0
파일: testing.py 프로젝트: zhihan/llvm
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)
예제 #4
0
파일: testing.py 프로젝트: zhihan/llvm
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)
예제 #5
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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))
예제 #6
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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())
예제 #7
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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))
예제 #8
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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())
예제 #9
0
파일: test_builder.py 프로젝트: zhihan/llvm
 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())
예제 #10
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testFAdd(self):
        ty = Type.double()
        a = Value.const_real(ty, 1.0)
        b = Value.const_real(ty, 1.0)
        bldr = Builder.create()
        c = bldr.fadd(a, b, "tmp1")

        x, l = c.get_double_value()
        self.assertTrue(x - 2.0 < 0.01 and 2.0 - x > -0.01)
예제 #11
0
파일: test_builder.py 프로젝트: zhihan/llvm
 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())
예제 #12
0
파일: testing.py 프로젝트: zhihan/llvm
    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)
예제 #13
0
파일: testing.py 프로젝트: zhihan/llvm
    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)
예제 #14
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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))
예제 #15
0
파일: testing.py 프로젝트: zhihan/llvm
    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()
예제 #16
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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))
예제 #17
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testFAdd(self):
        ty = Type.double()
        a = Value.const_real(ty, 1.0)
        b = Value.const_real(ty, 1.0)
        bldr = Builder.create()
        c = bldr.fadd(a, b, "tmp1")

        x, l = c.get_double_value()
        self.assertTrue(x - 2.0 < 0.01 and
                        2.0 - x > -0.01)
예제 #18
0
파일: testing.py 프로젝트: zhihan/llvm
    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()
예제 #19
0
파일: testing.py 프로젝트: zhihan/llvm
 def create_load():
     ft = Type.function(ty, [ptr_ty], False)
     f = mod.add_function('load', ft)
     bb = f.append_basic_block('body')
     bldr = Builder.create(mod.context)
     bldr.position_at_end(bb)
     
     offset = f.get_param(0)
     elem_ptr = bldr.gep(x, [v0, offset], 'elem')
     y = bldr.load(elem_ptr, 'y')
     bldr.ret(y)
예제 #20
0
파일: test_builder.py 프로젝트: zhihan/llvm
 def testGEP(self):
     ty = Type.int64()
     ptr_ty = Type.int64()
     bldr = Builder.create()
     arr_ty = Type.array(ty, 2)
     
     a = bldr.alloca(arr_ty, 'a')
     offset = Value.const_int(ptr_ty, 0, True)
     b = bldr.gep(a, [offset, offset], 'gep')
     self.assertEqual('  %gep = getelementptr [2 x i64]* %a, i64 0, i64 0',
                      str(b))
예제 #21
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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())
예제 #22
0
파일: testing.py 프로젝트: zhihan/llvm
    def create_load():
        ft = Type.function(ty, [ptr_ty], False)
        f = mod.add_function('load', ft)
        bb = f.append_basic_block('body')
        bldr = Builder.create(mod.context)
        bldr.position_at_end(bb)

        offset = f.get_param(0)
        elem_ptr = bldr.gep(x, [v0, offset], 'elem')
        y = bldr.load(elem_ptr, 'y')
        bldr.ret(y)
예제 #23
0
파일: testing.py 프로젝트: zhihan/llvm
def create_two_module():
    mod = Module.CreateWithName('module')
    ty = Type.int8(context=mod.context)
    ft = Type.function(ty, [], False)
    f = mod.add_function('two', ft)
    bb = f.append_basic_block('body')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)
    two = Value.const_int(ty, 2, True)
    bldr.ret(two)
    return mod
예제 #24
0
파일: test_builder.py 프로젝트: zhihan/llvm
    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())
예제 #25
0
파일: testing.py 프로젝트: zhihan/llvm
def create_two_module():
    mod = Module.CreateWithName('module')
    ty = Type.int8(context=mod.context)
    ft = Type.function(ty, [], False)
    f = mod.add_function('two', ft)
    bb = f.append_basic_block('body')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)
    two = Value.const_int(ty, 2, True)
    bldr.ret(two)    
    return mod
예제 #26
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testGEP(self):
        ty = Type.int64()
        ptr_ty = Type.int64()
        bldr = Builder.create()
        arr_ty = Type.array(ty, 2)

        a = bldr.alloca(arr_ty, 'a')
        offset = Value.const_int(ptr_ty, 0, True)
        b = bldr.gep(a, [offset, offset], 'gep')
        self.assertEqual('  %gep = getelementptr [2 x i64]* %a, i64 0, i64 0',
                         str(b))
예제 #27
0
파일: testing.py 프로젝트: zhihan/llvm
    def create_store():
        ft = Type.function(Type.void(), [ty, ptr_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)
        offset = f.get_param(1)
        elem_ptr = bldr.gep(x, [v0, offset], 'elem')
        bldr.store(xt, elem_ptr)
        bldr.ret_void()
예제 #28
0
파일: testing.py 프로젝트: zhihan/llvm
    def create_store():
        ft = Type.function(Type.void(), [ty, ptr_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)
        offset = f.get_param(1)
        elem_ptr = bldr.gep(x, [v0, offset], 'elem')
        bldr.store(xt, elem_ptr)
        bldr.ret_void()
예제 #29
0
    def setUp(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')
        bldr = Builder.create(mod.context)
        bldr.position_at_end(bb)

        self.bldr = bldr
        self.ty = ty
        self.f = f
예제 #30
0
파일: testing.py 프로젝트: zhihan/llvm
def create_timestwo_module():
    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')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)
    x = f.get_param(0)
    two = Value.const_int(ty, 2, True)
    y = bldr.mul(x, two, 'res')
    bldr.ret(y)    
    return (mod, f)
예제 #31
0
파일: testing.py 프로젝트: zhihan/llvm
def create_timestwo_module():
    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')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)
    x = f.get_param(0)
    two = Value.const_int(ty, 2, True)
    y = bldr.mul(x, two, 'res')
    bldr.ret(y)
    return (mod, f)
예제 #32
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testIntSGT(self):
        ty = Type.int8()
        a = Value.const_int(ty, 1, True)
        b = Value.const_int(ty, 2, True)
        bldr = Builder.create()

        c = bldr.int_signed_gt(a, b, "tmp1")
        self.assertEqual(0, c.get_zeroext_value())

        d = bldr.int_signed_gt(b, a, "tmp2")
        self.assertEqual(1, d.get_zeroext_value())

        e = bldr.int_signed_gt(a, a, "tmp3")
        self.assertEqual(0, e.get_zeroext_value())
예제 #33
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testInsertValue(self):
        ty = Type.int8()
        v = Value.const_int(ty, 1, True)
        n = Value.const_int(ty, 2, True)
        arr_ty = Type.array(ty, 2)
        bldr = Builder.create()

        a = bldr.alloca(arr_ty, 'a')
        a_content = bldr.load(a, 'content')
        b = bldr.insert_value(a_content, v, 0, 'b')
        self.assertEqual('  %b = insertvalue [2 x i8] %content, i8 1, 0', str(b))

        c = bldr.extract_value(a_content, 0, 'c')
        self.assertEqual('  %c = extractvalue [2 x i8] %content, 0', str(c))
예제 #34
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testIntSGT(self):
        ty = Type.int8()
        a = Value.const_int(ty, 1, True)
        b = Value.const_int(ty, 2, True)
        bldr = Builder.create()

        c = bldr.int_signed_gt(a, b, "tmp1")
        self.assertEqual(0, c.get_zeroext_value())

        d = bldr.int_signed_gt(b, a, "tmp2")
        self.assertEqual(1, d.get_zeroext_value())

        e = bldr.int_signed_gt(a, a, "tmp3")
        self.assertEqual(0, e.get_zeroext_value())
예제 #35
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testInsertValue(self):
        ty = Type.int8()
        v = Value.const_int(ty, 1, True)
        n = Value.const_int(ty, 2, True)
        arr_ty = Type.array(ty, 2)
        bldr = Builder.create()

        a = bldr.alloca(arr_ty, 'a')
        a_content = bldr.load(a, 'content')
        b = bldr.insert_value(a_content, v, 0, 'b')
        self.assertEqual('  %b = insertvalue [2 x i8] %content, i8 1, 0',
                         str(b))

        c = bldr.extract_value(a_content, 0, 'c')
        self.assertEqual('  %c = extractvalue [2 x i8] %content, 0', str(c))
예제 #36
0
파일: testing.py 프로젝트: zhihan/llvm
def create_lessthanzero_module():
    mod = Module.CreateWithName('module')

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

    f = mod.add_function('lessthanzero', ft)
    bb = f.append_basic_block('body')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)
    x = f.get_param(0)
    zero = Value.const_int(ty, 0, True)
    y = bldr.int_signed_lt(x, zero, 'res')
    bldr.ret(y)
    return (mod, f)
예제 #37
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testAddGlobalVal(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)
        g.set_const(True)

        t = g.type
        self.assertTrue(g.is_const())

        bldr = Builder.create()
        # Build instruction  %tmp1 = add i8 %tmp0, 1
        c = bldr.add(bldr.load(g, "tmp0"), a, 'tmp1')
        self.assertEqual('i8', c.type.name)
        self.assertEqual('tmp1', c.name)
예제 #38
0
파일: testing.py 프로젝트: zhihan/llvm
def create_lessthanzero_module():
    mod = Module.CreateWithName('module')

    ty = Type.int8(context=mod.context)
    bool_t = Type.int1(context=mod.context)
    ft = Type.function(bool_t, [ty], False)
    
    f = mod.add_function('lessthanzero', ft)
    bb = f.append_basic_block('body')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)
    x = f.get_param(0)
    zero = Value.const_int(ty, 0, True)
    y = bldr.int_signed_lt(x, zero, 'res')
    bldr.ret(y)    
    return (mod, f)
예제 #39
0
파일: test_builder.py 프로젝트: zhihan/llvm
    def testAddGlobalVal(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)
        g.set_const(True)

        t = g.type
        self.assertTrue(g.is_const())

        bldr = Builder.create()
        # Build instruction  %tmp1 = add i8 %tmp0, 1
        c = bldr.add(bldr.load(g, "tmp0"), a, 'tmp1')
        self.assertEqual('i8', c.type.name)
        self.assertEqual('tmp1', c.name)
예제 #40
0
파일: testing.py 프로젝트: zhihan/llvm
def create_timestwo_module_with_global():
    mod = Module.CreateWithName('module')
    ty = Type.int8(mod.context)
    k = Global.add(mod, ty, 'k')
    k.initializer = Value.const_int(ty, 2, True)

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

    x = f.get_param(0)
    two = bldr.load(k, "two")
    y = bldr.mul(x, two, 'res')
    bldr.ret(y)
    return (mod, f)
예제 #41
0
파일: testing.py 프로젝트: zhihan/llvm
def create_timestwo_module_with_global():
    mod = Module.CreateWithName('module')
    ty = Type.int8(mod.context)
    k = Global.add(mod, ty, 'k')
    k.initializer = Value.const_int(ty, 2, True)
    
    ft = Type.function(ty, [ty], False)
    f = mod.add_function('timestwo', ft)
    bb = f.append_basic_block('body')
    bldr = Builder.create(mod.context)
    bldr.position_at_end(bb)

    x = f.get_param(0)
    two = bldr.load(k, "two")
    y = bldr.mul(x, two, 'res')
    bldr.ret(y)    
    return (mod, f)