[pypy-svn] r14393 - in pypy/dist/pypy/translator/llvm2: . test
ericvrp at codespeak.net
ericvrp at codespeak.net
Thu Jul 7 18:23:23 CEST 2005
Author: ericvrp
Date: Thu Jul 7 18:23:21 2005
New Revision: 14393
Modified:
pypy/dist/pypy/translator/llvm2/codewriter.py
pypy/dist/pypy/translator/llvm2/extfunction.py
pypy/dist/pypy/translator/llvm2/genllvm.py
pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
Log:
Refactoring so we call the correct GC
Modified: pypy/dist/pypy/translator/llvm2/codewriter.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/codewriter.py (original)
+++ pypy/dist/pypy/translator/llvm2/codewriter.py Thu Jul 7 18:23:21 2005
@@ -1,7 +1,6 @@
import py
from itertools import count
from pypy.translator.llvm2.log import log
-from pypy.translator.llvm2.genllvm import use_boehm_gc
log = log.codewriter
show_line_numbers = False # True
@@ -94,18 +93,12 @@
"%(fromvar)s to %(targettype)s" % locals())
def malloc(self, targetvar, type_, size=1, atomic=False):
- if use_boehm_gc:
- cnt = count()
- if atomic:
- atomicString = '_atomic'
- else:
- atomicString = ''
- self.indent("%%malloc.Size.%(cnt)d = getelementptr %(type_)s* null, uint %(size)s" % locals())
- self.indent("%%malloc.SizeU.%(cnt)d = cast %(type_)s* %%malloc.Size.%(cnt)d to uint" % locals())
- self.indent("%%malloc.Ptr.%(cnt)d = call sbyte* %%GC_malloc%(atomicString)s(uint %%malloc.SizeU.%(cnt)d)" % locals())
- self.indent("%(targetvar)s = cast sbyte* %%malloc.Ptr.%(cnt)d to %(type_)s*" % locals())
- else:
- self.indent("%(targetvar)s = malloc %(type_)s, uint %(size)s" % locals())
+ cnt = count()
+ postfix = ('', '_atomic')[atomic]
+ self.indent("%%malloc.Size.%(cnt)d = getelementptr %(type_)s* null, uint %(size)s" % locals())
+ self.indent("%%malloc.SizeU.%(cnt)d = cast %(type_)s* %%malloc.Size.%(cnt)d to uint" % locals())
+ self.indent("%%malloc.Ptr.%(cnt)d = call sbyte* %%gc_malloc%(postfix)s(uint %%malloc.SizeU.%(cnt)d)" % locals())
+ self.indent("%(targetvar)s = cast sbyte* %%malloc.Ptr.%(cnt)d to %(type_)s*" % locals())
def getelementptr(self, targetvar, type, typevar, *indices):
res = "%(targetvar)s = getelementptr %(type)s %(typevar)s, int 0, " % locals()
Modified: pypy/dist/pypy/translator/llvm2/extfunction.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/extfunction.py (original)
+++ pypy/dist/pypy/translator/llvm2/extfunction.py Thu Jul 7 18:23:21 2005
@@ -3,6 +3,8 @@
; XXX these int's might need to be long's on 64 bit CPU's :(
+declare sbyte* %gc_malloc(uint)
+declare sbyte* %gc_malloc_atomic(uint)
declare int %time(int*) ;void* actually
declare int %clock()
declare void %sleep(int)
@@ -17,6 +19,37 @@
"""
+gc_boehm = """; Using Boehm GC
+
+declare sbyte* %GC_malloc(uint)
+declare sbyte* %GC_malloc_atomic(uint)
+
+sbyte* %gc_malloc(uint %n) {
+ %ptr = call sbyte* %GC_malloc(uint %n)
+ ret sbyte* %ptr
+}
+
+sbyte* %gc_malloc_atomic(uint %n) {
+ %ptr = call sbyte* %GC_malloc_atomic(uint %n)
+ ret sbyte* %ptr
+}
+
+"""
+
+gc_disabled = """; Using no GC
+
+sbyte* %gc_malloc(uint %n) {
+ %ptr = malloc sbyte, uint %n
+ ret sbyte* %ptr
+}
+
+sbyte* %gc_malloc_atomic(uint %n) {
+ %ptr = malloc sbyte, uint %n
+ ret sbyte* %ptr
+}
+
+"""
+
extfunctions = """; Helper function to convert LLVM <-> C types
sbyte* %cast(%st.rpy_string.0* %structstring) {
@@ -24,18 +57,18 @@
%reallength = load int* %reallengthptr
%length = add int %reallength, 1
%ulength = cast int %length to uint
- %dest = malloc sbyte, uint %ulength
-
+ %dest = call sbyte* %gc_malloc_atomic(uint %ulength)
+
%source1ptr = getelementptr %st.rpy_string.0* %structstring, int 0, uint 1, uint 1
%source1 = cast [0 x sbyte]* %source1ptr to sbyte*
%dummy = call sbyte* %strncpy(sbyte* %dest, sbyte* %source1, int %reallength)
-
+
%zeropos1 = cast sbyte* %dest to int
%zeropos2 = add int %zeropos1, %reallength
%zerodest = cast int %zeropos2 to sbyte*
store sbyte 0, sbyte* %zerodest
- ret sbyte* %dest ;XXX alloca freed at end of function. this will crash!
+ ret sbyte* %dest
}
; Wrapper functions that call external (C) functions
@@ -65,7 +98,6 @@
%mode = cast int 384 to int ;S_IRUSR=256, S_IWUSR=128
%dest = call sbyte* %cast(%st.rpy_string.0* %structstring)
%fd = call int %open(sbyte* %dest, int %flags, int %mode)
- free sbyte* %dest
ret int %fd
}
@@ -74,7 +106,6 @@
%reallength = load int* %reallengthptr
%dest = call sbyte* %cast(%st.rpy_string.0* %structstring)
%byteswritten = call int %write(int %fd, sbyte* %dest, int %reallength)
- free sbyte* %dest
ret int %byteswritten
}
Modified: pypy/dist/pypy/translator/llvm2/genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/genllvm.py (original)
+++ pypy/dist/pypy/translator/llvm2/genllvm.py Thu Jul 7 18:23:21 2005
@@ -13,7 +13,7 @@
from pypy.translator.llvm2.codewriter import CodeWriter
from pypy.translator.backendoptimization import remove_void
#from pypy.translator.backendoptimization import rename_extfunc_calls
-from pypy.translator.llvm2.extfunction import extdeclarations, extfunctions
+from pypy.translator.llvm2.extfunction import extdeclarations, extfunctions, gc_boehm, gc_disabled
from pypy.translator.translator import Translator
function_count = {}
@@ -45,17 +45,17 @@
nl(); comment("Function Prototypes") ; nl()
for extdecl in extdeclarations.split('\n'):
codewriter.append(extdecl)
- if use_boehm_gc:
- codewriter.declare('sbyte* %GC_malloc(uint)')
- codewriter.declare('sbyte* %GC_malloc_atomic(uint)')
- codewriter.declare('sbyte* %GC_realloc(sbyte*, uint)')
for typ_decl in db.getobjects():
typ_decl.writedecl(codewriter)
#import pdb ; pdb.set_trace()
nl(); comment("Function Implementation")
codewriter.startimpl()
- for extfunc in extfunctions.split('\n'):
+ if use_boehm_gc:
+ gc_funcs = gc_boehm
+ else:
+ gc_funcs = gc_disabled
+ for extfunc in (gc_funcs + extfunctions).split('\n'):
codewriter.append(extfunc)
for typ_decl in db.getobjects():
typ_decl.writeimpl(codewriter)
Modified: pypy/dist/pypy/translator/llvm2/test/test_extfunc.py
==============================================================================
--- pypy/dist/pypy/translator/llvm2/test/test_extfunc.py (original)
+++ pypy/dist/pypy/translator/llvm2/test/test_extfunc.py Thu Jul 7 18:23:21 2005
@@ -84,3 +84,26 @@
result = f(*map(ord, path))
assert os.path.exists(path) and open(path).read() == path
+def DONTtest_os_file_ops_open_write_read_close():
+ # the test is overly complicated because
+ # we don't have prebuilt string constants yet
+ import os
+ def openwriteclose_openreadclose(a,b,c,d,e,f):
+ s = chr(a) + chr(b) + chr(c) + chr(d) + chr(e) + chr(f)
+
+ fd = os.open(s, os.O_CREAT|os.O_RDWR)
+ byteswritten = os.write(fd, s)
+ os.close(fd)
+
+ fd = os.open(s, os.os.O_RD)
+ r = os.read(fd, n=1000)
+ os.close(fd)
+
+ return r
+
+ path = '/tmp/b'
+ if os.path.exists(path):
+ os.unlink(path)
+ f = compile_function(openwriteclose_openreadclose, [int] * len(path))
+ result = f(*map(ord, path))
+ assert os.path.exists(path) and open(path).read() is path and result is path
More information about the Pypy-commit
mailing list