1. Pypy
  2. Untitled project
  3. pypy

Source

pypy / pypy / translator / cli / constant.py

Diff from to

File pypy/translator/cli/constant.py

 # Different generators implementing different techniques for loading
 # constants (Static fields, singleton fields, etc)
 
-class CLIBaseConstGenerator(BaseConstantGenerator):
+class CLIConstantGenerator(BaseConstantGenerator):
     """
     Base of all CLI constant generators.  It implements the oosupport
     constant generator in terms of the CLI interface.
         return gen
 
     def _end_gen_constants(self, gen, numsteps):
-        assert gen.ilasm is self.ilasm
+
+        self.ilasm.begin_function('.cctor', [], 'void', False, 'static',
+                                  'specialname', 'rtspecialname', 'default')
+        self.ilasm.stderr('CONST: initialization starts', DEBUG_CONST_INIT)
+        for i in range(numsteps):
+            self.ilasm.stderr('CONST: step %d of %d' % (i, numsteps),
+                              DEBUG_CONST_INIT)
+            step_name = 'step%d' % i
+            self.ilasm.call('void %s::%s()' % (CONST_CLASS, step_name))
+        self.ilasm.stderr('CONST: initialization completed', DEBUG_CONST_INIT)
+        self.ilasm.ret()
+        self.ilasm.end_function()
+        
         self.end_class()
 
     def begin_class(self):
         else:
             return BaseConstantGenerator._create_complex_const(self, value)
 
-class FieldConstGenerator(CLIBaseConstGenerator):
-    pass
-
-class StaticFieldConstGenerator(FieldConstGenerator):
-
     # _________________________________________________________________
     # OOSupport interface
     
         gen.ilasm.ret()
         gen.ilasm.end_function()
 
-    def _end_gen_constants(self, gen, numsteps):
 
-        self.ilasm.begin_function('.cctor', [], 'void', False, 'static',
-                                  'specialname', 'rtspecialname', 'default')
-        self.ilasm.stderr('CONST: initialization starts', DEBUG_CONST_INIT)
-        for i in range(numsteps):
-            self.ilasm.stderr('CONST: step %d of %d' % (i, numsteps),
-                              DEBUG_CONST_INIT)
-            step_name = 'step%d' % i
-            self.ilasm.call('void %s::%s()' % (CONST_CLASS, step_name))
-        self.ilasm.stderr('CONST: initialization completed', DEBUG_CONST_INIT)
-        self.ilasm.ret()
-        self.ilasm.end_function()
 
-        super(StaticFieldConstGenerator, self)._end_gen_constants(
-            gen, numsteps)
-
-class InstanceFieldConstGenerator(FieldConstGenerator):
-    
-    # _________________________________________________________________
-    # OOSupport interface
-    
-    def push_constant(self, gen, const):
-        # load the singleton instance
-        gen.ilasm.opcode('ldsfld class %s %s::Singleton' % (CONST_CLASS, CONST_CLASS))
-        gen.ilasm.opcode('ldfld %s %s::%s' % (const.get_type(), CONST_CLASS, const.name))
-
-    def _push_constant_during_init(self, gen, const):
-        # during initialization, we load the 'this' pointer from our
-        # argument rather than the singleton argument
-        gen.ilasm.opcode('ldarg.0')
-        gen.ilasm.opcode('ldfld %s %s::%s' % (const.get_type(), CONST_CLASS, const.name))
-
-    def _pre_store_constant(self, gen, const):
-        gen.ilasm.opcode('ldarg.0')
-        
-    def _store_constant(self, gen, const):
-        gen.ilasm.set_field((const.get_type(), CONST_CLASS, const.name))
-
-    # _________________________________________________________________
-    # CLI interface
-
-    def _declare_const(self, gen, all_constants):
-        gen.ilasm.field(const.name, const.get_type(), static=False)
-    
-    def _declare_step(self, gen, stepnum):
-        gen.ilasm.begin_function('step%d' % stepnum, [], 'void', False)
-
-    def _close_step(self, gen, stepnum):
-        gen.ilasm.ret()
-        gen.ilasm.end_function()
-
-    def _end_gen_constants(self, gen, numsteps):
-
-        ilasm = gen.ilasm
-
-        ilasm.begin_function('.ctor', [], 'void', False, 'specialname', 'rtspecialname', 'instance')
-        ilasm.opcode('ldarg.0')
-        ilasm.call('instance void object::.ctor()')
-
-        ilasm.opcode('ldarg.0')
-        ilasm.opcode('stsfld class %s %s::Singleton' % (CONST_CLASS, CONST_CLASS))
-        
-        for i in range(numsteps):
-            step_name = 'step%d' % i
-            ilasm.opcode('ldarg.0')
-            ilasm.call('instance void %s::%s()' % (CONST_CLASS, step_name))
-        ilasm.ret()
-        ilasm.end_function()
-
-        # declare&init the Singleton containing the constants
-        ilasm.field('Singleton', 'class %s' % CONST_CLASS, static=True)
-        ilasm.begin_function('.cctor', [], 'void', False, 'static', 'specialname', 'rtspecialname', 'default')
-        if SERIALIZE:
-            self._serialize_ctor()
-        else:
-            self._plain_ctor()
-        ilasm.end_function()
-
-        super(StaticFieldConstGenerator, self)._end_gen_constants(gen, numsteps)
-
-    def _plain_ctor(self):
-        self.ilasm.new('instance void class %s::.ctor()' % CONST_CLASS)
-        self.ilasm.pop()
-        self.ilasm.ret()
-
-    def _serialize_ctor(self):
-        self.ilasm.opcode('ldstr "constants.dat"')
-        self.ilasm.call('object [pypylib]pypy.runtime.Utils::Deserialize(string)')
-        self.ilasm.opcode('dup')
-        self.ilasm.opcode('brfalse initialize')
-        self.ilasm.stderr('Constants deserialized successfully')        
-        self.ilasm.opcode('stsfld class %s %s::Singleton' % (CONST_CLASS, CONST_CLASS))
-        self.ilasm.ret()
-        self.ilasm.label('initialize')
-        self.ilasm.pop()
-        self.ilasm.stderr('Cannot deserialize constants... initialize them!')
-        self.ilasm.new('instance void class %s::.ctor()' % CONST_CLASS)
-        self.ilasm.opcode('ldstr "constants.dat"')
-        self.ilasm.call('void [pypylib]pypy.runtime.Utils::Serialize(object, string)')
-        self.ilasm.ret()
-
-class LazyConstGenerator(StaticFieldConstGenerator):
-    def push_constant(self, ilasm, const):
-        getter_name = '%s::%s' % (CONST_CLASS, 'get_%s' % const.name)
-        ilasm.call('%s %s()' % (const.get_type(), getter_name))
-
-    def _create_pointers(self, gen, all_constants):
-        # overload to do nothing since we handle everything in lazy fashion
-        pass
-
-    def _initialize_data(self, gen, all_constants):
-        # overload to do nothing since we handle everything in lazy fashion
-        pass
-
-    def _declare_const(self, gen, const):
-        # Declare the field
-        super(LazyConstGenerator, self)._declare_const(gen, const)
-
-        # Create the method for accessing the field
-        getter_name = 'get_%s' % const.name
-        type_ = const.get_type()
-        self.ilasm.begin_function(getter_name, [], type_, False, 'static')
-        self.ilasm.load_static_constant(type_, CONST_NAMESPACE, CONST_CLASS, const.name)
-        # if it's already initialized, just return it
-        self.ilasm.opcode('dup')
-        self.ilasm.opcode('brfalse', 'initialize')
-        self.ilasm.opcode('ret')
-        # else, initialize!
-        self.ilasm.label('initialize')
-        self.ilasm.opcode('pop') # discard the null value we know is on the stack
-        const.instantiate(ilasm)
-        self.ilasm.opcode('dup') # two dups because const.init pops the value at the end
-        self.ilasm.opcode('dup')
-        self.ilasm.store_static_constant(type_, CONST_NAMESPACE, CONST_CLASS, const.name)
-        const.init(ilasm)
-        self.ilasm.opcode('ret')
-        self.ilasm.end_function()
 
 # ______________________________________________________________________
 # Mixins
 class CLIStaticMethodConst(CLIBaseConstMixin, StaticMethodConst):
     def create_pointer(self, gen):
         assert not self.is_null()
-        signature = self.cts.graph_to_signature(self.value.graph)
+        signature = self.cts.static_meth_to_signature(self.value)
         gen.ilasm.opcode('ldnull')
         gen.ilasm.opcode('ldftn', signature)
         gen.ilasm.new('instance void class %s::.ctor(object, native int)' % self.delegate_type)