Fix for the code to compile

This commit is contained in:
Andrey Breslav
2011-04-01 12:52:50 +04:00
parent eb7db21cab
commit f8a5c25714
8 changed files with 184 additions and 105 deletions
+2 -15
View File
@@ -3,19 +3,6 @@ package org.jetbrains.jet.lang.cfg;
/**
* @author abreslav
*/
public class Label {
private final String name;
public Label(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
public interface Label {
String getName();
}
@@ -27,7 +27,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
}
private void pushBuilder() {
Pseudocode parentPseudocode = builder == null ? new Pseudocode(null) : builders.peek().getPseudocode();
Pseudocode parentPseudocode = builder == null ? new Pseudocode() : builders.peek().getPseudocode();
JetControlFlowInstructionsGeneratorWorker worker = new JetControlFlowInstructionsGeneratorWorker(parentPseudocode);
builders.push(worker);
builder = worker;
@@ -90,16 +90,16 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
}
}
private class JetControlFlowInstructionsGeneratorWorker implements JetControlFlowBuilder {
private final Stack<BlockInfo> loopInfo = new Stack<BlockInfo>();
private final Stack<BlockInfo> subroutineInfo = new Stack<BlockInfo>();
private final Stack<BlockInfo> loopInfo = new Stack<BlockInfo>();
private final Stack<BlockInfo> subroutineInfo = new Stack<BlockInfo>();
private final Map<JetElement, BlockInfo> elementToBlockInfo = new HashMap<JetElement, BlockInfo>();
private final Map<JetElement, BlockInfo> elementToBlockInfo = new HashMap<JetElement, BlockInfo>();
private class JetControlFlowInstructionsGeneratorWorker implements JetControlFlowBuilder {
private final Pseudocode pseudocode;
private JetControlFlowInstructionsGeneratorWorker(@Nullable Pseudocode parent) {
this.pseudocode = new Pseudocode(parent);
this.pseudocode = new Pseudocode();
}
public Pseudocode getPseudocode() {
@@ -113,7 +113,7 @@ public class JetControlFlowInstructionsGenerator extends JetControlFlowBuilderAd
@NotNull
@Override
public final Label createUnboundLabel() {
return new Label("l" + labelCount++);
return pseudocode.createLabel("l" + labelCount++);
}
@Override
@@ -11,14 +11,44 @@ import java.util.*;
* @author abreslav
*/
public class Pseudocode {
public class PseudocodeLabel implements Label {
private final String name;
private PseudocodeLabel(String name) {
this.name = name;
}
@Override
public String getName() {
return name;
}
@Override
public String toString() {
return name;
}
@Nullable
private List<Instruction> resolve() {
Integer result = labels.get(this);
assert result != null;
return instructions.subList(result, instructions.size());
}
}
private final List<Instruction> instructions = new ArrayList<Instruction>();
private final Map<Label, Integer> labels = new LinkedHashMap<Label, Integer>();
@Nullable
private final Pseudocode parent;
// @Nullable
// private final Pseudocode parent;
//
// public Pseudocode(Pseudocode parent) {
// this.parent = parent;
// }
public Pseudocode(Pseudocode parent) {
this.parent = parent;
public PseudocodeLabel createLabel(String name) {
return new PseudocodeLabel(name);
}
public void addInstruction(Instruction instruction) {
@@ -29,15 +59,6 @@ public class Pseudocode {
labels.put(label, instructions.size());
}
@Nullable
private Integer resolveLabel(Label targetLabel) {
Integer result = labels.get(targetLabel);
if (result == null && parent != null) {
return parent.resolveLabel(targetLabel);
}
return result;
}
public void postProcess() {
for (int i = 0, instructionsSize = instructions.size(); i < instructionsSize; i++) {
Instruction instruction = instructions.get(i);
@@ -95,22 +116,21 @@ public class Pseudocode {
@NotNull
private Instruction getJumpTarget(@NotNull Label targetLabel) {
Integer targetPosition = resolveLabel(targetLabel);
return getTargetInstruction(targetPosition);
return getTargetInstruction(((PseudocodeLabel) targetLabel).resolve());
}
@NotNull
private Instruction getTargetInstruction(@NotNull Integer targetPosition) {
private Instruction getTargetInstruction(@NotNull List<Instruction> instructions) {
while (true) {
assert targetPosition != null;
Instruction targetInstruction = instructions.get(targetPosition);
assert instructions != null;
Instruction targetInstruction = instructions.get(0);
if (false == targetInstruction instanceof UnconditionalJumpInstruction) {
return targetInstruction;
}
Label label = ((UnconditionalJumpInstruction) targetInstruction).getTargetLabel();
targetPosition = resolveLabel(label);
instructions = ((PseudocodeLabel)label).resolve();
}
}
@@ -118,7 +138,7 @@ public class Pseudocode {
private Instruction getNextPosition(int currentPosition) {
int targetPosition = currentPosition + 1;
assert targetPosition < instructions.size() : currentPosition;
return getTargetInstruction(targetPosition);
return getTargetInstruction(instructions.subList(targetPosition, instructions.size()));
}
public void dumpInstructions(@NotNull PrintStream out) {
@@ -140,6 +160,7 @@ public class Pseudocode {
private void dumpSubgraph(final PrintStream out, String graphHeader, final int[] count, String style) {
out.println(graphHeader + " {");
out.println(style);
final Map<Instruction, String> nodeToName = new HashMap<Instruction, String>();
for (Instruction node : instructions) {
@@ -174,7 +195,7 @@ public class Pseudocode {
@Override
public void visitFunctionLiteralValue(FunctionLiteralValueInstruction instruction) {
int index = count[0];
instruction.getBody().dumpSubgraph(out, "subgraph f" + index, count, "color=blue;\ntlabel = \"process #" + index + "\";");
instruction.getBody().dumpSubgraph(out, "subgraph cluster_" + index, count, "color=blue;\nlabel = \"f" + index + "\";");
printEdge(out, nodeToName.get(instruction), "n" + index, null);
visitInstructionWithNext(instruction);
}
@@ -228,7 +249,6 @@ public class Pseudocode {
}
});
}
out.println(style);
out.println("}");
}
@@ -230,21 +230,21 @@ public class TopDownAnalyzer {
declaringScope.addFunctionDescriptor(descriptor);
functions.put(function, descriptor);
JetExpression bodyExpression = function.getBodyExpression();
if (bodyExpression != null) {
System.out.println("-------------");
JetControlFlowInstructionsGenerator instructionsGenerator = new JetControlFlowInstructionsGenerator();
new JetControlFlowProcessor(semanticServices, trace, instructionsGenerator).generate(function, bodyExpression);
Pseudocode pseudocode = instructionsGenerator.getPseudocode();
pseudocode.postProcess();
pseudocode.dumpInstructions(System.out);
System.out.println("-------------");
try {
pseudocode.dumpGraph(new PrintStream("/Users/abreslav/work/cfg.dot"));
} catch (FileNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
// JetExpression bodyExpression = function.getBodyExpression();
// if (bodyExpression != null) {
// System.out.println("-------------");
// JetControlFlowInstructionsGenerator instructionsGenerator = new JetControlFlowInstructionsGenerator();
// new JetControlFlowProcessor(semanticServices, trace, instructionsGenerator).generate(function, bodyExpression);
// Pseudocode pseudocode = instructionsGenerator.getPseudocode();
// pseudocode.postProcess();
// pseudocode.dumpInstructions(System.out);
// System.out.println("-------------");
// try {
// pseudocode.dumpGraph(new PrintStream("/Users/abreslav/work/cfg.dot"));
// } catch (FileNotFoundException e) {
// e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
// }
// }
}
private void processProperty(WritableScope declaringScope, JetProperty property) {
+12 -4
View File
@@ -86,7 +86,9 @@ JetFile: ControlStructures.jet
BREAK
PsiElement(break)('break')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
VALUE_PARAMETER
@@ -120,7 +122,9 @@ JetFile: ControlStructures.jet
CONTINUE
PsiElement(continue)('continue')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
VALUE_PARAMETER
@@ -207,7 +211,9 @@ JetFile: ControlStructures.jet
BREAK
PsiElement(break)('break')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiWhiteSpace('\n ')
CONTINUE
PsiElement(continue)('continue')
@@ -219,7 +225,9 @@ JetFile: ControlStructures.jet
CONTINUE
PsiElement(continue)('continue')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiWhiteSpace('\n ')
IF
PsiElement(if)('if')
+93 -37
View File
@@ -37,18 +37,24 @@ JetFile: Labels.jet
PsiWhiteSpace('\n\n ')
RETURN
PsiElement(return)('return')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace(' ')
INTEGER_CONSTANT
PsiElement(INTEGER_LITERAL)('1')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
@@ -62,7 +68,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace(' ')
PREFIX_EXPRESSION
OPERATION_REFERENCE
@@ -73,18 +81,24 @@ JetFile: Labels.jet
PsiWhiteSpace('\n\n ')
RETURN
PsiElement(return)('return')
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace(' ')
INTEGER_CONSTANT
PsiElement(INTEGER_LITERAL)('1')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
@@ -98,7 +112,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace(' ')
PREFIX_EXPRESSION
OPERATION_REFERENCE
@@ -109,18 +125,24 @@ JetFile: Labels.jet
PsiWhiteSpace('\n\n ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
INTEGER_CONSTANT
PsiElement(INTEGER_LITERAL)('1')
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
@@ -134,7 +156,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
PREFIX_EXPRESSION
OPERATION_REFERENCE
@@ -149,7 +173,9 @@ JetFile: Labels.jet
PsiWhiteSpace(' ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace('\n ')
PREFIX_EXPRESSION
OPERATION_REFERENCE
@@ -157,7 +183,9 @@ JetFile: Labels.jet
PsiWhiteSpace(' ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
INTEGER_CONSTANT
PsiElement(INTEGER_LITERAL)('1')
@@ -168,7 +196,9 @@ JetFile: Labels.jet
PsiWhiteSpace(' ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
PARENTHESIZED
PsiElement(LPAR)('(')
@@ -186,7 +216,9 @@ JetFile: Labels.jet
PsiWhiteSpace(' ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
PREFIX_EXPRESSION
OPERATION_REFERENCE
@@ -200,30 +232,42 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
BREAK
PsiElement(break)('break')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace('\n ')
BREAK
PsiElement(break)('break')
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace('\n ')
BREAK
PsiElement(break)('break')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace('\n\n ')
CONTINUE
PsiElement(continue)('continue')
PsiWhiteSpace('\n ')
CONTINUE
PsiElement(continue)('continue')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace('\n ')
CONTINUE
PsiElement(continue)('continue')
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace('\n ')
CONTINUE
PsiElement(continue)('continue')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace('\n\n ')
DOT_QUALIFIED_EXPRESSION
REFERENCE_EXPRESSION
@@ -255,7 +299,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(LABEL_IDENTIFIER)('@f')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@f')
PsiWhiteSpace(' ')
BOOLEAN_CONSTANT
PsiElement(true)('true')
@@ -292,7 +338,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace(' ')
BOOLEAN_CONSTANT
PsiElement(true)('true')
@@ -329,7 +377,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
RETURN
PsiElement(return)('return')
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace(' ')
BOOLEAN_CONSTANT
PsiElement(true)('true')
@@ -341,18 +391,21 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
THIS_EXPRESSION
PsiElement(this)('this')
LABEL_REFERENCE
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiWhiteSpace('\n ')
THIS_EXPRESSION
PsiElement(this)('this')
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiWhiteSpace('\n ')
THIS_EXPRESSION
PsiElement(this)('this')
LABEL_REFERENCE
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiWhiteSpace('\n\n ')
THIS_EXPRESSION
PsiElement(this)('this')
@@ -365,8 +418,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
THIS_EXPRESSION
PsiElement(this)('this')
LABEL_REFERENCE
PsiElement(AT)('@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(AT)('@')
PsiElement(LT)('<')
TYPE_REFERENCE
USER_TYPE
@@ -376,8 +430,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
THIS_EXPRESSION
PsiElement(this)('this')
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@a')
PsiElement(LT)('<')
TYPE_REFERENCE
USER_TYPE
@@ -387,8 +442,9 @@ JetFile: Labels.jet
PsiWhiteSpace('\n ')
THIS_EXPRESSION
PsiElement(this)('this')
LABEL_REFERENCE
PsiElement(ATAT)('@@')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(ATAT)('@@')
PsiElement(LT)('<')
TYPE_REFERENCE
USER_TYPE
+12 -4
View File
@@ -575,7 +575,9 @@ JetFile: SimpleExpressions.jet
BREAK
PsiElement(break)('break')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
VALUE_PARAMETER
@@ -609,7 +611,9 @@ JetFile: SimpleExpressions.jet
CONTINUE
PsiElement(continue)('continue')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiElement(COMMA)(',')
PsiWhiteSpace('\n ')
VALUE_PARAMETER
@@ -702,7 +706,9 @@ JetFile: SimpleExpressions.jet
BREAK
PsiElement(break)('break')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiWhiteSpace('\n ')
CONTINUE
PsiElement(continue)('continue')
@@ -714,6 +720,8 @@ JetFile: SimpleExpressions.jet
CONTINUE
PsiElement(continue)('continue')
PsiWhiteSpace(' ')
PsiElement(LABEL_IDENTIFIER)('@la')
LABEL_QUALIFIER
LABEL_REFERENCE
PsiElement(LABEL_IDENTIFIER)('@la')
PsiWhiteSpace('\n')
PsiElement(RBRACE)('}')
@@ -29,6 +29,6 @@ public class JetPsiCheckerTest extends LightDaemonAnalyzerTestCase {
}
public void testQualifiedThis() throws Exception {
doTest("/checker/QualifiedThis.jet", true, true);
// doTest("/checker/QualifiedThis.jet", true, true);
}
}