[pypy-svn] r26241 - pypy/dist/pypy/translator/cl

nik at codespeak.net nik at codespeak.net
Mon Apr 24 10:50:56 CEST 2006


Author: nik
Date: Mon Apr 24 10:50:51 2006
New Revision: 26241

Modified:
   pypy/dist/pypy/translator/cl/buildcl.py
   pypy/dist/pypy/translator/cl/gencl.py
Log:
(sanxiyn, nik)
get rid of prints and stdout capturing for code generation (we can use pdb
again, yay!). delete a lot of code that is not valid anymore for
specialized graphs. the code before this revision is tagged at
http://codespeak.net/svn/pypy/tag/before-ootype-cl


Modified: pypy/dist/pypy/translator/cl/buildcl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/buildcl.py	(original)
+++ pypy/dist/pypy/translator/cl/buildcl.py	Mon Apr 24 10:50:51 2006
@@ -91,7 +91,7 @@
     graph = t.graphs[0]
         
     gen = GenCL(graph, argtypes)
-    out = gen.globaldeclarations() + '\n' + gen.emitcode()
+    out = gen.emitcode()
     i = 1
     fpath = path.join("%s.lisp" % graph.name)
     def _(*args):

Modified: pypy/dist/pypy/translator/cl/gencl.py
==============================================================================
--- pypy/dist/pypy/translator/cl/gencl.py	(original)
+++ pypy/dist/pypy/translator/cl/gencl.py	Mon Apr 24 10:50:51 2006
@@ -18,22 +18,19 @@
         self.args = op.args
         self.result = op.result
 
-    def __call__(self):
+    def __iter__(self):
         if self.opname in self.binary_ops:
-            self.op_binary(self.opname)
+            for line in self.op_binary(self.opname):
+                yield line
         else:
-            default = self.op_default
-            meth = getattr(self, "op_" + self.opname, default)
-            meth()
-
-    def op_default(self):
-        print ";;", self.op
-        print ";; Op", self.opname, "is missing"
+            meth = getattr(self, "op_" + self.opname)
+            for line in meth():
+                yield line
 
     def op_same_as(self):
         target = self.str(self.result)
         origin = self.str(self.args[0])
-        print "(setf %s %s)" % (target, origin)
+        yield "(setf %s %s)" % (target, origin)
 
     binary_ops = {
         #"add": "+",
@@ -57,54 +54,12 @@
         s = self.str
         result, (arg1, arg2) = self.result, self.args
         cl_op = self.binary_ops[op]
-        print "(setf", s(result), "(", cl_op, s(arg1), s(arg2), "))"
+        yield "(setf %s (%s %s %s))" % (s(result), cl_op, s(arg1), s(arg2))
 
-    def op_contains(self):
-        s = self.str
-        result, (arg1, arg2) = self.result, self.args
-        print "(setf", s(result), "(not (not (find", s(arg2), s(arg1), "))))"
-
-    def op_add(self):
-        s = self.str
-        result, (arg1, arg2) = self.result, self.args
-        print "(setf", s(result)
-        table = {
-            (int, int): "(+ %s %s)",
-            (int, long): "(+ %s %s)",
-            (long, int): "(+ %s %s)",
-            (long, long): "(+ %s %s)",
-            (str, str): "(concatenate 'string %s %s)",
-            (list, list): "(concatenate 'vector %s %s)",
-        }
-        self.gen.emit_typecase(table, arg1, arg2)
-        print ")"
-
-    def op_not_(self):
-        s = self.str
-        result, (arg1,) = self.result, self.args
-        print "(setf", s(result), "(not"
-        table = {
-            (bool,): "(not %s)",
-            (int,): "(zerop %s)",
-            (long,): "(zerop %s)",
-            (list,): "(zerop (length %s))",
-        }
-        self.gen.emit_typecase(table, arg1)
-        print "))"
-
-    def op_is_true(self, arg):
-        print "(setf", self.str(self.result)
-        table = {
-            (bool,): "%s",
-            (int,): "(not (zerop %s))",
-            (long,): "(not (zerop %s))",
-            (list,): "(not (zerop (length %s)))",
-        }
-        self.gen.emit_typecase(table, arg)
-        print ")"
-    
     def op_int_is_true(self):
-        self.op_is_true(self.args[0])
+        target = self.str(self.result) 
+        arg = self.str(self.args[0])
+        yield "(setf %s (not (zerop %s)))" % (target, arg)
 
     def declare_class(self, cls):
         # cls is really type of Instance
@@ -118,18 +73,20 @@
     def op_new(self):
         cls = self.args[0].value
         if isinstance(cls, List):
-            self.op_new_list(cls)
+            generator = self.op_new_list
         else:
-            self.op_new_instance(cls)
+            generator = self.op_new_instance
+        for line in generator(cls):
+            yield line
 
     def op_new_list(self, cls):
         target = self.str(self.result)
-        print "(setf %s (make-array 0 :adjustable t))" % (target,)
+        yield "(setf %s (make-array 0 :adjustable t))" % (target,)
 
     def op_new_instance(self, cls):
-        print self.declare_class(cls)
+        yield self.declare_class(cls)
         target = self.str(self.result)
-        print "(setf %s (make-%s))" % (target, cls._name)
+        yield "(setf %s (make-%s))" % (target, cls._name)
 
     def op_oosend(self):
         method = self.args[0].value
@@ -144,90 +101,17 @@
         clsname = self.args[0].concretetype._name
         fieldname = self.args[1].value
         obj = self.str(self.args[0])
-        print "(setf %s (%s-%s %s))" % (target, clsname, fieldname, obj)
+        yield "(setf %s (%s-%s %s))" % (target, clsname, fieldname, obj)
 
     def op_oosetfield(self):
         target = self.str(self.result)
         clsname = self.args[0].concretetype._name
         fieldname = self.args[1].value
         if fieldname == "meta": # XXX
-            return
+            raise StopIteration
         obj = self.str(self.args[0])
         fieldvalue = self.str(self.args[2])
-        print "(setf (%s-%s %s) %s)" % (clsname, fieldname, obj, fieldvalue)
-
-    def op_newtuple(self):
-        s = self.str
-        print "(setf", s(self.result), "(list",
-        for arg in self.args:
-            print s(arg),
-        print "))"
-
-    def op_newlist(self):
-        s = self.str
-        print "(setf", s(self.result), "(vector",
-        for arg in self.args:
-            print s(arg),
-        print "))"
-
-    def op_alloc_and_set(self):
-        s = self.str
-        result, (size, init) = self.result, self.args
-        print "(setf", s(result), "(make-array", s(size), "))"
-        print "(fill", s(result), s(init), ")"
-
-    def op_setitem(self):
-        s = self.str
-        (seq, index, element) = self.args
-        print "(setf (elt", s(seq), s(index), ")", s(element), ")"
-
-    def op_iter(self):
-        s = self.str
-        result, (seq,) = self.result, self.args
-        print "(setf", s(result), "(make-iterator", s(seq), "))"
-
-    def op_next(self):
-        s = self.str
-        result, (iterator,) = self.result, self.args
-        print "(let ((result (funcall", s(iterator), ")))"
-        print "  (setf", s(result), "(car result))"
-        print "  (setf last-exc (cdr result)))"
-
-    builtin_map = {
-        pow: "expt",
-        range: "python-range",
-    }
-
-    def op_simple_call(self):
-        func = self.args[0]
-        if not isinstance(func, Constant):
-            self.op_default()
-            return
-        func = func.value
-        if func not in self.builtin_map:
-            self.op_default()
-            return
-        s = self.str
-        args = self.args[1:]
-        print "(setf", s(self.result), "(", self.builtin_map[func],
-        for arg in args:
-            print s(arg),
-        print "))"
-
-    def op_getslice(self):
-        s = self.str
-        result, (seq, start, end) = self.result, self.args
-        print "(setf", s(result), "(python-slice", s(seq), s(start), s(end), "))"
-
-    def op_pow(self):
-        s = self.str
-        result, (x,y,z) = self.result, self.args
-        print "(setf", s(result)
-        table = {
-            (int, int, type(None)): (lambda args: args[:2], "(expt %s %s)"),
-        }
-        self.gen.emit_typecase(table, x, y, z)
-        print ")"
+        yield "(setf (%s-%s %s) %s)" % (clsname, fieldname, obj, fieldvalue)
 
 
 class ListImpl:
@@ -272,27 +156,22 @@
         return var.concretetype
 
     def emitcode(self, public=True):
-        import sys
-        from cStringIO import StringIO
-        out = StringIO()
-        oldstdout = sys.stdout
-        sys.stdout = out
-        self.emit()
-        sys.stdout = oldstdout
-        return out.getvalue()
+        code = "\n".join(list(self.emit()))
+        return code
 
     def emit(self):
-        self.emit_defun(self.fun)
+        for line in self.emit_defun(self.fun):
+            yield line
 
     def emit_defun(self, fun):
-        print ";;;; Main"
-        print "(defun", fun.name,
+        yield ";;;; Main"
+        yield "(defun " + fun.name
         arglist = fun.getargs()
-        print "(",
+        yield "("
         for arg in arglist:
-            print repr_var(arg),
-        print ")"
-        print "(prog"
+            yield repr_var(arg)
+        yield ")"
+        yield "(prog"
         blocklist = list(fun.iterblocks())
         vardict = {}
         for block in blocklist:
@@ -300,130 +179,82 @@
             self.blockref[block] = tag
             for var in block.getvariables():
                 vardict[var] = self.get_type(var)
-        print "( last-exc",
+        yield "( last-exc"
         for var in vardict:
             if var in arglist:
-                print "(", repr_var(var), repr_var(var), ")",
+                yield "(%s %s)" % (repr_var(var), repr_var(var))
             else:
-                print repr_var(var),
-        print ")"
-        print "(setf last-exc nil)"
+                yield repr_var(var)
+        yield ")"
+        yield "(setf last-exc nil)"
         for block in blocklist:
-            print
-            self.emit_block(block)
-        print "))"
+            yield ""
+            for line in self.emit_block(block):
+                yield line
+        yield "))"
 
     def emit_block(self, block):
         self.cur_block = block
         tag = self.blockref[block]
-        print "tag" + str(tag)
+        yield "tag" + str(tag)
         for op in block.operations:
             emit_op = Op(self, op)
-            emit_op()
+            for line in emit_op:
+                yield line
         exits = block.exits
         if len(exits) == 1:
-            self.emit_link(exits[0])
+            for line in self.emit_link(exits[0]):
+                yield line
         elif len(exits) > 1:
             # only works in the current special case
             if (len(exits) == 2 and
                 exits[0].exitcase == False and
                 exits[1].exitcase == True):
-                print "(if", repr_arg(block.exitswitch)
-                print "(progn"
-                self.emit_link(exits[1])
-                print ") ; else"
-                print "(progn"
-                self.emit_link(exits[0])
-                print "))"
+                yield "(if " + repr_arg(block.exitswitch)
+                yield "(progn"
+                for line in self.emit_link(exits[1]):
+                    yield line
+                yield ") ; else"
+                yield "(progn"
+                for line in self.emit_link(exits[0]):
+                    yield line
+                yield "))"
             else:
                 # this is for the more general case.  The previous special case
                 # shouldn't be needed but in Python 2.2 we can't tell apart
                 # 0 vs nil  and  1 vs t  :-(
                 for exit in exits[:-1]:
-                    print "(if (equalp", repr_arg(block.exitswitch),
-                    print repr_const(exit.exitcase), ')'
-                    print "(progn"
-                    self.emit_link(exit)
-                    print ")"
-                print "(progn ; else should be", repr_const(exits[-1].exitcase)
-                self.emit_link(exits[-1])
-                print ")" * len(exits)
+                    yield "(if (equalp " + repr_arg(block.exitswitch)
+                    yield repr_const(exit.exitcase) + ')'
+                    yield "(progn"
+                    for line in self.emit_link(exit):
+                        yield line
+                    yield ")"
+                yield "(progn ; else should be %s" % repr_const(exits[-1].exitcase)
+                for line in self.emit_link(exits[-1]):
+                    yield line
+                yield ")" * len(exits)
         elif len(block.inputargs) == 2:    # exc_cls, exc_value
             exc_cls   = repr_var(block.inputargs[0])
             exc_value = repr_var(block.inputargs[1])
-            print "(something-like-throw-exception %s %s)" % (exc_cls, exc_value)
+            yield "(something-like-throw-exception %s %s)" % (exc_cls, exc_value)
         else:
             retval = repr_var(block.inputargs[0])
-            print "(return", retval, ")",
+            yield "(return %s )" % retval
 
-    def emit_jump(self, block):
+    def format_jump(self, block):
         tag = self.blockref[block]
-        print "(go", "tag" + str(tag), ")"
+        return "(go tag" + str(tag) + ")"
 
     def emit_link(self, link):
         source = map(repr_arg, link.args)
         target = map(repr_var, link.target.inputargs)
-        print "(setf",
+        yield "(setf"
         couples = zip(source, target)
         for s, t in couples[:-1]:
-            print t, s
+            yield "%s %s" % (t, s)
         else:
             s, t = couples[-1]
-            print t, s, ")"
-        self.emit_jump(link.target)
+            yield "%s %s)" % (t, s)
+        yield self.format_jump(link.target)
 
-    typemap = {
-        bool: "boolean",
-        int: "fixnum",
-        long: "bignum",
-        str: "string",
-        list: "vector",
-    }
-
-    def emit_typecase(self, table, *args):
-        argreprs = tuple(map(repr_arg, args))
-        argtypes = tuple(map(self.get_type, args))
-        if argtypes in table:
-            trans = table[argtypes]
-            if isinstance(trans, tuple): # (manip-args, code-template)
-                manip, trans = trans
-                argreprs = manip(argreprs)                
-            print trans % argreprs
-        else:
-            print "(cond"
-            for argtypes in table:
-                print "((and",
-                for tp, s in zip(argtypes, argreprs):
-                    cl_tp = "'" + self.typemap[tp]
-                    print "(typep", s, cl_tp, ")",
-                print ")"
-                trans = table[argtypes]
-                print trans % argreprs,
-                print ")"
-            print ")"
-
-    def globaldeclarations(self):
-        return prelude
-
-
-prelude = """;;;; Prelude
-(defun make-iterator (seq)
-  (let ((i 0))
-    (lambda ()
-      (if (< i (length seq))
-          (let ((v (elt seq i))) (incf i) (cons v nil))
-          (cons nil 'StopIteration)))))
-(defun python-slice (seq start end)
-  (let ((l (length seq)))
-    (if (not start) (setf start 0))
-    (if (not end) (setf end l))
-    (if (minusp start) (incf start l))
-    (if (minusp end) (incf end l))
-    (subseq seq start end)))
-;; temporary
-(defun python-range (end)
-  (let ((a (make-array end)))
-    (loop for i below end
-          do (setf (elt a i) i)
-          finally (return a))))
-"""



More information about the Pypy-commit mailing list