This commit is contained in:
Siu Kwan Lam 2013-12-18 12:49:24 -06:00
commit f8c1c78df1
5 changed files with 70 additions and 13 deletions

View file

@ -9,6 +9,8 @@ version = extra.get_llvm_version()
del extra del extra
class Wrapper(object): class Wrapper(object):
__slots__ = '__ptr'
def __init__(self, ptr): def __init__(self, ptr):
assert ptr assert ptr
self.__ptr = ptr self.__ptr = ptr

View file

@ -379,6 +379,7 @@ class Module(llvm.Wrapper):
module_obj = Module.new('my_module') module_obj = Module.new('my_module')
""" """
__slots__ = '__weakref__'
__cache = weakref.WeakValueDictionary() __cache = weakref.WeakValueDictionary()
def __new__(cls, ptr): def __new__(cls, ptr):
@ -688,6 +689,7 @@ class Type(llvm.Wrapper):
Use one of the static methods to create an instance. Example: Use one of the static methods to create an instance. Example:
ty = Type.double() ty = Type.double()
""" """
__slots__ = '__name__'
_type_ = api.llvm.Type _type_ = api.llvm.Type
def __init__(self, ptr): def __init__(self, ptr):
@ -880,6 +882,7 @@ class Type(llvm.Wrapper):
class IntegerType(Type): class IntegerType(Type):
"""Represents an integer type.""" """Represents an integer type."""
__slots__ = ()
_type_ = api.llvm.IntegerType _type_ = api.llvm.IntegerType
@property @property
@ -889,6 +892,7 @@ class IntegerType(Type):
class FunctionType(Type): class FunctionType(Type):
"""Represents a function type.""" """Represents a function type."""
__slots__ = ()
_type_ = api.llvm.FunctionType _type_ = api.llvm.FunctionType
@property @property
@ -918,6 +922,7 @@ class FunctionType(Type):
class StructType(Type): class StructType(Type):
"""Represents a structure type.""" """Represents a structure type."""
_type_ = api.llvm.StructType _type_ = api.llvm.StructType
__slots__ = ()
@property @property
def element_count(self): def element_count(self):
@ -976,6 +981,7 @@ class StructType(Type):
class ArrayType(Type): class ArrayType(Type):
"""Represents an array type.""" """Represents an array type."""
_type_ = api.llvm.ArrayType _type_ = api.llvm.ArrayType
__slots__ = ()
@property @property
def element(self): def element(self):
@ -987,6 +993,7 @@ class ArrayType(Type):
class PointerType(Type): class PointerType(Type):
_type_ = api.llvm.PointerType _type_ = api.llvm.PointerType
__slots__ = ()
@property @property
def pointee(self): def pointee(self):
@ -998,6 +1005,7 @@ class PointerType(Type):
class VectorType(Type): class VectorType(Type):
_type_ = api.llvm.VectorType _type_ = api.llvm.VectorType
__slots__ = ()
@property @property
def element(self): def element(self):
@ -1009,6 +1017,7 @@ class VectorType(Type):
class Value(llvm.Wrapper): class Value(llvm.Wrapper):
_type_ = api.llvm.Value _type_ = api.llvm.Value
__slots__ = '__weakref__'
def __init__(self, builder, ptr): def __init__(self, builder, ptr):
assert builder is _ValueFactory assert builder is _ValueFactory
@ -1077,6 +1086,7 @@ class Value(llvm.Wrapper):
class User(Value): class User(Value):
_type_ = api.llvm.User _type_ = api.llvm.User
__slots__ = ()
@property @property
def operand_count(self): def operand_count(self):
@ -1091,6 +1101,7 @@ class User(Value):
class Constant(User): class Constant(User):
_type_ = api.llvm.Constant _type_ = api.llvm.Constant
__slots__ = ()
@staticmethod @staticmethod
def null(ty): def null(ty):
@ -1276,6 +1287,7 @@ class Constant(User):
class ConstantExpr(Constant): class ConstantExpr(Constant):
_type_ = api.llvm.ConstantExpr _type_ = api.llvm.ConstantExpr
__slots__ = ()
@property @property
def opcode(self): def opcode(self):
@ -1286,19 +1298,20 @@ class ConstantExpr(Constant):
return self._ptr.getOpcodeName() return self._ptr.getOpcodeName()
class ConstantAggregateZero(Constant): class ConstantAggregateZero(Constant):
pass __slots__ = ()
class ConstantDataArray(Constant): class ConstantDataArray(Constant):
pass __slots__ = ()
class ConstantDataVector(Constant): class ConstantDataVector(Constant):
pass __slots__ = ()
class ConstantInt(Constant): class ConstantInt(Constant):
_type_ = api.llvm.ConstantInt _type_ = api.llvm.ConstantInt
__slots__ = ()
@property @property
def z_ext_value(self): def z_ext_value(self):
@ -1314,30 +1327,32 @@ class ConstantInt(Constant):
class ConstantFP(Constant): class ConstantFP(Constant):
pass __slots__ = ()
class ConstantArray(Constant): class ConstantArray(Constant):
pass __slots__ = ()
class ConstantStruct(Constant): class ConstantStruct(Constant):
pass __slots__ = ()
class ConstantVector(Constant): class ConstantVector(Constant):
pass __slots__ = ()
class ConstantPointerNull(Constant): class ConstantPointerNull(Constant):
pass __slots__ = ()
class UndefValue(Constant): class UndefValue(Constant):
pass __slots__ = ()
class GlobalValue(Constant): class GlobalValue(Constant):
_type_ = api.llvm.GlobalValue _type_ = api.llvm.GlobalValue
__slots__ = ()
def _get_linkage(self): def _get_linkage(self):
return self._ptr.getLinkage() return self._ptr.getLinkage()
@ -1383,6 +1398,7 @@ class GlobalValue(Constant):
class GlobalVariable(GlobalValue): class GlobalVariable(GlobalValue):
_type_ = api.llvm.GlobalVariable _type_ = api.llvm.GlobalVariable
__slots__ = ()
@staticmethod @staticmethod
def new(module, ty, name, addrspace=0): def new(module, ty, name, addrspace=0):
@ -1443,6 +1459,7 @@ class GlobalVariable(GlobalValue):
thread_local = property(_get_thread_local, _set_thread_local) thread_local = property(_get_thread_local, _set_thread_local)
class Argument(Value): class Argument(Value):
__slots__ = ()
_type_ = api.llvm.Argument _type_ = api.llvm.Argument
_valid_attrs = frozenset([ATTR_BY_VAL, ATTR_NEST, ATTR_NO_ALIAS, _valid_attrs = frozenset([ATTR_BY_VAL, ATTR_NEST, ATTR_NO_ALIAS,
ATTR_NO_CAPTURE, ATTR_STRUCT_RET]) ATTR_NO_CAPTURE, ATTR_STRUCT_RET])
@ -1543,6 +1560,7 @@ class Argument(Value):
return self._ptr.hasStructRetAttr() return self._ptr.hasStructRetAttr()
class Function(GlobalValue): class Function(GlobalValue):
__slots__ = ()
_type_ = api.llvm.Function _type_ = api.llvm.Function
@staticmethod @staticmethod
@ -1681,6 +1699,7 @@ class Function(GlobalValue):
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
class InlineAsm(Value): class InlineAsm(Value):
__slots__ = ()
_type_ = api.llvm.InlineAsm _type_ = api.llvm.InlineAsm
@staticmethod @staticmethod
@ -1695,6 +1714,7 @@ class InlineAsm(Value):
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
class MetaData(Value): class MetaData(Value):
__slots__ = ()
_type_ = api.llvm.MDNode _type_ = api.llvm.MDNode
@staticmethod @staticmethod
@ -1751,6 +1771,7 @@ class MetaDataString(Value):
class NamedMetaData(llvm.Wrapper): class NamedMetaData(llvm.Wrapper):
__slots__ = ()
@staticmethod @staticmethod
def get_or_insert(mod, name): def get_or_insert(mod, name):
@ -1780,6 +1801,7 @@ class NamedMetaData(llvm.Wrapper):
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
class Instruction(User): class Instruction(User):
__slots__ = ()
_type_ = api.llvm.Instruction _type_ = api.llvm.Instruction
@property @property
@ -1861,6 +1883,7 @@ class Instruction(User):
class CallOrInvokeInstruction(Instruction): class CallOrInvokeInstruction(Instruction):
__slots__ = ()
_type_ = api.llvm.CallInst, api.llvm.InvokeInst _type_ = api.llvm.CallInst, api.llvm.InvokeInst
def _get_cc(self): def _get_cc(self):
@ -1916,6 +1939,7 @@ class CallOrInvokeInstruction(Instruction):
class PHINode(Instruction): class PHINode(Instruction):
__slots__ = ()
_type_ = api.llvm.PHINode _type_ = api.llvm.PHINode
@property @property
@ -1933,6 +1957,7 @@ class PHINode(Instruction):
class SwitchInstruction(Instruction): class SwitchInstruction(Instruction):
__slots__ = ()
_type_ = api.llvm.SwitchInst _type_ = api.llvm.SwitchInst
def add_case(self, const, bblk): def add_case(self, const, bblk):
@ -1940,6 +1965,7 @@ class SwitchInstruction(Instruction):
class CompareInstruction(Instruction): class CompareInstruction(Instruction):
__slots__ = ()
_type_ = api.llvm.CmpInst _type_ = api.llvm.CmpInst
@property @property
@ -1952,6 +1978,7 @@ class CompareInstruction(Instruction):
class AllocaInstruction(Instruction): class AllocaInstruction(Instruction):
__slots__ = ()
_type_ = api.llvm.AllocaInst _type_ = api.llvm.AllocaInst
@property @property
@ -1983,6 +2010,7 @@ class AllocaInstruction(Instruction):
#===----------------------------------------------------------------------=== #===----------------------------------------------------------------------===
class BasicBlock(Value): class BasicBlock(Value):
__slots__ = ()
_type_ = api.llvm.BasicBlock _type_ = api.llvm.BasicBlock
def insert_before(self, name): def insert_before(self, name):
@ -2009,6 +2037,7 @@ class BasicBlock(Value):
class _ValueFactory(object): class _ValueFactory(object):
__slots__ = ()
cache = weakref.WeakValueDictionary() cache = weakref.WeakValueDictionary()
# value ID -> class map # value ID -> class map
@ -2085,6 +2114,7 @@ _atomic_orderings = {
} }
class Builder(llvm.Wrapper): class Builder(llvm.Wrapper):
__slots__ = ()
@staticmethod @staticmethod
def new(basic_block): def new(basic_block):

View file

@ -37,7 +37,17 @@ def run(verbosity=1):
if sys.version_info[:2] > (2, 6): if sys.version_info[:2] > (2, 6):
kwargs['buffer'] = True kwargs['buffer'] = True
runner = unittest.TextTestRunner(**kwargs) runner = unittest.TextTestRunner(**kwargs)
testresult = runner.run(suite)
try:
from guppy import hpy
except ImportError:
testresult = runner.run(suite)
else:
hp = hpy()
hp.setref()
testresult = runner.run(suite)
print(hp.heap())
if testresult: if testresult:
# Run isolated tests # Run isolated tests

View file

@ -46,6 +46,7 @@ def _capsule_weakref_dtor(item):
class Capsule(object): class Capsule(object):
"Wraps PyCapsule so that we can build weakref of it." "Wraps PyCapsule so that we can build weakref of it."
__slots__ = 'pointer', 'capsule', 'name', '__weakref__'
from ._capsule import check, getClassName, getName, getPointer from ._capsule import check, getClassName, getName, getPointer
@ -97,7 +98,7 @@ class Capsule(object):
return not (self == other) return not (self == other)
class WeakRef(ref): class WeakRef(ref):
pass __slots__ = 'pointer', 'name', 'capsule'
_addr2refct = defaultdict(lambda: 0) _addr2refct = defaultdict(lambda: 0)
_capsule2weak = WeakKeyDictionary() _capsule2weak = WeakKeyDictionary()
@ -126,7 +127,7 @@ def obtain_ownership(cap):
if cls._has_dtor(): if cls._has_dtor():
addr = cap.pointer addr = cap.pointer
name = cap.name name = cap.name
assert _addr2dtor[addr] is None assert _addr2dtor[(name, addr)] is None
_addr2dtor[(name, addr)] = cls._delete_ _addr2dtor[(name, addr)] = cls._delete_
def has_ownership(cap): def has_ownership(cap):

View file

@ -1,5 +1,4 @@
import inspect, textwrap import inspect, textwrap
import functools
import codegen as cg import codegen as cg
import os import os
@ -8,10 +7,12 @@ namespaces = {}
RESERVED = frozenset(['None']) RESERVED = frozenset(['None'])
def makedir(directory): def makedir(directory):
if not os.path.exists(directory): if not os.path.exists(directory):
os.makedirs(directory) os.makedirs(directory)
class SubModule(object): class SubModule(object):
def __init__(self): def __init__(self):
self.methods = [] self.methods = []
@ -293,6 +294,10 @@ class Class(SubModule, _Type):
writer.println('@capsule.register_class("%s")' % self.fullname) writer.println('@capsule.register_class("%s")' % self.fullname)
with writer.block('class %(clsname)s(%(bases)s):' % locals()): with writer.block('class %(clsname)s(%(bases)s):' % locals()):
writer.println('_llvm_type_ = "%s"' % self.fullname) writer.println('_llvm_type_ = "%s"' % self.fullname)
if self.bases:
writer.println('__slots__ = ()')
else:
writer.println('__slots__ = "__weakref__"')
for enum in self.enums: for enum in self.enums:
enum.compile_py(writer) enum.compile_py(writer)
for meth in self.methods: for meth in self.methods:
@ -399,6 +404,7 @@ class Enum(object):
writer.println(fmt % locals()) writer.println(fmt % locals())
writer.println() writer.println()
class Method(object): class Method(object):
_kind_ = 'meth' _kind_ = 'meth'
@ -516,6 +522,7 @@ class Method(object):
with writer.block('if len(%s) > %d:' % (unwrapped, i)): with writer.block('if len(%s) > %d:' % (unwrapped, i)):
writer.release_ownership('%s[%d]' % (unwrapped, i)) writer.release_ownership('%s[%d]' % (unwrapped, i))
class CustomMethod(Method): class CustomMethod(Method):
def __init__(self, methodname, retty, *argtys): def __init__(self, methodname, retty, *argtys):
super(CustomMethod, self).__init__(retty, *argtys) super(CustomMethod, self).__init__(retty, *argtys)
@ -594,6 +601,7 @@ class CustomFunction(Function):
def fullname(self): def fullname(self):
return self.realname return self.realname
class Destructor(Method): class Destructor(Method):
_kind_ = 'dtor' _kind_ = 'dtor'
@ -625,6 +633,7 @@ class Constructor(StaticMethod):
ret = writer.declare(retty.fullname, stmt) ret = writer.declare(retty.fullname, stmt)
return ret return ret
class ref(_Type): class ref(_Type):
def __init__(self, element): def __init__(self, element):
assert isinstance(element, Class), type(element) assert isinstance(element, Class), type(element)
@ -686,13 +695,16 @@ class ptr(_Type):
return writer.pycapsule_new(val, self.element.capsule_name, return writer.pycapsule_new(val, self.element.capsule_name,
self.element.fullname) self.element.fullname)
class ownedptr(ptr): class ownedptr(ptr):
pass pass
def const(ptr_or_ref): def const(ptr_or_ref):
ptr_or_ref.const = True ptr_or_ref.const = True
return ptr_or_ref return ptr_or_ref
class cast(_Type): class cast(_Type):
format = 'O' format = 'O'
@ -757,6 +769,7 @@ class CustomPythonMethod(object):
for line in self.sourcelines: for line in self.sourcelines:
writer.println(line) writer.println(line)
class CustomPythonStaticMethod(CustomPythonMethod): class CustomPythonStaticMethod(CustomPythonMethod):
def compile_py(self, writer): def compile_py(self, writer):
writer.println('@staticmethod') writer.println('@staticmethod')
@ -845,6 +858,7 @@ class Attr(object):
TARGETS_BUILT = os.environ.get('LLVM_TARGETS_BUILT', '').split() TARGETS_BUILT = os.environ.get('LLVM_TARGETS_BUILT', '').split()
def _parse_llvm_version(ver): def _parse_llvm_version(ver):
import re import re
m = re.compile(r'(\d+)\.(\d+)').match(ver) m = re.compile(r'(\d+)\.(\d+)').match(ver)