[pypy-svn] r16449 - in pypy/dist/pypy/translator/llvm2: . module

rxe at codespeak.net rxe at codespeak.net
Thu Aug 25 11:25:32 CEST 2005


Author: rxe
Date: Thu Aug 25 11:25:31 2005
New Revision: 16449

Added:
   pypy/dist/pypy/translator/llvm2/module/genexterns.c
Modified:
   pypy/dist/pypy/translator/llvm2/genllvm.py
Log:
(ericvrp/rxe)
Ad hoc code to experiment with generating llvm code from C code.



Modified: pypy/dist/pypy/translator/llvm2/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm2/genllvm.py	Thu Aug 25 11:25:31 2005
@@ -1,7 +1,13 @@
 from os.path import exists
 use_boehm_gc = exists('/usr/lib/libgc.so') or exists('/usr/lib/libgc.a')
 
+import os
+import time
+import types
+import urllib
+
 import py
+
 from pypy.translator.llvm2 import build_llvm_module
 from pypy.translator.llvm2.database import Database 
 from pypy.translator.llvm2.pyxwrapper import write_pyx_wrapper 
@@ -17,10 +23,13 @@
 
 from pypy.translator.translator import Translator
 
-import time
 
 function_count = {}
 
+def get_ll(ccode):
+    request = urllib.urlencode({'ccode':ccode})
+    return urllib.urlopen('http://codespeak.net/pypy/llvm-gcc.cgi', request).read()
+
 class GenLLVM(object):
 
     def __init__(self, translator, debug=True):
@@ -42,7 +51,6 @@
         self.db.prepare_arg_value(c)
 
     def post_setup_externs(self):
-        import types
 
         rtyper = self.db._translator.rtyper
         from pypy.translator.c.extfunc import predeclare_all
@@ -54,20 +62,53 @@
 
         for c_name, obj in decls:
             if isinstance(obj, lltype.LowLevelType):
+                print 'XXX1', c_name
                 self.db.prepare_type(obj)
             elif isinstance(obj, types.FunctionType):
+                print 'XXX2', c_name
                 funcptr = getfunctionptr(self.translator, obj)
                 c = inputconst(lltype.typeOf(funcptr), funcptr)
                 self.db.prepare_arg_value(c)
-
             elif isinstance(lltype.typeOf(obj), lltype.Ptr):
+                print 'XXX3', c_name
                 self.db.prepare_constant(lltype.typeOf(obj), obj)
             else:
-                print "XXX  predeclare" , c_name, type(obj), obj
-                assert False
+                assert False, "unhandled predeclare %s %s %s" % (c_name, type(obj), obj)
 
         return decls
 
+    def generate_llfile(self, extern_decls):
+        # hack to get file
+
+        genllcode = ""
+
+        def predeclarefn(c_name, llname):
+            assert llname[0] == "%"
+            assert '\n' not in llname
+            return '#define\t%s\t%s' % (c_name, llname[1:])
+
+        for c_name, obj in extern_decls:
+            if isinstance(obj, lltype.LowLevelType):
+                pass
+            elif isinstance(obj, types.FunctionType):
+                funcptr = getfunctionptr(self.translator, obj)
+                c = inputconst(lltype.typeOf(funcptr), funcptr)
+                llname = self.db.repr_arg(c)
+                genllcode += predeclarefn(c_name, llname) + "\n"
+            #elif isinstance(lltype.typeOf(obj), lltype.Ptr):
+            #    if isinstance(obj.TO, lltype.FuncType):
+            #        llname = self.db.repr_constant(obj)[1]
+            #XXXXXXXXXXX        genllcode += predeclarefn(c_name, llname) + "\n"
+
+        j = os.path.join
+        p = j(j(os.path.dirname(__file__), "module"), "genexterns.c")
+        genllcode += open(p).read()
+        print genllcode
+
+        ll_str = get_ll(genllcode)
+        ll_str = ll_str.replace("%struct.", "%")
+        return ll_str
+    
     def replace_with_machine_words(self, s):
         return s.replace('UINT',self.db.get_machine_uword()).replace('INT',self.db.get_machine_word())
 
@@ -91,9 +132,12 @@
 
         # post set up externs
         extern_decls = self.post_setup_externs()
-        self.db._translator.rtyper.specialize_more_blocks()
+        self.translator.rtyper.specialize_more_blocks()
         self.db.setup_all()
 
+        print self.generate_llfile(extern_decls)
+        #XXXXassert False
+ 
         #if self.debug:  print 'gen_llvm_source typ_decl.writedatatypedecl) ' + time.ctime()
         #if self.debug:  print 'gen_llvm_source n_nodes) %d' % len(self.db.getnodes())
         #3 seconds
@@ -122,6 +166,7 @@
                     obj = obj.TO
                 l = "%%%s = type %s" % (c_name, self.db.repr_type(obj))
                 codewriter.append(l)
+                
             #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX   
             #elif isinstance(obj, types.FunctionType):
             #    #c.value._obj.graph.name = c_name

Added: pypy/dist/pypy/translator/llvm2/module/genexterns.c
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/translator/llvm2/module/genexterns.c	Thu Aug 25 11:25:31 2005
@@ -0,0 +1,41 @@
+#include <errno.h>
+#include <python2.3/Python.h>
+
+#define NULL (void *) 0
+
+#define LL_MATH_SET_ERANGE_IF_MATH_ERROR Py_SET_ERANGE_IF_OVERFLOW
+
+struct RPyFREXP_RESULT;
+struct RPyFREXP_RESULT *ll_frexp_result__Float_Signed(double, int);
+
+void prepare_and_raise_ValueError(char *);
+void prepare_and_raise_OverflowError(char *);
+
+int ll_math_is_error(double x) {
+	if (errno == ERANGE) {
+		if (!x) 
+			return 0;
+		prepare_and_raise_OverflowError("math range error");
+	} else {
+		prepare_and_raise_ValueError("math domain error");
+	}
+	return 1;
+}
+
+#define LL_MATH_ERROR_RESET errno = 0
+
+#define LL_MATH_CHECK_ERROR(x, errret) do {  \
+	LL_MATH_SET_ERANGE_IF_MATH_ERROR(x); \
+	if (errno && ll_math_is_error(x))    \
+		return errret;               \
+} while(0)
+
+
+struct RPyFREXP_RESULT* ll_math_frexp(double x) {
+	int expo;
+	double m;
+	LL_MATH_ERROR_RESET;
+	m= frexp(x, &expo);
+	LL_MATH_CHECK_ERROR(m, NULL);
+	return ll_frexp_result__Float_Signed(m, expo);
+}



More information about the Pypy-commit mailing list