summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbd-912 <bdunahu@colostate.edu>2024-04-19 23:21:01 -0600
committerbd-912 <bdunahu@colostate.edu>2024-04-19 23:21:01 -0600
commit8625d3a2708e6332083a1b780b4f016beacf67d3 (patch)
tree07ea3a578a5dcf6d7d20155747e78c3f9c38bbe4
parentb733f594c4ab0697aff9afdcc45e0421107dec85 (diff)
CFG captures both VBranch paths
-rw-r--r--vaporize/library/CFGNode.java17
-rw-r--r--vaporize/library/CFGSimp.java2
-rw-r--r--vaporize/library/ControlFlowGraph.java9
-rw-r--r--vaporize/library/Kettle.java107
4 files changed, 89 insertions, 46 deletions
diff --git a/vaporize/library/CFGNode.java b/vaporize/library/CFGNode.java
index a01bfb0..0afce60 100644
--- a/vaporize/library/CFGNode.java
+++ b/vaporize/library/CFGNode.java
@@ -1,6 +1,8 @@
package vaporize.library;
+import misc.*;
import cs132.vapor.ast.*;
+
import java.util.ArrayList;
class CFGNode {
@@ -25,9 +27,18 @@ class CFGNode {
return this.line;
}
- public boolean equals(Node other) {
- return other.sourcePos ==
- this.instruction.sourcePos;
+ /**
+ * For if we only have a line
+ * number. (VBranch issues)
+ */
+ // FIXME
+ public boolean equals(Object other) {
+ return (other instanceof Node &&
+ (((Node) other).sourcePos ==
+ this.instruction.sourcePos)) ||
+ (other instanceof Integer &&
+ (((Integer) other)
+ .equals(this.instruction.sourcePos.line)));
}
protected void addSource(CFGNode node) {
diff --git a/vaporize/library/CFGSimp.java b/vaporize/library/CFGSimp.java
index 3e39576..a759afc 100644
--- a/vaporize/library/CFGSimp.java
+++ b/vaporize/library/CFGSimp.java
@@ -113,6 +113,8 @@ public class CFGSimp extends VInstr.VisitorPR<ControlFlowGraph, String, RuntimeE
///////////////////////////////////////////////////////////////
CFGNode curr = cfg.getNode(n);
cfg.addEdge(this.curr, cfg.getNode(n));
+ cfg.addEdge(this.curr, cfg.getNode(new Integer(this.kettle.findLabelIndex(n))));
+
this.curr = curr;
///////////////////////////////////////////////////////////////
MinimalLogger.info(String.format("<-%s (\"%s\":%s)",
diff --git a/vaporize/library/ControlFlowGraph.java b/vaporize/library/ControlFlowGraph.java
index e7057ac..c0eff10 100644
--- a/vaporize/library/ControlFlowGraph.java
+++ b/vaporize/library/ControlFlowGraph.java
@@ -15,7 +15,7 @@ public class ControlFlowGraph {
this.start = null;
}
- protected CFGNode getNode(Node a) {
+ protected CFGNode getNode(Object a) {
CFGNode ret = null;
for (CFGNode n : this.nodes) {
if (n.equals(a)) {
@@ -24,9 +24,12 @@ public class ControlFlowGraph {
}
}
- if (ret == null)
+ if (ret == null) {
+ String str = (a instanceof Node) ? ((Node) a).sourcePos.toString() :
+ a.toString();
MinimalLogger.severe(String.format("Could not find a node matching %s",
- a.sourcePos.toString()));
+ str));
+ }
return ret;
}
diff --git a/vaporize/library/Kettle.java b/vaporize/library/Kettle.java
index 5baf69e..e413907 100644
--- a/vaporize/library/Kettle.java
+++ b/vaporize/library/Kettle.java
@@ -18,69 +18,96 @@ class Kettle {
ArrayList<String> vaporm;
protected Kettle(ArrayList<String> vapor) {
- MinimalLogger.info("Pouring program into kettle...");
- this.vapor = vapor;
- this.vaporm = new ArrayList<String>();
+ MinimalLogger.info("Pouring program into kettle...");
+ this.vapor = vapor;
+ this.vaporm = new ArrayList<String>();
}
protected String get(Node n) {
- /**
- * Given the source position of a Node, returns the original line.
- */
- return this.vapor.get(this.indexOf(n));
+ /**
+ * Given the source position of a Node, returns the original line.
+ */
+ return this.vapor.get(this.indexOf(n));
}
protected void replaceFunctionDeclare(VFunction prev, int in,
- int out, int local) {
- /**
- * Replaces a function declaraction in the program
- * with the vapor equivalent.
- */
- String next = String.format("func %s [in %d, out %d, local %d]",
- this.parseFuncName(prev),
- in,
- out,
- local);
-
- MinimalLogger.info(String.format("Replacing function declaraction %s with %s",
- this.get(prev),
- next));
-
- this.set(prev, next);
+ int out, int local) {
+ /**
+ * Replaces a function declaraction in the program
+ * with the vapor equivalent.
+ */
+ String next = String.format("func %s [in %d, out %d, local %d]",
+ this.parseFuncName(prev),
+ in,
+ out,
+ local);
+
+ MinimalLogger.info(String.format("Replacing function declaraction %s with %s",
+ this.get(prev),
+ next));
+
+ this.set(prev, next);
}
protected String parseFuncName(VFunction func) {
- /**
- * Helper for replaceFunctionDeclare
- * "func A_foo(this t.1)" -> "A_foo"
- */
- String str = this.get(func);
- int openParenIndex = str.indexOf('(');
- return str.substring(5, openParenIndex).trim();
+ /**
+ * Helper for replaceFunctionDeclare
+ * "func A_foo(this t.1)" -> "A_foo"
+ */
+ String str = this.get(func);
+ int openParenIndex = str.indexOf('(');
+ return str.substring(5, openParenIndex).trim();
}
protected String spill() {
- return null;
+ return null;
}
protected String backup() {
- return null;
+ return null;
}
protected String dump() {
- return String.join("\n", this.vapor);
+ return String.join("\n", this.vapor);
}
- private int indexOf(Node n) {
- return n.sourcePos.line-1;
+ /**
+ * Needed because VBranch doesn't seem to
+ * keep track of this...
+ */
+ protected int findLabelIndex(VBranch n) {
+ int index = -1;
+ // is this guarenteed?
+ String search = n.target.toString().substring(1);
+ String comp;
+ for (int i = 0; i < this.vapor.size(); ++i) {
+ if (!this.vapor.get(i).isEmpty()) {
+ comp = this.vapor.get(i).trim();
+ if (comp.substring(0, comp.length()-1)
+ .equals(search)) {
+ index = i;
+ break;
+ }
+ }
+ }
+
+ if (index == -1) {
+ MinimalLogger.severe(String.format("findLabelIndex could not compute label for %s!",
+ this.get(n).trim()));
+ }
+ return index;
+ }
+
+ protected int indexOf(Node n) {
+ return n.sourcePos.line-1;
}
private void set(Node n, String s) {
- /**
- * Sets the position of Node n in the original
- * file to String s.
- */
- this.vapor.set(this.indexOf(n), s);
+ /**
+ * Sets the position of Node n in the original
+ * file to String s.
+ */
+ this.vapor.set(this.indexOf(n), s);
}
}