[pypy-svn] r17325 - in pypy/dist/pypy/translator/llvm: . module test

ericvrp at codespeak.net ericvrp at codespeak.net
Wed Sep 7 15:11:14 CEST 2005


Author: ericvrp
Date: Wed Sep  7 15:11:12 2005
New Revision: 17325

Added:
   pypy/dist/pypy/translator/llvm/gc.py   (contents, props changed)
Modified:
   pypy/dist/pypy/translator/llvm/build_llvm_module.py
   pypy/dist/pypy/translator/llvm/genllvm.py
   pypy/dist/pypy/translator/llvm/module/extfunction.py
   pypy/dist/pypy/translator/llvm/pyxwrapper.py
   pypy/dist/pypy/translator/llvm/test/test_gc.py
Log:
Refactored garbage collection into gc.py (similar to genC)


Modified: pypy/dist/pypy/translator/llvm/build_llvm_module.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/build_llvm_module.py	(original)
+++ pypy/dist/pypy/translator/llvm/build_llvm_module.py	Wed Sep  7 15:11:12 2005
@@ -10,7 +10,6 @@
 
 from pypy.translator.tool.cbuild import make_c_from_pyxfile
 from pypy.translator.tool import stdoutcapture
-from pypy.translator.llvm.genllvm import use_boehm_gc
 from pypy.translator.llvm.log import log
 
 EXCEPTIONS_SWITCHES   = "-enable-correct-eh-support"
@@ -55,7 +54,7 @@
     log.build(cmd)
     cmdexec(cmd)
 
-def make_module_from_llvm(llvmfile, pyxfile=None, optimize=True, exe_name=None):
+def make_module_from_llvm(genllvm, llvmfile, pyxfile=None, optimize=True, exe_name=None):
     include_dir = py.magic.autopath().dirpath()
     dirpath = llvmfile.dirpath()
     lastdir = path.local()
@@ -67,12 +66,8 @@
     else:
         source_files = []
     object_files = []
-    library_files = []
-    if use_boehm_gc:
-        gc_libs = '-lgc -lpthread'
-        library_files.append('gc')
-    else:
-        gc_libs = ''
+    library_files = genllvm.gcpolicy.gc_libraries()
+    gc_libs = ' '.join(['-l' + lib for lib in library_files])
 
     if optimize:
         optimization_switches = OPTIMIZATION_SWITCHES

Added: pypy/dist/pypy/translator/llvm/gc.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm/gc.py	Wed Sep  7 15:11:12 2005
@@ -0,0 +1,85 @@
+class GcPolicy:
+    def __init__(self):
+        raise Exception, 'GcPolicy should not be used directly'
+
+    def gc_libraries(self):
+        return []
+
+    def llvm_code(self):
+        return '''
+internal fastcc sbyte* %gc_malloc(uint %n) {
+    %nn  = cast uint %n to uint
+    %ptr = malloc sbyte, uint %nn
+    ret sbyte* %ptr
+}
+
+internal fastcc sbyte* %gc_malloc_atomic(uint %n) {
+    %nn  = cast uint %n to uint
+    %ptr = malloc sbyte, uint %nn
+    ret sbyte* %ptr
+}
+'''
+
+    def pyrex_code(self):
+        return ''
+
+    def new(gcpolicy=None):  #factory
+        if gcpolicy is None or gcpolicy == 'boehm':
+            from os.path import exists
+            boehm_on_path = exists('/usr/lib/libgc.so') or exists('/usr/lib/libgc.a')
+            if not boehm_on_path:
+                raise Exception, 'Boehm GC libary not found in /usr/lib'
+            from pypy.translator.llvm.gc import BoehmGcPolicy
+            gcpolicy = BoehmGcPolicy()
+        elif gcpolicy == 'ref':
+            from pypy.translator.llvm.gc import RefcountingGcPolicy
+            gcpolicy = RefcountingGcPolicy()
+        elif gcpolicy == 'none':
+            from pypy.translator.llvm.gc import NoneGcPolicy
+            gcpolicy = NoneGcPolicy()
+        else:
+            raise Exception, 'unknown gcpolicy: ' + str(gcpolicy)
+        return gcpolicy
+    new = staticmethod(new)
+
+
+class NoneGcPolicy(GcPolicy):
+    def __init__(self):
+        pass
+
+
+class BoehmGcPolicy(GcPolicy):
+    def __init__(self):
+        pass
+
+    def gc_libraries(self):
+        return ['gc'] # xxx on windows?
+
+    def llvm_code(self):
+        return '''
+declare ccc sbyte* %GC_malloc(uint)
+declare ccc sbyte* %GC_malloc_atomic(uint)
+
+internal fastcc sbyte* %gc_malloc(uint %n) {
+    %ptr = call ccc sbyte* %GC_malloc(uint %n)
+    ret sbyte* %ptr
+}
+
+internal fastcc sbyte* %gc_malloc_atomic(uint %n) {
+    %ptr = call ccc sbyte* %GC_malloc_atomic(uint %n)
+    ret sbyte* %ptr
+}
+'''
+
+    def pyrex_code(self):
+        return '''
+cdef extern int GC_get_heap_size()
+
+def GC_get_heap_size_wrapper():
+    return GC_get_heap_size()
+'''
+
+
+class RefcountingGcPolicy(GcPolicy):
+    def __init__(self):
+        raise NotImplementedError, 'RefcountingGcPolicy'

Modified: pypy/dist/pypy/translator/llvm/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/genllvm.py	Wed Sep  7 15:11:12 2005
@@ -1,7 +1,3 @@
-from os.path import exists
-use_boehm_gc = exists('/usr/lib/libgc.so') or exists('/usr/lib/libgc.a')
-#use_boehm_gc = False
-
 import os
 import time
 import types
@@ -20,10 +16,11 @@
      DEFAULT_TAIL, DEFAULT_CCONV
 from pypy.translator.llvm import extfuncnode
 from pypy.translator.llvm.module.extfunction import extdeclarations, \
-     extfunctions, gc_boehm, gc_disabled, dependencies
+     extfunctions, dependencies
 from pypy.translator.llvm.node import LLVMNode
 from pypy.translator.llvm.structnode import StructNode
 from pypy.translator.llvm.externs2ll import post_setup_externs, generate_llfile
+from pypy.translator.llvm.gc import GcPolicy
 
 from pypy.translator.translator import Translator
 
@@ -34,12 +31,13 @@
 
 class GenLLVM(object):
 
-    def __init__(self, translator, debug=True):
+    def __init__(self, translator, gcpolicy=None, debug=True):
     
         # reset counters
         LLVMNode.nodename_count = {}    
         self.db = Database(translator)
         self.translator = translator
+        self.gcpolicy = gcpolicy
         translator.checkgraphs()
         extfuncnode.ExternalFuncNode.used_external_functions = {}
 
@@ -90,6 +88,20 @@
             print 'STATS', s
 
     def gen_llvm_source(self, func=None):
+        """
+        init took 00m00s
+        setup_all took 08m14s
+        setup_all externs took 00m00s
+        generate_ll took 00m02s
+        write externs type declarations took 00m00s
+        write data type declarations took 00m02s
+        write global constants took 09m49s
+        write function prototypes took 00m00s
+        write declarations took 00m03s
+        write implementations took 01m54s
+        write support functions took 00m00s
+        write external functions took 00m00s
+        """
         self._checkpoint()
 
         if func is None:
@@ -172,12 +184,8 @@
 
         nl(); comment("Function Implementation") 
         codewriter.startimpl()
-        if use_boehm_gc:
-            gc_funcs = gc_boehm
-        else:
-            gc_funcs = gc_disabled    
-        for gc_func in gc_funcs.split('\n'):
-            codewriter.append(gc_func)
+        
+        codewriter.append(self.gcpolicy.llvm_code())
 
         for typ_decl in self.db.getnodes():
             typ_decl.writeimpl(codewriter)
@@ -254,23 +262,23 @@
                       exe_name=None):
 
         if standalone:
-            return build_llvm_module.make_module_from_llvm(filename,
+            return build_llvm_module.make_module_from_llvm(self, filename,
                                                            optimize=optimize,
                                                            exe_name=exe_name)
         else:
             postfix = ''
             basename = filename.purebasename + '_wrapper' + postfix + '.pyx'
             pyxfile = filename.new(basename = basename)
-            write_pyx_wrapper(self.entrynode, pyxfile)    
-            return build_llvm_module.make_module_from_llvm(filename,
+            write_pyx_wrapper(self, pyxfile)    
+            return build_llvm_module.make_module_from_llvm(self, filename,
                                                            pyxfile=pyxfile,
                                                            optimize=optimize)
 
     def _debug_prototype(self, codewriter):
         codewriter.append("declare int %printf(sbyte*, ...)")
 
-def genllvm(translator, log_source=False, **kwds):
-    gen = GenLLVM(translator)
+def genllvm(translator, gcpolicy=None, log_source=False, **kwds):
+    gen = GenLLVM(translator, GcPolicy.new(gcpolicy))
     filename = gen.gen_llvm_source()
     if log_source:
         log.genllvm(open(filename).read())

Modified: pypy/dist/pypy/translator/llvm/module/extfunction.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/module/extfunction.py	(original)
+++ pypy/dist/pypy/translator/llvm/module/extfunction.py	Wed Sep  7 15:11:12 2005
@@ -9,33 +9,6 @@
 %last_exception_value = global %RPYTHON_EXCEPTION* null
 """
 
-gc_boehm = """declare ccc sbyte* %GC_malloc(uint)
-declare ccc sbyte* %GC_malloc_atomic(uint)
-
-internal fastcc sbyte* %gc_malloc(uint %n) {
-    %ptr = call ccc sbyte* %GC_malloc(uint %n)
-    ret sbyte* %ptr
-}
-
-internal fastcc sbyte* %gc_malloc_atomic(uint %n) {
-    %ptr = call ccc sbyte* %GC_malloc_atomic(uint %n)
-    ret sbyte* %ptr
-}
-"""
-
-gc_disabled = """internal fastcc sbyte* %gc_malloc(uint %n) {
-    %nn  = cast uint %n to uint
-    %ptr = malloc sbyte, uint %nn
-    ret sbyte* %ptr
-}
-
-internal fastcc sbyte* %gc_malloc_atomic(uint %n) {
-    %nn  = cast uint %n to uint
-    %ptr = malloc sbyte, uint %nn
-    ret sbyte* %ptr
-}
-"""
-
 extfunctions = {}   #dependencies, llvm-code
 
 import support

Modified: pypy/dist/pypy/translator/llvm/pyxwrapper.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/pyxwrapper.py	(original)
+++ pypy/dist/pypy/translator/llvm/pyxwrapper.py	Wed Sep  7 15:11:12 2005
@@ -1,7 +1,6 @@
 import sys
 from pypy.translator.llvm.log import log 
 from pypy.rpython import lltype 
-from pypy.translator.llvm.genllvm import use_boehm_gc
 log = log.pyrex 
 
 PRIMITIVES_TO_C = {lltype.Bool: "char",
@@ -24,7 +23,8 @@
 else:
     assert False, "Unsupported platform"        
 
-def write_pyx_wrapper(funcgen, targetpath): 
+def write_pyx_wrapper(genllvm, targetpath): 
+    funcgen = genllvm.entrynode
     def c_declaration():
         returntype = PRIMITIVES_TO_C[
             funcgen.graph.returnblock.inputargs[0].concretetype]
@@ -43,12 +43,7 @@
     append("class LLVMException(Exception):")
     append("    pass")
     append("")
-    if use_boehm_gc:
-        append("cdef extern int GC_get_heap_size()")
-        append("")
-        append("def GC_get_heap_size_wrapper():")
-        append("    return GC_get_heap_size()")
-        append("")
+    append(genllvm.gcpolicy.pyrex_code())
     append("def %s_wrapper(%s):" % (funcgen.ref.strip("%"), ", ".join(inputargs)))
     append("    result = __entrypoint__%s(%s)" % (funcgen.ref.strip("%"), ", ".join(inputargs)))
     append("    if __entrypoint__raised_LLVMException():    #not caught by the LLVM code itself")

Modified: pypy/dist/pypy/translator/llvm/test/test_gc.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/test/test_gc.py	(original)
+++ pypy/dist/pypy/translator/llvm/test/test_gc.py	Wed Sep  7 15:11:12 2005
@@ -1,16 +1,16 @@
 import sys
 import py
 
-from pypy.translator.llvm.genllvm import use_boehm_gc
 from pypy.translator.llvm.test.runtest import compile_module_function
 
 py.log.setconsumer("genllvm", py.log.STDOUT)
 py.log.setconsumer("genllvm database prepare", None)
 
 def test_GC_malloc(): 
-    if not use_boehm_gc:
-        py.test.skip("test_GC_malloc skipped because Boehm collector library was not found")
-        return
+    #XXX how to get to gcpolicy?
+    #if not use_boehm_gc:
+    #    py.test.skip("test_GC_malloc skipped because Boehm collector library was not found")
+    #    return
     def tuple_getitem(n): 
         x = 666
         i = 0



More information about the Pypy-commit mailing list