Skip to content

Commit

Permalink
(wip) upgraded cfgnode management
Browse files Browse the repository at this point in the history
  • Loading branch information
L0P0P committed Jul 3, 2024
1 parent 888fe80 commit 900ca3e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
1 change: 1 addition & 0 deletions progs/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ def test():
m = 1
for i in range(10):
m = i + 1
None
29 changes: 19 additions & 10 deletions src/ast/Python3VisitorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import parser.Python3ParserBaseVisitor;
import parser.Python3Parser.*;
import reachingDefinition.*;
import reachingDefinition.CFGNode.*;

import org.antlr.v4.runtime.tree.TerminalNode;

Expand All @@ -15,7 +16,7 @@
*/
public class Python3VisitorImpl extends Python3ParserBaseVisitor<Node> {

private ControlFlowGraph cfg = new ControlFlowGraph(new CFGNode(true));
private ControlFlowGraph cfg = null;

public ControlFlowGraph getCFG() {
return this.cfg;
Expand All @@ -28,7 +29,9 @@ public ControlFlowGraph getCFG() {
* ``` root : NEWLINE* (simple_stmts | compound_stmt)* EOF; ```
*/
public Node visitRoot(RootContext ctx) {
ArrayList<Node> childs = new ArrayList<Node>();
ArrayList<Node> childs = new ArrayList<>();

cfg = new ControlFlowGraph(new CFGNode(ctx.getStart().getLine(), true));

for (int i = 0; i < ctx.getChildCount(); i++) {
var child = ctx.getChild(i);
Expand All @@ -49,7 +52,7 @@ public Node visitRoot(RootContext ctx) {
* ``` simple_stmts : simple_stmt (';' simple_stmt)* ';'? NEWLINE ; ```
*/
public Node visitSimple_stmts(Simple_stmtsContext ctx) {
ArrayList<Node> stmts = new ArrayList<Node>();
ArrayList<Node> stmts = new ArrayList<>();

for (Simple_stmtContext stm : ctx.simple_stmt()) {
stmts.add(visit(stm));
Expand Down Expand Up @@ -131,18 +134,19 @@ public Node visitAssignment(AssignmentContext ctx) {
Node assign = visit(ctx.augassign());
Node rhr = visit(ctx.exprlist(1));

CFGNode assignNode = new CFGNode(false);
CFGNode assignNode = new CFGNode(ctx.getStart().getLine(), false);

/*
for (int i = cfg.getNodes().size(); i > 0; i--) {
if (cfg.getNodeById(i).isStmt()) {
cfg.getNodeById(i).addSuccessor(assignNode);
assignNode.addPredecessor(cfg.getNodeById(i));
break;
}
}
}*/

assignNode.addPredecessor(cfg.getNodeById(cfg.getNodes().size()));
cfg.getNodeById(cfg.getNodes().size()).addSuccessor(assignNode);
assignNode.addPredecessor(cfg.getNodeByLine(cfg.getNodes().size()));
cfg.getNodeByLine(cfg.getNodes().size()).addSuccessor(assignNode);
cfg.addNode(assignNode);

return new AssignmentNode(lhr, assign, rhr);
Expand Down Expand Up @@ -176,7 +180,7 @@ public Node visitImport_stm(Import_stmContext ctx) {

Node dottedName = visit(ctx.dotted_name());

ArrayList<String> names = new ArrayList<String>();
ArrayList<String> names = new ArrayList<>();

for (var s : ctx.NAME()) {
names.add(s.toString());
Expand All @@ -191,7 +195,7 @@ public Node visitImport_stm(Import_stmContext ctx) {
* ``` dotted_name : NAME ('.' NAME)* ; ```
*/
public Node visitDotted_name(Dotted_nameContext ctx) {
ArrayList<TerminalNode> names = new ArrayList<TerminalNode>();
ArrayList<TerminalNode> names = new ArrayList<>();

for (var name : ctx.NAME()) {
names.add(name);
Expand All @@ -206,6 +210,11 @@ public Node visitDotted_name(Dotted_nameContext ctx) {
* ``` funcdef : 'def' NAME '(' paramlist? ')' ':' block ; ```
*/
public Node visitFuncdef(FuncdefContext ctx) {
CFGNode funcNode = new CFGNode(ctx.getStart().getLine(), true);
funcNode.addPredecessor(cfg.getNodeByLine(cfg.getNodes().size()));
cfg.getNodeByLine(cfg.getNodes().size()).addSuccessor(funcNode);
cfg.addNode(funcNode);

Node paramlist = null;

if (ctx.paramlist() != null) {
Expand All @@ -226,7 +235,7 @@ public Node visitFuncdef(FuncdefContext ctx) {
* ```
*/
public Node visitParamlist(ParamlistContext ctx) {
ArrayList<Node> params = new ArrayList<Node>();
ArrayList<Node> params = new ArrayList<>();

for (ParamdefContext s : ctx.paramdef()) {
params.add(visit(s));
Expand Down
26 changes: 14 additions & 12 deletions src/reachingDefinition/CFGNode.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package reachingDefinition;
import java.util.*;

public class CFGNode {
private static int line = 1;
/**
* Class representing a node in the Control Flow Graph.
*/

private int id;
public class CFGNode {
private int line;

// Set of definitions generated from this node
private final Set<String> gen;
Expand All @@ -20,8 +22,8 @@ public class CFGNode {
private final boolean isStmt;


public CFGNode(boolean isStmt) {
this.id = CFGNode.line++;
public CFGNode(int line, boolean isStmt) {
this.line = line;
this.gen = new HashSet<>();
this.kill = new HashSet<>();
this.successors = new ArrayList<>();
Expand All @@ -35,12 +37,12 @@ public boolean isStmt() {
return this.isStmt;
}

public int getId() {
return this.id;
public int getLine() {
return this.line;
}

public int setId(int id) {
return this.id = id;
public int setLine(int line) {
return this.line = line;
}

public Set<String> getGen() {
Expand Down Expand Up @@ -151,7 +153,7 @@ public void removeOut(String var) {

public String toPrint(String prefix) {
StringBuilder sb = new StringBuilder();
sb.append(prefix + "Node " + this.id + "\n");
sb.append(prefix + "Node " + this.line + "\n");
sb.append(prefix + "\tGen: [ ");
for (String var : this.gen) {
sb.append(var + " ");
Expand All @@ -162,11 +164,11 @@ public String toPrint(String prefix) {
}
sb.append("]\n" + prefix + "\tSuccessors: [ ");
for (CFGNode node : this.successors) {
sb.append(node.getId() + " ");
sb.append(node.getLine() + " ");
}
sb.append("]\n" + prefix + "\tPredecessors: [ ");
for (CFGNode node : this.predecessors) {
sb.append(node.getId() + " ");
sb.append(node.getLine() + " ");
}
sb.append("]\n" + prefix + "\tIn: [ ");
for (String var : this.in) {
Expand Down
6 changes: 3 additions & 3 deletions src/reachingDefinition/ControlFlowGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public void generateGenKillSets() {
}
}

public CFGNode getNodeById(int id) {
public CFGNode getNodeByLine(int line) {
for (CFGNode node : this.nodes) {
if (node.getId() == id) {
if (node.getLine() == line) {
return node;
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ public void removeEdge(CFGNode from, CFGNode to) {
public String toPrint(String prefix) {
StringBuilder sb = new StringBuilder();
sb.append(prefix + "ControlFlowGraph\n");
sb.append(prefix + "Entry block: " + this.entryBlock.getId() + "\n");
sb.append(prefix + "Entry block: " + this.entryBlock.getLine() + "\n");
sb.append(prefix + "Nodes:\n");
for (CFGNode node : this.nodes) {
sb.append(node.toPrint(prefix + "\t"));
Expand Down

0 comments on commit 900ca3e

Please sign in to comment.