Created
April 21, 2013 22:25
-
-
Save pxpc2/5431312 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package us.brtm.cfg; | |
| import org.objectweb.asm.tree.AbstractInsnNode; | |
| import org.objectweb.asm.tree.LabelNode; | |
| import org.objectweb.asm.tree.MethodNode; | |
| import org.objectweb.asm.tree.analysis.*; | |
| /** | |
| * @author Pedro Daia Cardoso | |
| */ | |
| public class Graph { | |
| /** | |
| * The analyzer instance. | |
| */ | |
| public Analyzer<BasicValue> analyzer; | |
| /** | |
| * The primitive method. | |
| */ | |
| public MethodNode method; | |
| /** | |
| * All frames (nodes) created in this graph. | |
| */ | |
| public Frame<BasicValue>[] frames; | |
| public Graph(String supername, MethodNode node) { | |
| this.method = node; | |
| analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) { | |
| protected Frame<BasicValue> newFrame(int nLocals, int nStack) { | |
| return new Node<>(nLocals, nStack); | |
| } | |
| protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) { | |
| return new Node<>(src); | |
| } | |
| protected void newControlFlowEdge(int src, int dst) { | |
| Node<BasicValue> s = (Node<BasicValue>) getFrames()[src]; | |
| s.successors.add((Node<BasicValue>) getFrames()[dst]); | |
| } | |
| }; | |
| try { | |
| analyzer.analyze(supername, method); | |
| frames = analyzer.getFrames(); | |
| optimizeMethod(); | |
| } catch (AnalyzerException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| /** | |
| * Removes useless instructions from the method. | |
| */ | |
| public void optimizeMethod() { | |
| AbstractInsnNode[] instructions = method.instructions.toArray(); | |
| for (int i = 0; i < frames.length; i++) { | |
| AbstractInsnNode instruction = instructions[i]; | |
| if (frames[i] == null && !(instruction instanceof LabelNode)) { | |
| method.instructions.remove(instructions[i]); | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package us.brtm.cfg; | |
| import org.objectweb.asm.tree.analysis.BasicValue; | |
| import org.objectweb.asm.tree.analysis.Frame; | |
| import org.objectweb.asm.tree.analysis.Value; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| /** | |
| * @author Pedro Daia Cardoso | |
| */ | |
| public class Node<V extends Value> extends Frame<BasicValue> { | |
| public Set<Node<V>> successors = new HashSet<>(); | |
| public Node(Frame<? extends BasicValue> src) { | |
| super(src); | |
| } | |
| public Node(int nLocals, int nStack) { | |
| super(nLocals, nStack); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment