def fold_lvn_stmt(self, lvn_stmt): eval_string = str(self.lvn_dict.variable_dict.get_variable(lvn_stmt.left)) + \ operator_dict.get(lvn_stmt.operator) + \ str(self.lvn_dict.variable_dict.get_variable(lvn_stmt.right)) val_after_folded = eval(eval_string) lvn_stmt.left = self.lvn_dict.variable_dict._add_to_variable_dict( SsaVariable(val_after_folded)) lvn_stmt.operator = None lvn_stmt.right = None self.lvn_dict.simple_assign_dict.update_simp_assgn( lvn_stmt.target, lvn_stmt.left)
def test_value_number_to_var_list(self): as_tree = ast.parse( ms("""\ a = x + y b = x - z a = 2""")) ssa_code = SsaCode(as_tree) lvn_dict = LvnDict() for ssa in ssa_code: lvn_dict.variable_dict.enumerate(ssa) expected_list = [ SsaVariable("x"), SsaVariable("y"), SsaVariable("a"), SsaVariable("z"), SsaVariable("b"), SsaVariable(2), SsaVariable("a", 1) ] self.assert_variable_list_equal( lvn_dict.variable_dict.val_num_var_list, expected_list)
def test_SsaVariable_generation(self): ssa_var = SsaVariable('a') self.assertEqual(str(ssa_var), 'a_0') ssa_var = SsaVariable(3) self.assertEqual(str(ssa_var), '3')
def test_optimize_alg_ident_Div(self): alg_ident = AlgIdent() left, op, right = alg_ident.optimize_alg_identities( SsaVariable('a'), 'Div', SsaVariable('a')) expected_list = [SsaVariable(1), None, None] self.assert_variable_list_equal([left, op, right], expected_list) left, op, right = alg_ident.optimize_alg_identities( SsaVariable(0), 'Div', SsaVariable('a')) expected_list = [SsaVariable(0), None, None] self.assert_variable_list_equal([left, op, right], expected_list) left, op, right = alg_ident.optimize_alg_identities( SsaVariable('a'), 'Div', SsaVariable(1)) expected_list = [SsaVariable('a'), None, None] self.assert_variable_list_equal([left, op, right], expected_list) left, op, right = alg_ident.optimize_alg_identities( SsaVariable(1), 'Div', SsaVariable('a')) expected_list = [SsaVariable(1), 'Div', SsaVariable('a')] self.assert_variable_list_equal([left, op, right], expected_list) left, op, right = alg_ident.optimize_alg_identities( SsaVariable('a'), 'Div', SsaVariable('b')) expected_list = [SsaVariable('a'), 'Div', SsaVariable('b')] self.assert_variable_list_equal([left, op, right], expected_list)