Skip to content

Commit

Permalink
Troubles fixed in PseudocodeVariablesData.kt.
Browse files Browse the repository at this point in the history
Own tests on Constant Propagation created. This closes #1
  • Loading branch information
[email protected] authored and mglukhikh committed Jun 26, 2018
1 parent 3119877 commit 80d71f7
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,17 @@ class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingCon
when (instruction) {
is WriteValueInstruction -> {
val variableDescriptor = extractValWithTrivialInitializer(instruction)
val previousInstruction = instruction.previousInstructions.last()
if (variableDescriptor != null
&& instruction.isTrivialInitializer()
&& previousInstruction is ReadValueInstruction
&& (KotlinBuiltIns.isPrimitiveType(variableDescriptor.type)
|| KotlinBuiltIns.isString(variableDescriptor.type))) {
val previousInstruction = instruction.previousInstructions.last() as ReadValueInstruction
println("HERE")
descriptorToConstStateMap =
descriptorToConstStateMap.put(variableDescriptor,
createConstValueState(previousInstruction)?.valueState as VariableWithConstValue)
println(descriptorToConstStateMap.size())
}
}
}
Expand Down Expand Up @@ -719,6 +722,7 @@ class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingCon
PropagatedTypes.LONG -> VariableWithConstValue(
(this.constValue.toLong() * other.constValue.toLong()).toString(),
PropagatedTypes.LONG)

else -> VariableWithUnknownValue
}
}
Expand Down Expand Up @@ -784,9 +788,11 @@ class PseudocodeVariablesData(val pseudocode: Pseudocode, private val bindingCon
if (instruction is ReadValueInstruction) {
val varDescriptor = PseudocodeUtil.extractVariableDescriptorIfAny(instruction, bindingContext)
if (varDescriptor != null) {
val valueState = enterInstructionData[varDescriptor].get().valueState
if (valueState is VariableWithConstValue) {
pseudoValueToValue.put(instruction.outputValue, valueState)
if (enterInstructionData.containsKey(varDescriptor)) {
val valueState = enterInstructionData[varDescriptor]?.get()?.valueState
if (valueState is VariableWithConstValue) {
pseudoValueToValue.put(instruction.outputValue, valueState)
}
}
} else {
createConstValueState(instruction)?.let {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== foo ==

=====================
14 changes: 14 additions & 0 deletions compiler/testData/cfg-constant-propagation/basic/Branching.kt
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
== bar ==

=====================
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
}
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: {}
=====================
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public void dumpInstructions(
pseudocodeVariablesData.getVariableInitializers();
Map<Instruction, Edges<ReadOnlyControlFlowInfo<VariableUseState>>> useStatusData =
pseudocodeVariablesData.getVariableUseStatusData();
Map<Instruction, Edges<ReadOnlyConstValueControlFlowInfo>> variableValues =
pseudocodeVariablesData.getVariableValues();
String initPrefix = " INIT:";
String usePrefix = " USE:";
String valuePrefix = " VAL:";
int initializersColumnWidth = countDataColumnWidth(initPrefix, pseudocode.getInstructionsIncludingDeadCode(), variableInitializers,
pseudocodeVariablesData);

Expand All @@ -66,13 +63,6 @@ public void dumpInstructions(
if (useStatusEdges != null && !useStatusEdges.equals(nextUseStatusEdges)) {
result.append(dumpEdgesData(usePrefix, useStatusEdges, pseudocodeVariablesData));
}

Edges<ReadOnlyConstValueControlFlowInfo> valueEdges = variableValues.get(instruction);
Edges<ReadOnlyConstValueControlFlowInfo> nextValueEdges = variableValues.get(next);
if (valueEdges != null && !valueEdges.equals(nextValueEdges)) {
result.append(dumpEdgesData(valuePrefix, valueEdges, pseudocodeVariablesData));
}

return result.toString();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,14 @@ else if (correspondingElement instanceof KtNamedDeclaration) {
}

File expectedInstructionsFile = KotlinTestUtils.replaceExtension(file, getDataFileExtension());
try (PrintWriter pw = new PrintWriter("C:\\Users\\809279\\Desktop\\workdir\\res.kt")) {
File realOutputFile = KotlinTestUtils.replaceExtension(file, "rInstruction");
PrintWriter pw = new PrintWriter(realOutputFile);
try {
pw.write(instructionDump.toString());
}
catch (Exception ignored) {
} finally {
pw.close();
}
KotlinTestUtils.assertEqualsToFile(expectedInstructionsFile, instructionDump.toString());
}
Expand Down
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, ", ") + "}";
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package org.jetbrains.kotlin.generators.tests

import org.jetbrains.kotlin.asJava.AbstractCompilerLightClassTest
import org.jetbrains.kotlin.cfg.AbstractControlFlowTest
import org.jetbrains.kotlin.cfg.AbstractDataFlowTest
import org.jetbrains.kotlin.cfg.AbstractDiagnosticsWithModifiedMockJdkTest
import org.jetbrains.kotlin.cfg.AbstractPseudoValueTest
import org.jetbrains.kotlin.cfg.*
import org.jetbrains.kotlin.checkers.*
import org.jetbrains.kotlin.checkers.javac.*
import org.jetbrains.kotlin.cli.AbstractCliTest
Expand Down Expand Up @@ -314,6 +311,10 @@ fun main(args: Array<String>) {
model("cfgVariablesWithStdLib", testMethod = "doTestWithStdLib")
}

testClass<AbstractConstantPropogationTest> {
model("cfg-constant-propagation")
}

testClass<AbstractPseudoValueTest> {
model("cfg")
model("cfgWithStdLib", testMethod = "doTestWithStdLib")
Expand Down

0 comments on commit 80d71f7

Please sign in to comment.