Code cleanup, pylint, updated README, CHANGELOG

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@67 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2009-02-23 11:24:12 +00:00
commit 231aa4a5c4
13 changed files with 1055 additions and 922 deletions

View file

@ -1,45 +1,45 @@
#!/usr/bin/env python
# Import the llvm-py modules.
from llvm import *
from llvm.core import *
# Create an (empty) module.
my_module = Module.new('my_module')
# All the types involved here are "int"s. This type is represented
# by an object of the llvm.core.Type class:
ty_int = Type.int() # by default 32 bits
# We need to represent the class of functions that accept two integers
# and return an integer. This is represented by an object of the
# function type (llvm.core.FunctionType):
ty_func = Type.function(ty_int, [ty_int, ty_int])
# Now we need a function named 'sum' of this type. Functions are not
# free-standing (in llvm-py); it needs to be contained in a module.
f_sum = my_module.add_function(ty_func, "sum")
# Let's name the function arguments as 'a' and 'b'.
f_sum.args[0].name = "a"
f_sum.args[1].name = "b"
# Our function needs a "basic block" -- a set of instructions that
# end with a terminator (like return, branch etc.). By convention
# the first block is called "entry".
bb = f_sum.append_basic_block("entry")
# Let's add instructions into the block. For this, we need an
# instruction builder:
builder = Builder.new(bb)
# OK, now for the instructions themselves. We'll create an add
# instruction that returns the sum as a value, which we'll use
# a ret instruction to return.
tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp")
builder.ret(tmp)
# We've completed the definition now! Let's see the LLVM assembly
# language representation of what we've created:
print my_module
#!/usr/bin/env python
# Import the llvm-py modules.
from llvm import *
from llvm.core import *
# Create an (empty) module.
my_module = Module.new('my_module')
# All the types involved here are "int"s. This type is represented
# by an object of the llvm.core.Type class:
ty_int = Type.int() # by default 32 bits
# We need to represent the class of functions that accept two integers
# and return an integer. This is represented by an object of the
# function type (llvm.core.FunctionType):
ty_func = Type.function(ty_int, [ty_int, ty_int])
# Now we need a function named 'sum' of this type. Functions are not
# free-standing (in llvm-py); it needs to be contained in a module.
f_sum = my_module.add_function(ty_func, "sum")
# Let's name the function arguments as 'a' and 'b'.
f_sum.args[0].name = "a"
f_sum.args[1].name = "b"
# Our function needs a "basic block" -- a set of instructions that
# end with a terminator (like return, branch etc.). By convention
# the first block is called "entry".
bb = f_sum.append_basic_block("entry")
# Let's add instructions into the block. For this, we need an
# instruction builder:
builder = Builder.new(bb)
# OK, now for the instructions themselves. We'll create an add
# instruction that returns the sum as a value, which we'll use
# a ret instruction to return.
tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp")
builder.ret(tmp)
# We've completed the definition now! Let's see the LLVM assembly
# language representation of what we've created:
print my_module

View file

@ -1,3 +1,4 @@
#!/usr/bin/env python
from llvm.core import *
import llvm._core

View file

@ -1,48 +1,48 @@
#!/usr/bin/env python
# Tests accessing of instruction operands.
from llvm.core import *
#===----------------------------------------------------------------------===
# implement a test function
def make_function():
test_module = """
define i32 @prod(i32, i32) {
entry:
%2 = mul i32 %0, %1
ret i32 %2
}
define i32 @test_func(i32, i32, i32) {
entry:
%tmp1 = call i32 @prod(i32 %0, i32 %1)
%tmp2 = add i32 %tmp1, %2
%tmp3 = add i32 %tmp2, 1
ret i32 %tmp3
}
"""
class strstream(object):
def __init__(self): pass
def read(self): return test_module
m = Module.from_assembly(strstream())
print "-"*60
print m
print "-"*60
print "Examining function `test_func':"
return m.get_function_named("test_func")
#===----------------------------------------------------------------------===
func = make_function()
idx = 1
for inst in func.basic_blocks[0].instructions:
print "Instruction #%d:" % (idx,)
print " operand_count =", inst.operand_count
print " operands:"
oidx = 1
for op in inst.operands:
print " %d: %s" % (oidx, repr(op))
oidx += 1
idx += 1
#!/usr/bin/env python
# Tests accessing of instruction operands.
from llvm.core import *
#===----------------------------------------------------------------------===
# implement a test function
def make_function():
test_module = """
define i32 @prod(i32, i32) {
entry:
%2 = mul i32 %0, %1
ret i32 %2
}
define i32 @test_func(i32, i32, i32) {
entry:
%tmp1 = call i32 @prod(i32 %0, i32 %1)
%tmp2 = add i32 %tmp1, %2
%tmp3 = add i32 %tmp2, 1
ret i32 %tmp3
}
"""
class strstream(object):
def __init__(self): pass
def read(self): return test_module
m = Module.from_assembly(strstream())
print "-"*60
print m
print "-"*60
print "Examining function `test_func':"
return m.get_function_named("test_func")
#===----------------------------------------------------------------------===
func = make_function()
idx = 1
for inst in func.basic_blocks[0].instructions:
print "Instruction #%d:" % (idx,)
print " operand_count =", inst.operand_count
print " operands:"
oidx = 1
for op in inst.operands:
print " %d: %s" % (oidx, repr(op))
oidx += 1
idx += 1

View file

@ -162,9 +162,10 @@ def do_value():
k = Constant.int(ti, 42)
k.name = 'a'
s = k.name
t = s.type
t = k.type
s = str(k)
s = k == Constant.int(ti, 43)
i = k.value_id
def do_constant():
@ -473,6 +474,7 @@ def do_llvm_core():
do_module()
do_type()
do_typehandle()
do_value()
do_constant()
do_global_value()
do_global_variable()
@ -577,3 +579,20 @@ def main():
main()
# to add:
# IntegerType
# FunctionType
# StructType
# ArrayType
# PointerType
# VectorType
# ConstantExpr
# ConstantAggregateZero
# ConstantInt
# ConstantFP
# ConstantArray
# ConstantStruct
# ConstantVector
# ConstantPointerNull
# MemoryBuffer