[pypy-svn] r52543 - in pypy/branch/jit-refactoring/pypy/lang/js: . test

fijal at codespeak.net fijal at codespeak.net
Sat Mar 15 00:46:58 CET 2008


Author: fijal
Date: Sat Mar 15 00:46:57 2008
New Revision: 52543

Added:
   pypy/branch/jit-refactoring/pypy/lang/js/baseop.py   (contents, props changed)
Modified:
   pypy/branch/jit-refactoring/pypy/lang/js/jscode.py
   pypy/branch/jit-refactoring/pypy/lang/js/operations.py
   pypy/branch/jit-refactoring/pypy/lang/js/test/test_interp.py
Log:
First interpreter test passes!


Added: pypy/branch/jit-refactoring/pypy/lang/js/baseop.py
==============================================================================
--- (empty file)
+++ pypy/branch/jit-refactoring/pypy/lang/js/baseop.py	Sat Mar 15 00:46:57 2008
@@ -0,0 +1,23 @@
+
+""" Base operations implementations
+"""
+
+from pypy.lang.js.jsobj import W_String, W_IntNumber, W_FloatNumber
+
+def plus(ctx, nleft, nright):
+    if isinstance(nleft, W_String) or isinstance(nright, W_String):
+        sleft = nleft.ToString(ctx)
+        sright = nright.ToString(ctx)
+        return W_String(sleft + sright)
+    # hot path
+    if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
+        ileft = nleft.ToInt32()
+        iright = nright.ToInt32()
+        try:
+            return W_IntNumber(ovfcheck(ileft + iright))
+        except OverflowError:
+            return W_FloatNumber(float(ileft) + float(iright))
+    else:
+        fleft = nleft.ToNumber()
+        fright = nright.ToNumber()
+        return W_FloatNumber(fleft + fright)

Modified: pypy/branch/jit-refactoring/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/lang/js/jscode.py	(original)
+++ pypy/branch/jit-refactoring/pypy/lang/js/jscode.py	Sat Mar 15 00:46:57 2008
@@ -1,5 +1,7 @@
 
 from pypy.lang.js.jsobj import W_IntNumber, W_FloatNumber, W_String
+from pypy.rlib.unroll import unrolling_iterable
+from pypy.lang.js.baseop import plus
 
 class AlreadyRun(Exception):
     pass
@@ -11,6 +13,7 @@
         self.opcodes = []
         self.label_count = 0
         self.has_labels = True
+        self.stack = []
 
     def emit_label(self):
         num = self.prealocate_label()
@@ -31,6 +34,17 @@
             raise ValueError("Unknown opcode %s" % (operation,))
     emit._annspecialcase_ = 'specialize:arg(1)'
 
+    def run(self, ctx, check_stack=True):
+        i = 0
+        while i < len(self.opcodes):
+            for name, op in opcode_unrolling:
+                opcode = self.opcodes[i]
+                if isinstance(opcode, op):
+                    opcode.eval(ctx, self.stack)
+            i += 1
+        if check_stack:
+            assert not self.stack
+
     def remove_labels(self):
         """ Basic optimization to remove all labels and change
         jumps to addresses. Necessary to run code at all
@@ -95,7 +109,10 @@
         raise NotImplementedError
 
 class BaseBinaryOperation(Opcode):
-    pass
+    def eval(self, ctx, stack):
+        right = stack.pop()
+        left = stack.pop()
+        stack.append(self.operation(ctx, left, right))
 
 class BaseUnaryOperation(Opcode):
     pass
@@ -121,8 +138,8 @@
     def __init__(self, value):
         self.w_floatvalue = W_FloatNumber(float(value))
 
-    def eval(self, ctx):
-        return self.w_floatvalue
+    def eval(self, ctx, stack):
+        stack.append(self.w_floatvalue)
 
     def __repr__(self):
         return 'LOAD_FLOATCONSTANT %s' % (self.w_floatvalue.floatval,)
@@ -196,7 +213,8 @@
     pass
 
 class ADD(BaseBinaryOperation):
-    pass
+    def operation(self, ctx, left, right):
+        return plus(ctx, left, right)
 
 class MUL(BaseBinaryOperation):
     pass
@@ -292,3 +310,4 @@
     if name.upper() == name and issubclass(value, Opcode):
         OpcodeMap[name] = value
 
+opcode_unrolling = unrolling_iterable(OpcodeMap.items())

Modified: pypy/branch/jit-refactoring/pypy/lang/js/operations.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/lang/js/operations.py	(original)
+++ pypy/branch/jit-refactoring/pypy/lang/js/operations.py	Sat Mar 15 00:46:57 2008
@@ -724,24 +724,6 @@
     def mathop(self, ctx, n1, n2):
         raise NotImplementedError
 
-def plus(ctx, nleft, nright):
-    if isinstance(nleft, W_String) or isinstance(nright, W_String):
-        sleft = nleft.ToString(ctx)
-        sright = nright.ToString(ctx)
-        return W_String(sleft + sright)
-    # hot path
-    if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
-        ileft = nleft.ToInt32()
-        iright = nright.ToInt32()
-        try:
-            return W_IntNumber(ovfcheck(ileft + iright))
-        except OverflowError:
-            return W_FloatNumber(float(ileft) + float(iright))
-    else:
-        fleft = nleft.ToNumber()
-        fright = nright.ToNumber()
-        return W_FloatNumber(fleft + fright)
-
 def mult(ctx, nleft, nright):
     if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
         ileft = nleft.ToInt32()
@@ -787,9 +769,6 @@
 
 class Plus(BinaryNumberOp):
     operation_name = 'ADD'
-    def mathop(self, ctx, n1, n2):
-        return plus(ctx, n1, n2)
-
 
 class Mult(BinaryNumberOp):
     operation_name = 'MUL'

Modified: pypy/branch/jit-refactoring/pypy/lang/js/test/test_interp.py
==============================================================================
--- pypy/branch/jit-refactoring/pypy/lang/js/test/test_interp.py	(original)
+++ pypy/branch/jit-refactoring/pypy/lang/js/test/test_interp.py	Sat Mar 15 00:46:57 2008
@@ -4,14 +4,15 @@
 from pypy.lang.js.operations import AEC, IntNumber, FloatNumber, Position, Plus
 from pypy.lang.js.jsobj import W_Object, ExecutionContext, W_Root, w_Null
 from pypy.lang.js.execution import ThrowException
+from pypy.lang.js.jscode import JsCode
 
 def test_simple():
-    n1 = FloatNumber(Position(), 2.0)
-    n2 = FloatNumber(Position(), 4.0)
-    p = Plus(Position(), n1, n2)
-    assert p.eval(ExecutionContext([W_Object(),])).GetValue().ToNumber() == 6.0
-    l = []
-    interpreter.writer = l.append
+    bytecode = JsCode()
+    bytecode.emit('LOAD_FLOATCONSTANT', 2)
+    bytecode.emit('LOAD_FLOATCONSTANT', 4)
+    bytecode.emit('ADD')
+    bytecode.run(ExecutionContext([W_Object()]), check_stack=False)
+    assert bytecode.stack[0].GetValue().ToNumber() == 6.0
 
 def assertp(code, prints):
     l = []



More information about the Pypy-commit mailing list