https://github.com/saeg/asm-defuse
ASM powered by definitions/uses analysis
https://github.com/saeg/asm-defuse
asm java
Last synced: 4 months ago
JSON representation
ASM powered by definitions/uses analysis
- Host: GitHub
- URL: https://github.com/saeg/asm-defuse
- Owner: saeg
- License: other
- Created: 2013-12-04T19:58:06.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2025-06-28T13:47:28.000Z (12 months ago)
- Last Synced: 2025-08-16T21:34:40.363Z (10 months ago)
- Topics: asm, java
- Language: Java
- Homepage:
- Size: 660 KB
- Stars: 37
- Watchers: 3
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE-TEMPLATE.txt
Awesome Lists containing this project
README
# ASM-DefUse
[ASM](http://asm.ow2.org/) powered by definitions/uses analysis
[](https://maven-badges.herokuapp.com/maven-central/br.usp.each.saeg/asm-defuse)
ASM-DefUse extends [ASM](http://asm.ow2.org/) analysis API with control- and data-flow algorithms for definition/uses analysis.
## Requirements
* Java 6
## Setup
If you're using Maven just add this new dependency:
```xml
br.usp.each.saeg
asm-defuse
${asm-defuse.version}
```
## Control-flow analysis
The following code exemplify how to use `FlowAnalyzer` class
```java
MethodNode mn = ... // A regular MethodNode from ASM tree API
FlowAnalyzer analyzer = new FlowAnalyzer(new BasicInterpreter());
analyzer.analyze("package/ClassName", mn);
int[][] successors = analyzer.getSuccessors();
int[][] predecessors = analyzer.getPredecessors();
int[][] basicBlocks = analyzer.getBasicBlocks();
int[] leaders = analyzer.getLeaders();
for (int i = 0; i < mn.instructions.size(); i++) {
// successors[i] array contains the indexes of the successors of instruction i
System.out.println("Instruction " + i + " has " + successors[i].length + " successors");
// predecessors[i] array contains the indexes of the predecessors of instruction i
System.out.println("Instruction " + i + " has " + predecessors[i].length + " predecessors");
System.out.println("Instruction " + i + " belongs to basic block " + leaders[i]);
}
for (int i = 0; i < basicBlocks.length; i++) {
System.out.println("Basic block " + i + " contains " + basicBlocks[i].length + " instructions");
}
```
## Data-flow analysis
The following code exemplify how to use `DefUseAnalyzer` class
```java
MethodNode mn = ... // A regular MethodNode from ASM tree API
DefUseAnalyzer analyzer = new DefUseAnalyzer();
analyzer.analyze("package/ClassName", mn);
Variable[] variables = analyzer.getVariables();
DefUseFrame[] frames = analyzer.getDefUseFrames();
System.out.println("This method contains " + variables.length + " variables");
for (int i = 0; i < mn.instructions.size(); i++) {
System.out.println("Instruction " + i + " contains definitions of " + frames[i].getDefinitions());
System.out.println("Instruction " + i + " contains usage of " + frames[i].getUses());
}
```
## Definition-Use Chain
The following code exemplify how to compute `DefUseChain`
```java
MethodNode mn = ... // A regular MethodNode from ASM tree API
DefUseInterpreter interpreter = new DefUseInterpreter();
FlowAnalyzer flowAnalyzer = new FlowAnalyzer(interpreter);
DefUseAnalyzer analyzer = new DefUseAnalyzer(flowAnalyzer, interpreter);
analyzer.analyze("package/ClassName", mn);
Variable[] variables = analyzer.getVariables();
DefUseChain[] chains = new DepthFirstDefUseChainSearch().search(
analyzer.getDefUseFrames(),
analyzer.getVariables(),
flowAnalyzer.getSuccessors(),
flowAnalyzer.getPredecessors());
System.out.println("This method contains " + chains.length + " Definition-Use Chains");
for (int i = 0; i < chains.length; i++) {
DefUseChain chain = chains[i];
System.out.println("Instruction " + chain.def + " define variable " + variables[chain.var]);
System.out.println("Instruction " + chain.use + " uses variable " + variables[chain.var]);
// There is a path between chain.def and chain.use that not redefine chain.var
}
```