Added web pages, and some ee/passes stuff. *Broken*.

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@3 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2008-06-03 17:24:46 +00:00
commit 6ee3298bd2
46 changed files with 1120 additions and 8 deletions

View file

@ -0,0 +1,14 @@
"""Common classes related to LLVM.
"""
#===----------------------------------------------------------------------===
# Exceptions
#===----------------------------------------------------------------------===
class LLVMException(Exception):
def __init__(self, msg=""):
Exception.__init__(self, msg)

View file

@ -3,6 +3,10 @@
#include "wrap.h"
#include "extra.h"
/* LLVM includes */
#include "llvm-c/Analysis.h"
#include "llvm-c/Transforms/Scalar.h"
/* libc includes */
#include <stdarg.h> /* for malloc(), free() */
@ -600,6 +604,19 @@ _wrap_obj2obj(LLVMFinalizeFunctionPassManager, LLVMPassManagerRef, int)
_wrap_obj2none(LLVMDisposePassManager, LLVMPassManagerRef)
/*===----------------------------------------------------------------------===*/
/* Passes */
/*===----------------------------------------------------------------------===*/
_wrap_obj2none(LLVMAddConstantPropagationPass, LLVMPassManagerRef)
_wrap_obj2none(LLVMAddInstructionCombiningPass, LLVMPassManagerRef)
_wrap_obj2none(LLVMAddPromoteMemoryToRegisterPass, LLVMPassManagerRef)
_wrap_obj2none(LLVMAddDemoteMemoryToRegisterPass, LLVMPassManagerRef)
_wrap_obj2none(LLVMAddReassociatePass, LLVMPassManagerRef)
_wrap_obj2none(LLVMAddGVNPass, LLVMPassManagerRef)
_wrap_obj2none(LLVMAddCFGSimplificationPass, LLVMPassManagerRef)
/*===----------------------------------------------------------------------===*/
/* Python member method table */
/*===----------------------------------------------------------------------===*/
@ -922,6 +939,15 @@ static PyMethodDef core_methods[] = {
_method( LLVMFinalizeFunctionPassManager )
_method( LLVMDisposePassManager )
/* Passes */
_method( LLVMAddConstantPropagationPass )
_method( LLVMAddInstructionCombiningPass )
_method( LLVMAddPromoteMemoryToRegisterPass )
_method( LLVMAddDemoteMemoryToRegisterPass )
_method( LLVMAddReassociatePass )
_method( LLVMAddGVNPass )
_method( LLVMAddCFGSimplificationPass )
{ NULL }
};

39
llvm/_util.py Normal file
View file

@ -0,0 +1,39 @@
"""Utility functions and classes.
Not for public use!"""
import core
def _check_gen(obj, type):
if not isinstance(obj, type):
msg = "argument must be an instance of llvm.core.%s (or of a class derived from it)" % type_str
raise TypeError, msg
def check_is_type(obj): _check_gen(obj, core.Type)
def check_is_value(obj): _check_gen(obj, core.Value)
def check_is_pointer(obj): _check_gen(obj, core.Pointer)
def check_is_constant(obj): _check_gen(obj, core.Constant)
def check_is_function(obj): _check_gen(obj, core.Function)
def check_is_basic_block(obj): _check_gen(obj, core.BasicBlock)
def check_is_module_provider(obj): _check_gen(obj, core.ModuleProvider)
def unpack_gen(objlist, check_fn):
for obj in objlist: check_fn(obj)
return [ obj.ptr for obj in objlist ]
def unpack_types(objlist): return unpack_gen(objlist, check_is_type)
def unpack_values(objlist): return unpack_gen(objlist, check_is_value)
def unpack_constants(objlist): return unpack_gen(objlist, check_is_constant)
def wrapiter(first, next, container, wrapper):
ptr = first(container)
while ptr:
yield wrapper(ptr)
ptr = next(ptr)
class dummy_owner(object):
def __init__(self, ownee):
ownee._own(self)

View file

@ -4,7 +4,8 @@ The llvm.core module contains classes and constants required to build the
in-memory intermediate representation (IR) data structures."""
import _core
import llvm # top-level, for common stuff
import _core # C wrappers
#===----------------------------------------------------------------------===
@ -102,6 +103,7 @@ ATTR_READONLY = 1024
def _check_gen(obj, type, type_str):
if not isinstance(obj, type):
type_str = type.__module__ + "." + type.__name__
msg = "argument must be an instance of llvm.core.%s (or of a class derived from it)" % type_str
raise TypeError, msg
@ -278,7 +280,9 @@ class Module(object):
_core.LLVMGetNextFunction, self.ptr, Function)
def verify(self):
return _core.LLVMVerifyModule(self.ptr)
ret = _core.LLVMVerifyModule(self.ptr)
if ret != "":
raise llvm.LLVMException, ret
#===----------------------------------------------------------------------===

51
llvm/ee.py Normal file
View file

@ -0,0 +1,51 @@
"""Execution Engine and related classes.
"""
import llvm, core
from _util import *
import _core
class ExecutionEngine(object):
@staticmethod
def new(mp, force_interpreter=False):
check_is_module_provider(mp)
ret = _core.LLVMCreateExecutionEngine(mp.ptr, int(create_interpreter))
if isinstance(ret, str):
raise llvm.LLVMException, str
return ExecutionEngine(ret)
def __init__(self, ptr):
self.ptr = ptr
def __del__(self):
_core.LLVMDisposeExecutionEngine(self.ptr)
def run_static_constructors(self):
_core.LLVMRunStaticConstructors(self.ptr)
def run_static_destructors(self):
_core.LLVMRunStaticDestructors(self.ptr)
def run_function_as_main(self, fn, argv, envp):
check_is_function(fn)
_core.LLVMRunFunctionAsMain(self.ptr, fn.ptr, argv, envp)
def run_function(self, fn, args):
check_is_function(fn)
return _core.LLVMRunFunction(self.ptr, fn.ptr, args)
def free_machine_code_for_function(self, fn):
check_is_function(fn)
_core.LLVMFreeMachineCodeForFunction(self.ptr, fn.ptr)
def add_module_provider(self, mp):
pass
def remove_module_provider(self):
pass
@property
def target_data(self):
return TargetData(_core.LLVMGetExecutionEngineTargetData(self.ptr))

78
llvm/passes.py Normal file
View file

@ -0,0 +1,78 @@
from _util import *
import _core
# passes
PASS_CONSTANT_PROPAGATION = 1
PASS_INSTRUCTION_COMBINING = 2
PASS_PROMOTE_MEMORY_TO_REGISTER = 3
PASS_DEMOTE_MEMORY_TO_REGISTER = 4
PASS_REASSOCIATE = 5
PASS_GVN = 6
PASS_CFG_SIMPLIFICATION = 7
#===----------------------------------------------------------------------===
# Helper functions
#===----------------------------------------------------------------------===
_pass_creator = {
PASS_CONSTANT_PROPAGATION : _core.LLVMAddConstantPropagationPass,
PASS_INSTRUCTION_COMBINING : _core.LLVMAddInstructionCombiningPass,
PASS_PROMOTE_MEMORY_TO_REGISTER : _core.LLVMAddPromoteMemoryToRegisterPass,
PASS_DEMOTE_MEMORY_TO_REGISTER : _core.LLVMAddDemoteMemoryToRegisterPass,
PASS_REASSOCIATE : _core.LLVMAddReassociatePass,
PASS_GVN : _core.LLVMAddGVNPass,
PASS_CFG_SIMPLIFICATION : _core.LLVMAddCFGSimplificationPass,
}
#===----------------------------------------------------------------------===
# Pass manager
#===----------------------------------------------------------------------===
class PassManager(object):
@staticmethod
def new():
return PassManager(_core.LLVMCreatePassManager())
def __init__(self, ptr):
self.ptr = ptr
def __del__(self):
_core.LLVMDisposePassManager(self.ptr)
def add(self, pass_id):
assert pass_id in _pass_creator, 'Invalid pass_id ("' + str(pass_id) + '")'
cfn = _pass_creator[pass_id] # KeyError => pass_id is invalid
cfn(self.ptr)
def run(self, module):
check_is_module(module)
return _core.LLVMRunPassManager(self.ptr, module.ptr)
class FunctionPassManager(PassManager):
@staticmethod
def new(mp):
check_is_module_provider(mp)
return FunctionPassManager(_core.LLVMCreateFunctionPassManager(mp.ptr))
def __init__(self, ptr):
PassManager.__init__(self, ptr)
def __del__(self):
PassManager.__del__(self)
def initialize(self):
_core.LLVMInitializeFunctionPassManager(self.ptr)
def run(self, fn):
_check_is_function(fn)
return _core.LLVMRunFunctionPassManager(fn.ptr)
def finalize(self):
_core.LLVMFinalizeFunctionPassManager(self.ptr)

View file

@ -11,8 +11,8 @@
#include <structmember.h>
/* llvm includes */
#include <llvm-c/Core.h>
#include <llvm-c/Analysis.h>
#include "llvm-c/Core.h"
#include "llvm-c/Analysis.h"
/*===----------------------------------------------------------------------===*/