Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 9.81 KB | None | 0 0
  1. /*
  2.  * Created on 10 nov 2009
  3.  */
  4.  
  5. package interpreter.api.util;
  6.  
  7. import interpreter.api.ExecutionFrame;
  8. import interpreter.api.ExecutionThread;
  9. import interpreter.api.InterpreterProcess;
  10.  
  11. import java.lang.reflect.Array;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14.  
  15. public class InterpreterTypeUtil
  16. {
  17.    public static Class< ? > lookupClass(String classname)
  18.    {
  19.       classname = classname.replace('/', '.');
  20.  
  21.       try
  22.       {
  23.          return Class.forName(classname);
  24.       }
  25.       catch (Exception exc)
  26.       {
  27.          throw new IllegalStateException(exc);
  28.       }
  29.    }
  30.  
  31.    public static Object popValueFromStack(ExecutionFrame frame, Class< ? > type)
  32.    {
  33.       if (type.isPrimitive())
  34.       {
  35.          if (type == boolean.class)
  36.             return Boolean.valueOf(frame.popInt() != 0);
  37.          if (type == byte.class)
  38.             return Byte.valueOf((byte) frame.popInt());
  39.          if (type == short.class)
  40.             return Short.valueOf((short) frame.popInt());
  41.          if (type == char.class)
  42.             return Character.valueOf((char) frame.popInt());
  43.          if (type == int.class)
  44.             return Integer.valueOf(frame.popInt());
  45.          if (type == long.class)
  46.             return Long.valueOf(frame.popLong());
  47.          if (type == float.class)
  48.             return Float.valueOf(frame.popFloat());
  49.          if (type == double.class)
  50.             return Double.valueOf(frame.popDouble());
  51.       }
  52.       else if (type.isArray())
  53.       {
  54.          throw new IllegalStateException();
  55.       }
  56.  
  57.       int ref = frame.popReference();
  58.       if (InterpreterProcess.isJavaReference(ref))
  59.          return frame.thread.process.heap.lookupObject(ref);
  60.       return Integer.valueOf(ref);
  61.    }
  62.  
  63.    public static Object loadValueFromLocals(ExecutionFrame frame, int index, Class< ? > type)
  64.    {
  65.       if (type.isPrimitive())
  66.       {
  67.          if (type == boolean.class)
  68.             return Boolean.valueOf(frame.loadInt(index) != 0);
  69.          if (type == byte.class)
  70.             return Byte.valueOf((byte) frame.loadInt(index));
  71.          if (type == short.class)
  72.             return Short.valueOf((short) frame.loadInt(index));
  73.          if (type == char.class)
  74.             return Character.valueOf((char) frame.loadInt(index));
  75.          if (type == int.class)
  76.             return Integer.valueOf(frame.loadInt(index));
  77.          if (type == long.class)
  78.             return Long.valueOf(frame.loadLong(index));
  79.          if (type == float.class)
  80.             return Float.valueOf(frame.loadFloat(index));
  81.          if (type == double.class)
  82.             return Double.valueOf(frame.loadDouble(index));
  83.       }
  84.       else if (type.isArray())
  85.       {
  86.          throw new IllegalStateException();
  87.       }
  88.  
  89.       int ref = frame.loadReference(index);
  90.       if (InterpreterProcess.isJavaReference(ref))
  91.          return frame.thread.process.heap.lookupObject(ref);
  92.       return Integer.valueOf(ref);
  93.    }
  94.  
  95.    public static Object[] loadValuesFromLocals(ExecutionFrame frame, boolean isStatic, Class< ? >[] paramTypes)
  96.    {
  97.       Object[] args = new Object[paramTypes.length];
  98.       for (int i = 0; i < args.length; i++)
  99.          args[i] = loadValueFromLocals(frame, (isStatic ? 0 : 1) + i, paramTypes[i]);
  100.       return args;
  101.    }
  102.  
  103.    public static Object[] popValuesFromStack(ExecutionFrame frame, Class< ? >[] paramTypes)
  104.    {
  105.       Object[] args = new Object[paramTypes.length];
  106.       for (int i = args.length - 1; i >= 0; i--)
  107.          args[i] = popValueFromStack(frame, paramTypes[i]);
  108.       return args;
  109.    }
  110.  
  111.    public static boolean isNarrow(Class< ? > type)
  112.    {
  113.       return !(type == long.class || type == double.class);
  114.    }
  115.  
  116.    public static void fromCallSiteStackToTargetLocal(ExecutionFrame frame, Class< ? >[] paramTypes, ExecutionFrame target, boolean isStatic)
  117.    {
  118.       for (int i = paramTypes.length - 1, t = (isStatic ? 0 : 1); i >= 0; i--, t++)
  119.       {
  120.          Class< ? > pt = paramTypes[i];
  121.          if (isNarrow(pt))
  122.             target.storeInt(t, frame.popInt());
  123.          else
  124.             target.storeLong(t, frame.popLong());
  125.       }
  126.  
  127.       if (!isStatic)
  128.       {
  129.          int self = frame.popReference();
  130.  
  131.          if (InterpreterProcess.isJavaReference(self))
  132.          {
  133.             if (frame.thread.process.heap.lookupObject(self) == null)
  134.                throw new NullPointerException();
  135.          }
  136.  
  137.          target.storeReference(0, self);
  138.       }
  139.    }
  140.  
  141.    public static void pushTypedValueOnStack(ExecutionFrame frame, Class< ? > type, Object value)
  142.    {
  143.       if (type == void.class)
  144.       {
  145.          return;
  146.       }
  147.  
  148.       if (value == null)
  149.       {
  150.          frame.pushReference(0);
  151.       }
  152.       else if (type.isPrimitive())
  153.       {
  154.          if (type == boolean.class)
  155.             frame.pushInt(((Boolean) value).booleanValue() ? 1 : 0);
  156.          else if (type == byte.class)
  157.             frame.pushInt(((Byte) value).byteValue());
  158.          else if (type == short.class)
  159.             frame.pushInt(((Short) value).shortValue());
  160.          else if (type == char.class)
  161.             frame.pushInt(((Character) value).charValue());
  162.          else if (type == int.class)
  163.             frame.pushInt(((Integer) value).intValue());
  164.          else if (type == long.class)
  165.             frame.pushLong(((Long) value).longValue());
  166.          else if (type == float.class)
  167.             frame.pushFloat(((Float) value).floatValue());
  168.          else if (type == double.class)
  169.             frame.pushDouble(((Double) value).doubleValue());
  170.       }
  171.       else if (type.isArray())
  172.       {
  173.          throw new IllegalStateException();
  174.       }
  175.       else
  176.       {
  177.          int ref = ((Integer) value).intValue();
  178.  
  179.          if (InterpreterProcess.isJavaReference(ref))
  180.          {
  181.             frame.thread.process.heap.lookupObject(ref); // TODO
  182.          }
  183.  
  184.          frame.pushReference(ref);
  185.       }
  186.    }
  187.  
  188.    public static void returnTypedValue(ExecutionThread stack, Class< ? > type)
  189.    {
  190.       if (type == void.class)
  191.       {
  192.          stack.vreturn();
  193.          return;
  194.       }
  195.  
  196.       if (type.isPrimitive())
  197.       {
  198.          if (isNarrow(type))
  199.             stack.ireturn();
  200.          else
  201.             stack.lreturn();
  202.  
  203.          //if (type == boolean.class)
  204.          //  stack.ireturn();
  205.          //else if (type == byte.class)
  206.          //   stack.ireturn();
  207.          //else if (type == short.class)
  208.          //   stack.ireturn();
  209.          // else if (type == char.class)
  210.          //   stack.ireturn();
  211.          //else if (type == int.class)
  212.          //   stack.ireturn();
  213.          //else if (type == long.class)
  214.          //   stack.lreturn();
  215.          //else if (type == float.class)
  216.          //   stack.freturn();
  217.          //else if (type == double.class)
  218.          //   stack.dreturn();
  219.       }
  220.       else if (type.isArray())
  221.       {
  222.          throw new IllegalStateException();
  223.       }
  224.       else
  225.       {
  226.          stack.areturn();
  227.       }
  228.    }
  229.  
  230.    // ------
  231.  
  232.    public static String typeToTightString(Class< ? > type)
  233.    {
  234.       if (type == void.class)
  235.          return "V";
  236.       if (type == boolean.class)
  237.          return "Z";
  238.       if (type == byte.class)
  239.          return "B";
  240.       if (type == short.class)
  241.          return "S";
  242.       if (type == char.class)
  243.          return "C";
  244.       if (type == int.class)
  245.          return "I";
  246.       if (type == long.class)
  247.          return "J";
  248.       if (type == float.class)
  249.          return "F";
  250.       if (type == double.class)
  251.          return "D";
  252.       return "L" + type.getName() + ";";
  253.    }
  254.  
  255.    public static Class< ? > returnTypeStringToType(String returnType)
  256.    {
  257.       Class< ? >[] array = InterpreterTypeUtil.parameterTypeStringToTypes(returnType);
  258.       if (array.length != 1)
  259.          throw new IllegalStateException();
  260.       return array[0];
  261.    }
  262.  
  263.    public static Class< ? >[] parameterTypeStringToTypes(String parameters)
  264.    {
  265.       List<Class< ? >> params = new ArrayList<Class< ? >>();
  266.  
  267.       StringBuilder nameBuilder = null;
  268.       int dim = 0;
  269.       for (char c : parameters.toCharArray())
  270.       {
  271.          if (c == '[')
  272.          {
  273.             dim++;
  274.             continue;
  275.          }
  276.  
  277.          Class< ? > ptype = null;
  278.  
  279.          if (nameBuilder != null)
  280.          {
  281.             if (c != ';')
  282.             {
  283.                nameBuilder.append(c);
  284.                continue;
  285.             }
  286.  
  287.             ptype = lookupClass(nameBuilder.toString());
  288.             nameBuilder = null;
  289.          }
  290.  
  291.          if (ptype == null)
  292.          {
  293.             if (c == 'V')
  294.                ptype = void.class;
  295.             else if (c == 'Z')
  296.                ptype = boolean.class;
  297.             else if (c == 'B')
  298.                ptype = byte.class;
  299.             else if (c == 'S')
  300.                ptype = short.class;
  301.             else if (c == 'C')
  302.                ptype = char.class;
  303.             else if (c == 'I')
  304.                ptype = int.class;
  305.             else if (c == 'J')
  306.                ptype = long.class;
  307.             else if (c == 'F')
  308.                ptype = float.class;
  309.             else if (c == 'D')
  310.                ptype = double.class;
  311.             else if (c == 'L') // class type
  312.             {
  313.                if (nameBuilder != null)
  314.                   throw new IllegalStateException();
  315.                nameBuilder = new StringBuilder();
  316.                continue;
  317.             }
  318.             else
  319.             {
  320.                throw new IllegalStateException("c=" + c + ", p=" + parameters);
  321.             }
  322.          }
  323.  
  324.          for (int i = 0; i < dim; i++)
  325.             ptype = Array.newInstance(ptype, 0).getClass();
  326.          params.add(ptype);
  327.          dim = 0;
  328.       }
  329.  
  330.       if (dim != 0)
  331.          throw new IllegalStateException();
  332.       if (nameBuilder != null)
  333.          throw new IllegalStateException();
  334.  
  335.       return params.toArray(new Class[params.size()]);
  336.    }
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement