def dumpFuncs(self): meths = [] cases = [] args = ["PyObject", ("PyFrame", "frame")] access = "private static" callargs = [jast.Identifier("frame")] for name, funcid, code in self.funccodes: funcname = self.uniquename(name) meths.append(jast.Method(funcname, access, args, clean(code))) body = jast.Return(jast.InvokeStatic(self.name, funcname, callargs)) cases.append( [jast.IntegerConstant(funcid), jast.FreeBlock([body])]) defaultCase = jast.FreeBlock([jast.Return(jast.Null)]) switch = jast.Block( [jast.Switch(jast.Identifier('index'), cases, defaultCase)]) meths.insert( 0, jast.Method("call_function", "public", ["PyObject", ("int", "index"), ("PyFrame", "frame")], switch)) self.superclass = "PyFunctionTable" return meths
def callSuperMethod(self, name, supername, access, ret, sig, throws=[]): if self.issuperproxy: return self.supermethods[supername] = supername args = [typeName(ret)] argids = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = jast.Method(supername, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.statements.append(supermethod) return
def dumpCodes(self): self.constants.append([ "PyFunctionTable", self.getFunctionTable(), jast.New(self.name, []) ]) for label, name, code, frame in self.codes: code = frame.toCellPrepend(code) funcid = self.addFunctionCode(name, code) arglist = keyworddict = jast. False if frame.args_arglist(): arglist = jast. True if frame.args_keyworddict(): keyworddict = jast. True names = jast.StringArray(frame.getnames()) cellnames = StringArrayOrNull(frame.getcellnames()) freenames = StringArrayOrNull(frame.getfreenames()) npurecell = frame.get_npurecell() cargs = [ jast.IntegerConstant(frame.args_count()), names, jast.StringConstant(self.filename), jast.StringConstant(name), arglist, keyworddict, self.getFunctionTable(), jast.IntegerConstant(funcid), cellnames, freenames, jast.IntegerConstant(npurecell), jast.IntegerConstant((frame.opt_globals and CO_OPTIMIZED) | ( frame.scope.nested_scopes and CO_NESTED)) ] newcode = jast.InvokeStatic("Py", "newCode", cargs) self.constants.append(("PyCode", jast.Identifier(label), newcode))
def for_stmt(self, index, sequence, body, else_body=None): counter = self.frame.gettemp('int') item = self.factory.makePyObject(self.frame.gettemp("PyObject")) seq = self.frame.gettemp("PyObject") init = [] init.append(jast.Set(counter, jast.IntegerConstant(0))) init.append(jast.Set(seq, self.visit(sequence).asAny())) counter_inc = jast.PostOperation(counter, '++') test = jast.Set(item.asAny(), jast.Invoke(seq, "__finditem__", [counter_inc])) test = jast.Operation('!=', test, jast.Identifier('null')) suite = [] suite.append(self.set(index, item)) suite.append(self.visit(body)) suite = jast.Block(suite) if else_body is not None: else_body = jast.Block(self.visit(else_body)) wtmp = self.frame.gettemp('boolean') ret = [init, jast.WhileElse(test, suite, else_body, wtmp)] self.frame.freetemp(wtmp) return ret else: return [init, jast.While(test, suite)]
def makeClassFile(self): header = [] if self.package is not None: header.append(jast.Identifier("package %s" % self.package)) header.append(jast.Blank) header.append(jast.Import("org.python.core.*")) header.append(jast.Blank) return jast.FreeBlock([header, self.makeClass()])
def getConstant(self, value, code, prefix): if self.constantValues.has_key((value, prefix)): return self.constantValues[(value, prefix)] name = prefix + "$" + str(len(self.constants)) ret = jast.Identifier(name) self.constantValues[(value, prefix)] = ret self.constants.append(("PyObject", ret, code)) return ret
def callConstructor(self, access, sig, throws=[], dosuper=1): args = [] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if dosuper: supercall = jast.InvokeLocal("super", argids) else: supercall = jast.InvokeLocal("super", []) ## for saccess, ssig in self.jconstructors: ## if len(ssig) == len(sig): ## supercall = jast.InvokeLocal("super", argids) ## break ## else: ## supercall = jast.InvokeLocal("super", []) frozen = self.module.getFrozen() initargs = [objects] initproxy = jast.InvokeLocal("__initProxy__", initargs) code = jast.Block([supercall, initproxy]) self.statements.append( jast.Constructor(self.name, jast.Modifier.ModifierString(access), args, code, throws))
def addPyProxyInterface(self): self.statements.append( jast.Declare('private PyInstance', jast.Identifier('__proxy'))) code = jast.Set(jast.Identifier("__proxy"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPyInstance", "public", ["void", ("PyInstance", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__proxy"))]) self.statements.append( jast.Method("_getPyInstance", "public", ["PyInstance"], code)) self.statements.append( jast.Declare('private PySystemState', jast.Identifier('__sysstate'))) code = jast.Set(jast.Identifier("__sysstate"), jast.Identifier("inst")) code = jast.Block([code]) self.statements.append( jast.Method("_setPySystemState", "public", ["void", ("PySystemState", "inst")], code)) code = jast.Block([jast.Return(jast.Identifier("__sysstate"))]) self.statements.append( jast.Method("_getPySystemState", "public", ["PySystemState"], code)) frozen = self.module.getFrozen() this = jast.Identifier("this") initargs = [ this, jast.StringConstant(self.modname), jast.StringConstant(self.name), jast.Identifier("args"), self.packages, self.properties, frozen, jast.StringArray(self.modules) ] initproxy = jast.InvokeStatic("Py", "initProxy", initargs) code = jast.Block([initproxy]) self.statements.append( jast.Method("__initProxy__", "public", ["void", ("Object[]", "args")], code)) self.interfaces.append(org.python.core.PyProxy)
def wrapThrows(stmt, throws, retType): if len(throws) == 0: return stmt catches = [] throwableFound = 0 for i in range(len(throws)): throw = throws[i] exctype = throw excname = jast.Identifier("exc%d" % i) body = jast.Block([jast.Throw(excname)]) catches.append((exctype, excname, body)) if throw == "java.lang.Throwable": throwableFound = 1 if not throwableFound: body = jast.Block([ jast.Invoke(jast.Identifier("inst"), "_jthrow", [jast.Identifier("t")]), nullReturn(retType) ]) catches.append(("java.lang.Throwable", jast.Identifier("t"), body)) return jast.TryCatches(jast.Block([stmt]), catches)
def getDeclarations(self): if len(self.temporaries) == 0: return [] decs = [jast.SimpleComment("Temporary Variables")] for type, temps in self.temporaries.items(): names = [] for index in range(len(temps)): names.append("t$%d$%s" % (index, type.replace("[]", "__"))) ident = "%s %s" % (type, COMMASPACE.join(names)) decs.append(jast.Identifier(ident)) decs.append(jast.Blank) return decs
def getNew(self): args = [jast.StringConstant(self.name), PyObjectArray(self.bases), self.pycode, jast.Null] if self.isSuperclassJava(): args.append(jast.Identifier("%s.class" % self.proxyname)) clos = self.def_compiler.frame.makeClosure(self.scope) if clos: args.append(PyObjectArray(clos)) return jast.InvokeStatic("Py", "makeClass", args)
def addClassDictInit(self): self.interfaces.append(org.python.core.ClassDictInit) namelist = jast.InvokeStatic( "Py", "java2py", [jast.StringArray(self.supermethods.keys())]) code = jast.Invoke(jast.Identifier("dict"), "__setitem__", [jast.StringConstant("__supernames__"), namelist]) code = jast.Block([code]) self.statements.append( jast.Method("classDictInit", "static public", ["void", ("PyObject", "dict")], code))
def visitListComp(self, node): # Since this code generated here is placed in its own # java method, we need a new set of temp vrbls. oldtmps = self.frame.temporaries self.frame.temporaries = {} lst = self.factory.makeList([]) lsttmp, lstcode = self.makeTemp(lst) append = self.factory.makePyObject(jast.Invoke(lsttmp.asAny(), "__getattr__", [jast.StringConstant("append")])) appendtmp, appendcode = self.makeTemp(append) self.list_comprehension_count += 1 tmp_append = "_[%d]" % self.list_comprehension_count #tmp_append = "_[1]" n = ast.Expr(ast.Call(ast.Name(tmp_append, ast.Name.Load, node), [ node.elt ], [], None, None, node), node); for lc in node.generators[::-1]: for ifs in lc.ifs[::-1]: n = ast.If(ifs, [ n ], None, ifs); n = ast.For(lc.target, lc.iter, [ n ], None, lc); #visit(new Delete(new exprType[] { new Name(tmp_append, Name.Del) })); stmts = [ lstcode ] stmts.append(self.set_name(tmp_append, append)) #stmts.append(appendcode) stmts.append(self.visit(n)) stmts.append(jast.Return(lsttmp.asAny())) decs = self.frame.getDeclarations() if len(decs) != 0: stmts.insert(0, decs) idx = self.module.addFunctionCode("__listcomprehension", jast.Block(stmts)) self.freeTemp(lsttmp) self.freeTemp(appendtmp) self.frame.temporaries = oldtmps return self.factory.makePyObject( jast.InvokeLocal("__listcomprehension$%d" % (idx+1), [jast.Identifier("frame")]))
def __init__(self, compiler, scope=None): self.frame = jast.Identifier("frame") self.compiler = compiler self.names = {} self.temporaries = {} self.scope = scope self.fast_locals = 0 self.opt_globals = 0
def gettemp(self, type): temps = self.gettemps(type) index = 0 while index < len(temps): if temps[index] is None: break index = index + 1 if index == len(temps): temps.append(None) tname = "t$%d$%s" % (index, type.replace("[]", "__")) temp = jast.Identifier(tname) temp.type = type temps[index] = temp #print 'get temp', index, type, temps return temp
def dumpInitModule(self): meths = [] dict = jast.Identifier("dict") sargs = [ jast.StringConstant("__name__"), jast.New("PyString", [jast.StringConstant(self.name)]) ] rargs = [ jast.Invoke(jast.New("_PyInner", []), "getMain", []), dict, dict ] code = jast.Block([ jast.Invoke(dict, "__setitem__", sargs), jast.InvokeStatic("Py", "runCode", rargs) ]) meths.append( jast.Method("moduleDictInit", "public static", ["void", ("PyObject", "dict")], code)) return meths
def importfrom_stmt(self, top, names): module = self.get_module(top, 0) if names == '*': return self.importall_stmt(module) #print 'import * from', module #names = module.dir() modnames = [] asnames = [] for modname, asname in names: if asname is None: asname = modname asnames.append(asname) modnames.append(modname) topmodname = jast.StringConstant(".".join(top)) modnamesArray = jast.FilledArray( "String", map(lambda x: jast.StringConstant(x), modnames)) do_import = jast.InvokeStatic( "org.python.core.imp", "importFrom", [topmodname, modnamesArray, self.frame.frame]) if not self.imp_accu: imp_accu = self.imp_accu = jast.Identifier("imp_accu") self.makeFreeDecl("PyObject[]", imp_accu) else: imp_accu = self.imp_accu stmts = [jast.Set(imp_accu, do_import)] for i in range(len(asnames)): asname = asnames[i] modname = modnames[i] code = jast.Subscript(imp_accu, i) stmts.append( self.set_name(asname, module.getattr(modname).makeReference(code))) stmts.append(jast.Set(imp_accu, jast.Null)) return stmts
def list_comprehension(self, node): # Since this code generated here is placed in its own # java method, we need a new set of temp vrbls. oldtmps = self.frame.temporaries self.frame.temporaries = {} expr = node.getChild(0) suite = node.getChild(1) lst = self.factory.makeList([]) lsttmp, lstcode = self.makeTemp(lst) append = self.factory.makePyObject( jast.Invoke(lsttmp.asAny(), "__getattr__", [jast.StringConstant("append")])) appendtmp, appendcode = self.makeTemp(append) self.listComprehensionStack.append((appendtmp, expr)) stmts = [lstcode, appendcode] stmts.append(self.visit(suite)) stmts.append(jast.Return(lsttmp.asAny())) decs = self.frame.getDeclarations() if len(decs) != 0: stmts.insert(0, decs) self.listComprehensionStack.pop(-1) idx = self.module.addFunctionCode("__listcomprehension", jast.Block(stmts)) self.freeTemp(lsttmp) self.freeTemp(appendtmp) self.frame.temporaries = oldtmps return self.factory.makePyObject( jast.InvokeLocal("__listcomprehension$%d" % (idx + 1), [jast.Identifier("frame")]))
def compile(self, data, filename, name): if self.javapackage is not None: name = self.javapackage + '.' + name data = data + '\n\n' mod = PythonModule(name, filename, frozen=self.deep) fact = ObjectFactory() pi = SrcGenCompiler(mod, fact, options=self.options) fact.parent = pi code = pi.execstring(data) # __file__ code.insert( 0, jast.Invoke(jast.Identifier('frame'), "setglobal", [ jast.StringConstant('__file__'), mod.getStringConstant(filename) ])) code.insert(1, jast.BlankLine()) code = jast.Block(code) mod.addMain(code, pi) self.addDependencies(mod) return mod
def visitFor(self, node): iter = self.frame.gettemp('PyObject') item = self.factory.makePyObject(self.frame.gettemp("PyObject")) seq = self.visit(node.iter).asAny() init = [] init.append(jast.Set(iter, jast.Invoke(seq, "__iter__", []))) test = jast.Set(item.asAny(), jast.Invoke(iter, "__iternext__", [])) test = jast.Operation('!=', test, jast.Identifier('null')) suite = [] suite.append(self.set(node.target, item)) suite.append(self.suite(node.body)) suite = jast.Block(suite) if node.orelse is not None: orelse = jast.Block(self.suite(node.orelse)) wtmp = self.frame.gettemp('boolean') ret = [init, jast.WhileElse(test, suite, orelse, wtmp)] self.frame.freetemp(wtmp) return ret else: return [init, jast.While(test, suite)]
def dumpMain(self): meths = [] if self.javamain: code = [] newargs = jast.Identifier("newargs") code.append( jast.Declare("String[]", newargs, jast.NewArray("String", ["args.length+1"]))) code.append( jast.Set(jast.Identifier("newargs[0]"), jast.StringConstant(self.name))) args = [ jast.Identifier('args'), jast.IntegerConstant(0), jast.Identifier('newargs'), jast.IntegerConstant(1), jast.Identifier('args.length') ] code.append( jast.InvokeStatic("java.lang.System", "arraycopy", args)) args = [ jast.GetStaticAttribute( self.getclassname(self.name + '.' + self.pyinner.name), "class"), jast.Identifier('newargs'), self.getPackages(qual=1), self.getMainProperties(qual=1), self.getFrozen(), jast.StringArray(self.modules.keys()) ] code.append([jast.InvokeStatic("Py", "runMain", args)]) maincode = jast.Block(code) meths.append( jast.Method("main", "public static", ["void", ("String[]", "args")], maincode, ["java.lang.Exception"])) return meths
def getPackages(self, qual=0): if qual: return jast.GetStaticAttribute(self.name, "jpy$packages") else: return jast.Identifier("jpy$packages")
def getProxyProperties(self, qual=0): if qual: return jast.GetStaticAttribute(self.name, "jpy$proxyProperties") else: return jast.Identifier("jpy$proxyProperties")
def getFunctionTable(self): return jast.Identifier("funcTable")
def getCodeConstant(self, name, code, frame): label = "c$%d_%s" % (len(self.codes), legalJavaName(name)) ret = jast.Identifier(label) self.codes.append((label, name, code, frame)) return ret
class Object: def __init__(self, code, istypes, astypes): self.code = code self.istypes = istypes self.astypes = astypes def isa(self, type): if type in self.istypes: return self.code def asa(self, type): if type in self.istypes: return self.code for astype, ascode in self.astypes: if astype == type: return ascode one = Object(jast.IntegerConstant(1), [java.lang.Integer.TYPE], [(org.python.core.PyObject, jast.Identifier("one"))]) foo = Object( jast.Identifier("foo"), [org.python.core.PyObject], [(java.lang.Integer.TYPE, jast.InvokeStatic("Py", "toint", [jast.Identifier("foo")]))]) print f.invoke(foo, [one]) print f.invoke(foo, [foo]) print call(org.python.core.Py.py2int, [one])
def visitTryExcept(self, node): if node.orelse is not None: elseBool = self.frame.gettemp("boolean") jbody = jast.Block(self.suite(node.body)) tests = [] ifelse = None tname = jast.Identifier("x$%d" % self.nthrowables) self.nthrowables = self.nthrowables + 1 exctmp = self.frame.gettemp("PyException") setexc = jast.Set(exctmp, jast.InvokeStatic("Py", "setException", [tname, self.frame.frame])) for exchandler in node.handlers: if exchandler.type is None: ifelse = jast.Block(self.suite(exchandler.body)) continue type = self.visit(exchandler.type) t = jast.InvokeStatic("Py", "matchException", [exctmp, type.asAny()]) newbody = [] if exchandler.name is not None: exceptionValue = self.factory.makePyObject( jast.GetInstanceAttribute(exctmp, "value")) newbody.append(self.set(exchandler.name, exceptionValue)) #print self.visit(ebody) newbody.append(self.suite(exchandler.body)) #print newbody #print jast.Block(newbody) tests.append( (t, jast.Block(newbody)) ) if ifelse is None: ifelse = jast.Throw(exctmp) if len(tests) == 0: catchBody = ifelse else: catchBody = jast.MultiIf(tests, ifelse) catchBody = [setexc, catchBody] if node.orelse is not None: catchBody = [jast.Set(elseBool, jast.False), catchBody] catchBody = jast.Block([catchBody]) self.frame.freetemp(exctmp) ret = jast.TryCatch(jbody, "Throwable", tname, catchBody) if node.orelse is not None: ret = jast.Block([jast.Set(elseBool, jast.True), ret, jast.If(elseBool, jast.Block(self.suite(node.orelse)))]) self.frame.freetemp(elseBool) return ret
def callMethod(self, name, access, ret, sig, throws=[], dosuper=1): args = [typeName(ret)] argids = [] objects = [] throws = filterThrows(throws) for c in sig: if isinstance(c, TupleType): argname = c[1] c = c[0] else: argname = "arg" + str(len(argids)) args.append((typeName(c), argname)) argid = jast.Identifier(argname) argids.append(argid) objects.append(makeObject(argid, c)) objects = jast.FilledArray("Object", objects) stmts = [] this = jast.Identifier("this") if isinstance(access, IntType) and isAbstract(access): dosuper = 0 access = access & ~ABSTRACT if not dosuper and not self.isAdapter: getattr = jast.InvokeStatic("Py", "jgetattr", [this, jast.StringConstant(name)]) else: getattr = jast.InvokeStatic("Py", "jfindattr", [this, jast.StringConstant(name)]) inst = jast.Identifier("inst") if len(throws) == 0: jcall = "_jcall" else: jcall = "_jcallexc" jcall = makeReturn(jast.Invoke(inst, jcall, [objects]), ret) jcall = wrapThrows(jcall, throws, ret) if dosuper: supercall = jast.Invoke(jast.Identifier("super"), name, argids) if ret != Void.TYPE: supercall = jast.Return(supercall) supermethod = None if not self.issuperproxy and name not in self.getCandSupermethods( incl=0): supermethod = jast.Method("super__" + name, jast.Modifier.ModifierString(access), args, jast.Block([supercall]), throws) self.supermethods["super__" + name] = "super__" + name else: if self.isAdapter: supercall = nullReturn(ret) else: supercall = None supermethod = None if not dosuper and not self.isAdapter: test = jcall else: test = jast.If(jast.Operation("!=", inst, jast.Null), jcall, supercall) code = jast.Block([jast.Declare("PyObject", inst, getattr), test]) meth = jast.Method(name, jast.Modifier.ModifierString(access), args, code, throws) if supermethod is not None: self.statements.append(supermethod) self.statements.append(meth)
ret = JavaString() elif type == org.python.core.PyObject: ret = PyObject() elif type == org.python.core.PyString: ret = PyObject() else: ret = JavaObject(type) types[type] = ret return ret Generic = findType(org.python.core.PyObject) IntType = findType(java.lang.Integer.TYPE) StringType = findType(java.lang.String) if __name__ == '__main__': foo = Object(jast.Identifier("foo"), Generic) one = Object(jast.IntegerConstant(1), IntType) hello = Object(jast.StringConstant("hello"), StringType) print foo, one, hello print foo.binop("add", foo) print foo.binop("add", one) print foo.binop("add", hello) print foo.nonzero() print foo.getitem(foo) print foo.getitem(one) print foo.call([one, hello]) print foo.call([one, hello, foo])