Commit 3b61d3d2 authored by Lubomir Bulej's avatar Lubomir Bulej

PIResolver: simplified hash code calculation.

PIResolver, ProcGenerator, ProcMethodInstance: cleaned up the formatting to simplify subsequent merges.
parent d98c357c
package ch.usi.dag.disl.processor.generator;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import ch.usi.dag.disl.snippet.Shadow;
import ch.usi.dag.util.Maps;
public class PIResolver {
private Map<ResolverKey, ProcInstance> piStore =
new HashMap<ResolverKey, ProcInstance>();
private final Map <ResolverKey, ProcInstance> piStore = Maps.newHashMap ();
//
private static class ResolverKey {
private final Shadow shadow;
private final int instrPos;
private Shadow shadow;
private int instrPos;
public ResolverKey(Shadow shadow, int instrPos) {
super();
public ResolverKey (final Shadow shadow, final int instrPos) {
this.shadow = shadow;
this.instrPos = instrPos;
}
@Override
public int hashCode() {
@Override
public int hashCode () {
final int prime = 31;
int result = 1;
result = prime * result + instrPos;
result = prime * result
+ ((shadow == null) ? 0 : shadow.hashCode());
int result = instrPos;
result += prime;
result *= prime;
result += (shadow == null) ? 0 : shadow.hashCode ();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
@Override
public boolean equals (final Object obj) {
if (this == obj) {
return true;
}
if (obj == null)
if (obj == null) {
return false;
}
if (getClass() != obj.getClass())
if (getClass () != obj.getClass ()) {
return false;
}
ResolverKey other = (ResolverKey) obj;
if (instrPos != other.instrPos)
final ResolverKey other = (ResolverKey) obj;
if (instrPos != other.instrPos) {
return false;
}
if (shadow == null) {
if (other.shadow != null)
if (other.shadow != null) {
return false;
} else if (!shadow.equals(other.shadow))
}
} else if (!shadow.equals (other.shadow)) {
return false;
}
return true;
}
}
public ProcInstance get(Shadow shadow, int instrPos) {
ResolverKey key = new ResolverKey(shadow, instrPos);
return piStore.get(key);
public ProcInstance get (final Shadow shadow, final int instrPos) {
final ResolverKey key = new ResolverKey (shadow, instrPos);
return piStore.get (key);
}
public void set(Shadow shadow, int instrPos,
ProcInstance processorInstance) {
ResolverKey key = new ResolverKey(shadow, instrPos);
piStore.put(key, processorInstance);
public void set (
final Shadow shadow, final int instrPos,
final ProcInstance processorInstance
) {
final ResolverKey key = new ResolverKey (shadow, instrPos);
piStore.put (key, processorInstance);
}
public Collection<ProcInstance> getAllProcInstances() {
return piStore.values();
public Collection <ProcInstance> getAllProcInstances () {
return piStore.values ();
}
}
......@@ -25,23 +25,23 @@ public class ProcGenerator {
Map<ArgProcessor, ProcInstance> insideMethodPIs = new HashMap<ArgProcessor, ProcInstance>();
public PIResolver compute(Map<Snippet, List<Shadow>> snippetMarkings)
public PIResolver compute(final Map<Snippet, List<Shadow>> snippetMarkings)
throws ProcessorException {
PIResolver piResolver = new PIResolver();
final PIResolver piResolver = new PIResolver();
// for each snippet
for (Snippet snippet : snippetMarkings.keySet()) {
for (final Snippet snippet : snippetMarkings.keySet()) {
Map<Integer, ProcInvocation> invokedProcs = snippet.getCode()
final Map<Integer, ProcInvocation> invokedProcs = snippet.getCode()
.getInvokedProcessors();
for (Shadow shadow : snippetMarkings.get(snippet)) {
for (final Shadow shadow : snippetMarkings.get(snippet)) {
// for each processor defined in snippet
for (Integer instrPos : invokedProcs.keySet()) {
for (final Integer instrPos : invokedProcs.keySet()) {
ProcInvocation prcInv = invokedProcs.get(instrPos);
final ProcInvocation prcInv = invokedProcs.get(instrPos);
ProcInstance prcInst = null;
......@@ -74,8 +74,8 @@ public class ProcGenerator {
return piResolver;
}
private ProcInstance computeInsideMethod(Shadow shadow,
ProcInvocation prcInv) {
private ProcInstance computeInsideMethod(final Shadow shadow,
final ProcInvocation prcInv) {
// all instances of inside method processor will be the same
// if we have one, we can use it multiple times
......@@ -90,8 +90,8 @@ public class ProcGenerator {
return procInst;
}
private ProcInstance computeBeforeInvocation(Shadow shadow,
ProcInvocation prcInv) throws ProcessorException {
private ProcInstance computeBeforeInvocation(final Shadow shadow,
final ProcInvocation prcInv) throws ProcessorException {
// NOTE: ProcUnprocessedCode checks that CALLSITE_ARGS is
// used only with BytecodeMarker
......@@ -104,9 +104,9 @@ public class ProcGenerator {
// get instruction from the method code
// the method invocation is the instruction marked as end
AbstractInsnNode instr = shadow.getRegionEnds().get(0);
final AbstractInsnNode instr = shadow.getRegionEnds().get(0);
String fullMethodName = shadow.getClassNode().name + "."
final String fullMethodName = shadow.getClassNode().name + "."
+ shadow.getMethodNode().name;
// check - method invocation
......@@ -117,25 +117,25 @@ public class ProcGenerator {
+ fullMethodName);
}
MethodInsnNode methodInvocation = (MethodInsnNode) instr;
final MethodInsnNode methodInvocation = (MethodInsnNode) instr;
return createProcInstance(ArgumentProcessorMode.CALLSITE_ARGS,
methodInvocation.desc, shadow, prcInv);
}
private ProcInstance createProcInstance(ArgumentProcessorMode procApplyType,
String methodDesc, Shadow shadow, ProcInvocation prcInv) {
private ProcInstance createProcInstance(final ArgumentProcessorMode procApplyType,
final String methodDesc, final Shadow shadow, final ProcInvocation prcInv) {
List<ProcMethodInstance> procMethodInstances =
final List<ProcMethodInstance> procMethodInstances =
new LinkedList<ProcMethodInstance>();
// get argument types
Type[] argTypeArray = Type.getArgumentTypes(methodDesc);
final Type[] argTypeArray = Type.getArgumentTypes(methodDesc);
// create processor method instances for each argument if applicable
for (int i = 0; i < argTypeArray.length; ++i) {
List<ProcMethodInstance> pmis = createMethodInstances(i,
final List<ProcMethodInstance> pmis = createMethodInstances(i,
argTypeArray.length, argTypeArray[i],
prcInv.getProcessor(), shadow, prcInv);
......@@ -150,29 +150,28 @@ public class ProcGenerator {
return new ProcInstance(procApplyType, procMethodInstances);
}
private List<ProcMethodInstance> createMethodInstances(int argPos,
int argsCount, Type argType, ArgProcessor processor, Shadow shadow,
ProcInvocation prcInv) {
ArgProcessorKind methodArgType = ArgProcessorKind.valueOf(argType);
List<ProcMethodInstance> result = new LinkedList<ProcMethodInstance>();
private List <ProcMethodInstance> createMethodInstances (
final int argPos, final int argsCount, final Type argType,
final ArgProcessor processor, final Shadow shadow,
final ProcInvocation procInv
) {
final ArgProcessorKind methodArgType = ArgProcessorKind.valueOf (argType);
final List <ProcMethodInstance> result = new LinkedList <ProcMethodInstance> ();
// traverse all methods and find the proper ones
for (ArgProcessorMethod method : processor.getMethods()) {
for (final ArgProcessorMethod method : processor.getMethods ()) {
// check argument type
if(method.getTypes().contains(methodArgType)) {
ProcMethodInstance pmi = new ProcMethodInstance(argPos,
argsCount, methodArgType, argType.getDescriptor(),
method.getCode());
if (method.getTypes ().contains (methodArgType)) {
final ProcMethodInstance pmi = new ProcMethodInstance (
argPos, argsCount, methodArgType, argType.getDescriptor (),
method.getCode ()
);
// check guard
if (isPMGuardApplicable(method.getGuard(), shadow, pmi)) {
if (isPMGuardApplicable (method.getGuard (), shadow, pmi)) {
// add method
result.add(pmi);
result.add (pmi);
}
}
}
......@@ -180,11 +179,14 @@ public class ProcGenerator {
return result;
}
private boolean isPMGuardApplicable(Method guard, Shadow shadow,
ProcMethodInstance pmi) {
private boolean isPMGuardApplicable (
final Method guard, final Shadow shadow, final ProcMethodInstance pmi
) {
// evaluate processor method guard
return GuardHelper.guardApplicable(guard, shadow, pmi.getArgPos(),
pmi.getArgTypeDesc(), pmi.getArgsCount());
return GuardHelper.guardApplicable (
guard, shadow,
pmi.getArgPos (), pmi.getArgTypeDesc (), pmi.getArgsCount ()
);
}
}
......@@ -6,42 +6,51 @@ import ch.usi.dag.disl.processor.ProcCode;
public class ProcMethodInstance {
private int argPos;
private int argsCount;
private ArgProcessorKind argType;
private String argTypeDesc;
private ProcCode code;
public ProcMethodInstance(int argPos, int argsCount, ArgProcessorKind argType,
String argTypeDesc, ProcCode code) {
super();
private final int argPos;
private final int argsCount;
private final ArgProcessorKind argType;
private final String argTypeDesc;
private final ProcCode code;
//
public ProcMethodInstance (
final int argPos, final int argsCount, final ArgProcessorKind argType,
final String argTypeDesc, final ProcCode code
) {
this.argPos = argPos;
this.argsCount = argsCount;
this.argType = argType;
this.code = code;
this.argTypeDesc = argTypeDesc;
this.code = code;
}
public int getArgPos() {
public int getArgPos () {
return argPos;
}
public int getArgsCount() {
public int getArgsCount () {
return argsCount;
}
public ArgProcessorKind getArgType() {
public ArgProcessorKind getArgType () {
return argType;
}
public String getArgTypeDesc() {
public String getArgTypeDesc () {
return argTypeDesc;
}
// Note: Code is NOT cloned for each instance of ProcMethodInstance.
// Note: Code is NOT cloned for each ProcMethodInstance.
// If the weaver does not rely on this, we can reuse processor instances
// which can save us some computation
public ProcCode getCode() {
public ProcCode getCode () {
return code;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment