From b16bcb5bf33cba872ca9ac2b2a64a70fd18b293d Mon Sep 17 00:00:00 2001 From: msridhar1 Date: Wed, 30 Jan 2008 19:47:55 +0000 Subject: [PATCH] adding files git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@2497 f5eafffb-2e1d-0410-98e4-8ec43c5233c4 --- .../AbstractRefinementPolicy.java | 57 ++++++++++++ .../TunedFieldRefinementPolicy.java | 88 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/AbstractRefinementPolicy.java create mode 100644 com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/TunedFieldRefinementPolicy.java diff --git a/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/AbstractRefinementPolicy.java b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/AbstractRefinementPolicy.java new file mode 100644 index 000000000..ccf12e13a --- /dev/null +++ b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/AbstractRefinementPolicy.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.demandpa.alg.refinepolicy; + +/** + * Default {@link RefinementPolicy} implementation, delegating to some provided {@link FieldRefinePolicy} and + * {@link CallGraphRefinePolicy} + * @author manu + * + */ +public abstract class AbstractRefinementPolicy implements RefinementPolicy { + + protected final CallGraphRefinePolicy cgRefinePolicy; + + protected final FieldRefinePolicy fieldRefinePolicy; + + private static final int DEFAULT_NUM_PASSES = 3; + + private static final int[] DEFAULT_BUDGET_PER_PASS = { 1000, 12000, 12000 }; + + public int getBudgetForPass(int passNum) { + return DEFAULT_BUDGET_PER_PASS[passNum]; + } + + public CallGraphRefinePolicy getCallGraphRefinePolicy() { + return cgRefinePolicy; + } + + public FieldRefinePolicy getFieldRefinePolicy() { + return fieldRefinePolicy; + } + + public int getNumPasses() { + return DEFAULT_NUM_PASSES; + } + + public boolean nextPass() { + // don't short-circuit since nextPass() can have side-effects + boolean fieldNextPass = fieldRefinePolicy.nextPass(); + boolean callNextPass = cgRefinePolicy.nextPass(); + return fieldNextPass || callNextPass; + } + + public AbstractRefinementPolicy(FieldRefinePolicy fieldRefinePolicy, CallGraphRefinePolicy cgRefinePolicy) { + this.cgRefinePolicy = cgRefinePolicy; + this.fieldRefinePolicy = fieldRefinePolicy; + } + +} diff --git a/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/TunedFieldRefinementPolicy.java b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/TunedFieldRefinementPolicy.java new file mode 100644 index 000000000..5138dab7b --- /dev/null +++ b/com.ibm.wala.core/src/com/ibm/wala/demandpa/alg/refinepolicy/TunedFieldRefinementPolicy.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.demandpa.alg.refinepolicy; + +import java.util.Collection; + +import com.ibm.wala.classLoader.IClass; +import com.ibm.wala.classLoader.IField; +import com.ibm.wala.demandpa.util.ArrayContents; +import com.ibm.wala.ipa.cha.ClassHierarchy; +import com.ibm.wala.types.ClassLoaderReference; +import com.ibm.wala.types.TypeReference; +import com.ibm.wala.util.collections.HashSetFactory; + +public class TunedFieldRefinementPolicy implements FieldRefinePolicy { + + private final ClassHierarchy cha; + + private final Collection typesToRefine = HashSetFactory.make(); + + private IClass firstSkippedClass = null; + + public boolean nextPass() { + if (firstSkippedClass != null) { + typesToRefine.add(firstSkippedClass); + firstSkippedClass = null; + return true; + } else { + return false; + } + } + + public boolean shouldRefine(IField field) { + if (field == ArrayContents.v()) { + return true; + } + IClass classToCheck = removeInner(field.getDeclaringClass()); + if (superOfAnyEncountered(classToCheck)) { + return true; + } else { + if (firstSkippedClass == null) { + firstSkippedClass = classToCheck; + } + return false; + } + } + + private boolean superOfAnyEncountered(IClass klass) { + for (IClass toRefine : typesToRefine) { + if (cha.isAssignableFrom(klass, toRefine)) { + return true; + } + } + return false; + } + + /** + * + * @param klass + * @return the top-level {@link IClass} where klass is declared, or klass + * itself if klass is top-level or if top-level class not loaded + */ + private IClass removeInner(IClass klass) { + ClassLoaderReference cl = klass.getClassLoader().getReference(); + String klassStr = klass.getName().toString(); + int dollarIndex = klassStr.indexOf('$'); + if (dollarIndex == -1) { + return klass; + } else { + String topMostName = klassStr.substring(0, dollarIndex); + IClass topMostClass = cha.lookupClass(TypeReference.findOrCreate(cl, topMostName)); + return (topMostClass != null) ? topMostClass : klass; + } + } + + public TunedFieldRefinementPolicy(ClassHierarchy cha) { + this.cha = cha; + } + +} \ No newline at end of file