Skip to content
Snippets Groups Projects
Commit 89b1d565 authored by René Schöne's avatar René Schöne
Browse files

small test with if. added naive prettyPrint.

parent cc91014c
Branches wip-reusable
No related tags found
No related merge requests found
...@@ -24,11 +24,31 @@ import java.util.Queue; ...@@ -24,11 +24,31 @@ import java.util.Queue;
/** Helper attributes used to print a CFG in dot graph format. */ /** Helper attributes used to print a CFG in dot graph format. */
aspect PrintCfg { aspect PrintCfg {
@Override // @Override
public String CfgNode.toString() { // public String CfgNode.toString() {
return name(); // return name();
// }
syn String CFG.prettyPrint() {
StringBuilder sb = new StringBuilder();
for (CfgNode cfgNode : getCfgNodeList()) {
sb.append(cfgNode.prettyPrint());
}
return sb.toString();
} }
syn String CfgNode.prettyPrint() {
StringBuilder sb = new StringBuilder();
sb.append("Node ").append(name()).append(" for\n");
sb.append(getStmt().prettyPrint()).append("\n");
for (CfgNode succ : getSuccessorList()) {
sb.append("| ").append(name()).append(" -succ-> ").append(succ.name()).append("\n");
}
return sb.toString();
}
// ======================== old implementation below ========================
public void BodyDecl.printReverseCfg() { public void BodyDecl.printReverseCfg() {
entry().initPredecessors(); entry().initPredecessors();
System.out.println("digraph " + graphName() + " {"); System.out.println("digraph " + graphName() + " {");
...@@ -104,7 +124,7 @@ aspect PrintCfg { ...@@ -104,7 +124,7 @@ aspect PrintCfg {
eq CfgException.dotAttributes() = " [label=\"" + name() + "\",shape=box]"; eq CfgException.dotAttributes() = " [label=\"" + name() + "\",shape=box]";
eq CfgMarker.dotAttributes() = " [label=\"" + name() + "\",shape=box]"; eq CfgMarker.dotAttributes() = " [label=\"" + name() + "\",shape=box]";
syn String CfgNode.name() = "a node"; syn String CfgNode.name() = "CfgNode@" + hashCode();
eq CfgBranch.name() = branchLabel(); eq CfgBranch.name() = branchLabel();
eq CfgEntry.name() = "entry"; eq CfgEntry.name() = "entry";
eq CfgExit.name() = "exit"; eq CfgExit.name() = "exit";
......
aspect ReusableCfg { aspect ReusableCfg {
syn CfgNode BodyDecl.emptyCFG() = new CfgNode(); syn CFG BodyDecl.emptyCFG() = new CFG();
/** /**
* Get the relational NTA of the CFG. Empty by default. * Get the relational NTA of the CFG. Empty by default.
*/ */
syn CfgNode BodyDecl.reusableCFG() = emptyCFG(); syn CFG BodyDecl.reusableCFG() = emptyCFG();
eq MethodDecl.reusableCFG() = hasBlock() ? getBlock().buildReusableCFG() : emptyCFG(); eq MethodDecl.reusableCFG() = hasBlock() ? getBlock().buildReusableCFG() : emptyCFG();
eq ConstructorDecl.reusableCFG() = getBlock().buildReusableCFG(); eq ConstructorDecl.reusableCFG() = getBlock().buildReusableCFG();
/** /**
* Build the relational NTA of the CFG for a Block. * Build the relational NTA of the CFG for a Block.
*/ */
syn lazy CfgNode Block.buildReusableCFG() { syn lazy CFG Block.buildReusableCFG() {
// CfgNode result = entry(); // TODO alternative: take first expression CFG result = new CFG();
CfgNode result = new CfgNode();
if (getNumStmt() == 0) { if (getNumStmt() == 0) {
// dangling link to a stmt :(
return result; return result;
} }
Map<Stmt, CfgNode> cfgNodes = new HashMap<>(); Map<Stmt, CfgNode> cfgNodes = new HashMap<>();
// Stmt first = entry(); // TODO alternative: take first expression
Stmt first = getStmt(0); Stmt first = getStmt(0);
result.setStmt(first); CfgNode cfgNodeForFirst = first.cfgMakeCfgNode();
cfgNodes.put(first, result); result.addCfgNode(cfgNodeForFirst);
cfgNodes.put(first, cfgNodeForFirst);
Map<CfgNode, Stmt> todo = new HashMap<>(); Map<CfgNode, Stmt> todo = new HashMap<>();
first.cfgSuccessors().forEach(succ -> todo.put(result, succ)); first.cfgSuccessors().forEach(succ -> todo.put(cfgNodeForFirst, succ));
while (!todo.isEmpty()) { while (!todo.isEmpty()) {
// Stmt current = todo.pop(); // this should be an tuple (node, successor). to set the relation // Stmt current = todo.pop(); // this should be an tuple (node, successor). to set the relation
CfgNode cfgNodeForPredecessor = todo.entrySet().iterator().next().getKey(); CfgNode cfgNodeForPredecessor = todo.entrySet().iterator().next().getKey();
...@@ -36,6 +36,7 @@ aspect ReusableCfg { ...@@ -36,6 +36,7 @@ aspect ReusableCfg {
} else { } else {
cfgNodeForCurrent = current.cfgMakeCfgNode(); cfgNodeForCurrent = current.cfgMakeCfgNode();
cfgNodes.put(current, cfgNodeForCurrent); cfgNodes.put(current, cfgNodeForCurrent);
result.addCfgNode(cfgNodeForCurrent);
// add successors to be inspected // add successors to be inspected
current.cfgSuccessors().forEach(succ -> todo.put(cfgNodeForCurrent, succ)); current.cfgSuccessors().forEach(succ -> todo.put(cfgNodeForCurrent, succ));
} }
...@@ -45,8 +46,15 @@ aspect ReusableCfg { ...@@ -45,8 +46,15 @@ aspect ReusableCfg {
return result; return result;
} }
syn CfgNode Stmt.cfgMakeCfgNode() = null; // TODO implement // TODO implement
syn Set<Stmt> Stmt.cfgSuccessors() = null; // TODO implement syn CfgNode Stmt.cfgMakeCfgNode() {
CfgNode result = new CfgNode();
result.setStmt(this);
return result;
}
syn Set<Stmt> Stmt.cfgSuccessors() = Collections.emptySet(); // TODO implement
eq IfStmt.cfgSuccessors() = hasElse() ? smallSet(getThen(), getElse()) : Collections.singleton(getThen());
// ======================== old implementation below ======================== // ======================== old implementation below ========================
......
...@@ -15,10 +15,7 @@ ...@@ -15,10 +15,7 @@
*/ */
package de.tudresden.inf.st.reusablecfg; package de.tudresden.inf.st.reusablecfg;
import org.extendj.ast.BodyDecl; import org.extendj.ast.*;
import org.extendj.ast.CompilationUnit;
import org.extendj.ast.Program;
import org.extendj.ast.TypeDecl;
import org.extendj.parser.JavaParser; import org.extendj.parser.JavaParser;
import java.io.FileInputStream; import java.io.FileInputStream;
...@@ -56,10 +53,10 @@ public class PrintCfg { ...@@ -56,10 +53,10 @@ public class PrintCfg {
unit = program.getCompilationUnit(0); unit = program.getCompilationUnit(0);
for (TypeDecl type : unit.getTypeDeclList()) { for (TypeDecl type : unit.getTypeDeclList()) {
for (BodyDecl bd : type.getBodyDeclList()) { for (BodyDecl bd : type.getBodyDeclList()) {
if (reverse) { CFG cfg = bd.reusableCFG();
bd.printReverseCfg(); System.out.println(cfg.prettyPrint());
} else { if (cfg.getNumCfgNode() == 0) {
bd.printCfg(); System.out.println("No nodes in the CFG for '" + path + "'!");
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment