PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/translator/jvm/genjvm.py

https://bitbucket.org/quangquach/pypy
Python | 303 lines | 276 code | 12 blank | 15 comment | 0 complexity | a4d1f6ab3955b092b895f44ee0431d13 MD5 | raw file
  1. """
  2. Backend for the JVM.
  3. """
  4. from __future__ import with_statement
  5. import os
  6. import re
  7. import subprocess
  8. import sys
  9. import py
  10. from pypy.tool.udir import udir
  11. from pypy.translator.translator import TranslationContext
  12. from pypy.translator.oosupport.genoo import GenOO
  13. from pypy.translator.backendopt.all import backend_optimizations
  14. from pypy.translator.backendopt.checkvirtual import check_virtual_methods
  15. from pypy.translator.jvm.generator import JasminGenerator
  16. from pypy.translator.jvm.option import getoption
  17. from pypy.translator.jvm.database import Database
  18. from pypy.translator.jvm.log import log
  19. from pypy.translator.jvm.node import EntryPoint, Function
  20. from pypy.translator.jvm.opcodes import opcodes
  21. from pypy.rpython.ootypesystem import ootype
  22. from pypy.translator.jvm.constant import \
  23. JVMConstantGenerator, JVMStaticMethodConst, JVMCustomDictConst, \
  24. JVMWeakRefConst
  25. from pypy.translator.jvm.prebuiltnodes import create_interlink_node
  26. MIN_JAVA_VERSION = '1.6.0'
  27. class JvmError(Exception):
  28. """ Indicates an error occurred in JVM backend """
  29. def pretty_print(self):
  30. print str(self)
  31. pass
  32. class JvmSubprogramError(JvmError):
  33. """ Indicates an error occurred running some program """
  34. def __init__(self, res, args, stdout, stderr):
  35. self.res = res
  36. self.args = args
  37. self.stdout = stdout
  38. self.stderr = stderr
  39. def __str__(self):
  40. return "Error code %d running %s" % (self.res, repr(self.args))
  41. def pretty_print(self):
  42. JvmError.pretty_print(self)
  43. print "vvv Stdout vvv\n"
  44. print self.stdout
  45. print "vvv Stderr vvv\n"
  46. print self.stderr
  47. class JvmGeneratedSource(object):
  48. """
  49. An object which represents the generated sources. Contains methods
  50. to find out where they are located, to compile them, and to execute
  51. them.
  52. For those interested in the location of the files, the following
  53. attributes exist:
  54. tmpdir --- root directory from which all files can be found (py.path obj)
  55. javadir --- the directory containing *.java (py.path obj)
  56. classdir --- the directory where *.class will be generated (py.path obj)
  57. package --- a string with the name of the package (i.e., 'java.util')
  58. The following attributes also exist to find the state of the sources:
  59. compiled --- True once the sources have been compiled successfully
  60. """
  61. _cached = None
  62. def __init__(self, tmpdir, package):
  63. """
  64. 'tmpdir' --- the base directory where the sources are located
  65. 'package' --- the package the sources are in; if package is pypy.jvm,
  66. then we expect to find the sources in $tmpdir/pypy/jvm
  67. 'jfiles' --- list of files we need to run jasmin on
  68. """
  69. self.tmpdir = tmpdir
  70. self.package = package
  71. self.compiled = False
  72. self.jasmin_files = None
  73. # Determine various paths:
  74. self.thisdir = py.path.local(__file__).dirpath()
  75. self.rootdir = self.thisdir.join('src')
  76. self.srcdir = self.rootdir.join('pypy')
  77. self.jnajar = self.rootdir.join('jna.jar')
  78. self.jasminjar = self.rootdir.join('jasmin.jar')
  79. # Compute directory where .j files are
  80. self.javadir = self.tmpdir
  81. for subpkg in package.split('.'):
  82. self.javadir = self.javadir.join(subpkg)
  83. # Compute directory where .class files should go
  84. self.classdir = self.javadir
  85. def set_jasmin_files(self, jfiles):
  86. self.jasmin_files = jfiles
  87. def _invoke(self, args, allow_stderr):
  88. import sys
  89. if sys.platform == 'nt':
  90. shell = True
  91. else:
  92. shell = False
  93. subp = subprocess.Popen(
  94. args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  95. shell=shell, universal_newlines=True)
  96. stdout, stderr = subp.communicate()
  97. res = subp.wait()
  98. if res or (not allow_stderr and stderr):
  99. raise JvmSubprogramError(res, args, stdout, stderr)
  100. return stdout, stderr, res
  101. def _compile_helper(self):
  102. # HACK: compile the Java helper classes. Should eventually
  103. # use rte.py
  104. if JvmGeneratedSource._cached == self.classdir:
  105. return
  106. log.red('Compiling java classes')
  107. javafiles = self.srcdir.listdir('*.java')
  108. javasrcs = [str(jf) for jf in javafiles]
  109. self._invoke([getoption('javac'),
  110. '-nowarn',
  111. '-d', str(self.classdir),
  112. '-classpath', str(self.jnajar),
  113. ] + javasrcs,
  114. True)
  115. # NOTE if you are trying to add more caching: some .java files
  116. # compile to several .class files of various names.
  117. JvmGeneratedSource._cached = self.classdir
  118. def compile(self):
  119. """
  120. Compiles the .java sources into .class files, ready for execution.
  121. """
  122. jascmd = [
  123. getoption('java'),
  124. '-jar', str(self.jasminjar),
  125. '-g',
  126. '-d',
  127. str(self.javadir)]
  128. def split_list(files):
  129. "Split the files list into manageable pieces"
  130. # - On Windows 2000, commands in .bat are limited to 2047 chars.
  131. # - But the 'jasmin' script contains a line like
  132. # path_to_jre/java -jar path_to_jasmin/jasmin.jar $*
  133. # So we limit the length of arguments files to:
  134. MAXLINE = 1500
  135. chunk = []
  136. chunklen = 0
  137. for f in files:
  138. # Account for the space between items
  139. chunklen += len(f) + 1
  140. if chunklen > MAXLINE:
  141. yield chunk
  142. chunk = []
  143. chunklen = len(f)
  144. chunk.append(f)
  145. if chunk:
  146. yield chunk
  147. for files in split_list(self.jasmin_files):
  148. #print "Invoking jasmin on %s" % files
  149. self._invoke(jascmd + files, False)
  150. #print "... completed!"
  151. self.compiled = True
  152. self._compile_helper()
  153. def _make_str(self, a):
  154. if isinstance(a, ootype._string):
  155. return a._str
  156. return str(a)
  157. def execute(self, args):
  158. """
  159. Executes the compiled sources in a separate process. Returns the
  160. output as a string. The 'args' are provided as arguments,
  161. and will be converted to strings.
  162. """
  163. assert self.compiled
  164. strargs = [self._make_str(a) for a in args]
  165. cmd = [getoption('java'),
  166. '-Xmx256M', # increase the heapsize so the microbenchmarks run
  167. '-cp',
  168. str(self.javadir)+os.pathsep+str(self.jnajar),
  169. self.package+".Main"] + strargs
  170. print "Invoking java to run the code"
  171. stdout, stderr, retval = self._invoke(cmd, True)
  172. print "...done!"
  173. sys.stderr.write(stderr)
  174. return stdout, stderr, retval
  175. def generate_source_for_function(func, annotation, backendopt=False):
  176. """
  177. Given a Python function and some hints about its argument types,
  178. generates JVM sources that call it and print the result. Returns
  179. the JvmGeneratedSource object.
  180. """
  181. if hasattr(func, 'im_func'):
  182. func = func.im_func
  183. t = TranslationContext()
  184. ann = t.buildannotator()
  185. ann.build_types(func, annotation)
  186. t.buildrtyper(type_system="ootype").specialize()
  187. if backendopt:
  188. check_virtual_methods(ootype.ROOT)
  189. backend_optimizations(t)
  190. main_graph = t.graphs[0]
  191. if getoption('view'): t.view()
  192. if getoption('wd'): tmpdir = py.path.local('.')
  193. else: tmpdir = udir
  194. jvm = GenJvm(tmpdir, t, EntryPoint(main_graph, True, True))
  195. return jvm.generate_source()
  196. _missing_support_programs = None
  197. def detect_missing_support_programs():
  198. global _missing_support_programs
  199. if _missing_support_programs is not None:
  200. if _missing_support_programs:
  201. py.test.skip(_missing_support_programs)
  202. return
  203. def missing(msg):
  204. global _missing_support_programs
  205. _missing_support_programs = msg
  206. py.test.skip(msg)
  207. for cmd in 'javac', 'java':
  208. if py.path.local.sysfind(getoption(cmd)) is None:
  209. missing("%s is not on your path" % cmd)
  210. if not _check_java_version(MIN_JAVA_VERSION):
  211. missing('Minimum of Java %s required' % MIN_JAVA_VERSION)
  212. _missing_support_programs = False
  213. def _check_java_version(version):
  214. """Determine if java meets the specified version"""
  215. cmd = [getoption('java'), '-version']
  216. with open(os.devnull, 'w') as devnull:
  217. stderr = subprocess.Popen(cmd, stdout=devnull,
  218. stderr=subprocess.PIPE).communicate()[1]
  219. search = re.search('[\.0-9]+', stderr)
  220. return search and search.group() >= version
  221. class GenJvm(GenOO):
  222. """ Master object which guides the JVM backend along. To use,
  223. create with appropriate parameters and then invoke
  224. generate_source(). *You can not use one of these objects more than
  225. once.* """
  226. TypeSystem = lambda X, db: db # TypeSystem and Database are the same object
  227. Function = Function
  228. Database = Database
  229. opcodes = opcodes
  230. log = log
  231. ConstantGenerator = JVMConstantGenerator
  232. CustomDictConst = JVMCustomDictConst
  233. StaticMethodConst = JVMStaticMethodConst
  234. WeakRefConst = JVMWeakRefConst
  235. def __init__(self, tmpdir, translator, entrypoint):
  236. """
  237. 'tmpdir' --- where the generated files will go. In fact, we will
  238. put our binaries into the directory pypy/jvm
  239. 'translator' --- a TranslationContext object
  240. 'entrypoint' --- if supplied, an object with a render method
  241. """
  242. GenOO.__init__(self, tmpdir, translator, entrypoint)
  243. self.jvmsrc = JvmGeneratedSource(tmpdir, getoption('package'))
  244. def append_prebuilt_nodes(self):
  245. create_interlink_node(self.db)
  246. def generate_source(self):
  247. """ Creates the sources, and returns a JvmGeneratedSource object
  248. for manipulating them """
  249. GenOO.generate_source(self)
  250. self.jvmsrc.set_jasmin_files(self.db.jasmin_files())
  251. return self.jvmsrc
  252. def create_assembler(self):
  253. """ Creates and returns a Generator object according to the
  254. configuration. Right now, however, there is only one kind of
  255. generator: JasminGenerator """
  256. return JasminGenerator(self.db, self.jvmsrc.javadir)