View Javadoc

1   /* Soot - a J*va Optimization Framework
2    * Copyright (C) 1999 Patrick Lam
3    * Copyright (C) 2004 Ondrej Lhotak
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the
17   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18   * Boston, MA 02111-1307, USA.
19   */
20  
21  /*
22   * Modified by the Sable Research Group and others 1997-1999.  
23   * See the 'credits' file distributed with Soot for the complete list of
24   * contributors.  (Soot is distributed at http://www.sable.mcgill.ca/soot)
25   */
26  
27  
28  
29  
30  
31  
32  package org.kit.furia.fragment.soot.representation;
33  
34  import soot.*;
35  import soot.jimple.*;
36  import soot.jimple.internal.JCaughtExceptionRef;
37  import soot.jimple.internal.JNewExpr;
38  
39  import org.kit.furia.fragment.soot.representation.internal.*;
40  
41  import soot.shimple.PhiExpr;
42  import soot.shimple.internal.SPhiExpr;
43  import soot.util.*;
44  
45  import java.util.*;
46  import org.apache.log4j.Logger;
47  
48  /**
49      The Grimp class contains all the constructors for the components of the Grimp
50      grammar for the Grimp body. <br><br>
51  
52      Immediate -> Local | Constant <br>
53      RValue -> Local | Constant | ConcreteRef | Expr<br>
54      Variable -> Local | ArrayRef | InstanceFieldRef | StaticFieldRef <br>
55   */
56  
57  
58  public class Frimp
59  {
60  	
61  	private final static Logger logger = Logger.getLogger(Frimp.class);
62  	
63  	private static Frimp v = new Frimp();
64  	
65      public Frimp( Singletons.Global g ) {}
66      
67      public Frimp(){}
68      // TODO check if we really need this (compliance to style). I don't think so
69      public static Frimp v() { return v; }
70  
71      /**
72          Constructs a XorExpr(Expr, Expr) grammar chunk.
73       */
74  
75      public XorExpr newXorExpr(Value op1, Value op2)
76      {
77          return new FXorExpr(op1, op2);
78      }
79  
80  
81      /**
82          Constructs a UshrExpr(Expr, Expr) grammar chunk.
83       */
84  
85      public UshrExpr newUshrExpr(Value op1, Value op2)
86      {
87          return new FUshrExpr(op1, op2);
88      }
89  
90  
91      /**
92          Constructs a SubExpr(Expr, Expr) grammar chunk.
93       */
94  
95      public SubExpr newSubExpr(Value op1, Value op2)
96      {
97          return new FSubExpr(op1, op2);
98      }
99      
100 
101 
102 
103     /**
104         Constructs a ShrExpr(Expr, Expr) grammar chunk.
105      */
106 
107     public ShrExpr newShrExpr(Value op1, Value op2)
108     {
109         return new FShrExpr(op1, op2);
110     }
111 
112 
113     /**
114         Constructs a ShlExpr(Expr, Expr) grammar chunk.
115      */
116 
117     public ShlExpr newShlExpr(Value op1, Value op2)
118     {
119         return new FShlExpr(op1, op2);
120     }
121 
122 
123     /**
124         Constructs a RemExpr(Expr, Expr) grammar chunk.
125      */
126 
127     public RemExpr newRemExpr(Value op1, Value op2)
128     {
129         return new FRemExpr(op1, op2);
130     }
131 
132 
133     /**
134         Constructs a OrExpr(Expr, Expr) grammar chunk.
135      */
136 
137     public OrExpr newOrExpr(Value op1, Value op2)
138     {
139         return new FOrExpr(op1, op2);
140     }
141 
142 
143     /**
144         Constructs a NeExpr(Expr, Expr) grammar chunk.
145      */
146 
147     public NeExpr newNeExpr(Value op1, Value op2)
148     {
149         return new FNeExpr(op1, op2);
150     }
151 
152 
153     /**
154         Constructs a MulExpr(Expr, Expr) grammar chunk.
155      */
156 
157     public MulExpr newMulExpr(Value op1, Value op2)
158     {
159         return new FMulExpr(op1, op2);
160     }
161 
162 
163     /**
164         Constructs a LeExpr(Expr, Expr) grammar chunk.
165      */
166 
167     public LeExpr newLeExpr(Value op1, Value op2)
168     {
169         return new FLeExpr(op1, op2);
170     }
171 
172 
173     /**
174         Constructs a GeExpr(Expr, Expr) grammar chunk.
175      */
176 
177     public GeExpr newGeExpr(Value op1, Value op2)
178     {
179         return new FGeExpr(op1, op2);
180     }
181 
182 
183     /**
184         Constructs a EqExpr(Expr, Expr) grammar chunk.
185      */
186 
187     public EqExpr newEqExpr(Value op1, Value op2)
188     {
189         return new FEqExpr(op1, op2);
190     }
191 
192     /**
193         Constructs a DivExpr(Expr, Expr) grammar chunk.
194      */
195 
196     public DivExpr newDivExpr(Value op1, Value op2)
197     {
198         return new FDivExpr(op1, op2);
199     }
200 
201 
202     /**
203         Constructs a CmplExpr(Expr, Expr) grammar chunk.
204      */
205 
206     public CmplExpr newCmplExpr(Value op1, Value op2)
207     {
208         return new FCmplExpr(op1, op2);
209     }
210 
211 
212     /**
213         Constructs a CmpgExpr(Expr, Expr) grammar chunk.
214      */
215 
216     public CmpgExpr newCmpgExpr(Value op1, Value op2)
217     {
218         return new FCmpgExpr(op1, op2);
219     }
220 
221 
222     /**
223         Constructs a CmpExpr(Expr, Expr) grammar chunk.
224      */
225 
226     public CmpExpr newCmpExpr(Value op1, Value op2)
227     {
228         return new FCmpExpr(op1, op2);
229     }
230 
231 
232     /**
233         Constructs a GtExpr(Expr, Expr) grammar chunk.
234      */
235 
236     public GtExpr newGtExpr(Value op1, Value op2)
237     {
238         return new FGtExpr(op1, op2);
239     }
240 
241 
242     /**
243         Constructs a LtExpr(Expr, Expr) grammar chunk.
244      */
245 
246     public LtExpr newLtExpr(Value op1, Value op2)
247     {
248         return new FLtExpr(op1, op2);
249     }
250 
251     /**
252         Constructs a AddExpr(Expr, Expr) grammar chunk.
253      */
254 
255     public AddExpr newAddExpr(Value op1, Value op2)
256     {
257         return new FAddExpr(op1, op2);
258     }
259 
260 
261     /**
262         Constructs a AndExpr(Expr, Expr) grammar chunk.
263      */
264 
265     public AndExpr newAndExpr(Value op1, Value op2)
266     {
267         return new FAndExpr(op1, op2);
268     }
269 
270 
271     /**
272         Constructs a NegExpr(Expr, Expr) grammar chunk.
273      */
274 
275     public NegExpr newNegExpr(Value op)
276     {
277         return new FNegExpr(op);
278     }
279 
280 
281     /**
282         Constructs a LengthExpr(Expr) grammar chunk.
283      */
284 
285     public LengthExpr newLengthExpr(Value op)
286     {
287         return new FLengthExpr(op);
288     }
289 
290 
291     /**
292         Constructs a CastExpr(Expr, Type) grammar chunk.
293      */
294 
295     public CastExpr newCastExpr(Value op1, Type t)
296     {
297         return new FCastExpr(op1, t);
298     }
299 
300     /**
301         Constructs a InstanceOfExpr(Expr, Type)
302         grammar chunk.
303      */
304 
305     public InstanceOfExpr newInstanceOfExpr(Value op1, Type t)
306     {
307         return new FInstanceOfExpr(op1, t);
308     }
309 
310 
311     /**
312         Constructs a NewExpr(RefType) grammar chunk.
313      */
314 
315     NewExpr newNewExpr(RefType type)
316     {
317         return Jimple.v().newNewExpr(type);
318     }
319 
320 
321     /**
322         Constructs a NewArrayExpr(Type, Expr) grammar chunk.
323      */
324 
325     public NewArrayExpr newNewArrayExpr(Type type, Value size)
326     {
327         return new FNewArrayExpr(type, size);
328     }
329 
330     /**
331         Constructs a NewMultiArrayExpr(ArrayType, List of Expr) grammar chunk.
332      */
333 
334     public NewMultiArrayExpr newNewMultiArrayExpr(ArrayType type, List sizes)
335     {
336         return new FNewMultiArrayExpr(type, sizes);
337     }
338 
339     /**
340         Constructs a NewInvokeExpr(Local base, List of Expr) grammar chunk.
341      */
342 
343     public NewInvokeExpr newNewInvokeExpr(RefType base, SootMethodRef method, List args)
344     {
345         return (NewInvokeExpr)new FNewInvokeExpr(base, method, args);
346     }
347 
348     /**
349         Constructs a StaticInvokeExpr(ArrayType, List of Expr) grammar chunk.
350      */
351 
352     public StaticInvokeExpr newStaticInvokeExpr(SootMethodRef method, List args)
353     {
354         return new FStaticInvokeExpr(method, args);
355     }
356 
357 
358     /**
359         Constructs a SpecialInvokeExpr(Local base, SootMethodRef method, List of Expr) grammar chunk.
360      */
361 
362     public SpecialInvokeExpr newSpecialInvokeExpr(Local base, SootMethodRef method, List args)
363     {
364         return new FSpecialInvokeExpr(base, method, args);
365     }
366 
367 
368     /**
369         Constructs a VirtualInvokeExpr(Local base, SootMethodRef method, List of Expr) grammar chunk.
370      */
371 
372     public VirtualInvokeExpr newVirtualInvokeExpr(Local base, SootMethodRef method, List args)
373     {
374         return new FVirtualInvokeExpr(base, method, args);
375     }
376 
377 
378     /**
379         Constructs a InterfaceInvokeExpr(Local base, SootMethodRef method, List of Expr) grammar chunk.
380      */
381 
382     public InterfaceInvokeExpr newInterfaceInvokeExpr(Local base, SootMethodRef method, List args)
383     {
384         return new FInterfaceInvokeExpr(base, method, args);
385     }
386 
387 
388     /**
389         Constructs a ThrowStmt(Expr) grammar chunk.
390      */
391 
392     public ThrowStmt newThrowStmt(Value op)
393     {
394         return new FThrowStmt(op);
395     }
396 
397     public ThrowStmt newThrowStmt(ThrowStmt s)
398     {
399         return new FThrowStmt(s.getOp());
400     }
401 
402     /**
403         Constructs a ExitMonitorStmt(Expr) grammar chunk
404      */
405 
406     public ExitMonitorStmt newExitMonitorStmt(Value op)
407     {
408         return new FExitMonitorStmt(op);
409     }
410 
411     public ExitMonitorStmt newExitMonitorStmt(ExitMonitorStmt s)
412     {
413         return new FExitMonitorStmt(s.getOp());
414     }
415 
416     /**
417         Constructs a EnterMonitorStmt(Expr) grammar chunk.
418      */
419 
420     public EnterMonitorStmt newEnterMonitorStmt(Value op)
421     {
422         return new FEnterMonitorStmt(op);
423     }
424 
425     public EnterMonitorStmt newEnterMonitorStmt(EnterMonitorStmt s)
426     {
427         return new FEnterMonitorStmt(s.getOp());
428     }
429 
430     /**
431         Constructs a BreakpointStmt() grammar chunk.
432      */
433 
434     public BreakpointStmt newBreakpointStmt()
435     {
436         return Jimple.v().newBreakpointStmt();
437     }
438     
439     public BreakpointStmt newBreakpointStmt(BreakpointStmt s)
440     {
441         return Jimple.v().newBreakpointStmt();
442     }
443 
444     /**
445         Constructs a GotoStmt(Stmt) grammar chunk.
446      */
447 
448     public GotoStmt newGotoStmt(Unit target)
449     {
450         return Jimple.v().newGotoStmt(target);
451     }
452 
453     public GotoStmt newGotoStmt(GotoStmt s)
454     {
455         return Jimple.v().newGotoStmt(s.getTarget());
456     }
457 
458     /**
459         Constructs a NopStmt() grammar chunk.
460      */
461 
462     public NopStmt newNopStmt()
463     {
464         return Jimple.v().newNopStmt();
465     }
466 
467     public NopStmt newNopStmt(NopStmt s)
468     {
469         return Jimple.v().newNopStmt();
470     }
471 
472     /**
473         Constructs a ReturnVoidStmt() grammar chunk.
474      */
475 
476     public ReturnVoidStmt newReturnVoidStmt()
477     {
478         return Jimple.v().newReturnVoidStmt();
479     }
480 
481     public ReturnVoidStmt newReturnVoidStmt(ReturnVoidStmt s)
482     {
483         return Jimple.v().newReturnVoidStmt();
484     }
485 
486     /**
487         Constructs a ReturnStmt(Expr) grammar chunk.
488      */
489 
490     public ReturnStmt newReturnStmt(Value op)
491     {
492         return new FReturnStmt(op);
493     }
494 
495     public ReturnStmt newReturnStmt(ReturnStmt s)
496     {
497         return new FReturnStmt(s.getOp());
498     }
499 
500     /**
501         Constructs a IfStmt(Condition, Stmt) grammar chunk.
502      */
503 
504     public IfStmt newIfStmt(Value condition, Unit target)
505     {
506         return new FIfStmt(condition, target);
507     }
508 
509     public IfStmt newIfStmt(IfStmt s)
510     {
511         return new FIfStmt(s.getCondition(), s.getTarget());
512     }
513 
514     /**
515         Constructs a IdentityStmt(Local, IdentityRef) grammar chunk.
516      */
517 
518     public IdentityStmt newIdentityStmt(Value local, Value identityRef)
519     {
520         return new FIdentityStmt(local, identityRef);
521     }
522 
523     public IdentityStmt newIdentityStmt(IdentityStmt s)
524     {
525         return new FIdentityStmt(s.getLeftOp(), s.getRightOp());
526     }
527 
528     /**
529         Constructs a AssignStmt(Variable, RValue) grammar chunk.
530      */
531 
532     public AssignStmt newAssignStmt(Value variable, Value rvalue)
533     {
534         return new FAssignStmt(variable, rvalue);
535     }
536 
537     public AssignStmt newAssignStmt(AssignStmt s)
538     {
539         return new FAssignStmt(s.getLeftOp(), s.getRightOp());
540     }
541 
542     /**
543         Constructs a InvokeStmt(InvokeExpr) grammar chunk.
544      */
545 
546     public InvokeStmt newInvokeStmt(Value op)
547     {
548         return new FInvokeStmt(op);
549     }
550 
551     public InvokeStmt newInvokeStmt(InvokeStmt s)
552     {
553         return new FInvokeStmt(s.getInvokeExpr());
554     }
555 
556     /**
557         Constructs a TableSwitchStmt(Expr, int, int, List of Unit, Stmt) grammar chunk.
558      */
559 
560     public TableSwitchStmt newTableSwitchStmt(Value key, int lowIndex, int highIndex, List targets, Unit defaultTarget)
561     {
562         return new FTableSwitchStmt(key, lowIndex, highIndex, targets, defaultTarget);
563     }
564 
565     public TableSwitchStmt newTableSwitchStmt(TableSwitchStmt s)
566     {
567         return new FTableSwitchStmt(s.getKey(), s.getLowIndex(), 
568                                     s.getHighIndex(), s.getTargets(),
569                                     s.getDefaultTarget());
570     }
571 
572     /**
573         Constructs a LookupSwitchStmt(Expr, List of Expr, List of Unit, Stmt) grammar chunk.
574      */
575 
576     public LookupSwitchStmt newLookupSwitchStmt(Value key, List lookupValues, List targets, Unit defaultTarget)
577     {
578         return new FLookupSwitchStmt(key, lookupValues, targets, defaultTarget);
579     }
580 
581     public LookupSwitchStmt newLookupSwitchStmt(LookupSwitchStmt s)
582     {
583         return new FLookupSwitchStmt(s.getKey(), s.getLookupValues(),
584                                      s.getTargets(), s.getDefaultTarget());
585     }
586 
587     /**
588         Constructs a Local with the given name and type.
589     */
590 
591     public Local newLocal(String name, Type t)
592     {
593         return Jimple.v().newLocal(name, t);
594     }
595 
596     /**
597         Constructs a new Trap for the given exception on the given Stmt range with the given Stmt handler.
598     */
599 
600     public Trap newTrap(SootClass exception, Unit beginStmt, Unit endStmt, Unit handlerStmt)
601     {
602         return new FTrap(exception, beginStmt, endStmt, handlerStmt);
603     }
604 
605     public Trap newTrap(Trap trap)
606     {
607         return new FTrap(trap.getException(), trap.getBeginUnit(),
608                          trap.getEndUnit(), trap.getHandlerUnit());
609     }
610 
611     /**
612         Constructs a StaticFieldRef(SootFieldRef) grammar chunk.
613      */
614 
615     public StaticFieldRef newStaticFieldRef(SootFieldRef f)
616     {
617         return Jimple.v().newStaticFieldRef(f);
618     }
619 
620 
621     /**
622         Constructs a ThisRef(RefType) grammar chunk.
623      */
624 
625     public ThisRef newThisRef(RefType t)
626     {
627         return Jimple.v().newThisRef(t);
628     }
629 
630 
631     /**
632         Constructs a ParameterRef(SootMethod, int) grammar chunk.
633      */
634 
635     public ParameterRef newParameterRef(Type paramType, int number)
636     {
637         return Jimple.v().newParameterRef(paramType, number);
638     }
639 
640     /**
641         Constructs a InstanceFieldRef(Value, SootFieldRef) grammar chunk.
642      */
643 
644     public InstanceFieldRef newInstanceFieldRef(Value base, SootFieldRef f)
645     {
646         return new FInstanceFieldRef(base, f);
647     }
648 
649 
650     /**
651         Constructs a CaughtExceptionRef() grammar chunk.
652      */
653 
654     public CaughtExceptionRef newCaughtExceptionRef()
655     {
656         return Jimple.v().newCaughtExceptionRef();
657     }
658 
659 
660     /**
661         Constructs a ArrayRef(Local, Expr) grammar chunk.
662      */
663 
664     public ArrayRef newArrayRef(Value base, Value index)
665     {
666         return new FArrayRef(base, index);
667     }
668 
669     public ValueBox newVariableBox(Value value)
670     {
671         return Jimple.v().newVariableBox(value);
672     }
673 
674     public ValueBox newLocalBox(Value value)
675     {
676         return Jimple.v().newLocalBox(value);
677     }
678 
679     public ValueBox newRValueBox(Value value)
680     {
681         return new FRValueBox(value);
682     }
683 
684     public ValueBox newImmediateBox(Value value)
685     {
686         return Jimple.v().newImmediateBox(value);
687     }
688 
689     public ValueBox newExprBox(Value value)
690     {
691         return new FExprBox(value);
692     }
693 
694     public ValueBox newArgBox(Value value)
695     {
696         return new FExprBox(value);
697     }
698 
699     public ValueBox newObjExprBox(Value value)
700     {
701         return new FObjExprBox(value);
702     }
703 
704     public ValueBox newIdentityRefBox(Value value)
705     {
706         return Jimple.v().newIdentityRefBox(value);
707     }
708 
709     public ValueBox newConditionExprBox(Value value)
710     {
711         return Jimple.v().newConditionExprBox(value);
712     }
713 
714     public ValueBox newInvokeExprBox(Value value)
715     {
716         return Jimple.v().newInvokeExprBox(value);
717     }
718 
719     public UnitBox newStmtBox(Unit unit)
720     {
721         return Jimple.v().newStmtBox((Stmt) unit);
722     }
723 
724     
725     
726     /**
727      * Create a PhiExpr with the provided list of Values (Locals or
728      * Constants) and the corresponding control flow predecessor
729      * Blocks.  Instead of a list of predecessor blocks, you may
730      * provide a list of the tail Units from the corresponding blocks.
731      * an FPhiExpr has conditions related to each 
732      **/
733     public PhiExpr newPhiExpr(List args, List preds)
734     {
735         return new FPhiExpr(args, preds);
736     }
737     
738     /**
739      * Receives a list of value boxes and returns a list
740      * of values
741      * @param valueBoxes
742      * @return
743      */
744     public static List valueBoxToValue(List valueBoxes){
745     	Iterator it = valueBoxes.iterator();
746     	Vector res = new Vector();
747     	while(it.hasNext()){
748     		ValueBox vb = (ValueBox) it.next();
749     		res.addElement(vb.getValue());
750     	}
751     	return res;
752     }
753     
754     /** Carries out the mapping from other Value's to Grimp Value's */
755     public Value newExpr(Value value)
756     {
757     	if (value instanceof SPhiExpr){
758     		final FExprBox returnedExpr = new FExprBox(IntConstant.v(0));
759     		returnedExpr.setValue
760             (newPhiExpr( valueBoxToValue( ((SPhiExpr)value).getArgs()) ,
761             		((SPhiExpr)value).getPreds()));
762     		return returnedExpr.getValue();
763     	}else
764         if (value instanceof Expr)
765             {
766                 final FExprBox returnedExpr = new FExprBox(IntConstant.v(0));
767                 ((Expr)value).apply(new AbstractExprSwitch()
768                 {
769                 	
770                 	
771                     public void caseAddExpr(AddExpr v)
772                     {
773                         returnedExpr.setValue
774                             (newAddExpr(newExpr(v.getOp1()),
775                                         newExpr(v.getOp2())));
776                     }
777 
778                     public void caseAndExpr(AndExpr v)
779                     {
780                         returnedExpr.setValue
781                             (newAndExpr(newExpr(v.getOp1()),
782                                         newExpr(v.getOp2())));
783                     }
784 
785                     public void caseCmpExpr(CmpExpr v)
786                     {
787                         returnedExpr.setValue
788                             (newCmpExpr(newExpr(v.getOp1()),
789                                         newExpr(v.getOp2())));
790                     }
791 
792                     public void caseCmpgExpr(CmpgExpr v)
793                     {
794                         returnedExpr.setValue
795                             (newCmpgExpr(newExpr(v.getOp1()),
796                                         newExpr(v.getOp2())));
797                     }
798 
799                     public void caseCmplExpr(CmplExpr v)
800                     {
801                         returnedExpr.setValue
802                             (newCmplExpr(newExpr(v.getOp1()),
803                                         newExpr(v.getOp2())));
804                     }
805 
806                     public void caseDivExpr(DivExpr v)
807                     {
808                         returnedExpr.setValue
809                             (newDivExpr(newExpr(v.getOp1()),
810                                         newExpr(v.getOp2())));
811                     }
812 
813                     public void caseEqExpr(EqExpr v)
814                     {
815                         returnedExpr.setValue
816                             (newEqExpr(newExpr(v.getOp1()),
817                                         newExpr(v.getOp2())));
818                     }
819 
820                     public void caseNeExpr(NeExpr v)
821                     {
822                         returnedExpr.setValue
823                             (newNeExpr(newExpr(v.getOp1()),
824                                         newExpr(v.getOp2())));
825                     }
826 
827                     public void caseGeExpr(GeExpr v)
828                     {
829                         returnedExpr.setValue
830                             (newGeExpr(newExpr(v.getOp1()),
831                                         newExpr(v.getOp2())));
832                     }
833 
834                     public void caseGtExpr(GtExpr v)
835                     {
836                         returnedExpr.setValue
837                             (newGtExpr(newExpr(v.getOp1()),
838                                         newExpr(v.getOp2())));
839                     }
840 
841                     public void caseLeExpr(LeExpr v)
842                     {
843                         returnedExpr.setValue
844                             (newLeExpr(newExpr(v.getOp1()),
845                                         newExpr(v.getOp2())));
846                     }
847 
848                     public void caseLtExpr(LtExpr v)
849                     {
850                         returnedExpr.setValue
851                             (newLtExpr(newExpr(v.getOp1()),
852                                         newExpr(v.getOp2())));
853                     }
854 
855                     public void caseMulExpr(MulExpr v)
856                     {
857                         returnedExpr.setValue
858                             (newMulExpr(newExpr(v.getOp1()),
859                                         newExpr(v.getOp2())));
860                     }
861 
862                     public void caseOrExpr(OrExpr v)
863                     {
864                         returnedExpr.setValue
865                             (newOrExpr(newExpr(v.getOp1()),
866                                         newExpr(v.getOp2())));
867                     }
868 
869                     public void caseRemExpr(RemExpr v)
870                     {
871                         returnedExpr.setValue
872                             (newRemExpr(newExpr(v.getOp1()),
873                                         newExpr(v.getOp2())));
874                     }
875 
876                     public void caseShlExpr(ShlExpr v)
877                     {
878                         returnedExpr.setValue
879                             (newShlExpr(newExpr(v.getOp1()),
880                                         newExpr(v.getOp2())));
881                     }
882 
883                     public void caseShrExpr(ShrExpr v)
884                     {
885                         returnedExpr.setValue
886                             (newShrExpr(newExpr(v.getOp1()),
887                                         newExpr(v.getOp2())));
888                     }
889 
890                     public void caseUshrExpr(UshrExpr v)
891                     {
892                         returnedExpr.setValue
893                             (newUshrExpr(newExpr(v.getOp1()),
894                                         newExpr(v.getOp2())));
895                     }
896 
897                     public void caseSubExpr(SubExpr v)
898                     {
899                         returnedExpr.setValue
900                             (newSubExpr(newExpr(v.getOp1()),
901                                         newExpr(v.getOp2())));
902                     }
903 
904                     public void caseXorExpr(XorExpr v)
905                     {
906                         returnedExpr.setValue
907                             (newXorExpr(newExpr(v.getOp1()),
908                                         newExpr(v.getOp2())));
909                     }
910 
911                     public void caseInterfaceInvokeExpr(InterfaceInvokeExpr v)
912                     {
913                         ArrayList newArgList = new ArrayList();
914                         for (int i = 0; i < v.getArgCount(); i++)
915                             newArgList.add(newExpr(v.getArg(i)));
916                         returnedExpr.setValue
917                             (newInterfaceInvokeExpr((Local)(v.getBase()),
918                                                     v.getMethodRef(),
919                                                     newArgList));
920                     }
921 
922                     public void caseSpecialInvokeExpr(SpecialInvokeExpr v)
923                     {
924                         ArrayList newArgList = new ArrayList();
925                         for (int i = 0; i < v.getArgCount(); i++)
926                             newArgList.add(newExpr(v.getArg(i)));
927                         returnedExpr.setValue
928                             (newSpecialInvokeExpr((Local)(v.getBase()),
929                                                     v.getMethodRef(),
930                                                     newArgList));
931                     }
932 
933                     public void caseStaticInvokeExpr(StaticInvokeExpr v)
934                     {
935                         ArrayList newArgList = new ArrayList();
936                         for (int i = 0; i < v.getArgCount(); i++)
937                             newArgList.add(newExpr(v.getArg(i)));
938                         returnedExpr.setValue
939                             (newStaticInvokeExpr(v.getMethodRef(),
940                                                  newArgList));
941                     }
942 
943                     public void caseVirtualInvokeExpr(VirtualInvokeExpr v)
944                     {
945                         ArrayList newArgList = new ArrayList();
946                         for (int i = 0; i < v.getArgCount(); i++)
947                             newArgList.add(newExpr(v.getArg(i)));
948                         returnedExpr.setValue
949                             (newVirtualInvokeExpr((Local)(v.getBase()),
950                                                   v.getMethodRef(),
951                                                   newArgList));
952                     }
953 
954                     public void caseCastExpr(CastExpr v)
955                     {
956                         returnedExpr.setValue(newCastExpr(newExpr(v.getOp()),
957                                                           v.getType()));
958                     }
959 
960                     public void caseInstanceOfExpr(InstanceOfExpr v)
961                     {
962                         returnedExpr.setValue(newInstanceOfExpr
963                                               (newExpr(v.getOp()),
964                                                v.getCheckType()));
965                     }
966 
967                     public void caseNewArrayExpr(NewArrayExpr v)
968                     {
969                         returnedExpr.setValue(newNewArrayExpr(v.getBaseType(),
970                                               v.getSize()));
971                     }
972 
973                     public void caseNewMultiArrayExpr(NewMultiArrayExpr v)
974                      {
975                         returnedExpr.setValue(newNewMultiArrayExpr
976                                               (v.getBaseType(),
977                                               v.getSizes()));
978                     }
979 
980                     public void caseNewExpr(NewExpr v)
981                     {
982                         returnedExpr.setValue(newNewExpr(v.getBaseType()));
983                     }
984 
985                     public void caseLengthExpr(LengthExpr v)
986                       {
987                         returnedExpr.setValue(newLengthExpr
988                                               (newExpr(v.getOp())));
989                     }
990 
991                     public void caseNegExpr(NegExpr v)
992                     {
993                         returnedExpr.setValue(newNegExpr(newExpr(v.getOp())));
994                     }
995 
996                     public void defaultCase(Object v)
997                     {
998                         returnedExpr.setValue((Expr)v);
999                     }                        
1000                 });
1001                 return returnedExpr.getValue();
1002             }
1003         else 
1004             {
1005                 if (value instanceof ArrayRef)
1006                     return newArrayRef(((ArrayRef)value).getBase(), 
1007                                        newExpr(((ArrayRef)value).getIndex()));
1008                 if (value instanceof InstanceFieldRef)
1009                     return newInstanceFieldRef
1010                         (newExpr((((InstanceFieldRef)value).getBase())),
1011                          ((InstanceFieldRef)value).getFieldRef());
1012                 /* have Ref/Value, which is fine -- not Jimple-specific. */
1013                 return value;
1014             }
1015     }
1016 
1017     /** Returns an empty GrimpBody associated with method m. */
1018     public FrimpBody newBody(SootMethod m)
1019     {
1020         return new FrimpBody(m);
1021     }
1022 
1023     /** Returns a GrimpBody constructed from b. */
1024     public FrimpBody newBody(Body b, String phase)
1025     {
1026         return new FrimpBody(b);
1027     }
1028 
1029     public static Value cloneIfNecessary(Value val) 
1030     {
1031         if( val instanceof Local || val instanceof Constant )
1032             return val;
1033         else
1034             return (Value) val.clone();
1035     } 
1036     
1037     /**
1038      * This method copies deeply a value
1039      * 
1040      * @param x
1041      * @return
1042      */
1043     /*public static Value cloneValueDeeply(Value x){
1044         Value copy = Frimp.cloneIfNecessary((Value) x);
1045         
1046         HashMap bindings = new HashMap();
1047         
1048         // Clone local units.
1049         Iterator it = x.getUseBoxes().iterator();
1050         
1051         while(it.hasNext()) {
1052             ValueBox original = (ValueBox) it.next();
1053             
1054             Value cp = (Value) Frimp.cloneIfNecessary(original.getValue());
1055 
1056             // Add cloned unit to our trap list.
1057             
1058 
1059             // Build old <-> new mapping.
1060             bindings.put(original.getValue(), cp);
1061         }
1062 
1063         // backpatching all local variables.
1064         it = copy.getUseBoxes().iterator();
1065         while(it.hasNext()) {
1066             ValueBox vb = (ValueBox) it.next();
1067             if(vb.getValue() instanceof Local)
1068                 vb.setValue((Value) bindings.get(vb.getValue()));
1069         }
1070         
1071         return copy;
1072         
1073     }*/
1074     public static String toQ(Type x){
1075     	return FuriaConstructDefinitions.FURIA_ftype + "(" + ")";
1076     }
1077     
1078     public static String toQ(SootFieldRef x){
1079     	return FuriaConstructDefinitions.FURIA_ffieldRef + "(" + ")";
1080     }
1081     
1082     public static String toQ(SootMethodRef x){
1083     	
1084     	return FuriaConstructDefinitions.FURIA_fmethodRef + "(" +  ")";
1085     }
1086     
1087     public static String quote(String x){
1088     	String res = x;
1089     	res = StringTools.getEscapedStringOf(x);
1090     	res = res.replaceAll("\\n", "n");
1091     	res = res.replaceAll("\\\"", "&q" );
1092     	res = res.replaceAll("[\"]", "&q" );
1093     	res = res.replace('"' , 'q' );
1094     	res = res.replace('"' , 'q' );
1095     	
1096     	res = res.replace('\\' , 'b' );
1097     	
1098        	return "\"" + res  + "\"";
1099     }
1100     
1101     public static String toQ(Value x)throws Exception {
1102 		String res = "(x . x)";
1103 		// exceptions (objects from soot that we didn't inherit or re-create
1104 		// anything we change here must be changed in FuriaSemanticExpression.getObjectFromHashCode(int)
1105 		assert x != null;
1106 		
1107 		if (x instanceof Qable) {	
1108 			
1109 			Qable t = (Qable) x;
1110 			res = t.toQ();
1111 			
1112 		} else if (x instanceof Local) {
1113 			
1114 			res = FuriaConstructDefinitions.FURIA_flocalRef + "(" + ")";
1115 			
1116 		}else if (x instanceof NullConstant) {
1117 			
1118 			res = FuriaConstructDefinitions.FURIA_fnull + "()";
1119 			
1120 		}else if (x instanceof ParameterRef) {
1121 			
1122 			ParameterRef p = (ParameterRef)x;			
1123 			res = FuriaConstructDefinitions.FURIA_fparameterRef + "("  + ")";
1124 			
1125 		}else if (x instanceof JNewExpr) {
1126 			JNewExpr n = (JNewExpr)x;
1127 			
1128 			res = FuriaConstructDefinitions.FURIA_fnew + "("  +")";
1129 			
1130 		}else if (x instanceof IntConstant) {
1131 			
1132 			res = FuriaConstructDefinitions.FURIA_fintConstant + "(" + formatNum((IntConstant)x) + ")";
1133 			
1134 		}else if (x instanceof FloatConstant) {
1135 			
1136 			res = FuriaConstructDefinitions.FURIA_ffloatConstant + "(" + formatNum((FloatConstant)x) + ")";
1137 			
1138 		}else if (x instanceof DoubleConstant) {
1139 			
1140 			res = FuriaConstructDefinitions.FURIA_fdoubleConstant +  "(" + formatNum((DoubleConstant)x) + ")";
1141 				
1142 		}else if (x instanceof JCaughtExceptionRef) {
1143 			
1144 			res = FuriaConstructDefinitions.FURIA_fcaughtException + "()";
1145 				
1146 		}else if (x instanceof StringConstant) {
1147 			
1148 			res = FuriaConstructDefinitions.FURIA_fstringConstant + "("  + ")";
1149 			
1150 		}else if (x instanceof StaticFieldRef) {
1151 			
1152 			StaticFieldRef s = (StaticFieldRef)x;
1153 			
1154 			res = FuriaConstructDefinitions.FURIA_fstaticFieldRef + "(" +")"; 
1155 					
1156 		}else if (x instanceof ThisRef) {
1157 			
1158 			res =  FuriaConstructDefinitions.FURIA_fthisRef + "(" + ")";
1159 		}else if (x instanceof LongConstant){
1160 			
1161 			res = FuriaConstructDefinitions.FURIA_flongConstant + "(" + formatNum((LongConstant)x) + ")";
1162 			
1163 		}else if(x instanceof ClassConstant){
1164 			
1165 			res = FuriaConstructDefinitions.FURIA_fclassConstant + "(" + ")";
1166 			
1167 		}else {
1168 			String msg = "This value is not Q-able: " + x
1169 					+ " Value class name: " + x.getClass().getName();
1170 			logger.error(msg);
1171 			logger.error("This furia process is screwed up from now on!");
1172 			assert false : msg;
1173 		}
1174 
1175 		return res;
1176 	}
1177     
1178     public static String formatNum(LongConstant x){
1179     	float r = x.value;
1180     	return "" + r;
1181     }
1182     
1183     public static String formatNum(FloatConstant x){
1184     	float r = x.value;
1185     	return "" + r;
1186     }
1187     
1188     public static String formatNum(IntConstant x){
1189     	int r = x.value;
1190     	return "" + r;
1191     }
1192     
1193     public static String formatNum(DoubleConstant x){
1194     	double r = x.value;
1195     	return "" + r;
1196     }
1197     
1198 
1199     
1200     public static List convertUseBoxesListToValues(List useBoxes){
1201     	Iterator it = useBoxes.iterator();
1202     	List res = new LinkedList();
1203     	while(it.hasNext()){
1204     		Value v = ((ValueBox)it.next()).getValue();
1205     		res.add(v);
1206     	}
1207     	return res;
1208     }
1209     
1210 }