Skip to content

Commit 70b9786

Browse files
committed
refactoring of the trace aliasing
1 parent 055ae49 commit 70b9786

File tree

3 files changed

+68
-31
lines changed

3 files changed

+68
-31
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Java Bytecode Snapshot Vm and Symbolic Executor [![Build Status](https://travis-ci.org/lexicalscope/svm.png?branch=master)](https://travis-ci.org/lexicalscope/svm)
22
=========================================
33

4-
If you would like to help out, please contact me or just fork and get going.
4+
If you would like to help out, please contact me or just fork and get going. You will need to use maven 3 to build things, and I have put eclipse config files in the repo so you should be able to get going with eclipse pretty quickly. You might need to pick a particular JDK version to run with (try OpenJDK "1.7.0_51")
55

66
Particularly anyone who wants to get more of the bytecode instructions implemented, or who wants to help with java standard library compatiblity, or work on performance/scalability.
77

svm-partition-trace/src/main/java/com/lexicalscope/svm/partition/trace/Trace.java

+7-30
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.lexicalscope.svm.partition.trace;
22

33
import static com.lexicalscope.svm.partition.trace.Trace.CallReturn.CALL;
4-
import static java.util.Arrays.copyOf;
54

65
import java.util.Arrays;
76
import java.util.HashMap;
@@ -44,38 +43,16 @@ private Trace(
4443
}
4544

4645
public Trace extend(final SMethodDescriptor methodCalled, final CallReturn callReturn, final Object ... args) {
47-
final Object[] normalisedArgs = copyOf(args, args.length);
48-
int newNextAlias = nextAlias;
49-
Map<Object, Alias> newMap = map;
50-
51-
if(callReturn.equals(CallReturn.CALL)) {
52-
for (final int i : methodCalled.objectArgIndexes()) {
53-
Alias alias;
54-
if(null == (alias = newMap.get(args[i + 1]))) {
55-
// TODO[tim] do COW here
56-
if(map == newMap) {
57-
newMap = new HashMap<>(map);
58-
}
59-
alias = new Alias(newNextAlias++);
60-
newMap.put(args[i + 1], alias);
61-
}
62-
normalisedArgs[i + 1] = alias;
63-
}
46+
final TraceExtender traceExtender = new TraceExtender(args, map, nextAlias);
47+
48+
if(callReturn.equals(CALL)) {
49+
traceExtender.aliasesForCallArguments(methodCalled.objectArgIndexes());
6450
}
65-
if(callReturn.equals(CallReturn.CALL) || methodCalled.returnIsObject()) {
66-
Alias alias;
67-
if(null == (alias = newMap.get(args[0]))) {
68-
// TODO[tim] do COW here
69-
if(map == newMap) {
70-
newMap = new HashMap<>(map);
71-
}
72-
alias = new Alias(newNextAlias++);
73-
newMap.put(args[0], alias);
74-
}
75-
normalisedArgs[0] = alias;
51+
if(callReturn.equals(CALL) || methodCalled.returnIsObject()) {
52+
traceExtender.aliasesForZerothArguments();
7653
}
7754

78-
return new Trace(this, newMap, newNextAlias, methodCalled, callReturn, normalisedArgs);
55+
return new Trace(this, traceExtender.newMap(), traceExtender.newNextAlias(), methodCalled, callReturn, traceExtender.normalisedArgs());
7956
}
8057

8158
public List<Trace> asList() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.lexicalscope.svm.partition.trace;
2+
3+
import static java.util.Arrays.copyOf;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class TraceExtender {
9+
private final Object[] normalisedArgs;
10+
private int newNextAlias;
11+
private Map<Object, Alias> newMap;
12+
private final Map<Object, Alias> map;
13+
14+
public TraceExtender(final Object[] args, final Map<Object, Alias> map, final int nextAlias) {
15+
this.normalisedArgs = copyOf(args, args.length);
16+
this.newNextAlias = nextAlias;
17+
this.map = map;
18+
this.newMap = map;
19+
}
20+
21+
public void aliasesForZerothArguments() {
22+
normalisedArgs[0] = aliasForArg(normalisedArgs[0]);
23+
}
24+
25+
public void aliasesForCallArguments(final int[] objectArgIndexes) {
26+
for (final int i : objectArgIndexes) {
27+
normalisedArgs[i + 1] = aliasForArg(normalisedArgs[i + 1]);
28+
}
29+
}
30+
31+
private Alias aliasForArg(final Object arg) {
32+
Alias alias;
33+
if(null == (alias = newMap.get(arg))) {
34+
alias = newAlias(arg);
35+
}
36+
return alias;
37+
}
38+
39+
private Alias newAlias(final Object arg) {
40+
// TODO[tim] do proper COW here
41+
if(map == newMap) {
42+
newMap = new HashMap<>(map);
43+
}
44+
final Alias alias = new Alias(newNextAlias++);
45+
newMap.put(arg, alias);
46+
return alias;
47+
}
48+
49+
public Map<Object, Alias> newMap() {
50+
return newMap;
51+
}
52+
53+
public int newNextAlias() {
54+
return newNextAlias;
55+
}
56+
57+
public Object[] normalisedArgs() {
58+
return normalisedArgs;
59+
}
60+
}

0 commit comments

Comments
 (0)