Skip to content

Commit

Permalink
Code normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
victorjmarin committed Nov 28, 2017
1 parent a632e6b commit 78ebbcd
Show file tree
Hide file tree
Showing 25 changed files with 1,031 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
era_bcb_sample/
.DS_Store
.classpath
.gradle
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Expand Up @@ -14,4 +14,5 @@ dependencies {
compile group: 'org.jgrapht', name: 'jgrapht-ext', version: '1.0.1'
compile group: 'org.antlr', name: 'antlr4-runtime', version: '4.7'
compile group: 'com.github.javaparser', name: 'javaparser-core', version: '3.5.2'
compile group: 'com.github.javaparser', name: 'java-symbol-solver-core', version: '0.6.1'
}
2 changes: 2 additions & 0 deletions programs/java8/Triangle4.java
Expand Up @@ -4,6 +4,8 @@

public class Triangle4 {

int attr;

void triangle4() {
int he = 0;
Scanner in = new Scanner(System.in);
Expand Down
9 changes: 2 additions & 7 deletions programs/java8/normalization/Assignment.java
@@ -1,13 +1,8 @@
package edu.rit.goal.sdg.java8.antlr4;

import java.util.ArrayList;

public class Assignment {

// Source program
public void f(int a, int b, int c) {
if (true) {
int d = b;
int d = c - (a + b) * 99 - 4;
if (x - y + z > 0 && x > 100 || y <= 1) {
}
}

Expand Down
11 changes: 11 additions & 0 deletions programs/java8/normalization/Foreach.java
@@ -0,0 +1,11 @@

public class Foreach {

public void f() {
final List<Integer> l = new ArrayList<>();
for (Integer i : l) {
System.out.println(i);
}
}

}
26 changes: 26 additions & 0 deletions programs/java8/normalization/MethodCall.java
@@ -0,0 +1,26 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Norm2 {

public void f(int a, int b, int c) {
// int i = it.next(a + b + c, it.next(a) + 1, m(c - b)) + 2;

// E e = null;
// while ((e = it.next()) != null) {
// a = 8;
// c = -1 - 3 - 4;
// b = 4 + 2;
// }

// while (4 + 3 > 3 - 2 + 1) {
// }

int a = 0;
do {
a++;
} while (4 > (a + 1) / 2);
}

}
16 changes: 16 additions & 0 deletions programs/java8/normalization/Norm2.java
@@ -0,0 +1,16 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Norm2 {

public void f() {
final List<Integer> l = new ArrayList<>();
final Iterator<Integer> it = l.iterator();
Integer i = 0;
while (it.hasNext() && (i = it.next(a + b + c)) != null) {
System.out.println(i);
}
}

}
24 changes: 22 additions & 2 deletions src/main/java/edu/rit/goal/sdg/graph/Vertex.java
Expand Up @@ -15,25 +15,33 @@ public class Vertex implements Serializable {
private Set<String> readingVariables;
private Integer startLine;
private Integer endLine;
private final Set<Vertex> in;
private Set<Vertex> out;

public Vertex() {}
public Vertex() {
in = new HashSet<>();
out = new HashSet<>();
}

public Vertex(final int id) {
this.id = id;
readingVariables = new HashSet<>();
in = new HashSet<>();
out = new HashSet<>();
}

public Vertex(final int id, final VertexType type, final String label) {
this(id, type, label, null);
}

public Vertex(final int id, final VertexType type, final String label, final String lookupId) {
super();
this.id = id;
this.type = type;
this.label = label;
this.lookupId = lookupId;
readingVariables = new HashSet<>();
in = new HashSet<>();
out = new HashSet<>();
}

public int getId() {
Expand Down Expand Up @@ -102,6 +110,18 @@ public void setEndLine(final Integer endLine) {
this.endLine = endLine;
}

public Set<Vertex> getIn() {
return in;
}

public Set<Vertex> getOut() {
return out;
}

public void setOut(final Set<Vertex> out) {
this.out = out;
}

@Override
public String toString() {
String result = id + "-" + type;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/rit/goal/sdg/graph/VertexType.java
Expand Up @@ -2,7 +2,7 @@

public enum VertexType {

CUNIT, PKG, IMPORT, CLASS, ENTRY, FORMAL_IN, FORMAL_OUT, ACTUAL_IN, ACTUAL_OUT, CALL, ASSIGN, RETURN, INITIAL_STATE, BREAK, CONTINUE, CTRL;
CUNIT, PKG, IMPORT, CLASS, ENTRY, FORMAL_IN, FORMAL_OUT, ACTUAL_IN, ACTUAL_OUT, CALL, DECL, ASSIGN, RETURN, INITIAL_STATE, BREAK, CONTINUE, CTRL;

public boolean isFormalParam() {
return FORMAL_IN.equals(this) || FORMAL_OUT.equals(this);
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/edu/rit/goal/sdg/interpreter/Interpreter.java
Expand Up @@ -36,6 +36,7 @@
import edu.rit.goal.sdg.interpreter.stmt.Cls;
import edu.rit.goal.sdg.interpreter.stmt.Continue;
import edu.rit.goal.sdg.interpreter.stmt.CtrlEdge;
import edu.rit.goal.sdg.interpreter.stmt.Decl;
import edu.rit.goal.sdg.interpreter.stmt.Def;
import edu.rit.goal.sdg.interpreter.stmt.DoWhile;
import edu.rit.goal.sdg.interpreter.stmt.EdgeStmt;
Expand Down Expand Up @@ -80,7 +81,8 @@ public Interpreter() {}
public Program interpret(final Program program) {
vtxId = 0;
final Program result = _interpret(program);
result.sdg.computeDataFlow(vtxId);
result.sdg.computeDataDeps(vtxId);
// result.sdg.computeDataFlow(vtxId);
return result;
}

Expand Down Expand Up @@ -211,6 +213,8 @@ else if (ioS2 == null)
s -> def.matchFor(s.b)),
caseof(Seq.class,
s -> seq.matchFor(s.s1)),
caseof(Decl.class,
__ -> this::declRule),
caseof(Assign.class,
s -> assign.matchFor(s.e)),
caseof(CtrlEdge.class,
Expand Down Expand Up @@ -302,7 +306,11 @@ private Program edgeSkipRule(final Program program) {
for (final Vertex ve : program.Ve) {
program.sdg.addEdge(s.v, ve, s.t);
}
for (final Vertex vc : program.Vc) {
program.sdg.addEdge(s.v, vc, s.t);
}
program.Ve = new HashSet<>();
program.Vc = new HashSet<>();
program.s = new Skip();
return program;
}
Expand Down Expand Up @@ -406,6 +414,16 @@ private Program seqSkipRule(final Program program) {
return program;
}

private Program declRule(final Program program) {
printRule("declRule");
final Decl s = (Decl) program.s;
final Vertex v = new Vertex(vtxId++, VertexType.DECL, s.toString());
program.sdg.addVertex(v);
program.Vc.add(v);
program.s = new Skip();
return program;
}

private Program assignRule(final Program program) {
printRule("assignRule");
final Assign assign = (Assign) program.s;
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/edu/rit/goal/sdg/interpreter/Main.java
Expand Up @@ -12,19 +12,16 @@

public class Main {

private static final File FILE = new File("programs/java8/Triangle4.java");
private static final File FILE = new File("programs/java8/normalization/Foreach.java");

public static void main(final String[] args) throws IOException {
final long t = System.currentTimeMillis();
final Translator translator = new Translator();
final Stmt stmt = translator.from(FILE);
final Interpreter intrprtr = new Interpreter();
final Program pstmt = new Program(stmt);
final Program p = intrprtr.interpret(pstmt);
final SysDepGraph sdg = p.sdg;
final Map<String, DirectedGraph<Vertex, Edge>> methodSubgraphs = sdg.getMethodSubgraphs();
System.out.println(System.currentTimeMillis() - t + " ms. to build the PDG");
System.out.println(sdg);
final DirectedGraph<Vertex, Edge> und = p.F.get("RunningExample.f");
Utils.exportAsDot(sdg, "/Users/goal/Desktop", "und");
// for (final Entry<String, DirectedGraph<Vertex, Edge>> e :
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/edu/rit/goal/sdg/interpreter/Translator.java
Expand Up @@ -32,6 +32,7 @@
import edu.rit.goal.sdg.java8.antlr4.JavaParser.FormalParameterContext;
import edu.rit.goal.sdg.java8.antlr4.ParseResult;
import edu.rit.goal.sdg.java8.antlr4.SourceDGJavaVisitor;
import edu.rit.goal.sdg.java8.normalization.Normalizer;

public class Translator {

Expand All @@ -43,8 +44,11 @@ private enum Language {

public Stmt from(final File file) throws IOException {
cUnitName = file.getName();
final Normalizer exprNorm = new Normalizer(file);
final String normalizedProgram = exprNorm.normalize();
final String path = file.getPath();
return parse(CharStreams.fromFileName(path), detectLang(path));
final CharStream charStream = CharStreams.fromString(normalizedProgram);
return parse(charStream, detectLang(path));
}

private Stmt parse(final CharStream chrStream, final Language lang) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/edu/rit/goal/sdg/interpreter/stmt/Decl.java
@@ -0,0 +1,18 @@
package edu.rit.goal.sdg.interpreter.stmt;

public class Decl extends BaseStmt {

public String t;
public String x;

public Decl(final String t, final String x) {
this.t = t;
this.x = x;
}

@Override
public String toString() {
return t + " " + x;
}

}
14 changes: 14 additions & 0 deletions src/main/java/edu/rit/goal/sdg/interpreter/stmt/ForControl.java
@@ -0,0 +1,14 @@
package edu.rit.goal.sdg.interpreter.stmt;

public class ForControl extends BaseStmt {

public Stmt si, sc, su;

public ForControl(final Stmt si, final Stmt sc, final Stmt su) {
super();
this.si = si;
this.sc = sc;
this.su = su;
}

}
44 changes: 35 additions & 9 deletions src/main/java/edu/rit/goal/sdg/java8/antlr4/JavaParser.java
Expand Up @@ -7145,10 +7145,37 @@ public final SwitchLabelContext switchLabel() throws RecognitionException {
}

public static class ForControlContext extends ParserRuleContext {
public ExpressionListContext forUpdate;
public ForControlContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_forControl; }

public ForControlContext() { }
public void copyFrom(ForControlContext ctx) {
super.copyFrom(ctx);
}
}
public static class ForControlEnhancedContext extends ForControlContext {
public EnhancedForControlContext enhancedForControl() {
return getRuleContext(EnhancedForControlContext.class,0);
}
public ForControlEnhancedContext(ForControlContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof JavaParserListener ) ((JavaParserListener)listener).enterForControlEnhanced(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof JavaParserListener ) ((JavaParserListener)listener).exitForControlEnhanced(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof JavaParserVisitor ) return ((JavaParserVisitor<? extends T>)visitor).visitForControlEnhanced(this);
else return visitor.visitChildren(this);
}
}
public static class ForControlRegularContext extends ForControlContext {
public ExpressionListContext forUpdate;
public ForInitContext forInit() {
return getRuleContext(ForInitContext.class,0);
}
Expand All @@ -7158,21 +7185,18 @@ public ExpressionContext expression() {
public ExpressionListContext expressionList() {
return getRuleContext(ExpressionListContext.class,0);
}
public ForControlContext(ParserRuleContext parent, int invokingState) {
super(parent, invokingState);
}
@Override public int getRuleIndex() { return RULE_forControl; }
public ForControlRegularContext(ForControlContext ctx) { copyFrom(ctx); }
@Override
public void enterRule(ParseTreeListener listener) {
if ( listener instanceof JavaParserListener ) ((JavaParserListener)listener).enterForControl(this);
if ( listener instanceof JavaParserListener ) ((JavaParserListener)listener).enterForControlRegular(this);
}
@Override
public void exitRule(ParseTreeListener listener) {
if ( listener instanceof JavaParserListener ) ((JavaParserListener)listener).exitForControl(this);
if ( listener instanceof JavaParserListener ) ((JavaParserListener)listener).exitForControlRegular(this);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
if ( visitor instanceof JavaParserVisitor ) return ((JavaParserVisitor<? extends T>)visitor).visitForControl(this);
if ( visitor instanceof JavaParserVisitor ) return ((JavaParserVisitor<? extends T>)visitor).visitForControlRegular(this);
else return visitor.visitChildren(this);
}
}
Expand All @@ -7186,13 +7210,15 @@ public final ForControlContext forControl() throws RecognitionException {
_errHandler.sync(this);
switch ( getInterpreter().adaptivePredict(_input,123,_ctx) ) {
case 1:
_localctx = new ForControlEnhancedContext(_localctx);
enterOuterAlt(_localctx, 1);
{
setState(1017);
enhancedForControl();
}
break;
case 2:
_localctx = new ForControlRegularContext(_localctx);
enterOuterAlt(_localctx, 2);
{
setState(1019);
Expand Down Expand Up @@ -7225,7 +7251,7 @@ public final ForControlContext forControl() throws RecognitionException {
if ((((_la) & ~0x3f) == 0 && ((1L << _la) & ((1L << BOOLEAN) | (1L << BYTE) | (1L << CHAR) | (1L << DOUBLE) | (1L << FLOAT) | (1L << INT) | (1L << LONG) | (1L << NEW) | (1L << SHORT) | (1L << SUPER) | (1L << THIS) | (1L << VOID) | (1L << DECIMAL_LITERAL) | (1L << HEX_LITERAL) | (1L << OCT_LITERAL) | (1L << BINARY_LITERAL) | (1L << FLOAT_LITERAL) | (1L << HEX_FLOAT_LITERAL) | (1L << BOOL_LITERAL) | (1L << CHAR_LITERAL) | (1L << STRING_LITERAL) | (1L << NULL_LITERAL) | (1L << LPAREN))) != 0) || ((((_la - 72)) & ~0x3f) == 0 && ((1L << (_la - 72)) & ((1L << (LT - 72)) | (1L << (BANG - 72)) | (1L << (TILDE - 72)) | (1L << (INC - 72)) | (1L << (DEC - 72)) | (1L << (ADD - 72)) | (1L << (SUB - 72)) | (1L << (AT - 72)) | (1L << (IDENTIFIER - 72)))) != 0)) {
{
setState(1026);
((ForControlContext)_localctx).forUpdate = expressionList();
((ForControlRegularContext)_localctx).forUpdate = expressionList();
}
}

Expand Down

0 comments on commit 78ebbcd

Please sign in to comment.