00001 package edu.ksu.cis.bandera.birc;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 import ca.mcgill.sable.soot.SootMethod;
00036 import ca.mcgill.sable.soot.SootField;
00037
00038 import java.util.*;
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 public class JimpleLiteralPrinter implements JimpleLiteralSwitch {
00049
00050 Hashtable objectTable =
00051 new Hashtable();
00052 int objectCount = 0;
00053 Vector printQueue = new Vector();
00054 String prefix;
00055
00056 public JimpleLiteralPrinter(String prefix) {
00057 this.prefix = prefix;
00058 }
00059 public void caseArrayLiteral(ArrayLiteral literal) {
00060 System.out.print("[ ");
00061 for (int i = 0; i < literal.length; i++) {
00062 if (i > 0)
00063 System.out.print(", ");
00064 literal.contents[i].apply(this);
00065 }
00066 System.out.print(" ]");
00067 }
00068 public void caseBooleanLiteral(BooleanLiteral literal) {
00069 System.out.print("" + literal.value);
00070 }
00071 public void caseClassLiteral(ClassLiteral literal) {
00072 Vector fields = literal.getFields();
00073 String className = literal.getSource().getName();
00074 System.out.print(className + " { ");
00075 for (int i = 0; i < fields.size(); i++) {
00076 SootField field = (SootField) fields.elementAt(i);
00077 if (i > 0)
00078 System.out.print(", ");
00079 System.out.print(field.getName() + " = ");
00080 literal.getFieldValue(field).apply(this);
00081 }
00082 System.out.print(" } ");
00083 LockLiteral lock = literal.getLockValue();
00084 if (lock != null)
00085 lock.apply(this);
00086 }
00087 public void caseIntegerLiteral(IntegerLiteral literal) {
00088 System.out.print("" + literal.value);
00089 }
00090 public void caseLockLiteral(LockLiteral literal) {
00091 SootMethod method = literal.getHoldingThread();
00092 String holding = "-";
00093 if (method != null)
00094 holding = method.getDeclaringClass().getName();
00095 String waiting = ";";
00096 for (int i = 0; i < literal.getNumWaiting(); i++) {
00097 if (i > 0)
00098 waiting += ",";
00099 waiting +=
00100 literal.getWaitingThread(i).getDeclaringClass().getName();
00101 }
00102 System.out.print("<" + holding + waiting + ">");
00103 }
00104 public void caseReferenceLiteral(ReferenceLiteral literal) {
00105 if (literal.value == null)
00106 System.out.print("null");
00107 else {
00108 Integer objectNum = (Integer) objectTable.get(literal.value);
00109 if (objectNum == null) {
00110 objectNum = new Integer(++objectCount);
00111 objectTable.put(literal.value,objectNum);
00112 printQueue.addElement(literal.value);
00113 }
00114
00115 int objectID = ((ObjectLiteral)literal.value).getId();
00116 System.out.print("#" + objectID);
00117 }
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 public void flush() {
00129 while (printQueue.size() > 0) {
00130 ObjectLiteral lit = (ObjectLiteral) printQueue.elementAt(0);
00131 printQueue.removeElementAt(0);
00132 System.out.print(prefix + "#" + lit.getId() + " = ");
00133 lit.apply(this);
00134 System.out.println();
00135 }
00136 }
00137 }