Skip to content

Commit

Permalink
Merge branch 'reaching-definition' of github.com:boozec/clp_project i…
Browse files Browse the repository at this point in the history
…nto reaching-definition
  • Loading branch information
boozec committed Jul 14, 2024
2 parents 3a56c4b + ba41bb5 commit 7944fd7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 15 deletions.
70 changes: 61 additions & 9 deletions src/ast/Python3VisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.AbstractMap.SimpleEntry;

import ast.nodes.*;
import ast.types.*;
import codegen.Label;
import java.lang.reflect.Array;
import parser.Python3Lexer;
import parser.Python3ParserBaseVisitor;
import parser.Python3Parser.*;
Expand Down Expand Up @@ -347,12 +345,18 @@ public Node visitWhile_stmt(While_stmtContext ctx) {
int lineStart = ctx.getStart().getLine();
int lineStop = ctx.getStop().getLine();
int index = ctx.getStart().getTokenIndex();
optimizeBlock(block, lineStart, lineStop, index);
optimizeWithSecond(block, lineStart, lineStop, index);

// optimize while's guard
int counter = 0;
var exprs = expr.getExprs();
for (var e : exprs) {
if (e instanceof ExprNode) {
ExprNode exprNode = (ExprNode) e;
if (exprNode.typeCheck() instanceof AtomType) {
continue;
}
}
ArrayList<String> al = findAtomPresent(e, new ArrayList<>());
if (!al.isEmpty()) {
boolean constant = true;
Expand All @@ -374,6 +378,8 @@ public Node visitWhile_stmt(While_stmtContext ctx) {
counter++;
}

optimizeWithThird(block, lineStart, lineStop, index);

return whileStmt;
}

Expand All @@ -395,7 +401,7 @@ private ArrayList<String> findAtomPresent(Node e, ArrayList<String> Acc) {
return Acc;
}

private void optimizeBlock(BlockNode block, int lineStart, int lineStop, int index) {
private void optimizeWithSecond(BlockNode block, int lineStart, int lineStop, int index) {
rewriter.insertAfter(index, " ");
ArrayList<AssignmentNode> assignments = new ArrayList<>();
for (var child : block.getChilds()) {
Expand Down Expand Up @@ -439,10 +445,6 @@ private void optimizeBlock(BlockNode block, int lineStart, int lineStop, int ind
rewriter.insertBefore(index, lhr.toPrint("") + "=" + rhr.toPrint("") + "\n");
rewriter.replace(assignment.getLhrIndex(), assignment.getRhrIndex(), "");
optimizationDone = true;
// int lastToken = ctx.expr().expr(counter).getStop().getTokenIndex();
// int firstToken = ctx.expr().expr(counter).getStart().getTokenIndex();
// rewriter.replace(firstToken, lastToken, newVar);
// System.out.println("1 " + assignment.toPrint(""));
} else {
rewriter.insertBefore(assignment.getLhrIndex(), "\t");
}
Expand Down Expand Up @@ -493,11 +495,61 @@ public Node visitFor_stmt(For_stmtContext ctx) {
// `foriinlists` becomes `for i in lists`
rewriter.insertAfter(index + 1, " ");
rewriter.insertAfter(index + 2, " ");
optimizeBlock(block, lineStart, lineStop, index);
optimizeWithSecond(block, lineStart, lineStop, index);
optimizeWithThird(block, lineStart, lineStop, index);

return forNode;
}

private void optimizeWithThird(BlockNode block, int lineStart, int lineStop, int index) {
int counter = 0;
ArrayList<Node> stms = block.getChilds();
for (var e : stms) {
if (e instanceof SimpleStmtsNode) {
SimpleStmtsNode stmss = (SimpleStmtsNode) e;
for (Node stm : stmss.getStmts()) {
SimpleStmtNode singleStm = (SimpleStmtNode) stm;
AssignmentNode ass = (AssignmentNode) singleStm.getAssignment();
if (ass != null) {
ExprListNode rhr = ass.getRhr();
ExprNode rExpr = (ExprNode) rhr.getElem(0);
ArrayList<Node> exprsList = rExpr.getExprs();
if (exprsList.size() > 1) {
List<Node> exprsLists = exprsList.subList(0, exprsList.size() - 1);
for (var elem : exprsLists) {
if (elem instanceof ExprNode) {
ExprNode exprNode = (ExprNode) elem;
if (exprNode.typeCheck() instanceof AtomType) {
continue;
}
}
ArrayList<String> al = findAtomPresent(elem, new ArrayList<>());
if (!al.isEmpty()) {
boolean constant = true;
for (String a : al) {
int n = R.get(a);
if (n > lineStart && n <= lineStop) {
constant = false;
break;
}
}
if (constant) {
String newVar = Label.newVar();
rewriter.insertBefore(index, newVar + "=" + elem.toPrint("") + "\n");
int firstToken = ass.getLhrIndex() + 2;
int lastToken = ass.getRhrIndex() - 2;
rewriter.replace(firstToken, lastToken, newVar);
}
}
counter++;
}
}
}
}
}
}
}

/**
* Returns a `BlockNode`. A block can be be a simple_stmts or a list of
* simple_stms and/or compound_stmt, so we just use a list for each kind.
Expand Down
7 changes: 5 additions & 2 deletions test/2a.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
tmp = 2 * x
m = m * tmp
n = n - 1

print(m + tmp)
c = 0
for i in range(m):
g = 2 * m
c = c + i + g
print(m + c)
12 changes: 8 additions & 4 deletions test/2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
x = 3
y = 7
c = 0
m = 1
while y > 1:
tmp = 2 * x + y
m = m * tmp
y = y - 1
for i in n:
g = 5 * x + y
i = g * c
c = c + 1
print(n)
g = 2 * m + i
c = c + i + g
print(m + c)
1 change: 1 addition & 0 deletions test/2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
c = y * n + g
y = y - 1

y = 5
for i in range(y):
g = 2 * y
c = c + i + g
Expand Down

0 comments on commit 7944fd7

Please sign in to comment.