001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1999 Patrick Lam, Patrick Pominville and Raja Vallee-Rai
003: * Copyright (C) 2004 Ondrej Lhotak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the
017: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
018: * Boston, MA 02111-1307, USA.
019: */
020:
021: /*
022: * Modified by the Sable Research Group and others 1997-1999.
023: * See the 'credits' file distributed with Soot for the complete list of
024: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
025: */
026:
027: package soot.baf;
028:
029: import soot.*;
030: import soot.jimple.*;
031: import soot.jimple.internal.*;
032: import soot.baf.internal.*;
033: import java.util.*;
034:
035: public class Baf {
036: public Baf(Singletons.Global g) {
037: }
038:
039: public static Baf v() {
040: return G.v().soot_baf_Baf();
041: }
042:
043: public static Type getDescriptorTypeOf(Type opType) {
044: if (opType instanceof NullType || opType instanceof ArrayType
045: || opType instanceof RefType)
046: opType = RefType.v();
047:
048: return opType;
049: }
050:
051: /**
052: Constructs a Local with the given name and type.
053: */
054:
055: public Local newLocal(String name, Type t) {
056: return new BafLocal(name, t);
057: }
058:
059: /**
060: Constructs a new BTrap for the given exception on the given Unit range with the given Unit handler.
061: */
062:
063: public Trap newTrap(SootClass exception, Unit beginUnit,
064: Unit endUnit, Unit handlerUnit) {
065: return new BTrap(exception, beginUnit, endUnit, handlerUnit);
066: }
067:
068: /**
069: Constructs a ExitMonitorInst() grammar chunk
070: */
071:
072: public ExitMonitorInst newExitMonitorInst() {
073: return new BExitMonitorInst();
074: }
075:
076: /**
077: Constructs a EnterMonitorInst() grammar chunk.
078: */
079:
080: public EnterMonitorInst newEnterMonitorInst() {
081: return new BEnterMonitorInst();
082: }
083:
084: public ReturnVoidInst newReturnVoidInst() {
085: return new BReturnVoidInst();
086: }
087:
088: public NopInst newNopInst() {
089: return new BNopInst();
090: }
091:
092: public GotoInst newGotoInst(Unit unit) {
093: return new BGotoInst(unit);
094: }
095:
096: public JSRInst newJSRInst(Unit unit) {
097: return new BJSRInst(unit);
098: }
099:
100: public PlaceholderInst newPlaceholderInst(Unit source) {
101: return new PlaceholderInst(source);
102: }
103:
104: public UnitBox newInstBox(Unit unit) {
105: return new InstBox((Inst) unit);
106: }
107:
108: public PushInst newPushInst(Constant c) {
109: return new BPushInst(c);
110: }
111:
112: public IdentityInst newIdentityInst(Value local, Value identityRef) {
113: return new BIdentityInst(local, identityRef);
114: }
115:
116: public ValueBox newLocalBox(Value value) {
117: return new BafLocalBox(value);
118: }
119:
120: public ValueBox newIdentityRefBox(Value value) {
121: return new IdentityRefBox(value);
122: }
123:
124: /**
125: Constructs a ThisRef(RefType) grammar chunk.
126: */
127:
128: public ThisRef newThisRef(RefType t) {
129: return new ThisRef(t);
130: }
131:
132: /**
133: Constructs a ParameterRef(SootMethod, int) grammar chunk.
134: */
135:
136: public ParameterRef newParameterRef(Type paramType, int number) {
137: return new ParameterRef(paramType, number);
138: }
139:
140: public StoreInst newStoreInst(Type opType, Local l) {
141: return new BStoreInst(opType, l);
142: }
143:
144: public LoadInst newLoadInst(Type opType, Local l) {
145: return new BLoadInst(opType, l);
146: }
147:
148: public ArrayWriteInst newArrayWriteInst(Type opType) {
149: return new BArrayWriteInst(opType);
150: }
151:
152: public ArrayReadInst newArrayReadInst(Type opType) {
153: return new BArrayReadInst(opType);
154: }
155:
156: public StaticGetInst newStaticGetInst(SootFieldRef fieldRef) {
157: return new BStaticGetInst(fieldRef);
158: }
159:
160: public StaticPutInst newStaticPutInst(SootFieldRef fieldRef) {
161: return new BStaticPutInst(fieldRef);
162: }
163:
164: public FieldGetInst newFieldGetInst(SootFieldRef fieldRef) {
165: return new BFieldGetInst(fieldRef);
166: }
167:
168: public FieldPutInst newFieldPutInst(SootFieldRef fieldRef) {
169: return new BFieldPutInst(fieldRef);
170: }
171:
172: public AddInst newAddInst(Type opType) {
173: return new BAddInst(opType);
174: }
175:
176: public PopInst newPopInst(Type aType) {
177: return new BPopInst(aType);
178: }
179:
180: public SubInst newSubInst(Type opType) {
181: return new BSubInst(opType);
182: }
183:
184: public MulInst newMulInst(Type opType) {
185: return new BMulInst(opType);
186: }
187:
188: public DivInst newDivInst(Type opType) {
189: return new BDivInst(opType);
190: }
191:
192: public AndInst newAndInst(Type opType) {
193: return new BAndInst(opType);
194: }
195:
196: public ArrayLengthInst newArrayLengthInst() {
197: return new BArrayLengthInst();
198: }
199:
200: public NegInst newNegInst(Type opType) {
201: return new BNegInst(opType);
202: }
203:
204: public OrInst newOrInst(Type opType) {
205: return new BOrInst(opType);
206: }
207:
208: public RemInst newRemInst(Type opType) {
209: return new BRemInst(opType);
210: }
211:
212: public ShlInst newShlInst(Type opType) {
213: return new BShlInst(opType);
214: }
215:
216: public ShrInst newShrInst(Type opType) {
217: return new BShrInst(opType);
218: }
219:
220: public UshrInst newUshrInst(Type opType) {
221: return new BUshrInst(opType);
222: }
223:
224: public XorInst newXorInst(Type opType) {
225: return new BXorInst(opType);
226: }
227:
228: public InstanceCastInst newInstanceCastInst(Type opType) {
229: return new BInstanceCastInst(opType);
230: }
231:
232: public InstanceOfInst newInstanceOfInst(Type opType) {
233: return new BInstanceOfInst(opType);
234: }
235:
236: public PrimitiveCastInst newPrimitiveCastInst(Type fromType,
237: Type toType) {
238: return new BPrimitiveCastInst(fromType, toType);
239: }
240:
241: public NewInst newNewInst(RefType opType) {
242: return new BNewInst(opType);
243: }
244:
245: public NewArrayInst newNewArrayInst(Type opType) {
246: return new BNewArrayInst(opType);
247: }
248:
249: public NewMultiArrayInst newNewMultiArrayInst(ArrayType opType,
250: int dimensions) {
251: return new BNewMultiArrayInst(opType, dimensions);
252: }
253:
254: public StaticInvokeInst newStaticInvokeInst(SootMethodRef methodRef) {
255: return new BStaticInvokeInst(methodRef);
256: }
257:
258: public SpecialInvokeInst newSpecialInvokeInst(
259: SootMethodRef methodRef) {
260: return new BSpecialInvokeInst(methodRef);
261: }
262:
263: public VirtualInvokeInst newVirtualInvokeInst(
264: SootMethodRef methodRef) {
265: return new BVirtualInvokeInst(methodRef);
266: }
267:
268: public InterfaceInvokeInst newInterfaceInvokeInst(
269: SootMethodRef methodRef, int argCount) {
270: return new BInterfaceInvokeInst(methodRef, argCount);
271: }
272:
273: public ReturnInst newReturnInst(Type opType) {
274: return new BReturnInst(opType);
275: }
276:
277: public IfCmpEqInst newIfCmpEqInst(Type opType, Unit unit) {
278: return new BIfCmpEqInst(opType, unit);
279: }
280:
281: public IfCmpGeInst newIfCmpGeInst(Type opType, Unit unit) {
282: return new BIfCmpGeInst(opType, unit);
283: }
284:
285: public IfCmpGtInst newIfCmpGtInst(Type opType, Unit unit) {
286: return new BIfCmpGtInst(opType, unit);
287: }
288:
289: public IfCmpLeInst newIfCmpLeInst(Type opType, Unit unit) {
290: return new BIfCmpLeInst(opType, unit);
291: }
292:
293: public IfCmpLtInst newIfCmpLtInst(Type opType, Unit unit) {
294: return new BIfCmpLtInst(opType, unit);
295: }
296:
297: public IfCmpNeInst newIfCmpNeInst(Type opType, Unit unit) {
298: return new BIfCmpNeInst(opType, unit);
299: }
300:
301: public CmpInst newCmpInst(Type opType) {
302: return new BCmpInst(opType);
303: }
304:
305: public CmpgInst newCmpgInst(Type opType) {
306: return new BCmpgInst(opType);
307: }
308:
309: public CmplInst newCmplInst(Type opType) {
310: return new BCmplInst(opType);
311: }
312:
313: public IfEqInst newIfEqInst(Unit unit) {
314: return new BIfEqInst(unit);
315: }
316:
317: public IfGeInst newIfGeInst(Unit unit) {
318: return new BIfGeInst(unit);
319: }
320:
321: public IfGtInst newIfGtInst(Unit unit) {
322: return new BIfGtInst(unit);
323: }
324:
325: public IfLeInst newIfLeInst(Unit unit) {
326: return new BIfLeInst(unit);
327: }
328:
329: public IfLtInst newIfLtInst(Unit unit) {
330: return new BIfLtInst(unit);
331: }
332:
333: public IfNeInst newIfNeInst(Unit unit) {
334: return new BIfNeInst(unit);
335: }
336:
337: public IfNullInst newIfNullInst(Unit unit) {
338: return new BIfNullInst(unit);
339: }
340:
341: public IfNonNullInst newIfNonNullInst(Unit unit) {
342: return new BIfNonNullInst(unit);
343: }
344:
345: public ThrowInst newThrowInst() {
346: return new BThrowInst();
347: }
348:
349: public SwapInst newSwapInst(Type fromType, Type toType) {
350: return new BSwapInst(fromType, toType);
351: }
352:
353: /*
354: public DupInst newDupInst(Type type)
355: {
356: return new BDupInst(new ArrayList(), Arrays.asList(new Type[] {type}));
357: }*/
358:
359: public Dup1Inst newDup1Inst(Type type) {
360: return new BDup1Inst(type);
361: }
362:
363: public Dup2Inst newDup2Inst(Type aOp1Type, Type aOp2Type) {
364: return new BDup2Inst(aOp1Type, aOp2Type);
365: }
366:
367: public Dup1_x1Inst newDup1_x1Inst(Type aOpType, Type aUnderType) {
368: return new BDup1_x1Inst(aOpType, aUnderType);
369: }
370:
371: public Dup1_x2Inst newDup1_x2Inst(Type aOpType, Type aUnder1Type,
372: Type aUnder2Type) {
373: return new BDup1_x2Inst(aOpType, aUnder1Type, aUnder2Type);
374: }
375:
376: public Dup2_x1Inst newDup2_x1Inst(Type aOp1Type, Type aOp2Type,
377: Type aUnderType) {
378: return new BDup2_x1Inst(aOp1Type, aOp2Type, aUnderType);
379: }
380:
381: public Dup2_x2Inst newDup2_x2Inst(Type aOp1Type, Type aOp2Type,
382: Type aUnder1Type, Type aUnder2Type) {
383: return new BDup2_x2Inst(aOp1Type, aOp2Type, aUnder1Type,
384: aUnder2Type);
385: }
386:
387: public IncInst newIncInst(Local aLocal, Constant aConstant) {
388: return new BIncInst(aLocal, aConstant);
389: }
390:
391: public LookupSwitchInst newLookupSwitchInst(Unit defaultTarget,
392: List lookupValues, List targets) {
393: return new BLookupSwitchInst(defaultTarget, lookupValues,
394: targets);
395: }
396:
397: public TableSwitchInst newTableSwitchInst(Unit defaultTarget,
398: int lowIndex, int highIndex, List targets) {
399: return new BTableSwitchInst(defaultTarget, lowIndex, highIndex,
400: targets);
401: }
402:
403: public static String bafDescriptorOf(Type type) {
404: TypeSwitch sw;
405:
406: type.apply(sw = new TypeSwitch() {
407: public void caseBooleanType(BooleanType t) {
408: setResult("b");
409: }
410:
411: public void caseByteType(ByteType t) {
412: setResult("b");
413: }
414:
415: public void caseCharType(CharType t) {
416: setResult("c");
417: }
418:
419: public void caseDoubleType(DoubleType t) {
420: setResult("d");
421: }
422:
423: public void caseFloatType(FloatType t) {
424: setResult("f");
425: }
426:
427: public void caseIntType(IntType t) {
428: setResult("i");
429: }
430:
431: public void caseLongType(LongType t) {
432: setResult("l");
433: }
434:
435: public void caseShortType(ShortType t) {
436: setResult("s");
437: }
438:
439: public void defaultCase(Type t) {
440: throw new RuntimeException("Invalid type: " + t);
441: }
442:
443: public void caseRefType(RefType t) {
444: setResult("r");
445: }
446:
447: });
448:
449: return (String) sw.getResult();
450: }
451:
452: /** Returns an empty BafBody associated with method m. */
453: public BafBody newBody(SootMethod m) {
454: return new BafBody(m);
455: }
456:
457: /** Returns a BafBody constructed from b. */
458: public BafBody newBody(Body b) {
459: return new BafBody(b, new HashMap());
460: }
461:
462: /** Returns a BafBody constructed from b. */
463: public BafBody newBody(Body b, String phase) {
464: Map options = PhaseOptions.v().getPhaseOptions(phase);
465: return new BafBody(b, options);
466: }
467: }
|