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

ericvrp at codespeak.net ericvrp at codespeak.net
Mon Oct 10 22:03:04 CEST 2005


Author: ericvrp
Date: Mon Oct 10 22:03:02 2005
New Revision: 18350

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/exception.py
   pypy/dist/pypy/translator/js/funcnode.py
   pypy/dist/pypy/translator/js/gc.py
   pypy/dist/pypy/translator/js/opwriter.py
   pypy/dist/pypy/translator/js/test/test_lltype.py
   pypy/dist/pypy/translator/js/varsize.py
Log:
Intermediate commit, need checkpoint.
Working on arrays.


Modified: pypy/dist/pypy/translator/js/arraynode.py
==============================================================================
--- pypy/dist/pypy/translator/js/arraynode.py	(original)
+++ pypy/dist/pypy/translator/js/arraynode.py	Mon Oct 10 22:03:02 2005
@@ -43,7 +43,7 @@
     #
     def writedatatypedecl(self, codewriter):
         codewriter.arraydef(self.ref,
-                            self.db.get_machine_word(),
+                            'int',
                             self.db.repr_type(self.arraytype))
 
     def writedecl(self, codewriter): 
@@ -67,7 +67,7 @@
         self.ref = "arraytype_Void"
 
     def writedatatypedecl(self, codewriter):
-        td = "%s = type { %s }" % (self.ref, self.db.get_machine_word())
+        td = "%s = type { int }" % self.ref
         codewriter.append(td)
         
 class ArrayNode(ConstantLLVMNode):
@@ -83,7 +83,7 @@
         self.db = db
         self.value = value
         self.arraytype = lltype.typeOf(value).OF
-        prefix = '%arrayinstance'
+        prefix = 'arrayinstance'
         name = '' #str(value).split()[1]
         self.ref = self.make_ref(prefix, name)
 
@@ -113,18 +113,15 @@
     def get_typerepr(self):
         arraylen = self.get_arrayvalue()[0]
         typeval = self.db.repr_type(self.arraytype)
-        return "{ %s, [%s x %s] }" % (self.db.get_machine_word(),
-                                      arraylen, typeval)
+        return "{ int, [%s x %s] }" % (arraylen, typeval)
 
     def get_ref(self):
-        typeval = self.db.repr_type(lltype.typeOf(self.value))
-        ref = "cast (%s* %s to %s*)" % (self.get_typerepr(),
-                                        self.ref,
-                                        typeval)
-
+        #typeval = self.db.repr_type(lltype.typeOf(self.value))
+        #ref = "cast (%s* %s to %s*)" % (self.get_typerepr(), self.ref, typeval)
         p, c = lltype.parentlink(self.value)
         assert p is None, "child arrays are NOT needed by rtyper"
-        return ref
+        #return ref
+        return self.ref
 
     def get_pbcref(self, toptr):
         ref = self.ref
@@ -146,11 +143,14 @@
         typeval = self.db.repr_type(self.arraytype)
 
         # first length is logical, second is physical
-        value = "%s %s, [%s x %s] %s" % (self.db.get_machine_word(),
-                                         self.get_length(),
-                                         physicallen,
-                                         typeval,
-                                         arrayrepr)
+        value = "[%s, %s]" % (self.get_length(), arrayrepr)
+        return value
+
+        # first length is logical, second is physical
+        value = "int %s, [%s x %s] %s" % (self.get_length(),
+                                          physicallen,
+                                          typeval,
+                                          arrayrepr)
 
         s = "%s {%s}" % (self.get_typerepr(), value)
         return s
@@ -186,11 +186,9 @@
         assert isinstance(lltype.typeOf(value), lltype.Array)
         self.db = db
         self.value = value
-        prefix = '%arrayinstance'
+        prefix = 'arrayinstance'
         name = '' #str(value).split()[1]
         self.ref = self.make_ref(prefix, name)
 
     def constantvalue(self):
-        return "{ %s } {%s %s}" % (self.db.get_machine_word(),
-                                   self.db.get_machine_word(),
-                                   len(self.value.items))
+        return "{ int } {int %s}" % len(self.value.items)

Modified: pypy/dist/pypy/translator/js/codewriter.py
==============================================================================
--- pypy/dist/pypy/translator/js/codewriter.py	(original)
+++ pypy/dist/pypy/translator/js/codewriter.py	Mon Oct 10 22:03:02 2005
@@ -33,20 +33,25 @@
     openblock = label
 
     def closeblock(self):
-        self.append('break')
+        self.append('continue')
 
-    def globalinstance(self, name, typeandata):
-        self.llvm("%s = %s global %s" % (name, "internal", typeandata))
+    def globalinstance(self, name, typeanddata):
+        #self.append('%s = %s' % (name, typeanddata[1:].split('{')[1][:-1]), 0)
+        lines = typeanddata.split('\n')
+        #self.llvm("%s = global %s" % (name, lines[0]), 0)
+        self.append("%s = %s" % (name, lines[0]), 0)
+        for line in lines[1:]:
+            self.llvm(line, 0)
 
     def structdef(self, name, typereprs):
-        self.llvm("%s = type { %s }" %(name, ", ".join(typereprs)))
+        self.llvm("%s = type { %s }" %(name, ", ".join(typereprs)), 0)
 
     def arraydef(self, name, lentype, typerepr):
-        self.llvm("%s = type { %s, [0 x %s] }" % (name, lentype, typerepr))
+        self.llvm("%s = type { %s, [0 x %s] }" % (name, lentype, typerepr), 0)
 
     def funcdef(self, name, rettyperepr, argtypereprs):
         self.llvm("%s = type %s (%s)" % (name, rettyperepr,
-                                           ", ".join(argtypereprs)))
+                                           ", ".join(argtypereprs)), 0)
 
     def declare(self, decl):
         #self.llvm("declare %s" % decl, 0)
@@ -139,20 +144,25 @@
             self.llvm("%s = %s %s %s, %s" % (targetvar, name, type_, ref1, ref2))
 
     def call(self, targetvar, returntype, functionref, argrefs, argtypes, label=None, except_label=None):
-        args = ", ".join(["%s %s" % item for item in zip(argtypes, argrefs)])
+        #args = ", ".join(["%s %s" % item for item in zip(argtypes, argrefs)])
+        args = ", ".join(argrefs)
         if except_label:
             self.js.exceptionpolicy.invoke(self, targetvar, returntype, functionref, args, label, except_label)
         else:
             if returntype == 'void':
-                self.llvm("call void %s(%s)" % (functionref, args))
+                #self.llvm("call void %s(%s)" % (functionref, args))
+                self.append('%s(%s)' % (functionref, args))
             else:
-                self.llvm("%s = call %s %s(%s)" % (targetvar, returntype, functionref, args))
+                #self.llvm("%s = call %s %s(%s)" % (targetvar, returntype, functionref, args))
+                self.append('%s = %s(%s)' % (targetvar, functionref, args))
 
     def cast(self, targetvar, fromtype, fromvar, targettype):
+        self.comment('codewriter cast 1 targettype=%(targettype)s, targetvar=%(targetvar)s, fromtype=%(fromtype)s, fromvar=%(fromvar)s' % locals())
     	if fromtype == 'void' and targettype == 'void':
 		return
+        self.comment('codewriter cast 2')
         if targettype == fromtype:
-            self.append("%(targetvar)s = %(fromvar)s%(convfunc)s" % locals())
+            self.append("%(targetvar)s = %(fromvar)s" % locals())
         elif targettype in ('int','uint',):
             self.append("%(targetvar)s = Math.floor(%(fromvar)s)" % locals())
         elif targettype in ('double',):
@@ -170,16 +180,30 @@
         res = "%(targetvar)s = getelementptr %(type)s %(typevar)s, word 0, " % locals()
         res += ", ".join(["%s %s" % (t, i) for t, i in indices])
         self.llvm(res)
-
-    def load(self, targetvar, targettype, ptr):
-        self.llvm("%(targetvar)s = load %(targettype)s* %(ptr)s" % locals())
-
-    def store(self, valuetype, valuevar, ptr): 
-        self.llvm("store %(valuetype)s %(valuevar)s, "
-                    "%(valuetype)s* %(ptr)s" % locals())
+        
+        #res = "%(targetvar)s = %(typevar)s" % locals()
+        #res += ''.join(['[%s]' % i for t, i in indices])
+        #self.append(res)
+
+    #def load(self, targetvar, targettype, ptr):
+    #    self.llvm("%(targetvar)s = load %(targettype)s* %(ptr)s" % locals())
+
+    def load(self, destvar, src, srcindices):
+        res  = "%(destvar)s = %(src)s" % locals()
+        res += ''.join(['[%s]' % index for index in srcindices])
+        self.append(res)
+
+    #def store(self, valuetype, valuevar, ptr): 
+    #    self.llvm("store %(valuetype)s %(valuevar)s, %(valuetype)s* %(ptr)s" % locals())
+
+    def store(self, dest, destindices, srcvar):
+        res  = dest
+        res += ''.join(['[%s]' % index for index in destindices])
+        res += " = %(srcvar)s" % locals()
+        self.append(res)
 
     def debugcomment(self, tempname, len, tmpname):
-        res = "%s = call ccc %(word)s (sbyte*, ...)* printf(" % locals()
+        res = "%s = call %(word)s (sbyte*, ...)* printf(" % locals()
         res += "sbyte* getelementptr ([%s x sbyte]* %s, word 0, word 0) )" % locals()
         res = res % (tmpname, len, tmpname)
         self.llvm(res)

Modified: pypy/dist/pypy/translator/js/database.py
==============================================================================
--- pypy/dist/pypy/translator/js/database.py	(original)
+++ pypy/dist/pypy/translator/js/database.py	Mon Oct 10 22:03:02 2005
@@ -16,6 +16,16 @@
 log = log.database 
 
 class Database(object): 
+
+    primitives = {
+            lltype.Char: "sbyte",
+            lltype.Bool: "bool",
+            lltype.Float: "double",
+            lltype.Signed: "int",
+            lltype.Unsigned: "uint",
+            lltype.UniChar: "uint",
+            lltype.Void: "void"}
+
     def __init__(self, genllvm, translator): 
         self.genllvm = genllvm
         self.translator = translator
@@ -26,33 +36,6 @@
         # debug operation comments
         self._opcomments = {}
 
-        self.primitives_init()
-
-    def primitives_init(self):
-        primitives = {
-            lltype.Char: "sbyte",
-            lltype.Bool: "bool",
-            lltype.Float: "double",
-            lltype.UniChar: "uint",
-            lltype.Void: "void"}
-
-        # 32 bit platform
-        if sys.maxint == 2**31-1:
-            primitives.update({
-                lltype.Signed: "int",
-                lltype.Unsigned: "uint" })
-            
-        # 64 bit platform
-        elif sys.maxint == 2**63-1:        
-            primitives.update({
-                lltype.Signed: "long",
-                lltype.Unsigned: "ulong" })
-            
-        else:
-            assert False, "Unsupported platform"        
-
-        self.primitives = primitives
-        
     #_______for debugging llvm code_________________________
 
     def add_op2comment(self, lenofopstr, op):
@@ -347,12 +330,6 @@
             repr = str(value)
         return repr
 
-    def get_machine_word(self):
-        return self.primitives[lltype.Signed]
-
-    def get_machine_uword(self):
-        return self.primitives[lltype.Unsigned]
-
     # __________________________________________________________
     # Other helpers
 

Modified: pypy/dist/pypy/translator/js/exception.py
==============================================================================
--- pypy/dist/pypy/translator/js/exception.py	(original)
+++ pypy/dist/pypy/translator/js/exception.py	Mon Oct 10 22:03:02 2005
@@ -3,7 +3,7 @@
     RINGBUFFER_ENTRY_MAXSIZE = 16
     RINGBUGGER_OVERSIZE      = RINGBUGGER_SIZE + RINGBUFFER_ENTRY_MAXSIZE
     RINGBUFFER_LLVMCODE      = '''
-internal fastcc sbyte* %%malloc_exception(uint %%nbytes) {
+sbyte* %%malloc_exception(uint %%nbytes) {
     %%cond = setle uint %%nbytes, %d
     br bool %%cond, label %%then, label %%else
 
@@ -16,7 +16,7 @@
     ret sbyte* %%tmp.4
 
 else:
-    %%tmp.8  = call ccc sbyte* %%GC_malloc(uint %%nbytes)
+    %%tmp.8  = call sbyte* %%GC_malloc(uint %%nbytes)
     ret sbyte* %%tmp.8
 }
 ''' % (RINGBUFFER_ENTRY_MAXSIZE, RINGBUGGER_OVERSIZE, RINGBUGGER_SIZE-1)
@@ -46,7 +46,7 @@
         return noresult
 
     def new(exceptionpolicy=None):  #factory
-        exceptionpolicy = exceptionpolicy or 'explicit'
+        exceptionpolicy = exceptionpolicy or 'invokeunwind'
         if exceptionpolicy == 'invokeunwind':
             from pypy.translator.js.exception import InvokeUnwindExceptionPolicy
             exceptionpolicy = InvokeUnwindExceptionPolicy()
@@ -75,7 +75,7 @@
         returntype, entrypointname =  entrynode.getdecl().split('%', 1)
         noresult = self._noresult(returntype)
         return '''
-ccc %(returntype)s%%__entrypoint__%(entrypointname)s {
+%(returntype)s%%__entrypoint__%(entrypointname)s {
     %%result = invoke %(returntype)s%%%(entrypointname)s to label %%no_exception except label %%exception
 
 no_exception:
@@ -86,13 +86,13 @@
     ret %(noresult)s
 }
 
-ccc int %%__entrypoint__raised_LLVMException() {
+int %%__entrypoint__raised_LLVMException() {
     %%tmp    = load %%RPYTHON_EXCEPTION_VTABLE** %%last_exception_type
     %%result = cast %%RPYTHON_EXCEPTION_VTABLE* %%tmp to int
     ret int %%result
 }
 
-internal fastcc void %%unwind() {
+void %%unwind() {
     unwind
 }
 ''' % locals() + self.RINGBUFFER_LLVMCODE
@@ -164,7 +164,7 @@
         returntype, entrypointname = entrynode.getdecl().split('%', 1)
         noresult = self._noresult(returntype)
         return '''
-ccc %(returntype)s%%__entrypoint__%(entrypointname)s {
+%(returntype)s%%__entrypoint__%(entrypointname)s {
     store %%RPYTHON_EXCEPTION_VTABLE* null, %%RPYTHON_EXCEPTION_VTABLE** %%last_exception_type
     %%result = call %(returntype)s%%%(entrypointname)s
     %%tmp    = load %%RPYTHON_EXCEPTION_VTABLE** %%last_exception_type
@@ -178,13 +178,13 @@
     ret %(noresult)s
 }
 
-ccc int %%__entrypoint__raised_LLVMException() {
+int %%__entrypoint__raised_LLVMException() {
     %%tmp    = load %%RPYTHON_EXCEPTION_VTABLE** %%last_exception_type
     %%result = cast %%RPYTHON_EXCEPTION_VTABLE* %%tmp to int
     ret int %%result
 }
 
-internal fastcc void %%unwind() {
+void %%unwind() {
     ret void
 }
 ''' % locals() + self.RINGBUFFER_LLVMCODE

Modified: pypy/dist/pypy/translator/js/funcnode.py
==============================================================================
--- pypy/dist/pypy/translator/js/funcnode.py	(original)
+++ pypy/dist/pypy/translator/js/funcnode.py	Mon Oct 10 22:03:02 2005
@@ -12,21 +12,25 @@
 log = log.funcnode
 
 class FuncTypeNode(LLVMNode):
+    #def __init__(self, db, type_):
+    #    #XXX not sure if we need FuncTypeNode with Javascript
+    #    pass
+
     __slots__ = "db type_ ref".split()
-    
+ 
     def __init__(self, db, type_):
         self.db = db
         assert isinstance(type_, lltype.FuncType)
         self.type_ = type_
         self.ref = self.make_ref('%functiontype', '')
-
+ 
     def __str__(self):
         return "<FuncTypeNode %r>" % self.ref
-
+ 
     def setup(self):
         self.db.prepare_type(self.type_.RESULT)
         self.db.prepare_type_multi(self.type_._trueargs())
-
+ 
     def writedatatypedecl(self, codewriter):
         returntype = self.db.repr_type(self.type_.RESULT)
         inputargtypes = [self.db.repr_type(a) for a in self.type_._trueargs()]

Modified: pypy/dist/pypy/translator/js/gc.py
==============================================================================
--- pypy/dist/pypy/translator/js/gc.py	(original)
+++ pypy/dist/pypy/translator/js/gc.py	Mon Oct 10 22:03:02 2005
@@ -56,8 +56,8 @@
 
     def declarations(self):
         return '''
-declare ccc sbyte* %GC_malloc(uint)
-declare ccc sbyte* %GC_malloc_atomic(uint)
+declare sbyte* %GC_malloc(uint)
+declare sbyte* %GC_malloc_atomic(uint)
 %GC_all_interior_pointers = external global int
 '''
 
@@ -69,12 +69,12 @@
         t = '''
 %%malloc.Size%(cnt)s  = getelementptr %(type_)s* null, %(uword)s %(s)s
 %%malloc.SizeU%(cnt)s = cast %(type_)s* %%malloc.Size%(cnt)s to %(uword)s
-%%malloc.Ptr%(cnt)s   = call ccc sbyte* %%GC_malloc%(atomic)s(%(uword)s %%malloc.SizeU%(cnt)s)
+%%malloc.Ptr%(cnt)s   = call sbyte* %%GC_malloc%(atomic)s(%(uword)s %%malloc.SizeU%(cnt)s)
 %(targetvar)s = cast sbyte* %%malloc.Ptr%(cnt)s to %(type_)s*
 ''' % locals()
         if is_atomic:
             t += '''
-call ccc void %%llvm.memset(sbyte* %%malloc.Ptr%(cnt)s, ubyte 0, uint %%malloc.SizeU%(cnt)s, uint 0)
+call void %%llvm.memset(sbyte* %%malloc.Ptr%(cnt)s, ubyte 0, uint %%malloc.SizeU%(cnt)s, uint 0)
 ''' % locals()
         return t
 

Modified: pypy/dist/pypy/translator/js/opwriter.py
==============================================================================
--- pypy/dist/pypy/translator/js/opwriter.py	(original)
+++ pypy/dist/pypy/translator/js/opwriter.py	Mon Oct 10 22:03:02 2005
@@ -224,6 +224,7 @@
         targettype = self.db.repr_arg_type(op.result)
         fromvar = self.db.repr_arg(op.args[0])
         fromtype = self.db.repr_arg_type(op.args[0])
+        self.codewriter.comment(op.opname)
         self.codewriter.cast(targetvar, fromtype, fromvar, targettype)
     same_as = cast_primitive
 
@@ -444,8 +445,6 @@
         index = self._getindexhelper(op.args[1].value, op.args[0].concretetype.TO)
         valuevar, valuetype = self.db.repr_argwithtype(op.args[2])
         if valuetype != "void": 
-            #Structure types require uint constants!
-            #see: http://llvm.cs.uiuc.edu/docs/LangRef.html#i_getelementptr
             self.codewriter.getelementptr(tmpvar, structtype, struct,
                                           ("uint", index))
             self.codewriter.store(valuetype, valuevar, tmpvar) 
@@ -456,13 +455,14 @@
         array, arraytype = self.db.repr_argwithtype(op.args[0])
         index = self.db.repr_arg(op.args[1])
         indextype = self.db.repr_arg_type(op.args[1])
-        tmpvar = self.db.repr_tmpvar()
         targetvar = self.db.repr_arg(op.result)
         targettype = self.db.repr_arg_type(op.result)
         if targettype != "void":
-            self.codewriter.getelementptr(tmpvar, arraytype, array,
-                                          ("uint", 1), (indextype, index))
-            self.codewriter.load(targetvar, targettype, tmpvar)
+            #tmpvar = self.db.repr_tmpvar()
+            #self.codewriter.getelementptr(tmpvar, arraytype, array,
+            #                              ("uint", 1), (indextype, index))
+            #self.codewriter.load(targetvar, targettype, tmpvar)
+            self.codewriter.load(targetvar, array, (1, index))
         else:
             self._skipped(op)
 
@@ -478,15 +478,14 @@
         array, arraytype = self.db.repr_argwithtype(op.args[0])
         index = self.db.repr_arg(op.args[1])
         indextype = self.db.repr_arg_type(op.args[1])
-
-        tmpvar = self.db.repr_tmpvar()
-
         valuevar = self.db.repr_arg(op.args[2]) 
         valuetype = self.db.repr_arg_type(op.args[2])
         if valuetype != "void":
-            self.codewriter.getelementptr(tmpvar, arraytype, array,
-                                      ("uint", 1), (indextype, index))
-            self.codewriter.store(valuetype, valuevar, tmpvar) 
+            #tmpvar = self.db.repr_tmpvar()
+            #self.codewriter.getelementptr(tmpvar, arraytype, array,
+            #                          ("uint", 1), (indextype, index))
+            #self.codewriter.store(valuetype, valuevar, tmpvar) 
+            self.codewriter.store(array, (1, index), valuevar)
         else:
             self._skipped(op)
 

Modified: pypy/dist/pypy/translator/js/test/test_lltype.py
==============================================================================
--- pypy/dist/pypy/translator/js/test/test_lltype.py	(original)
+++ pypy/dist/pypy/translator/js/test/test_lltype.py	Mon Oct 10 22:03:02 2005
@@ -127,7 +127,7 @@
     assert f(2) == 0
     assert f(3) == 17    
 
-def test_array_constant():
+def test_array_constant1():
     A = lltype.GcArray(lltype.Signed)
     a = lltype.malloc(A, 3)
     a[0] = 100

Modified: pypy/dist/pypy/translator/js/varsize.py
==============================================================================
--- pypy/dist/pypy/translator/js/varsize.py	(original)
+++ pypy/dist/pypy/translator/js/varsize.py	Mon Oct 10 22:03:02 2005
@@ -3,7 +3,7 @@
 def write_constructor(db, codewriter, ref, constructor_decl, ARRAY, 
                       indices_to_array=()): 
 
-    codewriter.comment('TODO: ' + constructor_decl)
+    codewriter.comment('TODO: ' + constructor_decl, 0)
     return
 
     #varsized arrays and structs look like this: 



More information about the Pypy-commit mailing list