001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2000 Feng Qian
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.jimple.toolkits.annotation.profiling;
027:
028: import soot.*;
029: import soot.jimple.*;
030: import soot.util.*;
031:
032: import java.util.*;
033: import soot.options.ProfilingOptions;
034:
035: public class ProfilingGenerator extends BodyTransformer {
036: public ProfilingGenerator(Singletons.Global g) {
037: }
038:
039: public static ProfilingGenerator v() {
040: return G
041: .v()
042: .soot_jimple_toolkits_annotation_profiling_ProfilingGenerator();
043: }
044:
045: public String mainSignature = "void main(java.lang.String[])";
046:
047: // private String mainSignature = "long runBenchmark(java.lang.String[])";
048:
049: protected void internalTransform(Body body, String phaseName,
050: Map opts) {
051: ProfilingOptions options = new ProfilingOptions(opts);
052: if (options.notmainentry())
053: mainSignature = "long runBenchmark(java.lang.String[])";
054:
055: {
056: SootMethod m = body.getMethod();
057:
058: SootClass counterClass = Scene.v().loadClassAndSupport(
059: "MultiCounter");
060: SootMethod reset = counterClass.getMethod("void reset()");
061: SootMethod report = counterClass.getMethod("void report()");
062:
063: boolean isMainMethod = m.getSubSignature().equals(
064: mainSignature);
065:
066: Chain units = body.getUnits();
067:
068: if (isMainMethod) {
069: units.addFirst(Jimple.v()
070: .newInvokeStmt(
071: Jimple.v().newStaticInvokeExpr(
072: reset.makeRef())));
073: }
074:
075: Iterator stmtIt = body.getUnits().snapshotIterator();
076: while (stmtIt.hasNext()) {
077: Stmt stmt = (Stmt) stmtIt.next();
078:
079: if (stmt instanceof InvokeStmt) {
080: InvokeExpr iexpr = ((InvokeStmt) stmt)
081: .getInvokeExpr();
082:
083: if (iexpr instanceof StaticInvokeExpr) {
084: SootMethod tempm = ((StaticInvokeExpr) iexpr)
085: .getMethod();
086:
087: if (tempm.getSignature().equals(
088: "<java.lang.System: void exit(int)>")) {
089: units
090: .insertBefore(
091: Jimple
092: .v()
093: .newInvokeStmt(
094: Jimple
095: .v()
096: .newStaticInvokeExpr(
097: report
098: .makeRef())),
099: stmt);
100:
101: }
102: }
103: } else if (isMainMethod
104: && (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt)) {
105: units.insertBefore(Jimple.v().newInvokeStmt(
106: Jimple.v().newStaticInvokeExpr(
107: report.makeRef())), stmt);
108: }
109: }
110: }
111: }
112: }
|