-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Troubles fixed in PseudocodeVariablesData.kt.
Own tests on Constant Propagation created. This closes #1
- Loading branch information
1 parent
3119877
commit 80d71f7
Showing
11 changed files
with
241 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
compiler/testData/cfg-constant-propagation/basic/Branching.instructions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
== foo == | ||
|
||
===================== |
14 changes: 14 additions & 0 deletions
14
compiler/testData/cfg-constant-propagation/basic/Branching.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
fun foo(x: Boolean) { | ||
var b: Int | ||
if (x) { | ||
b = 3 | ||
} else { | ||
b = 2 | ||
} | ||
var c: Int | ||
if (x) { | ||
c = 4 | ||
} else { | ||
c = 4 | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
compiler/testData/cfg-constant-propagation/basic/SimpleInitialization.instructions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
== bar == | ||
|
||
===================== |
7 changes: 7 additions & 0 deletions
7
compiler/testData/cfg-constant-propagation/basic/SimpleInitialization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
fun bar() { | ||
val b: Int | ||
b = 3 | ||
|
||
val a = 4 | ||
var c | ||
} |
26 changes: 26 additions & 0 deletions
26
compiler/testData/cfg-constant-propagation/basic/SimpleInitialization.rInstruction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
== bar == | ||
fun bar() { | ||
val b: Int | ||
b = 3 | ||
|
||
val a = 4 | ||
var c | ||
} | ||
--------------------- | ||
L0: | ||
1 <START> | ||
2 mark({ val b: Int b = 3 val a = 4 var c }) VAL: in: {} out: {} | ||
v(val b: Int) VAL: in: {} out: {b=C?} | ||
r(3) -> <v0> VAL: in: {b=C?} out: {b=C?} | ||
w(b|<v0>) VAL: in: {b=C?} out: {b=C: <3, INT>} | ||
v(val a = 4) | ||
r(4) -> <v1> VAL: in: {b=C: <3, INT>} out: {b=C: <3, INT>} | ||
w(a|<v1>) VAL: in: {b=C: <3, INT>} out: {b=C: <3, INT>} | ||
v(var c) VAL: in: {b=C: <3, INT>} out: {b=C: <3, INT>, c=C?} | ||
L1: | ||
1 <END> | ||
error: | ||
<ERROR> | ||
sink: | ||
<SINK> VAL: in: {} out: {} | ||
===================== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
compiler/tests/org/jetbrains/kotlin/cfg/AbstractConstantPropogationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright 2010-2018 JetBrains s.r.o. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.jetbrains.kotlin.cfg; | ||
|
||
import com.google.common.collect.Lists; | ||
import com.intellij.openapi.util.text.StringUtil; | ||
import javaslang.Tuple2; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.kotlin.cfg.pseudocode.PseudocodeImpl; | ||
import org.jetbrains.kotlin.cfg.pseudocode.instructions.Instruction; | ||
import org.jetbrains.kotlin.cfg.pseudocodeTraverser.Edges; | ||
import org.jetbrains.kotlin.descriptors.VariableDescriptor; | ||
import org.jetbrains.kotlin.resolve.BindingContext; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
||
|
||
public class AbstractConstantPropogationTest extends AbstractDataFlowTest { | ||
|
||
@Override | ||
public void dumpInstructions( | ||
@NotNull PseudocodeImpl pseudocode, | ||
@NotNull StringBuilder out, | ||
@NotNull BindingContext bindingContext | ||
) { | ||
PseudocodeVariablesData pseudocodeVariablesData = new PseudocodeVariablesData(pseudocode.getRootPseudocode(), bindingContext); | ||
Map<Instruction, Edges<ReadOnlyConstValueControlFlowInfo>> variableValues = | ||
pseudocodeVariablesData.getVariableValues(); | ||
String valuePrefix = " VAL:"; | ||
int initializersColumnWidth = countDataColumnWidth(valuePrefix, pseudocode.getInstructionsIncludingDeadCode(), variableValues, | ||
pseudocodeVariablesData); | ||
|
||
dumpInstructions(pseudocode, out, (instruction, next, prev) -> { | ||
StringBuilder result = new StringBuilder(); | ||
Edges<ReadOnlyConstValueControlFlowInfo> valueEdges = variableValues.get(instruction); | ||
Edges<ReadOnlyConstValueControlFlowInfo> nextValueEdges = variableValues.get(next); | ||
String valuesData = ""; | ||
if (valueEdges != null && !valueEdges.equals(nextValueEdges)) { | ||
valuesData = dumpEdgesData(valuePrefix, valueEdges, pseudocodeVariablesData); | ||
} | ||
result.append(String.format("%1$-" + initializersColumnWidth + "s", valuesData)); | ||
return result.toString(); | ||
}); | ||
} | ||
|
||
private static int countDataColumnWidth( | ||
@NotNull String prefix, | ||
@NotNull List<Instruction> instructions, | ||
@NotNull Map<Instruction, Edges<ReadOnlyConstValueControlFlowInfo>> data, | ||
@NotNull PseudocodeVariablesData variablesData | ||
) { | ||
int maxWidth = 0; | ||
for (Instruction instruction : instructions) { | ||
Edges<ReadOnlyConstValueControlFlowInfo> edges = data.get(instruction); | ||
if (edges == null) continue; | ||
int length = dumpEdgesData(prefix, edges, variablesData).length(); | ||
if (maxWidth < length) { | ||
maxWidth = length; | ||
} | ||
} | ||
|
||
return maxWidth; | ||
} | ||
|
||
@NotNull | ||
private static <S, I extends ReadOnlyControlFlowInfo<S>> String dumpEdgesData( | ||
String prefix, | ||
@NotNull Edges<I> edges, | ||
@NotNull PseudocodeVariablesData variablesData | ||
) { | ||
return prefix + | ||
" in: " + renderVariableMap(edges.getIncoming().asMap(), variablesData) + | ||
" out: " + renderVariableMap(edges.getOutgoing().asMap(), variablesData); | ||
} | ||
|
||
private static <S> String renderVariableMap( | ||
javaslang.collection.Map<VariableDescriptor, S> map, | ||
PseudocodeVariablesData variablesData | ||
) { | ||
List<String> result = Lists.newArrayList(); | ||
for (Tuple2<VariableDescriptor, S> entry : map) { | ||
VariableDescriptor variable = entry._1; | ||
S state = entry._2; | ||
|
||
if (variablesData.isVariableWithTrivialInitializer(variable)) continue; | ||
|
||
result.add(variable.getName() + "=" + state); | ||
} | ||
Collections.sort(result); | ||
return "{" + StringUtil.join(result, ", ") + "}"; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
compiler/tests/org/jetbrains/kotlin/cfg/ConstantPropogationTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters