def test_rename_given_repeated_definition(self): as_tree = AstBuilder().string_build( dedent("""\ a = 3 # 1st if a > 3: # | a = 3 # 2nd a = 98 else: # 3rd z = 4 # | # expected phi func for 'a' here y = a # 4th a = 4 """)) cfg_real = Cfg(as_tree) cfg_real.fill_df() cfg_real.gather_initial_info() cfg_real.ins_phi_function_all() cfg_real.rename_to_ssa() self.assertBlockSsaList( cfg_real.block_list, { "Module": dedent("""\ Module """), "L1": dedent("""\ Assign: (a_0,) = 3 a_0 > 3 """), "L3": dedent("""\ Assign: (a_1,) = 3 Assign: (a_2,) = 98 """), "L6": dedent("""\ Assign: (z_0,) = 4 """), "L8": """\ Assign: (z_1,) = Phi(z, z_0) Assign: (a_3,) = Phi(a_2, a_0) Assign: (y_0,) = a_3 Assign: (a_4,) = 4 """, }, )
def test_insert_phi_function_pruned_4_blocks(self): blocks, as_tree = self.build_arbitrary_blocks( block_links={"A": ["B", "C"], "B": ["D"], "C": ["D"], "D": []}, code={ "A": dedent( """\ pass """ ), "B": dedent( """\ var = 3 """ ), "C": dedent( """\ pass """ ), "D": dedent( """\ if var < 3: pass """ ), }, block_type={"A": ParentScopeBlock}, ) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.as_tree = as_tree cfg_real.root = cfg_real.block_list[0] cfg_real.root.blocks = blocks cfg_real.fill_df() cfg_real.gather_initial_info() cfg_real.compute_live_out() cfg_real.ins_phi_function_all() expected_phi_list = {"A": set(), "B": set(), "C": set(), "D": {"var"}} self.assertPhiListEqual(cfg_real.block_list, expected_phi_list)
def test_insert_phi_function_if_else(self): as_tree = AstBuilder().string_build( dedent( """\ a = 3 # 1st if a > 3: # | a = 3 # 2nd b = 3 else: # 3rd z = 4 # | # expected phi func for 'a' here y = a # 4th """ ) ) cfg_real = Cfg(as_tree) cfg_real.fill_df() cfg_real.gather_initial_info() cfg_real.ins_phi_function_all() assert cfg_real.block_list[-2]._phi_repr == {"a", "b", "z"} cfg_real.rename_to_ssa() assert "Phi(a_1, a_0)" in str(cfg_real.block_list[-2].ssa_code.code_list)
def test_insert_phi_function_pruned(self): r""" Note: '|' with no arrows means pointing down A | B <------| / \ | C F | | / \ | | G I | | \ / | | H | \ / | D-----------| | E """ blocks, as_tree = self.build_arbitrary_blocks( block_links={ "A": ["B"], "B": ["C", "F"], "C": ["D"], "D": ["E", "B"], "E": [], "F": ["G", "I"], "G": ["H"], "H": ["D"], "I": ["H"], }, code={ "A": dedent( """\ i = 1 """ ), "B": dedent( """\ a = temp_0 c = temp_1 if a < c: pass """ ), "C": dedent( """\ b = temp_2 c = temp_3 d = temp_4 """ ), "D": dedent( """\ y = a + b z = c + d i = i + 1 if i < 100: pass """ ), "E": "return\n", "F": dedent( """\ a = temp_5 d = temp_6 if a < d: pass """ ), "G": dedent( """\ d = temp """ ), "H": dedent( """\ b = temp """ ), "I": dedent( """\ c = temp """ ), }, block_type={"A": ParentScopeBlock}, ) cfg_real = Cfg() cfg_real.block_list = blocks cfg_real.as_tree = as_tree cfg_real.root = cfg_real.block_list[0] cfg_real.root.blocks = blocks cfg_real.fill_df() cfg_real.gather_initial_info() cfg_real.compute_live_out() cfg_real.ins_phi_function_all() expected_phi_list = { "A": set(), "B": {"i"}, "C": set(), "D": {"a", "b", "c", "d"}, "E": set(), "F": set(), "G": set(), "H": {"c", "d"}, "I": set(), } self.assertPhiListEqual(cfg_real.block_list, expected_phi_list)