Commits

Shashank Bharadwaj committed feaf935

cleaned up code, should now compile

  • Participants
  • Parent commits abce518

Comments (0)

Files changed (6)

File src/org/python/core/PyCode.java

 
 import java.lang.invoke.MethodHandle;
 
-import org.python.compiler.typechecker.Type;
+import org.python.core.opt.SpecializeCallSite;
 import org.python.core.opt.Threesome;
 
 /**
     public class RuntimeInfo {
         public MethodHandle mHandle = null;
         public boolean frameless = false;
-        public Threesome castType = null;
+        public Threesome cast = null;
+        public boolean castDone = false;
+
+        public void updateThreesome(Threesome other) {
+            if (this.cast == null) {
+                this.cast = other;
+            } else {
+                this.cast.merge(other);
+                castDone = false;
+            }
+        }
+
+        public MethodHandle getMHWithCast(MethodHandle target) throws Exception {
+            if (target == null) { // Update the casts using internal mh
+                if (cast != null && !castDone) {
+                    mHandle = SpecializeCallSite.insertFunctionCast(mHandle, cast);
+                    castDone = true;
+                }
+                return mHandle;
+            } else {
+                if (cast != null) {
+                    return SpecializeCallSite.insertFunctionCast(target, cast);
+                }
+                return target;
+            }
+        }
     }
     
     public RuntimeInfo runtimeInfo = new RuntimeInfo();

File src/org/python/core/PyFunction.java

         return frame;
     }
     
-    
-    public Threesome type;
-    
     public PyFunction clone() {
         PyObject[] closures = func_closure == null ? null : ((PyTuple) func_closure).getArray();
         PyFunction ret = new PyFunction(func_globals, func_defaults, func_code.clone(), __doc__,
                 closures);
         return ret;
     }
-    /**
-     * Helper function to set the gradual-type of this function. 
-     * @return the current object so that it's easier to thread things.
-     * @throws Exception 
-     */
-    public PyFunction setThreesome(String type) throws Exception {
-    	System.err.println("Pyfunction: got: " + type);
-    	this.type = TypeUtil.getGradualType(type);
-    	System.err.println("set gType to: " + this.type);
-    	return this;
-	}
     
 }

File src/org/python/core/PyMethodCode.java

         if(args != null){
             arglen = args.length;
         }
+//        MethodHandle mHandle = runtimeInfo.getMHWithCast(null);
         MethodHandle mHandle = runtimeInfo.mHandle;
-        if(runtimeInfo.castType != null){
-            // System.err.println("PyMethodCode: " + runtimeInfo.castType);
-            mHandle = SpecializeCallSite.insertFunctionCast(mHandle, runtimeInfo.castType);
+        if(runtimeInfo.cast != null){
+             System.err.println("PyMethodCode: " + runtimeInfo.cast + "handle " + mHandle);
+                mHandle = SpecializeCallSite.insertFunctionCast(mHandle, runtimeInfo.cast);
         }
+
 //        Py.writeWarning("PyMethodCode:" + co_name, "callUsingMH:args: " + arglen);
         switch(arglen){
             case 0:
 
     @Override
     public PyCode clone() {
-        return new PyMethodCode(co_argcount, co_varnames, co_filename, co_name, co_firstlineno,
+        PyMethodCode ret = new PyMethodCode(co_argcount, co_varnames, co_filename, co_name, co_firstlineno,
                 varargs, varkwargs, funcs, func_id, co_cellvars, co_freevars, jy_npurecell,
                 co_flags.toBits(), runtimeInfo.mHandle, runtimeInfo.frameless);
+        ret.runtimeInfo.cast = this.runtimeInfo.cast;
+        return ret;
 
     }
 }

File src/org/python/core/RuntimeCasts.java

 package org.python.core;
 
-import java.lang.invoke.MethodHandle;
-
-import org.python.core.opt.SpecializeCallSite;
 import org.python.core.opt.Threesome;
 import org.python.core.opt.TypeUtil;
 
 public class RuntimeCasts {
 
     public static PyFunction functionCast(PyFunction input, String gradualCast) throws Exception {
-//        System.err.println("Gradual Cast: " + gradualCast);
+        // System.err.println("Gradual Cast: " + gradualCast);
         Threesome cast = TypeUtil.getGradualType(gradualCast);
-        PyFunction retFunction = new PyCastedFunction(input);
-        if(retFunction.type == null){
-            retFunction.type = cast;
-        } else {
-            retFunction.type = Threesome.merge(retFunction.type, cast);
-        }
-        retFunction.func_code.runtimeInfo.castType = retFunction.type;
-//        MethodHandle inHandler = input.func_code.mHandle;
-//        System.err.println("in handle: " + inHandler);
-//        retFunction.func_code.mHandle = SpecializeCallSite.insertFunctionCast(inHandler, cast);
-//        System.err.println("out handle: " + retFunction.func_code.mHandle);
+        PyFunction retFunction = input.clone();
+        retFunction.func_code.runtimeInfo.updateThreesome(cast);
         return retFunction;
     }
 

File src/org/python/core/opt/SpecializeCallSite.java

 	                    MethodHandle target = getCachedMH(callSite, pyFunc);
 	                    // System.err.println("Target type: " + target);
 	                    target = target.bindTo(callSite);
-	                    if(pyFunc.type != null){
-	                    	target = insertFunctionCast(target, pyFunc.type);
-	                    }
+	                    target = pyFunc.func_code.runtimeInfo.getMHWithCast(target);
 	                    target = target.asType(callSite.type());
 	                    // System.err.println("Target: " + target + " callsite type: " + callSite.type());
 	                    
     	return fallback(callSite, args);
     }
     
-    private static MethodHandle getCachedMH(JythonCallsite callSite, PyFunction pyFunc) throws NoSuchMethodException, IllegalAccessException {
+    private static MethodHandle getCachedMH(JythonCallsite callSite, PyFunction pyFunc) throws Exception {
         MethodHandle target = cachedMHs.get(Py.id(pyFunc));
         if (target == null) {
             target = pyFunc.func_code.runtimeInfo.mHandle;
                 // target);
                 target = MethodHandles.dropArguments(target, 0, JythonCallsite.class);
             }
-            // }
             callSite.funcId = Py.id(pyFunc);
             cachedMHs.put(Py.id(pyFunc), target);
-            if (pyFunc instanceof PyCastedFunction) {
-                target = SpecializeCallSite.insertFunctionCast(target, pyFunc.type);
-                // System.err.println("casted return is: " + target);
-            }
+            target = pyFunc.func_code.runtimeInfo.getMHWithCast(target);
         }
         if (callSite.funcId == 0) {
             callSite.funcId = Py.id(pyFunc);

File src/org/python/core/opt/Threesome.java

                 .append(toType).toString();
     }
 
-    public final void merge(Threesome other) {
+    public void merge(Threesome other) {
         assert this.toType == other.fromType;
         this.toType = other.toType;
         this.midType = merge(this.midType, other.midType);
     }
 
     public static final Threesome merge(Threesome one, Threesome two) {
-        // System.err.println("one: " + one);
-        // System.err.println("two: " + two);
         assert one.toType == two.fromType;
         Type finalToType = two.toType;
         Type finalFromType = one.fromType;
         Type finalMidType = merge(one.midType, two.midType);
         Threesome merged = new Threesome(finalFromType, finalToType, finalMidType);
-        // System.err.println("out: " + merged);
         return merged;
     }