|
1 | 1 | package com.lexicalscope.svm.partition.trace;
|
2 | 2 |
|
3 | 3 | import static com.lexicalscope.svm.partition.trace.Trace.CallReturn.CALL;
|
| 4 | +import static java.util.Arrays.copyOf; |
4 | 5 |
|
5 | 6 | import java.util.Arrays;
|
| 7 | +import java.util.HashMap; |
6 | 8 | import java.util.Iterator;
|
7 | 9 | import java.util.LinkedList;
|
8 | 10 | import java.util.List;
|
| 11 | +import java.util.Map; |
9 | 12 |
|
10 | 13 | import com.google.common.base.Joiner;
|
11 | 14 | import com.lexicalscope.svm.stack.trace.SMethodName;
|
| 15 | +import com.lexicalscope.svm.vm.j.klass.SMethodDescriptor; |
12 | 16 |
|
13 | 17 | public class Trace implements Iterable<Trace> {
|
14 | 18 | private final Trace previous;
|
15 |
| - private final SMethodName method; |
| 19 | + private final SMethodDescriptor method; |
16 | 20 | private final CallReturn callReturn;
|
17 | 21 | private final Object[] args;
|
| 22 | + private final Map<Object, Alias> map; |
| 23 | + private final int nextAlias; |
18 | 24 | public enum CallReturn { CALL, RETURN }
|
19 | 25 |
|
20 | 26 | public Trace() {
|
21 |
| - this(null, null, null, new Object[0]); |
| 27 | + this(null, new HashMap<Object, Alias>(), 0, null, null, new Object[0]); |
22 | 28 | }
|
23 | 29 |
|
24 |
| - private Trace(final Trace trace, final SMethodName method, final CallReturn callReturn, final Object[] args) { |
| 30 | + private Trace( |
| 31 | + final Trace trace, |
| 32 | + final Map<Object, Alias> map, |
| 33 | + final int nextAlias, |
| 34 | + final SMethodDescriptor method, |
| 35 | + final CallReturn callReturn, |
| 36 | + final Object[] args) { |
| 37 | + this.nextAlias = nextAlias; |
25 | 38 | assert !Arrays.asList(args).contains(null);
|
26 | 39 | this.previous = trace;
|
| 40 | + this.map = map; |
27 | 41 | this.method = method;
|
28 | 42 | this.callReturn = callReturn;
|
29 | 43 | this.args = args;
|
30 | 44 | }
|
31 | 45 |
|
32 |
| - public Trace extend(final SMethodName methodCalled, final CallReturn callReturn, final Object ... args) { |
33 |
| - return new Trace(this, methodCalled, callReturn, args); |
| 46 | + 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 | + } |
| 64 | + } |
| 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; |
| 76 | + } |
| 77 | + |
| 78 | + return new Trace(this, newMap, newNextAlias, methodCalled, callReturn, normalisedArgs); |
34 | 79 | }
|
35 | 80 |
|
36 | 81 | public List<Trace> asList() {
|
|
0 commit comments