|
1 | 1 | import dis |
2 | | -from copy import deepcopy |
3 | 2 | from dataclasses import dataclass |
4 | 3 | from typing import Generator, Callable |
5 | 4 |
|
@@ -61,90 +60,51 @@ def from_bytecode(code: Callable) -> "ByteFlow": # type: ignore |
61 | 60 | scfg = flowinfo.build_basicblocks() |
62 | 61 | return ByteFlow(bc=bc, scfg=scfg) |
63 | 62 |
|
64 | | - def _join_returns(self) -> "ByteFlow": |
| 63 | + def _join_returns(self) -> None: |
65 | 64 | """Joins the return blocks within the corresponding SCFG. |
66 | 65 |
|
67 | | - This method creates a deep copy of the SCFG and performs |
68 | | - operation to join return blocks within the control flow. |
69 | | - It returns a new ByteFlow object with the updated SCFG. |
70 | | -
|
71 | | - Returns |
72 | | - ------- |
73 | | - byteflow: ByteFlow |
74 | | - The new ByteFlow object with updated SCFG. |
| 66 | + This method performs operation to join return blocks within |
| 67 | + the control flow. |
75 | 68 | """ |
76 | | - scfg = deepcopy(self.scfg) |
77 | | - scfg.join_returns() |
78 | | - return ByteFlow(bc=self.bc, scfg=scfg) |
| 69 | + self.scfg.join_returns() |
79 | 70 |
|
80 | | - def _restructure_loop(self) -> "ByteFlow": |
| 71 | + def _restructure_loop(self) -> None: |
81 | 72 | """Restructures the loops within the corresponding SCFG. |
82 | 73 |
|
83 | | - Creates a deep copy of the SCFG and performs the operation to |
84 | | - restructure loop constructs within the control flow using |
85 | | - the algorithm LOOP RESTRUCTURING from section 4.1 of Bahmann2015. |
| 74 | + Performs the operation to restructure loop constructs within |
| 75 | + the control flow using the algorithm LOOP RESTRUCTURING from |
| 76 | + section 4.1 of Bahmann2015. |
86 | 77 | It applies the restructuring operation to both the main SCFG |
87 | | - and any subregions within it. It returns a new ByteFlow object |
88 | | - with the updated SCFG. |
89 | | -
|
90 | | - Returns |
91 | | - ------- |
92 | | - byteflow: ByteFlow |
93 | | - The new ByteFlow object with updated SCFG. |
| 78 | + and any subregions within it. |
94 | 79 | """ |
95 | | - scfg = deepcopy(self.scfg) |
96 | | - restructure_loop(scfg.region) |
97 | | - for region in _iter_subregions(scfg): |
| 80 | + restructure_loop(self.scfg.region) |
| 81 | + for region in _iter_subregions(self.scfg): |
98 | 82 | restructure_loop(region) |
99 | | - return ByteFlow(bc=self.bc, scfg=scfg) |
100 | 83 |
|
101 | | - def _restructure_branch(self) -> "ByteFlow": |
| 84 | + def _restructure_branch(self) -> None: |
102 | 85 | """Restructures the branches within the corresponding SCFG. |
103 | 86 |
|
104 | | - Creates a deep copy of the SCFG and performs the operation to |
105 | | - restructure branch constructs within the control flow. It applies |
106 | | - the restructuring operation to both the main SCFG and any |
107 | | - subregions within it. It returns a new ByteFlow object with |
108 | | - the updated SCFG. |
109 | | -
|
110 | | - Returns |
111 | | - ------- |
112 | | - byteflow: ByteFlow |
113 | | - The new ByteFlow object with updated SCFG. |
| 87 | + This method applies restructuring branch operation to both |
| 88 | + the main SCFG and any subregions within it. |
114 | 89 | """ |
115 | | - scfg = deepcopy(self.scfg) |
116 | | - restructure_branch(scfg.region) |
117 | | - for region in _iter_subregions(scfg): |
| 90 | + restructure_branch(self.scfg.region) |
| 91 | + for region in _iter_subregions(self.scfg): |
118 | 92 | restructure_branch(region) |
119 | | - return ByteFlow(bc=self.bc, scfg=scfg) |
120 | 93 |
|
121 | | - def restructure(self) -> "ByteFlow": |
| 94 | + def restructure(self) -> None: |
122 | 95 | """Applies join_returns, restructure_loop and restructure_branch |
123 | 96 | in the respective order on the SCFG. |
124 | 97 |
|
125 | | - Creates a deep copy of the SCFG and applies a series of |
126 | | - restructuring operations to it. The operations include |
127 | | - joining return blocks, restructuring loop constructs, and |
128 | | - restructuring branch constructs. It returns a new ByteFlow |
129 | | - object with the updated SCFG. |
130 | | -
|
131 | | - Returns |
132 | | - ------- |
133 | | - byteflow: ByteFlow |
134 | | - The new ByteFlow object with updated SCFG. |
| 98 | + Applies a series of restructuring operations to given SCFG. |
| 99 | + The operations include joining return blocks, restructuring |
| 100 | + loop constructs, and restructuring branch constructs. |
135 | 101 | """ |
136 | | - scfg = deepcopy(self.scfg) |
137 | 102 | # close |
138 | | - scfg.join_returns() |
| 103 | + self._join_returns() |
139 | 104 | # handle loop |
140 | | - restructure_loop(scfg.region) |
141 | | - for region in _iter_subregions(scfg): |
142 | | - restructure_loop(region) |
| 105 | + self._restructure_loop() |
143 | 106 | # handle branch |
144 | | - restructure_branch(scfg.region) |
145 | | - for region in _iter_subregions(scfg): |
146 | | - restructure_branch(region) |
147 | | - return ByteFlow(bc=self.bc, scfg=scfg) |
| 107 | + self._restructure_branch() |
148 | 108 |
|
149 | 109 |
|
150 | 110 | def _iter_subregions(scfg: SCFG) -> Generator[RegionBlock, SCFG, None]: |
|
0 commit comments