diff --git a/test/operands.py b/test/operands.py index 36a4c85..b1a47ac 100755 --- a/test/operands.py +++ b/test/operands.py @@ -3,6 +3,8 @@ # Tests accessing of instruction operands. from llvm.core import * +from StringIO import StringIO +import logging, unittest m = None @@ -24,54 +26,54 @@ entry: ret i32 %tmp3 } """ -class strstream(object): - def __init__(self): pass - def read(self): return test_module -m = Module.from_assembly(strstream()) -print("-"*60) -print(m) -print("-"*60) -test_func = m.get_function_named("test_func") -prod = m.get_function_named("prod") +class TestOperands(unittest.TestCase): + def test_operands(self): + m = Module.from_assembly(StringIO(test_module)) + logging.debug("-"*60) + logging.debug(m) + logging.debug("-"*60) -#===----------------------------------------------------------------------=== -# test operands + test_func = m.get_function_named("test_func") + prod = m.get_function_named("prod") -print() -i1 = test_func.basic_blocks[0].instructions[0] -i2 = test_func.basic_blocks[0].instructions[1] -print("Testing User.operand_count ..", end=' ') -if i1.operand_count == 3 and i2.operand_count == 2: - print("OK") -else: - print("FAIL") + #===----------------------------------------------------------------------=== + # test operands -print("Testing User.operands ..", end=' ') -c1 = i1.operands[0] is prod -c2 = i1.operands[1] is test_func.args[0] -c3 = i1.operands[2] is test_func.args[1] -c4 = i2.operands[0] is i1 -c5 = i2.operands[1] is test_func.args[2] -c6 = len(i1.operands) == 3 -c7 = len(i2.operands) == 2 -if c1 and c2 and c3 and c5 and c6 and c7: - print("OK") -else: - print("FAIL") -print() -#===----------------------------------------------------------------------=== -# show test_function + i1 = test_func.basic_blocks[0].instructions[0] + i2 = test_func.basic_blocks[0].instructions[1] + logging.debug("Testing User.operand_count ..") + + self.assertEqual(i1.operand_count, 3) + self.assertEqual(i2.operand_count, 2) + + logging.debug("Testing User.operands ..") + + self.assertIs(i1.operands[-1], prod) + self.assertIs(i1.operands[0], test_func.args[0]) + self.assertIs(i1.operands[1], test_func.args[1]) + self.assertIs(i2.operands[0], i1) + self.assertIs(i2.operands[1], test_func.args[2]) + self.assertEqual(len(i1.operands), 3) + self.assertEqual(len(i2.operands), 2) + + #===----------------------------------------------------------------------=== + # show test_function + + logging.debug("Examining test_function `test_test_func':") + + idx = 1 + for inst in test_func.basic_blocks[0].instructions: + logging.debug("Instruction #%d:", idx) + logging.debug(" operand_count = %d", inst.operand_count) + logging.debug(" operands:") + oidx = 1 + for op in inst.operands: + logging.debug(" %d: %s", oidx, repr(op)) + oidx += 1 + idx += 1 + +if __name__ == '__main__': + unittest.main() -print("Examining test_function `test_test_func':") -idx = 1 -for inst in test_func.basic_blocks[0].instructions: - print("Instruction #%d:" % (idx,)) - print(" operand_count =", inst.operand_count) - print(" operands:") - oidx = 1 - for op in inst.operands: - print(" %d: %s" % (oidx, repr(op))) - oidx += 1 - idx += 1 diff --git a/test/passes.py b/test/passes.py index 03447e9..669bdf3 100755 --- a/test/passes.py +++ b/test/passes.py @@ -4,6 +4,10 @@ from llvm.core import * from llvm.passes import * from llvm.ee import * +from StringIO import StringIO + +import logging, unittest + # A helper class. class strstream(object): def __init__(self, s): @@ -35,43 +39,68 @@ entry: ret i32 %tmp } """ -m = Module.from_assembly(strstream(asm)) -print("-"*72) -print(m) -# Let's run a module-level inlining pass. First, create a pass manager. -pm = PassManager.new() +class TestPasses(unittest.TestCase): + def test_passes(self): + m = Module.from_assembly(StringIO(asm)) + logging.debug("-"*72) + logging.debug(m) -# Add the target data as the first "pass". This is mandatory. -pm.add( TargetData.new('') ) + fn_test1 = m.get_function_named('test1') + fn_test2 = m.get_function_named('test2') -# Add the inlining pass. -pm.add( PASS_FUNCTION_INLINING ) + original_test1 = str(fn_test1) + original_test2 = str(fn_test2) -# Run it! -pm.run(m) + # Let's run a module-level inlining pass. First, create a pass manager. + pm = PassManager.new() -# Done with the pass manager. -del pm + # Add the target data as the first "pass". This is mandatory. + pm.add( TargetData.new('') ) -# Print the result. Note the change in @test2. -print("-"*72) -print(m) + # Add the inlining pass. + pm.add( PASS_FUNCTION_INLINING ) + + # Run it! + pm.run(m) -# Let's run a DCE pass on the the function 'test1' now. First create a -# function pass manager. -fpm = FunctionPassManager.new(m) + # Done with the pass manager. + del pm -# Add the target data as first "pass". This is mandatory. -fpm.add( TargetData.new('') ) + # Print the result. Note the change in @test2. + logging.debug("-"*72) + logging.debug(m) -# Add a DCE pass -fpm.add( PASS_AGGRESSIVE_DCE ) + # Make sure test2 is inlined + self.assertNotEqual(str(fn_test2).strip(), original_test2.strip()) -# Run the pass on the function 'test1' -fpm.run( m.get_function_named('test1') ) + bb_entry = fn_test2.basic_blocks[0] + + self.assertEqual(len(bb_entry.instructions), 1) + self.assertEqual(bb_entry.instructions[0].opcode_name, 'ret') + + # Let's run a DCE pass on the the function 'test1' now. First create a + # function pass manager. + fpm = FunctionPassManager.new(m) + + # Add the target data as first "pass". This is mandatory. + fpm.add( TargetData.new('') ) + + # Add a DCE pass + fpm.add( PASS_AGGRESSIVE_DCE ) + + # Run the pass on the function 'test1' + fpm.run( m.get_function_named('test1') ) + + # Print the result. Note the change in @test1. + logging.debug("-"*72) + logging.debug(m) + + # Make sure test1 is modified + self.assertNotEqual(str(fn_test1).strip(), original_test1.strip()) + + +if __name__ == '__main__': + unittest.main() -# Print the result. Note the change in @test1. -print("-"*72) -print(m)