Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
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)
Beispiel #5
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))
Beispiel #6
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())
Beispiel #7
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))
Beispiel #8
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())
Beispiel #9
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())
Beispiel #10
0
    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)
Beispiel #11
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())
Beispiel #12
0
    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)
Beispiel #13
0
    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)
Beispiel #14
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))
Beispiel #15
0
    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()
Beispiel #16
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))
Beispiel #17
0
    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)
Beispiel #18
0
    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()
Beispiel #19
0
 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)
Beispiel #20
0
 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))
Beispiel #21
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())
Beispiel #22
0
    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)
Beispiel #23
0
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
Beispiel #24
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())
Beispiel #25
0
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
Beispiel #26
0
    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))
Beispiel #27
0
    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()
Beispiel #28
0
    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()
Beispiel #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
Beispiel #30
0
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)
Beispiel #31
0
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)
Beispiel #32
0
    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())
Beispiel #33
0
    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))
Beispiel #34
0
    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())
Beispiel #35
0
    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))
Beispiel #36
0
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)
Beispiel #37
0
    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)
Beispiel #38
0
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)
Beispiel #39
0
    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)
Beispiel #40
0
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)
Beispiel #41
0
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)