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)
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())
def testValueFromString(self): ty = Type.double() a = Value.const_real(ty, 1.0) b = Value.const_real(ty, "1.0") x, _ = a.get_double_value() y, _ = b.get_double_value() self.assertEqual(x, y)
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())
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)
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())
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)
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_)
def test_replace_uses(self): x = self.f.get_param(0) two = Value.const_int(self.ty, 2, True) y = self.bldr.mul(x, two, 'res') c = Value.const_int(self.ty, 3, True) x.replace_uses_with(c) #print(y) self.assertEqual(2, len(y.operands)) self.assertEqual([c, two], y.operands)
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())
def testIter(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) g = list(GlobalIterator(self.module)) self.assertEqual([x, y], g) g = list(GlobalIterator(self.module, reverse=True)) self.assertEqual([y, x], g)
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))
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())
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)
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
def testNullInt8(self): ty = Type.int8() v = Value.null(ty) self.assertTrue(v.is_null()) self.assertTrue(v.is_const_int()) v.dump()
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)
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())
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())
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)
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
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))
def testUse(self): x = self.f.get_param(0) two = Value.const_int(self.ty, 2, True) y = self.bldr.mul(x, two, 'res') use = Use.first(x) self.assertTrue(use.next is None) self.assertEqual(y, use.user) self.assertEqual(x, use.used_value) uses = list(x.uses_iter()) self.assertEqual([use], uses)
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)
def create_global_load_save_array_module(): mod = Module.CreateWithName('module') ty = Type.int8(mod.context) array_ty = Type.array(ty, 2) x = Global.add(mod, array_ty, 'x') v = Value.const_int(ty, 0, True) ptr_ty = Type.int64(mod.context) v0 = Value.const_int(ptr_ty, 0, True) x.initializer = Value.const_array(ty, [v, v]) 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() 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) create_store() create_load() return mod
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)
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)
def testOperands(self): x = self.f.get_param(0) two = Value.const_int(self.ty, 2, True) y = self.bldr.mul(x, two, 'res') ops = y.operands self.assertEqual(2, len(ops)) self.assertEqual([x, two], ops) uses = y.operand_uses self.assertEqual(2, len(uses)) self.assertEqual(x, uses[0].used_value) self.assertEqual(y, uses[0].user) self.assertEqual(two, uses[1].used_value) self.assertEqual(y, uses[1].user) y.set_operand(0, two) # y.dump() ops = y.operands self.assertEqual(2, len(ops)) self.assertEqual([two, two], ops)
def testConstString(self): v = Value.const_string('abc') self.assertTrue(v.is_const_string()) a = Value.const_string('cde', self.context) self.assertEqual('cde', a.get_string())
def testConstantCannotBeNamed(self): ty = Type.int8() v = Value.const_int(ty, 3, True) self.assertEqual('', v.name) v.name = 'something' self.assertEqual('', v.name)