[pypy-svn] r35095 - in pypy/dist/pypy/jit/codegen/llvm: . lib test
ericvrp at codespeak.net
ericvrp at codespeak.net
Tue Nov 28 23:46:40 CET 2006
Author: ericvrp
Date: Tue Nov 28 23:46:37 2006
New Revision: 35095
Added:
pypy/dist/pypy/jit/codegen/llvm/autopath.py (contents, props changed)
Modified:
pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h
pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
Log:
llvm jit codegen modification of global data works.
test enabled.
Added: pypy/dist/pypy/jit/codegen/llvm/autopath.py
==============================================================================
--- (empty file)
+++ pypy/dist/pypy/jit/codegen/llvm/autopath.py Tue Nov 28 23:46:37 2006
@@ -0,0 +1,114 @@
+"""
+self cloning, automatic path configuration
+
+copy this into any subdirectory of pypy from which scripts need
+to be run, typically all of the test subdirs.
+The idea is that any such script simply issues
+
+ import autopath
+
+and this will make sure that the parent directory containing "pypy"
+is in sys.path.
+
+If you modify the master "autopath.py" version (in pypy/tool/autopath.py)
+you can directly run it which will copy itself on all autopath.py files
+it finds under the pypy root directory.
+
+This module always provides these attributes:
+
+ pypydir pypy root directory path
+ this_dir directory where this autopath.py resides
+
+"""
+
+
+def __dirinfo(part):
+ """ return (partdir, this_dir) and insert parent of partdir
+ into sys.path. If the parent directories don't have the part
+ an EnvironmentError is raised."""
+
+ import sys, os
+ try:
+ head = this_dir = os.path.realpath(os.path.dirname(__file__))
+ except NameError:
+ head = this_dir = os.path.realpath(os.path.dirname(sys.argv[0]))
+
+ while head:
+ partdir = head
+ head, tail = os.path.split(head)
+ if tail == part:
+ break
+ else:
+ raise EnvironmentError, "'%s' missing in '%r'" % (partdir, this_dir)
+
+ pypy_root = os.path.join(head, '')
+ try:
+ sys.path.remove(head)
+ except ValueError:
+ pass
+ sys.path.insert(0, head)
+
+ munged = {}
+ for name, mod in sys.modules.items():
+ if '.' in name:
+ continue
+ fn = getattr(mod, '__file__', None)
+ if not isinstance(fn, str):
+ continue
+ newname = os.path.splitext(os.path.basename(fn))[0]
+ if not newname.startswith(part + '.'):
+ continue
+ path = os.path.join(os.path.dirname(os.path.realpath(fn)), '')
+ if path.startswith(pypy_root) and newname != part:
+ modpaths = os.path.normpath(path[len(pypy_root):]).split(os.sep)
+ if newname != '__init__':
+ modpaths.append(newname)
+ modpath = '.'.join(modpaths)
+ if modpath not in sys.modules:
+ munged[modpath] = mod
+
+ for name, mod in munged.iteritems():
+ if name not in sys.modules:
+ sys.modules[name] = mod
+ if '.' in name:
+ prename = name[:name.rfind('.')]
+ postname = name[len(prename)+1:]
+ if prename not in sys.modules:
+ __import__(prename)
+ if not hasattr(sys.modules[prename], postname):
+ setattr(sys.modules[prename], postname, mod)
+
+ return partdir, this_dir
+
+def __clone():
+ """ clone master version of autopath.py into all subdirs """
+ from os.path import join, walk
+ if not this_dir.endswith(join('pypy','tool')):
+ raise EnvironmentError("can only clone master version "
+ "'%s'" % join(pypydir, 'tool',_myname))
+
+
+ def sync_walker(arg, dirname, fnames):
+ if _myname in fnames:
+ fn = join(dirname, _myname)
+ f = open(fn, 'rwb+')
+ try:
+ if f.read() == arg:
+ print "checkok", fn
+ else:
+ print "syncing", fn
+ f = open(fn, 'w')
+ f.write(arg)
+ finally:
+ f.close()
+ s = open(join(pypydir, 'tool', _myname), 'rb').read()
+ walk(pypydir, sync_walker, s)
+
+_myname = 'autopath.py'
+
+# set guaranteed attributes
+
+pypydir, this_dir = __dirinfo('pypy')
+
+if __name__ == '__main__':
+ __clone()
Modified: pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp (original)
+++ pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.cpp Tue Nov 28 23:46:37 2006
@@ -41,7 +41,7 @@
PassList(cl::desc("Optimizations available:"));
//some global data for the tests to play with
-char g_char[2] = {10,0};
+int g_data;
//
@@ -106,23 +106,24 @@
}
-char* get_pointer_to_global_char() {
- printf("get_pointer_to_global_char g_char=%08x\n", g_char);
- return g_char;
+int get_global_data() {
+ return g_data;
}
-void add_global_mapping(const char* name, void* address) {
- //GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps, LinkageTypes linkage, const std::string &name = "");
+void set_global_data(int n) {
+ g_data = n;
+}
- /// GlobalVariable ctor - If a parent module is specified, the global is
- // /// automatically inserted into the end of the specified modules global list.
- // GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
- // Constant *Initializer = 0, const std::string &Name = "",
- // Module *Parent = 0);
-
- GlobalVariable var(Type::UByteTy, false, GlobalVariable::ExternalLinkage, 0, name, gp_module);
- printf("add_global_mapping address=%08x\n", address);
- gp_execution_engine->addGlobalMapping(&var, address);
+
+int* get_pointer_to_global_data() {
+ return &g_data;
+}
+
+
+void add_global_mapping(const char* name, void* address) {
+ //note: using getNamedGlobal implies that we can not have globals of different type
+ // but with identical names! This is probably easy to do.
+ gp_execution_engine->addGlobalMapping(gp_module->getNamedGlobal(name), address);
}
Modified: pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h (original)
+++ pypy/dist/pypy/jit/codegen/llvm/lib/libllvmjit.h Tue Nov 28 23:46:37 2006
@@ -9,7 +9,9 @@
int compile(const char* llsource);
void* find_function(const char* funcname);
int execute(const void* function, int param);
-char* get_pointer_to_global_char();
+int get_global_data();
+void set_global_data(int n);
+int* get_pointer_to_global_data();
void add_global_mapping(const char* name, void* address);
#ifdef __cplusplus
Modified: pypy/dist/pypy/jit/codegen/llvm/llvmjit.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/llvmjit.py (original)
+++ pypy/dist/pypy/jit/codegen/llvm/llvmjit.py Tue Nov 28 23:46:37 2006
@@ -6,6 +6,7 @@
This file contains the ctypes specification to use the llvmjit library!
'''
+import autopath
from pypy.rpython.rctypes import implementation
from pypy.rpython.rctypes.tool.util import load_library
@@ -13,10 +14,11 @@
from ctypes import *
import os
-path = os.path.join(os.path.dirname(__file__), 'libllvmjit.so')
-
+newdir = os.path.dirname(__file__)
+path = os.path.join(newdir, 'libllvmjit.so')
curdir = os.getcwd()
-os.chdir(os.path.dirname(__file__))
+if newdir:
+ os.chdir(newdir)
#With py.test --session=R the master server rsyncs the .so library too!?!
#So we always need to recompile the library if its platform (output of file libllvmjit.so)
@@ -61,11 +63,17 @@
execute.restype = c_int
execute.argtypes = [c_void_p, c_int]
-get_pointer_to_global_char= llvmjit.get_pointer_to_global_char
-get_pointer_to_global_char.restype = c_char_p
-get_pointer_to_global_char.argtypes = []
+get_global_data= llvmjit.get_global_data
+get_global_data.restype = c_int
+get_global_data.argtypes = []
+
+set_global_data= llvmjit.set_global_data
+set_global_data.argtypes = [c_int]
+
+get_pointer_to_global_data= llvmjit.get_pointer_to_global_data
+get_pointer_to_global_data.restype = POINTER(c_int)
+get_pointer_to_global_data.argtypes = []
add_global_mapping = llvmjit.add_global_mapping
-#add_global_mapping.restype = c_void
add_global_mapping.argtypes = [c_char_p, c_void_p]
Modified: pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py
==============================================================================
--- pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py (original)
+++ pypy/dist/pypy/jit/codegen/llvm/test/test_llvmjit.py Tue Nov 28 23:46:37 2006
@@ -66,18 +66,16 @@
ret int %n3
}'''
-llglobalmul4 = '''%my_global_ubyte = external global ubyte
+llglobalmul4 = '''%my_global_data = external global int
implementation
int %globalmul4(int %a) {
- %aa = cast int %a to ubyte
- %v0 = load ubyte* %my_global_ubyte
- %v1 = mul ubyte %v0, 4
- %v2 = add ubyte %v1, %aa
- store ubyte %v2, ubyte* %my_global_ubyte
- %v3 = cast ubyte %v2 to int
- ret int %v3
+ %v0 = load int* %my_global_data
+ %v1 = mul int %v0, 4
+ %v2 = add int %v1, %a
+ store int %v2, int* %my_global_data
+ ret int %v2
}'''
#helpers
@@ -161,16 +159,16 @@
assert llvmjit.transform("instcombine simplifycfg printm verify")
assert llvmjit.execute(deadcode, 30) == 30 * 2
-def DONTtest_modify_global_data():
+def test_modify_global_data():
llvmjit.restart()
- gp_char = llvmjit.get_pointer_to_global_char()
- assert len(gp_char) == 1
- assert ord(gp_char[0]) == 10
- llvmjit.add_global_mapping('my_global_ubyte', gp_char) #note: should be prior to compile()
- llvmjit.compile(llglobalmul4) #XXX assert error, incorrect types???
+ llvmjit.set_global_data(10)
+ assert llvmjit.get_global_data() == 10
+ gp_data = llvmjit.get_pointer_to_global_data()
+ llvmjit.compile(llglobalmul4)
+ llvmjit.add_global_mapping('my_global_data', gp_data) #note: should be prior to execute()
globalmul4 = llvmjit.find_function('globalmul4')
assert llvmjit.execute(globalmul4, 5) == 10 * 4 + 5
- assert ord(gp_char[0]) == 10 * 4 + 5
+ assert llvmjit.get_global_data() == 10 * 4 + 5
def DONTtest_call_back_to_parent(): #call JIT-compiler again for it to add case(s) to flexswitch
pass
More information about the Pypy-commit
mailing list