[pypy-svn] r51491 - in pypy/dist/pypy/jit/codegen/cli: . test

antocuni at codespeak.net antocuni at codespeak.net
Thu Feb 14 16:18:34 CET 2008


Author: antocuni
Date: Thu Feb 14 16:18:33 2008
New Revision: 51491

Modified:
   pypy/dist/pypy/jit/codegen/cli/operation.py
   pypy/dist/pypy/jit/codegen/cli/rgenop.py
   pypy/dist/pypy/jit/codegen/cli/test/test_rgenop.py
Log:
test_hide_and_reveal_p finally works :-)



Modified: pypy/dist/pypy/jit/codegen/cli/operation.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/cli/operation.py	(original)
+++ pypy/dist/pypy/jit/codegen/cli/operation.py	Thu Feb 14 16:18:33 2008
@@ -17,7 +17,7 @@
         if self._gv_res is None:
             restype = self.restype()
             if restype is not None:
-                loc = self.il.DeclareLocal(restype)
+                loc = self.builder.il.DeclareLocal(restype)
                 self._gv_res = GenLocalVar(loc)
         return self._gv_res
 
@@ -28,38 +28,38 @@
         raise NotImplementedError
 
     def storeResult(self):
-        self.gv_res().store(self.il)
+        self.gv_res().store(self.builder)
 
 
 class UnaryOp(Operation):
-    def __init__(self, il, gv_x):
-        self.il = il
+    def __init__(self, builder, gv_x):
+        self.builder = builder
         self.gv_x = gv_x
 
     def pushAllArgs(self):
-        self.gv_x.load(self.il)
+        self.gv_x.load(self.builder)
 
     def emit(self):
         self.pushAllArgs()
-        self.il.Emit(self.getOpCode())
+        self.builder.il.Emit(self.getOpCode())
         self.storeResult()
 
     def getOpCode(self):
         raise NotImplementedError
 
 class BinaryOp(Operation):
-    def __init__(self, il, gv_x, gv_y):
-        self.il = il
+    def __init__(self, builder, gv_x, gv_y):
+        self.builder = builder
         self.gv_x = gv_x
         self.gv_y = gv_y
 
     def pushAllArgs(self):
-        self.gv_x.load(self.il)
-        self.gv_y.load(self.il)
+        self.gv_x.load(self.builder)
+        self.gv_y.load(self.builder)
 
     def emit(self):
         self.pushAllArgs()
-        self.il.Emit(self.getOpCode())
+        self.builder.il.Emit(self.getOpCode())
         self.storeResult()
 
     def getOpCode(self):
@@ -69,25 +69,25 @@
 class SameAs(UnaryOp):
     def emit(self):
         gv_res = self.gv_res()
-        self.gv_x.load(self.il)
-        self.gv_res().store(self.il)
+        self.gv_x.load(self.builder)
+        self.gv_res().store(self.builder)
 
 class MarkLabel(Operation):
 
-    def __init__(self, il, label):
-        self.il = il
+    def __init__(self, builder, label):
+        self.builder = builder
         self.label = label
 
     def restype(self):
         return None
 
     def emit(self):
-        self.il.MarkLabel(self.label)
+        self.builder.il.MarkLabel(self.label)
 
 class FollowLink(Operation):
     
-    def __init__(self, il, outputargs_gv, inputargs_gv, label):
-        self.il = il
+    def __init__(self, builder, outputargs_gv, inputargs_gv, label):
+        self.builder = builder
         self.outputargs_gv = outputargs_gv
         self.inputargs_gv = inputargs_gv
         self.label = label
@@ -97,14 +97,14 @@
 
     def emit(self):
         for i in range(len(self.outputargs_gv)):
-            self.outputargs_gv[i].load(self.il)
-            self.inputargs_gv[i].store(self.il)
-        self.il.Emit(OpCodes.Br, self.label)
+            self.outputargs_gv[i].load(self.builder)
+            self.inputargs_gv[i].store(self.builder)
+        self.builder.il.Emit(OpCodes.Br, self.label)
 
 class Branch(Operation):
     
-    def __init__(self, il, gv_cond, opcode, label):
-        self.il = il
+    def __init__(self, builder, gv_cond, opcode, label):
+        self.builder = builder
         self.gv_cond = gv_cond
         self.opcode = opcode
         self.label = label
@@ -114,27 +114,27 @@
 
     def emit(self):
         if self.gv_cond is not None:
-            self.gv_cond.load(self.il)
-        self.il.Emit(self.opcode, self.label)
+            self.gv_cond.load(self.builder)
+        self.builder.il.Emit(self.opcode, self.label)
 
 class Return(Operation):
 
-    def __init__(self, il, gv_x):
-        self.il = il
+    def __init__(self, builder, gv_x):
+        self.builder = builder
         self.gv_x = gv_x
 
     def restype(self):
         return None
 
     def emit(self):
-        self.gv_x.load(self.il)
-        self.il.Emit(OpCodes.Ret)
+        self.gv_x.load(self.builder)
+        self.builder.il.Emit(OpCodes.Ret)
 
 class Call(Operation):
 
-    def __init__(self, il, sigtoken, gv_fnptr, args_gv):
+    def __init__(self, builder, sigtoken, gv_fnptr, args_gv):
         from pypy.jit.codegen.cli.rgenop import token2clitype
-        self.il = il
+        self.builder = builder
         self.sigtoken = sigtoken
         self.gv_fnptr = gv_fnptr
         self.args_gv = args_gv
@@ -147,11 +147,11 @@
         from pypy.jit.codegen.cli.rgenop import sigtoken2clitype
         delegate_type = sigtoken2clitype(self.sigtoken)
         meth_invoke = delegate_type.GetMethod('Invoke')
-        self.gv_fnptr.load(self.il)
-        self.il.Emit(OpCodes.Castclass, delegate_type)
+        self.gv_fnptr.load(self.builder)
+        self.builder.il.Emit(OpCodes.Castclass, delegate_type)
         for gv_arg in self.args_gv:
-            gv_arg.load(self.il)
-        self.il.EmitCall(OpCodes.Callvirt, meth_invoke, None)
+            gv_arg.load(self.builder)
+        self.builder.il.EmitCall(OpCodes.Callvirt, meth_invoke, None)
         self.storeResult()
 
         
@@ -218,7 +218,7 @@
             if 'call' in step:
                 return # XXX, fix this
             attrname = opcode2attrname(step)
-            body.append('self.il.Emit(OpCodes.%s)' % attrname)
+            body.append('self.builder.il.Emit(OpCodes.%s)' % attrname)
         elif isinstance(step, cli_opcodes.MapException):
             return # XXX, TODO
         else:

Modified: pypy/dist/pypy/jit/codegen/cli/rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/cli/rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/cli/rgenop.py	Thu Feb 14 16:18:33 2008
@@ -43,10 +43,10 @@
     def getCliType(self):
         raise NotImplementedError
     
-    def load(self, il):
+    def load(self, builder):
         raise NotImplementedError
 
-    def store(self, il):
+    def store(self, builder):
         raise NotImplementedError
 
 class GenArgVar(GenVar):
@@ -57,20 +57,20 @@
     def getCliType(self):
         return self.cliType
 
-    def load(self, il):
+    def load(self, builder):
         if self.index == 0:
-            il.Emit(OpCodes.Ldarg_0)
+            builder.il.Emit(OpCodes.Ldarg_0)
         elif self.index == 1:
-            il.Emit(OpCodes.Ldarg_1)
+            builder.il.Emit(OpCodes.Ldarg_1)
         elif self.index == 2:
-            il.Emit(OpCodes.Ldarg_2)
+            builder.il.Emit(OpCodes.Ldarg_2)
         elif self.index == 3:
-            il.Emit(OpCodes.Ldarg_3)
+            builder.il.Emit(OpCodes.Ldarg_3)
         else:
-            il.Emit(OpCodes.Ldarg, self.index)
+            builder.il.Emit(OpCodes.Ldarg, self.index)
 
-    def store(self, il):
-        il.Emit(OpCodes.Starg, self.index)
+    def store(self, builder):
+        builder.il.Emit(OpCodes.Starg, self.index)
 
     def __repr__(self):
         return "GenArgVar(%d)" % self.index
@@ -82,11 +82,11 @@
     def getCliType(self):
         return self.v.get_LocalType()
 
-    def load(self, il):
-        il.Emit(OpCodes.Ldloc, self.v)
+    def load(self, builder):
+        builder.il.Emit(OpCodes.Ldloc, self.v)
 
-    def store(self, il):
-        il.Emit(OpCodes.Stloc, self.v)
+    def store(self, builder):
+        builder.il.Emit(OpCodes.Stloc, self.v)
 
 
 class IntConst(GenConst):
@@ -106,13 +106,14 @@
     def getCliType(self):
         return typeof(System.Int32)
 
-    def load(self, il):
-        il.Emit(OpCodes.Ldc_I4, self.value)
+    def load(self, builder):
+        builder.il.Emit(OpCodes.Ldc_I4, self.value)
 
     def __repr__(self):
         return "const=%s" % self.value
 
-class BaseConst(GenConst):
+class FunctionConst(GenConst):
+    
     def __init__(self, num):
         self.num = num
         self.fieldname = "const" + str(num)
@@ -125,19 +126,33 @@
         t = typeof(Constants)
         t.GetField(self.fieldname).SetValue(None, obj)
 
-    def load(self, il):
+    def load(self, builder):
         t = typeof(Constants)
         field = t.GetField(self.fieldname)
-        il.Emit(OpCodes.Ldsfld, field)
-
+        builder.il.Emit(OpCodes.Ldsfld, field)
 
-class FunctionConst(BaseConst):
-    
     @specialize.arg(1)
     def revealconst(self, T):
         return clidowncast(self.getobj(), T)
 
-class ObjectConst(BaseConst):
+class ObjectConst(GenConst):
+
+    def __init__(self, obj):
+        self.obj = obj
+
+    def load(self, builder):
+        # check whether there is already an index associated to this const
+        try:
+            index = builder.genconsts[self.obj]
+        except KeyError:
+            index = len(builder.genconsts)
+            builder.genconsts[self.obj] = index
+
+        t = self.obj.GetType()
+        builder.il.Emit(OpCodes.Ldarg_0)
+        builder.il.Emit(OpCodes.Ldc_i4, index)
+        builder.il.Emit(OpCodes.Ldelem_ref)
+        builder.il.Emit(OpCodes.Castclass, t)
 
     @specialize.arg(1)
     def revealconst(self, T):
@@ -172,9 +187,7 @@
         elif T is ootype.Bool:
             return IntConst(int(llvalue))
         elif isinstance(T, ootype.OOType):
-            const = self.newconst(ObjectConst)
-            const.setobj(llvalue)
-            return const
+            return ObjectConst(llvalue)
         else:
             assert False, "XXX not implemented"
 
@@ -195,9 +208,10 @@
 
     def newgraph(self, sigtoken, name):
         argtoks, restok = sigtoken
-        args = new_array(System.Type, len(argtoks))
+        args = new_array(System.Type, len(argtoks)+1)
+        args[0] = System.Type.GetType("System.Object[]")
         for i in range(len(argtoks)):
-            args[i] = token2clitype(argtoks[i])
+            args[i+1] = token2clitype(argtoks[i])
         res = token2clitype(restok)
         builder = Builder(self, name, res, args, sigtoken)
         return builder, builder.gv_entrypoint, builder.inputargs_gv[:]
@@ -212,7 +226,8 @@
         if DUMP_IL:
             self.il = DumpGenerator(self.il)
         self.inputargs_gv = []
-        for i in range(len(args)):
+        # we start from 1 because the 1st arg is an Object[] containing the genconsts
+        for i in range(1, len(args)):
             self.inputargs_gv.append(GenArgVar(i, args[i]))
         self.gv_entrypoint = self.rgenop.newconst(FunctionConst)
         self.sigtoken = sigtoken
@@ -220,11 +235,12 @@
         self.operations = []
         self.branches = []
         self.returnblocks = []
+        self.genconsts = {}
 
     @specialize.arg(1)
     def genop1(self, opname, gv_arg):
         opcls = ops.getopclass1(opname)
-        op = opcls(self.il, gv_arg)
+        op = opcls(self, gv_arg)
         self.emit(op)
         gv_res = op.gv_res()
         if DEBUG:
@@ -238,7 +254,7 @@
     @specialize.arg(1)
     def genop2(self, opname, gv_arg1, gv_arg2):
         opcls = ops.getopclass2(opname)
-        op = opcls(self.il, gv_arg1, gv_arg2)
+        op = opcls(self, gv_arg1, gv_arg2)
         self.emit(op)
         gv_res = op.gv_res()
         if DEBUG:
@@ -258,12 +274,12 @@
             assert False
 
     def genop_call(self, sigtoken, gv_fnptr, args_gv):
-        op = ops.Call(self.il, sigtoken, gv_fnptr, args_gv)
+        op = ops.Call(self, sigtoken, gv_fnptr, args_gv)
         self.emit(op)
         return op.gv_res()
 
     def genop_same_as(self, kindtoken, gv_x):
-        op = ops.SameAs(self.il, gv_x)
+        op = ops.SameAs(self, gv_x)
         self.emit(op)
         return op.gv_res()
         
@@ -281,7 +297,7 @@
 
     def finish_and_return(self, sigtoken, gv_returnvar):
         retlabel = self.il.DefineLabel()
-        op = ops.Branch(self.il, None, OpCodes.Br, retlabel)
+        op = ops.Branch(self, None, OpCodes.Br, retlabel)
         self.emit(op)
         self.appendreturn(retlabel, gv_returnvar)
         self.isOpen = False
@@ -289,7 +305,7 @@
     def finish_and_goto(self, outputargs_gv, target):
         inputargs_gv = target.inputargs_gv
         assert len(inputargs_gv) == len(outputargs_gv)
-        op = ops.FollowLink(self.il, outputargs_gv, inputargs_gv, target.label)
+        op = ops.FollowLink(self, outputargs_gv, inputargs_gv, target.label)
         self.emit(op)
         self.isOpen = False
 
@@ -301,26 +317,30 @@
         # render the return blocks for last, else the verifier could complain
         for retlabel, gv_returnvar in self.returnblocks:
             self.il.MarkLabel(retlabel)
-            op = ops.Return(self.il, gv_returnvar)
+            op = ops.Return(self, gv_returnvar)
             self.emit(op)
 
+        # initialize the array of genconsts
+        consts = new_array(System.Object, len(self.genconsts))
+        for obj, i in self.genconsts.iteritems():
+            consts[i] = obj
         # build the delegate
         delegate_type = sigtoken2clitype(self.sigtoken)
-        myfunc = self.meth.CreateDelegate(delegate_type)
+        myfunc = self.meth.CreateDelegate(delegate_type, consts)
         self.gv_entrypoint.setobj(myfunc)
 
     def enter_next_block(self, kinds, args_gv):
         for i in range(len(args_gv)):
-            op = ops.SameAs(self.il, args_gv[i])
+            op = ops.SameAs(self, args_gv[i])
             op.emit()
             args_gv[i] = op.gv_res()
         label = self.il.DefineLabel()
-        self.emit(ops.MarkLabel(self.il, label))
+        self.emit(ops.MarkLabel(self, label))
         return Label(label, args_gv)
 
     def _jump_if(self, gv_condition, opcode):
         label = self.il.DefineLabel()
-        op = ops.Branch(self.il, gv_condition, opcode, label)
+        op = ops.Branch(self, gv_condition, opcode, label)
         self.emit(op)
         branch = BranchBuilder(self, label)
         self.appendbranch(branch)

Modified: pypy/dist/pypy/jit/codegen/cli/test/test_rgenop.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/cli/test/test_rgenop.py	(original)
+++ pypy/dist/pypy/jit/codegen/cli/test/test_rgenop.py	Thu Feb 14 16:18:33 2008
@@ -10,7 +10,7 @@
         'test_adder',
         'test_dummy',
         'test_hide_and_reveal',
-        # 'test_hide_and_reveal_p', # think about this
+        'test_hide_and_reveal_p',
         'test_largedummy_direct', # _compile works if we set a higher maxstack
         'test_branching',
         'test_goto',



More information about the Pypy-commit mailing list