[pypy-svn] r19173 - in pypy/dist/pypy/translator: . c goal

pedronis at codespeak.net pedronis at codespeak.net
Sun Oct 30 01:56:18 CEST 2005


Author: pedronis
Date: Sun Oct 30 01:56:16 2005
New Revision: 19173

Modified:
   pypy/dist/pypy/translator/c/database.py
   pypy/dist/pypy/translator/c/gc.py
   pypy/dist/pypy/translator/c/genc.py
   pypy/dist/pypy/translator/goal/driver.py
   pypy/dist/pypy/translator/goal/targetpypystandalone.py
   pypy/dist/pypy/translator/goal/translate_pypy.py
   pypy/dist/pypy/translator/translator.py
Log:

- concrete support for target specific arguments/options

- add --thread option to targetpypystandalone to have threads explicitly enabled,
  for now this is disabled because a thread-safe stack_too_big is needed to have
  working thread again

GcPolicies now take a thread_enabled flag so that they can do what is necessary for thread support.



Modified: pypy/dist/pypy/translator/c/database.py
==============================================================================
--- pypy/dist/pypy/translator/c/database.py	(original)
+++ pypy/dist/pypy/translator/c/database.py	Sun Oct 30 01:56:16 2005
@@ -14,7 +14,7 @@
 
 class LowLevelDatabase:
 
-    def __init__(self, translator=None, standalone=False, gcpolicy=None):
+    def __init__(self, translator=None, standalone=False, gcpolicy=None, thread_enabled=False):
         self.translator = translator
         self.standalone = standalone
         self.structdefnodes = {}
@@ -31,7 +31,7 @@
         if gcpolicy is None:
             from pypy.translator.c import gc
             gcpolicy = gc.RefcountingGcPolicy
-        self.gcpolicy = gcpolicy(self)
+        self.gcpolicy = gcpolicy(self, thread_enabled)
 
     def gettypedefnode(self, T, varlength=1):
         if varlength <= 1:

Modified: pypy/dist/pypy/translator/c/gc.py
==============================================================================
--- pypy/dist/pypy/translator/c/gc.py	(original)
+++ pypy/dist/pypy/translator/c/gc.py	Sun Oct 30 01:56:16 2005
@@ -9,8 +9,9 @@
 
 class BasicGcPolicy:
     
-    def __init__(self, db):
+    def __init__(self, db, thread_enabled=False):
         self.db = db
+        self.thread_enabled = thread_enabled
 
     def pyobj_incref(self, expr, T):
         return 'Py_XINCREF(%s);' % expr
@@ -296,6 +297,12 @@
 
     deallocator_lines = RefcountingGcPolicy.deallocator_lines.im_func
 
+    def common_after_definition(self, defnode):
+        if defnode.gcinfo:
+            gcinfo = defnode.gcinfo
+            if gcinfo.finalizer:
+                yield 'void %s(GC_PTR obj, GC_PTR ignore);' % (gcinfo.finalizer,)
+
     # for arrays
 
     def array_setup(self, arraydefnode):
@@ -313,12 +320,16 @@
                     yield '\t' + line
                 yield '}'
 
+    array_after_definition = common_after_definition
+
     # for structs
     def struct_setup(self, structdefnode, rtti):
         if isinstance(structdefnode.LLTYPE, GcStruct) and list(self.deallocator_lines(structdefnode, '')):
             gcinfo = structdefnode.gcinfo = RefcountingInfo()
             gcinfo.finalizer = self.db.namespace.uniquename('finalize_'+structdefnode.barename)
 
+    struct_after_definition = common_after_definition
+
     def struct_implementationcode(self, structdefnode):
         if structdefnode.gcinfo:
             gcinfo = structdefnode.gcinfo
@@ -360,9 +371,9 @@
         return ['gc']
 
     def pre_pre_gc_code(self):
-        #if sys.platform == "linux2":
-        #    yield "#define _REENTRANT 1"
-        #    yield "#define GC_LINUX_THREADS 1"
+        if sys.platform == "linux2" and self.thread_enabled:
+            yield "#define _REENTRANT 1"
+            yield "#define GC_LINUX_THREADS 1"
         yield '#include <gc/gc.h>'
         yield '#define USING_BOEHM_GC'
 

Modified: pypy/dist/pypy/translator/c/genc.py
==============================================================================
--- pypy/dist/pypy/translator/c/genc.py	(original)
+++ pypy/dist/pypy/translator/c/genc.py	Sun Oct 30 01:56:16 2005
@@ -18,9 +18,10 @@
     symboltable = None
     stackless = False
     
-    def __init__(self, translator, gcpolicy=None, libraries=None):
+    def __init__(self, translator, gcpolicy=None, libraries=None, thread_enabled=False):
         self.translator = translator
         self.gcpolicy = gcpolicy
+        self.thread_enabled = thread_enabled
 
         if libraries is None:
             libraries = []
@@ -28,7 +29,8 @@
 
     def build_database(self):
         translator = self.translator
-        db = LowLevelDatabase(translator, standalone=self.standalone, gcpolicy=self.gcpolicy)
+        db = LowLevelDatabase(translator, standalone=self.standalone, 
+                              gcpolicy=self.gcpolicy, thread_enabled=self.thread_enabled)
 
         if self.stackless:
             from pypy.translator.c.stackless import StacklessData

Modified: pypy/dist/pypy/translator/goal/driver.py
==============================================================================
--- pypy/dist/pypy/translator/goal/driver.py	(original)
+++ pypy/dist/pypy/translator/goal/driver.py	Sun Oct 30 01:56:16 2005
@@ -16,6 +16,9 @@
 
 DEFAULT_OPTIONS = optparse.Values(defaults={
   'gc': 'ref',
+
+  'thread': False, # influences GC policy
+
   'stackless': False,
   'debug': True,
   'insist': False,
@@ -190,7 +193,9 @@
             from pypy.translator.c import gc
             gcpolicy = gc.NoneGcPolicy
 
-        cbuilder = translator.cbuilder(standalone=standalone, gcpolicy=gcpolicy)
+        cbuilder = translator.cbuilder(standalone=standalone, 
+                                       gcpolicy=gcpolicy,
+                                       thread_enabled = getattr(opt, 'thread', False))
         cbuilder.stackless = opt.stackless
         database = cbuilder.build_database()
         self.log.info("database for generating C source was created")

Modified: pypy/dist/pypy/translator/goal/targetpypystandalone.py
==============================================================================
--- pypy/dist/pypy/translator/goal/targetpypystandalone.py	(original)
+++ pypy/dist/pypy/translator/goal/targetpypystandalone.py	Sun Oct 30 01:56:16 2005
@@ -44,9 +44,31 @@
 
 # _____ Define and setup target ___
 
+# for now this will do for option handling
+
+take_options = True
+
+def opt_parser():
+    import py
+    defl = {'thread': False}
+    parser = py.compat.optparse.OptionParser(usage="target PyPy standalone", add_help_option=False)
+    parser.set_defaults(**defl)
+    parser.add_option("--thread", action="store_true", dest="thread", help="enable threading")
+    return parser
+
+def print_help():
+    opt_parser().print_help()
+
+
 def target(driver, args):
     options = driver.options
 
+    tgt_options, _ = opt_parser().parse_args(args)
+
+    translate_pypy.log_options(tgt_options, "target PyPy options in effect")
+
+    options.thread = tgt_options.thread
+
     global space, w_entry_point
 
     geninterp = not getattr(options, 'lowmem', False)
@@ -58,12 +80,13 @@
 
     # disable translation of the whole of classobjinterp.py
     StdObjSpace.setup_old_style_classes = lambda self: None
-    if options.gc == 'boehm':
-        #print "disabling thread with boehm for stabilitiy (combination not tested)"
-        #print "trying threads and boehm"
-        usemodules = []
-    else:
-        usemodules = ['thread']
+
+    usemodules = []
+    if options.thread:
+        print "threads unsupported right now: need thread-safe stack_too_big"
+        raise SystemExit        
+        usemodules.append('thread')
+        
     space = StdObjSpace(nofaking=True,
                         compiler="ast", # interpreter/astcompiler
                         translating=True,

Modified: pypy/dist/pypy/translator/goal/translate_pypy.py
==============================================================================
--- pypy/dist/pypy/translator/goal/translate_pypy.py	(original)
+++ pypy/dist/pypy/translator/goal/translate_pypy.py	Sun Oct 30 01:56:16 2005
@@ -84,6 +84,8 @@
 }
 
 defaults = {
+    'help': False,
+
     'targetspec': 'targetpypystandalone',
     
     'goals': [],
@@ -139,24 +141,31 @@
         
 def goal_cb(option, opt, value, parser, enable, goal):
     if enable:
-        if goal not in parser.values.goals:
+        if goal not in parser.values.ensure_value('goals', []):
             parser.values.goals = parser.values.goals + [goal]
     else:
-        if goal not in parser.values.skipped_goals:
+        if goal not in parser.values.ensure_value('skipped_goals', []):
             parser.values.skipped_goals = parser.values.skipped_goals + [goal]
 
 def load_target(targetspec):
     log.info("Translating target as defined by %s" % targetspec)
     if not targetspec.endswith('.py'):
         targetspec += '.py'
-    targetspec_dic = {'__name__':'__rpythonmain__'}
+    thismod = sys.modules[__name__]
+    targetspec_dic = {'__name__':'__rpythonmain__',
+                      'translate_pypy': thismod}
     sys.path.insert(0, os.path.dirname(targetspec))
     execfile(targetspec, targetspec_dic)
     return targetspec_dic
 
 def parse_options_and_load_target():
-    opt_parser = optparse.OptionParser(usage="%prog [options] [target]", prog="translate_pypy",
-                                       formatter=OptHelpFormatter())
+    opt_parser = optparse.OptionParser(usage="%prog [options] [target] [target-specific-options]",
+                                       prog="translate_pypy",
+                                       formatter=OptHelpFormatter(),
+                                       add_help_option=False)
+
+    opt_parser.disable_interspersed_args()
+
     for group_name, grp_opts in bunchiter(opts):
         grp = opt_parser.add_option_group(group_name)
         for dest, dest_opts in bunchiter(grp_opts):
@@ -183,7 +192,10 @@
 
                 grp.add_option(*names, **opt_setup)
 
-    opt_parser.set_defaults(**defaults)
+    # add help back as a flag
+    opt_parser.add_option("-h", "--help",
+                          action="store_true", dest="help",
+                          help="show this help message and exit")
 
     options, args = opt_parser.parse_args()
 
@@ -195,16 +207,49 @@
                 "ambiguous file naming, please rename %s" % arg)
             options.targetspec = arg
         elif os.path.isfile(arg) and arg.endswith('.py'):
-            options.targetspec = arg[:-3]        
+            options.targetspec = arg[:-3]
+        else:
+            args = [arg] + args
+
+    # for help, applied later
+    opt_parser.set_defaults(**defaults)
+
+    targetspec = options.ensure_value('targetspec', opt_parser.defaults['targetspec'])
+    targetspec_dic = load_target(targetspec)
+
+    if args and not targetspec_dic.get('take_options', False):
+        log.WARNING("target specific arguments supplied but will be ignored: %s" % ' '.join(args))
 
-    targespec_dic = load_target(options.targetspec)
+    # target specific defaults taking over
+    if 'opt_defaults' in targetspec_dic:
+        opt_parser.set_defaults(targetspec_dic['op_defaults'])
+
+    if options.help:
+        opt_parser.print_help()
+        if 'print_help' in targetspec_dic:
+            print
+            targetspec_dic['print_help']()
+        sys.exit(0)
+
+    # apply defaults
+    for name, val in opt_parser.defaults.iteritems():
+        options.ensure_value(name, val)
 
     # tweak: default_goals into default_goal
     del options.default_goals
     options.default_goal = 'compile'
     
-    return targespec_dic, options, args
+    return targetspec_dic, options, args
 
+def log_options(options, header="options in effect"):
+    # list options (xxx filter, filter for target)
+    log('%s:' % header)
+    optnames = options.__dict__.keys()
+    optnames.sort()
+    for name in optnames:
+        optvalue = getattr(options, name)
+        log('%25s: %s' %(name, optvalue))
+   
 def main():
     targetspec_dic, options, args = parse_options_and_load_target()
 
@@ -259,14 +304,8 @@
 
         pdb_plus_show.start(tb, server_setup, graphic=not options.text)
 
-    # list options (xxx filter, filter for target)
-    log('options in effect:')
-    optnames = options.__dict__.keys()
-    optnames.sort()
-    for name in optnames:
-        optvalue = getattr(options, name)
-        log('%25s: %s' %(name, optvalue))
-   
+    log_options(options)
+
     try:
         drv = driver.TranslationDriver.from_targetspec(targetspec_dic, options, args,
                                                       empty_translator=t,

Modified: pypy/dist/pypy/translator/translator.py
==============================================================================
--- pypy/dist/pypy/translator/translator.py	(original)
+++ pypy/dist/pypy/translator/translator.py	Sun Oct 30 01:56:16 2005
@@ -303,12 +303,12 @@
         cbuilder.import_module()    
         return cbuilder.get_entry_point()
 
-    def cbuilder(self, standalone=False, gcpolicy=None):
+    def cbuilder(self, standalone=False, gcpolicy=None, thread_enabled=False):
         from pypy.translator.c import genc
         if standalone:
-            return genc.CStandaloneBuilder(self, gcpolicy=gcpolicy)
+            return genc.CStandaloneBuilder(self, gcpolicy=gcpolicy, thread_enabled=thread_enabled)
         else:
-            return genc.CExtModuleBuilder(self, gcpolicy=gcpolicy)
+            return genc.CExtModuleBuilder(self, gcpolicy=gcpolicy, thread_enabled=thread_enabled)
 
     def llvmcompile(self, really_compile=True, standalone=False, optimize=True, exe_name=None, gcpolicy=None):
         """llvmcompile(self, really_compile=True, standalone=False, optimize=True) -> LLVM translation



More information about the Pypy-commit mailing list