[pypy-svn] r18471 - in pypy/dist/pypy/translator/js: . test

ericvrp at codespeak.net ericvrp at codespeak.net
Wed Oct 12 15:51:54 CEST 2005


Author: ericvrp
Date: Wed Oct 12 15:51:52 2005
New Revision: 18471

Modified:
   pypy/dist/pypy/translator/js/arraynode.py
   pypy/dist/pypy/translator/js/codewriter.py
   pypy/dist/pypy/translator/js/database.py
   pypy/dist/pypy/translator/js/funcnode.py
   pypy/dist/pypy/translator/js/js.py
   pypy/dist/pypy/translator/js/structnode.py
   pypy/dist/pypy/translator/js/test/test_genllvm1.py
Log:
working on (Gc)Structs


Modified: pypy/dist/pypy/translator/js/arraynode.py
==============================================================================
--- pypy/dist/pypy/translator/js/arraynode.py	(original)
+++ pypy/dist/pypy/translator/js/arraynode.py	Wed Oct 12 15:51:52 2005
@@ -166,7 +166,7 @@
             else:
                 s.append("\\%02x" % ord(c))
                 
-        r = 'c"%s"' % "".join(s)
+        r = '"%s"' % "".join(s)
         return item_length, r
 
 class VoidArrayNode(ConstantLLVMNode):

Modified: pypy/dist/pypy/translator/js/codewriter.py
==============================================================================
--- pypy/dist/pypy/translator/js/codewriter.py	(original)
+++ pypy/dist/pypy/translator/js/codewriter.py	Wed Oct 12 15:51:52 2005
@@ -71,29 +71,39 @@
         #self.llvm("implementation", 0)
         pass
 
-    def br_uncond(self, block): 
-        self.append('prevblock = ' + str(self._currentblock))
+    def _goto_block(self, block, indentation_level=4):
         if block == self._currentblock + 1:
             self._skip_closeblock = True
         else:
-            self.append('block = ' + str(block))
+            self.append('block = ' + str(block), indentation_level)
+            self.append('break', indentation_level)
 
-    def br(self, cond, block_false, block_true):
-        self.append('prevblock = ' + str(self._currentblock))
+    def _phi(self, targetblock, exit, indentation_level=4):
+        #self.comment('target.inputargs=%s, args=%s, targetblock=%d' % (exit.target.inputargs, exit.args, targetblock), indentation_level)
+        for i, exitarg in enumerate(exit.args):
+            dest = str(exit.target.inputargs[i])
+            src = str(exitarg)
+            if src == 'False':
+                src = 'false'
+            elif src == 'True':
+                src = 'true'
+            if dest != src:
+                self.append('%s = %s' % (dest, src), indentation_level)
+
+    def br_uncond(self, block, exit): 
+        self._phi(block, exit)
+        self._goto_block(block)
+        self._skip_closeblock = True
+
+    def br(self, cond, block_false, exit_false, block_true, exit_true):
         self.append('if (%s) {' % cond)
-        if block_true == self._currentblock + 1:
-            self._skip_closeblock = True
-        else:
-            self.append('block = ' + str(block_true), 5)
-            self.append('break', 5)
-        if block_false == self._currentblock + 1:
-            self._skip_closeblock = True
-        else:
-            self.append('} else {')
-            self.append('block = ' + str(block_false), 5)
-            self.append('break', 5)
+        self._phi(block_true, exit_true, 5)
+        self._goto_block(block_true, 5)
+        self.append('} else {')
+        self._phi(block_false, exit_false, 5)
+        self._goto_block(block_false, 5)
         self.append('}')
-        self.comment('block = %s ? %d : %d' % (cond, block_true, block_false))
+        self._skip_closeblock = True
 
     def switch(self, intty, cond, defaultdest, value_label):
         labels = ''
@@ -108,6 +118,10 @@
         self.blocks   = blocks
         usedvars      = {}  #XXX could probably be limited to inputvars
         for block in blocks:
+            if block != blocks[0]:  #don't double startblock inputargs
+                for inputarg in block.inputargs:
+                    targetvar = self.js.db.repr_arg(inputarg)
+                    usedvars[targetvar] = True
             for op in block.operations:
                 targetvar = self.js.db.repr_arg(op.result)
                 usedvars[targetvar] = True
@@ -130,36 +144,6 @@
             self.append("return " + ref)
         self._skip_closeblock = True
 
-    def phi(self, targetvar, type_, refs, blocks): 
-        assert refs and len(refs) == len(blocks), "phi node requires blocks" 
-        mergelist = ", ".join(
-            ["[%s, %s]" % item 
-                for item in zip(refs, blocks)])
-        s = "%s = phi %s %s" % (targetvar, type_, mergelist)
-        self.llvm(s)
-
-        all_refs_identical = True
-        for ref in refs:
-            if ref != refs[0]:
-                all_refs_identical = False
-                break
-        if all_refs_identical:
-            if targetvar != refs[0]:
-                self.append('%s = %s' % (targetvar, refs[0]))
-        else:
-            if len(blocks) == 1:
-                self.append('%s = %s' % (targetvar, refs[i]))
-            else:
-                n = 0
-                for i, block in enumerate(blocks):
-                    if targetvar != refs[i]:
-                        if n > 0:
-                            s = 'else '
-                        else:
-                            s = ''
-                        self.append('%sif (prevblock == %d) %s = %s' % (s, block, targetvar, refs[i]))
-                        n += 1
-
     def binaryop(self, name, targetvar, type_, ref1, ref2):
         self.append("%(targetvar)s = %(ref1)s %(name)s %(ref2)s" % locals())
 

Modified: pypy/dist/pypy/translator/js/database.py
==============================================================================
--- pypy/dist/pypy/translator/js/database.py	(original)
+++ pypy/dist/pypy/translator/js/database.py	Wed Oct 12 15:51:52 2005
@@ -260,7 +260,8 @@
         type_ = lltype.typeOf(value)
         if isinstance(type_, lltype.Primitive):
             repr = self.primitive_to_str(type_, value)
-            return None, "%s %s" % (self.repr_type(type_), repr)
+            return None, repr
+            #return None, "%s %s" % (self.repr_type(type_), repr)
 
         elif isinstance(type_, lltype.Ptr):
             toptr = self.repr_type(type_)

Modified: pypy/dist/pypy/translator/js/funcnode.py
==============================================================================
--- pypy/dist/pypy/translator/js/funcnode.py	(original)
+++ pypy/dist/pypy/translator/js/funcnode.py	Wed Oct 12 15:51:52 2005
@@ -42,14 +42,15 @@
     def __init__(self, db, value):
         self.db = db
         self.value = value
-        self.ref   = self.make_ref('pypy_', value.graph.name)
+        pypy_prefix     = '' #pypy_
+        self.ref   = self.make_ref(pypy_prefix, value.graph.name)
         self.graph = value.graph
 
         self.db.genllvm.exceptionpolicy.transform(self.db.translator, self.graph)
         #remove_exception_mallocs(self.db.translator, self.graph, self.ref)
         #merge_mallocs(self.db.translator, self.graph, self.ref)
 
-        remove_double_links(self.db.translator, self.graph)
+        #remove_double_links(self.db.translator, self.graph)
 
     def __str__(self):
         return "<FuncNode %r>" %(self.ref,)
@@ -145,30 +146,30 @@
         return self.ref + "(%s)" % ", ".join(inputargs)
 
     def write_block(self, codewriter, block):
-        self.write_block_phi_nodes(codewriter, block)
+        #self.write_block_phi_nodes(codewriter, block)
         self.write_block_operations(codewriter, block)
         self.write_block_branches(codewriter, block)
 
-    def get_phi_data(self, block):
-        data = []
-        entrylinks = mkentrymap(self.graph)[block]
-        entrylinks = [x for x in entrylinks if x.prevblock is not None]
-        inputargs = self.db.repr_arg_multi(block.inputargs)
-        inputargtypes = self.db.repr_arg_type_multi(block.inputargs)
-        for i, (arg, type_) in enumerate(zip(inputargs, inputargtypes)):
-            names = self.db.repr_arg_multi([link.args[i] for link in entrylinks])
-            blocknames = [self.blockindex[link.prevblock] for link in entrylinks]
-            for i, link in enumerate(entrylinks):   #XXX refactor into a transformation
-                if link.prevblock.exitswitch == Constant(last_exception) and \
-                   link.prevblock.exits[0].target != block:
-                    blocknames[i] += '_exception_found_branchto_' + self.blockindex[block]
-            data.append( (arg, type_, names, blocknames) )
-        return data
-
-    def write_block_phi_nodes(self, codewriter, block):
-        for arg, type_, names, blocknames in self.get_phi_data(block):
-            if type_ != "void":
-                codewriter.phi(arg, type_, names, blocknames)
+    #def get_phi_data(self, block):
+    #    data = []
+    #    entrylinks = mkentrymap(self.graph)[block]
+    #    entrylinks = [x for x in entrylinks if x.prevblock is not None]
+    #    inputargs = self.db.repr_arg_multi(block.inputargs)
+    #    inputargtypes = self.db.repr_arg_type_multi(block.inputargs)
+    #    for i, (arg, type_) in enumerate(zip(inputargs, inputargtypes)):
+    #        names = self.db.repr_arg_multi([link.args[i] for link in entrylinks])
+    #        blocknames = [self.blockindex[link.prevblock] for link in entrylinks]
+    #        for i, link in enumerate(entrylinks):   #XXX refactor into a transformation
+    #            if link.prevblock.exitswitch == Constant(last_exception) and \
+    #               link.prevblock.exits[0].target != block:
+    #                blocknames[i] += '_exception_found_branchto_' + self.blockindex[block]
+    #        data.append( (arg, type_, names, blocknames) )
+    #    return data
+    #
+    #def write_block_phi_nodes(self, codewriter, block):
+    #    for arg, type_, names, blocknames in self.get_phi_data(block):
+    #        if type_ != "void":
+    #            codewriter.phi(arg, type_, names, blocknames)
 
     def write_block_branches(self, codewriter, block):
         #assert len(block.exits) <= 2    #more exits are possible (esp. in combination with exceptions)
@@ -176,11 +177,12 @@
             #codewriter.comment('FuncNode(ConstantLLVMNode) *last_exception* write_block_branches @%s@' % str(block.exits))
             return
         if len(block.exits) == 1:
-            codewriter.br_uncond(self.blockindex[block.exits[0].target])
+            codewriter.br_uncond(self.blockindex[block.exits[0].target], block.exits[0])
         elif len(block.exits) == 2:
             cond = self.db.repr_arg(block.exitswitch)
-            codewriter.br(cond, self.blockindex[block.exits[0].target],
-                          self.blockindex[block.exits[1].target])
+            codewriter.br(cond,
+                          self.blockindex[block.exits[0].target], block.exits[0],
+                          self.blockindex[block.exits[1].target], block.exits[1])
 
     def write_block_operations(self, codewriter, block):
         opwriter = OpWriter(self.db, codewriter, self, block)
@@ -212,7 +214,7 @@
 
     def write_returnblock(self, codewriter, block):
         assert len(block.inputargs) == 1
-        self.write_block_phi_nodes(codewriter, block)
+        #self.write_block_phi_nodes(codewriter, block)
         inputargtype = self.db.repr_arg_type(block.inputargs[0])
         inputarg = self.db.repr_arg(block.inputargs[0])
         codewriter.ret(inputargtype, inputarg)

Modified: pypy/dist/pypy/translator/js/js.py
==============================================================================
--- pypy/dist/pypy/translator/js/js.py	(original)
+++ pypy/dist/pypy/translator/js/js.py	Wed Oct 12 15:51:52 2005
@@ -105,13 +105,15 @@
         for typ_decl in self.db.getnodes():
             typ_decl.writeimpl(codewriter)
 
+        pypy_prefix = '' #pypy_
+
         #codewriter.append(self.exceptionpolicy.llvmcode(self.entrynode))
         #
         ## XXX we need to create our own main() that calls the actual entry_point function
-        #if entryfunc_name == 'pypy_entry_point': #XXX just to get on with translate_pypy
+        #if entryfunc_name == pypy_prefix + 'entry_point': #XXX just to get on with translate_pypy
         #    extfuncnode.ExternalFuncNode.used_external_functions['%main'] = True
         #
-        #elif entryfunc_name == 'pypy_main_noargs': #XXX just to get on with bpnn & richards
+        #elif entryfunc_name == pypy_prefix + 'main_noargs': #XXX just to get on with bpnn & richards
         #    extfuncnode.ExternalFuncNode.used_external_functions['%main_noargs'] = True
         #
         #for f in support_functions:
@@ -134,7 +136,7 @@
         graph      = self.db.obj2node[entry_point].graph
         startblock = graph.startblock
         args       = ','.join(['arguments[%d]' % i for i,v in enumerate(startblock.inputargs)])
-        self.wrappertemplate = "load('%s'); print(pypy_%s(%%s))" % (self.filename, graph.name)
+        self.wrappertemplate = "load('%s'); print(%s%s(%%s))" % (self.filename, pypy_prefix, graph.name)
 
         #codewriter.newline()
         #codewriter.comment("Wrapper code for the Javascript CLI", 0)

Modified: pypy/dist/pypy/translator/js/structnode.py
==============================================================================
--- pypy/dist/pypy/translator/js/structnode.py	(original)
+++ pypy/dist/pypy/translator/js/structnode.py	Wed Oct 12 15:51:52 2005
@@ -147,9 +147,16 @@
     
     def constantvalue(self):
         """ Returns the constant representation for this node. """
-        values = self._getvalues()
-        all_values = ",\n  ".join(values)
-        return "%s {\n  %s\n  }\n" % (self.get_typerepr(), all_values)
+        vars = []
+        for i, value in enumerate(self._getvalues()):
+            name = self._get_types[i][0]
+            var  = (name, str(value))
+            vars.append(var)
+        return "(%s)" % ", ".join(["{%s:%s}" % var for var in vars])
+
+        #values = self._getvalues()
+        #all_values = ",\n  ".join(values)
+        #return "%s {\n  %s\n  }\n" % (self.get_typerepr(), all_values)
                 
                 
 class StructVarsizeNode(StructNode):

Modified: pypy/dist/pypy/translator/js/test/test_genllvm1.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_genllvm1.py	(original)
+++ pypy/dist/pypy/translator/js/test/test_genllvm1.py	Wed Oct 12 15:51:52 2005
@@ -28,7 +28,7 @@
 
     def test_ackermann(self):
         f = compile_function(llvmsnippet.ackermann, [int, int])
-        for i in range(10):
+        for i in range(7):  #>7 js error: too much recursion?!?
             assert f(0, i) == i + 1
             assert f(1, i) == i + 2
             assert f(2, i) == 2 * i + 3



More information about the Pypy-commit mailing list