0.3, in progress: + * Intrinsics added. + * JIT Tutorials ported (Sebastian Binet). * GenericValue added. Used by ExecutionEngine.run(). * Build cleanly on OpenBSD, x86-64/amd64 (Laurence Tratt). * Updated documentation. @@ -156,7 +158,7 @@ package.
From 6d57b0e5bcf050eaef0e80303494383d939f8609 Mon Sep 17 00:00:00 2001
From: "mdevan.foobar"
0.3, in progress: + * Intrinsics added. + * JIT Tutorials ported (Sebastian Binet). * GenericValue added. Used by ExecutionEngine.run(). * Build cleanly on OpenBSD, x86-64/amd64 (Laurence Tratt). * Updated documentation. @@ -156,7 +158,7 @@ package.
int sum(int a, int b) +diff --git a/www/web/userguide.html b/www/web/userguide.html index bf8a955..881cba2 100644 --- a/www/web/userguide.html +++ b/www/web/userguide.html @@ -49,7 +49,7 @@ window.onload = function(){generateToc(2)}int sum(int a, int b) { return a + b; } @@ -59,20 +59,20 @@ http://www.gnu.org/software/src-highlite --> from llvm.core import * # Create an (empty) module. -my_module = Module.new('my_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 +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]) +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") +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" @@ -81,17 +81,17 @@ f_sum.ar # 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") +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) +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) +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: @@ -123,35 +123,35 @@ http://www.gnu.org/software/src-highlite --> from llvm.ee import * # new import: ee = Execution Engine # Create a module, as in the previous example. -my_module = Module.new('my_module') -ty_int = Type.int() # by default 32 bits -ty_func = Type.function(ty_int, [ty_int, ty_int]) -f_sum = my_module.add_function(ty_func, "sum") +my_module = Module.new('my_module') +ty_int = Type.int() # by default 32 bits +ty_func = Type.function(ty_int, [ty_int, ty_int]) +f_sum = my_module.add_function(ty_func, "sum") f_sum.args[0].name = "a" f_sum.args[1].name = "b" -bb = f_sum.append_basic_block("entry") -builder = Builder.new(bb) -tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") -builder.ret(tmp) +bb = f_sum.append_basic_block("entry") +builder = Builder.new(bb) +tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") +builder.ret(tmp) # Create a module provider object first. Modules can come from # in-memory IRs like what we created now, or from bitcode (.bc) # files. The module provider abstracts this detail. -mp = ModuleProvider.new(my_module) +mp = ModuleProvider.new(my_module) # Create an execution engine object. This will create a JIT compiler # on platforms that support it, or an interpreter otherwise. -ee = ExecutionEngine.new(mp) +ee = ExecutionEngine.new(mp) # The arguments needs to be passed as "GenericValue" objects. -arg1 = GenericValue.int(ty_int, 100) -arg2 = GenericValue.int(ty_int, 42) +arg1 = GenericValue.int(ty_int, 100) +arg2 = GenericValue.int(ty_int, 42) # Now let's compile and run! -retval = ee.run_function(f_sum, [arg1, arg2]) +retval = ee.run_function(f_sum, [arg1, arg2]) # The return value is also GenericValue. Let's print it. -print "returned", retval.as_int() +print "returned", retval.as_int()And here's the output:
diff --git a/www/web/examples/JITTutorial1.py b/www/web/examples/JITTutorial1.py deleted file mode 100644 index 294380d..0000000 --- a/www/web/examples/JITTutorial1.py +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env python - -from llvm.core import * - -# create a module -module = Module.new ("tut1") - -# create a function type taking 3 32-bit integers, return a 32-bit integer -ty_int = Type.int (32) -func_type = Type.function (ty_int, (ty_int,)*3) - -# create a function of that type -mul_add = Function.new (module, func_type, "mul_add") -mul_add.calling_convention = CC_C -x = mul_add.args[0]; x.name = "x" -y = mul_add.args[1]; y.name = "y" -z = mul_add.args[2]; z.name = "z" - -# implement the function - -# new block -blk = mul_add.append_basic_block ("entry") - -# IR builder -bldr = Builder.new (blk) -tmp_1 = bldr.mul (x, y, "tmp_1") -tmp_2 = bldr.add (tmp_1, z, "tmp_2") - -bldr.ret (tmp_2) - -print module diff --git a/www/web/examples/JITTutorial2.py b/www/web/examples/JITTutorial2.py deleted file mode 100644 index ea1df8e..0000000 --- a/www/web/examples/JITTutorial2.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python - -from llvm.core import * - -# create a module -module = Module.new ("tut2") - -# create a function type taking 2 integers, return a 32-bit integer -ty_int = Type.int (32) -func_type = Type.function (ty_int, (ty_int, ty_int)) - -# create a function of that type -gcd = Function.new (module, func_type, "gcd") - -# name function args -x = gcd.args[0]; x.name = "x" -y = gcd.args[1]; y.name = "y" - -# implement the function - -# blocks... -entry = gcd.append_basic_block ("entry") -ret = gcd.append_basic_block ("return") -cond_false = gcd.append_basic_block ("cond_false") -cond_true = gcd.append_basic_block ("cond_true") -cond_false_2 = gcd.append_basic_block ("cond_false_2") - -# create a llvm::IRBuilder -bldr = Builder.new (entry) -x_eq_y = bldr.icmp (IPRED_EQ, x, y, "tmp") -bldr.cbranch (x_eq_y, ret, cond_false) - -bldr.position_at_end (ret) -bldr.ret(x) - -bldr.position_at_end (cond_false) -x_lt_y = bldr.icmp (IPRED_ULT, x, y, "tmp") -bldr.cbranch (x_lt_y, cond_true, cond_false_2) - -bldr.position_at_end (cond_true) -y_sub_x = bldr.sub (y, x, "tmp") -recur_1 = bldr.call (gcd, (x, y_sub_x,), "tmp") -bldr.ret (recur_1) - -bldr.position_at_end (cond_false_2) -x_sub_y = bldr.sub (x, y, "x_sub_y") -recur_2 = bldr.call (gcd, (x_sub_y, y,), "tmp") -bldr.ret (recur_2) - -print module diff --git a/www/web/index.html b/www/web/index.html index d801d50..df1defb 100644 --- a/www/web/index.html +++ b/www/web/index.html @@ -81,7 +81,7 @@ minimal changes, if any. diff --git a/www/web/license.html b/www/web/license.html index 64bb49a..b33edbd 100644 --- a/www/web/license.html +++ b/www/web/license.html @@ -74,7 +74,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This document is updated frequently (last updated on 05-Jul-2008). +
This document is updated frequently (last updated on 21-Jul-2008). Check back often.
/* compute sum of 1..n */ -unsigned sum(unsigned n) +unsigned sum(unsigned n) { if (n == 0) return 0; else - return n + sum(n-1); + return n + sum(n-1); }
The corresponding LLVM assembly:
@@ -758,7 +758,7 @@ http://www.gnu.org/software/src-highlite --> from llvm.core import * # create a module -my_module = Module.new('my_module') +my_module = Module.new('my_module')The constructor of the Module class should not be used to instantiate a Module object. This is a common feature for all llvm-py classes.
@@ -1292,8 +1292,8 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -assert Type.int().kind == TYPE_INTEGER -assert Type.void().kind == TYPE_VOID +assert Type.int().kind == TYPE_INTEGER +assert Type.void().kind == TYPE_VOID@@ -1392,11 +1392,11 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -func_type = Type.function( Type.int(), [ Type.int(), Type.int() ] ) +func_type = Type.function( Type.int(), [ Type.int(), Type.int() ] ) for arg in func_type.args: assert arg.kind == TYPE_INTEGER - assert arg == Type.int() -assert func_type.arg_count == len(func_type.args) + assert arg == Type.int() +assert func_type.arg_count == len(func_type.args)@@ -1442,11 +1442,11 @@ http://www.gnu.org/software/src-highlite --> by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> - struct_type = Type.struct( [ Type.int(), Type.int() ] ) +struct_type = Type.struct( [ Type.int(), Type.int() ] ) for elem in struct_type.elements: assert elem.kind == TYPE_INTEGER - assert elem == Type.int() -assert struct_type.element_count == len(struct_type.elements) + assert elem == Type.int() +assert struct_type.element_count == len(struct_type.elements)@@ -1555,35 +1555,35 @@ http://www.gnu.org/software/src-highlite --> #!/usr/bin/env python # integers -int_ty = Type.int() -bool_ty = Type.int(1) -int_64bit = Type.int(64) +int_ty = Type.int() +bool_ty = Type.int(1) +int_64bit = Type.int(64) # floats -sprec_real = Type.float() -dprec_real = Type.double() +sprec_real = Type.float() +dprec_real = Type.double() # arrays and vectors -intar_ty = Type.array( int_ty, 10 ) # "typedef int intar_ty[10];" -twodim = Type.array( intar_ty , 10 ) # "typedef int twodim[10][10];" -vec = Type.array( int_ty, 10 ) +intar_ty = Type.array( int_ty, 10 ) # "typedef int intar_ty[10];" +twodim = Type.array( intar_ty , 10 ) # "typedef int twodim[10][10];" +vec = Type.array( int_ty, 10 ) # structures -s1_ty = Type.struct( [ int_ty, sprec_real ] ) +s1_ty = Type.struct( [ int_ty, sprec_real ] ) # "struct s1_ty { int v1; float v2; };" # pointers -intptr_ty = Type.pointer(int_ty) # "typedef int *intptr_ty;" +intptr_ty = Type.pointer(int_ty) # "typedef int *intptr_ty;" # functions -f1 = Type.function( int_ty, [ int_ty ] ) +f1 = Type.function( int_ty, [ int_ty ] ) # functions that take 1 int_ty and return 1 int_ty -f2 = Type.function( Type.void(), [ int_ty ] ) +f2 = Type.function( Type.void(), [ int_ty ] ) # functions that take 1 int_ty and return nothing -fnargs = [ Type.pointer( Type.int(8) ) ] -printf = Type.function( Type.int(), fnargs, True ) +fnargs = [ Type.pointer( Type.int(8) ) ] +printf = Type.function( Type.int(), fnargs, True ) # variadic functionValues (llvm.core)
@@ -1673,15 +1673,15 @@ http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite -->#!/usr/bin/env python -ti = Type.int() # a 32-bit int type +ti = Type.int() # a 32-bit int type -k1 = Constant.int(ti, 42) # "int k1 = 42;" -k2 = k1.add( Constant.int( ti, 10 ) ) # "int k2 = k1 + 10;" +k1 = Constant.int(ti, 42) # "int k1 = 42;" +k2 = k1.add( Constant.int( ti, 10 ) ) # "int k2 = k1 + 10;" -tr = Type.float() +tr = Type.float() -r1 = Constant.real(tr, "3.141592") # create from a string -r2 = Constant.real(tr, 1.61803399) # create from a Python float +r1 = Constant.real(tr, "3.141592") # create from a string +r2 = Constant.real(tr, 1.61803399) # create from a Python floatThe following constructors (static methods) can be used to create constants:
@@ -2491,7 +2491,7 @@ to the C function: by Lorenzo Bettini http://www.lorenzobettini.it http://www.gnu.org/software/src-highlite --> -int sum(int a, int b) +int sum(int a, int b) { return a + b; } @@ -2509,20 +2509,20 @@ http://www.gnu.org/software/src-highlite --> from llvm.core import * # Create an (empty) module. -my_module = Module.new('my_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 +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]) +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") +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" @@ -2531,17 +2531,17 @@ f_sum.ar # 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") +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) +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) +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: @@ -2573,35 +2573,35 @@ http://www.gnu.org/software/src-highlite --> from llvm.ee import * # new import: ee = Execution Engine # Create a module, as in the previous example. -my_module = Module.new('my_module') -ty_int = Type.int() # by default 32 bits -ty_func = Type.function(ty_int, [ty_int, ty_int]) -f_sum = my_module.add_function(ty_func, "sum") +my_module = Module.new('my_module') +ty_int = Type.int() # by default 32 bits +ty_func = Type.function(ty_int, [ty_int, ty_int]) +f_sum = my_module.add_function(ty_func, "sum") f_sum.args[0].name = "a" f_sum.args[1].name = "b" -bb = f_sum.append_basic_block("entry") -builder = Builder.new(bb) -tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") -builder.ret(tmp) +bb = f_sum.append_basic_block("entry") +builder = Builder.new(bb) +tmp = builder.add(f_sum.args[0], f_sum.args[1], "tmp") +builder.ret(tmp) # Create a module provider object first. Modules can come from # in-memory IRs like what we created now, or from bitcode (.bc) # files. The module provider abstracts this detail. -mp = ModuleProvider.new(my_module) +mp = ModuleProvider.new(my_module) # Create an execution engine object. This will create a JIT compiler # on platforms that support it, or an interpreter otherwise. -ee = ExecutionEngine.new(mp) +ee = ExecutionEngine.new(mp) # The arguments needs to be passed as "GenericValue" objects. -arg1 = GenericValue.int(ty_int, 100) -arg2 = GenericValue.int(ty_int, 42) +arg1 = GenericValue.int(ty_int, 100) +arg2 = GenericValue.int(ty_int, 42) # Now let's compile and run! -retval = ee.run_function(f_sum, [arg1, arg2]) +retval = ee.run_function(f_sum, [arg1, arg2]) # The return value is also GenericValue. Let's print it. -print "returned", retval.as_int() +print "returned", retval.as_int()And here's the output:
@@ -2633,7 +2633,7 @@ reached at mdevan.foobar@gmail.com.