~/llvm-2.3$ ./configure --enable-pic --enable-optimized+
diff --git a/llvm/core.py b/llvm/core.py index e547232..2161fef 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -1483,7 +1483,10 @@ class GlobalVariable(GlobalValue): @staticmethod def get(module, name): - return GlobalVariable(_core.LLVMGetNamedGlobal(module.ptr, name), module) + ptr = _core.LLVMGetNamedGlobal(module.ptr, name) + if not ptr: + raise llvm.LLVMException, ("no global named `%s`" % name) + return GlobalVariable(ptr, name) def __init__(self, ptr, module): GlobalValue.__init__(self, ptr, module) @@ -1539,7 +1542,10 @@ class Function(GlobalValue): @staticmethod def get(module, name): - return Function(_core.LLVMGetNamedFunction(module.ptr, name), module) + ptr = _core.LLVMGetNamedFunction(module.ptr, name) + if not ptr: + raise llvm.LLVMException, ("no function named `%s`" % name) + return Function(ptr, module) @staticmethod def intrinsic(module, id, types): diff --git a/www/src/layout.conf b/www/src/layout.conf index 8bc2972..46be827 100644 --- a/www/src/layout.conf +++ b/www/src/layout.conf @@ -36,8 +36,8 @@ monospacedwords=(?u)\\?\basciidoc\(1\) (?u)\\?\ba2x\(1\)
- - + + @@ -69,7 +69,7 @@ endif::toc[]0.3, in progress: * Intrinsics added. - * JIT Tutorials ported (Sebastian Binet). + * JIT Tutorials ported (Sebastien Binet). * GenericValue added. Used by ExecutionEngine.run(). * Build cleanly on OpenBSD, x86-64/amd64 (Laurence Tratt). * Updated documentation. @@ -158,7 +158,7 @@ package. diff --git a/www/web/examples.html b/www/web/examples.html index f0257fb..434dd5b 100644 --- a/www/web/examples.html +++ b/www/web/examples.html @@ -3,8 +3,8 @@ - - + + @@ -228,7 +228,7 @@ Conclusion and other useful LLVM tidbits (TODO) diff --git a/www/web/examples/JITTutorial1.html b/www/web/examples/JITTutorial1.html new file mode 100644 index 0000000..f7a9fe4 --- /dev/null +++ b/www/web/examples/JITTutorial1.html @@ -0,0 +1,36 @@ + +diff --git a/www/web/style/xhtml11.css b/www/web/style/xhtml11.css index fd931dd..4c11b48 100644 --- a/www/web/style/xhtml11.css +++ b/www/web/style/xhtml11.css @@ -274,3 +274,7 @@ div.toclevel4 { margin-left: 6em; font-size: 0.9em; } + +div.listingblock { + line-height: 1.2em; +} diff --git a/www/web/userguide.html b/www/web/userguide.html index 881cba2..44f6a5e 100644 --- a/www/web/userguide.html +++ b/www/web/userguide.html @@ -3,8 +3,8 @@ - - + + @@ -33,7 +33,7 @@ window.onload = function(){generateToc(2)}#!/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.html b/www/web/examples/JITTutorial2.html new file mode 100644 index 0000000..3baa722 --- /dev/null +++ b/www/web/examples/JITTutorial2.html @@ -0,0 +1,55 @@ + +#!/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 df1defb..d89153a 100644 --- a/www/web/index.html +++ b/www/web/index.html @@ -3,8 +3,8 @@ - - + + @@ -81,7 +81,7 @@ minimal changes, if any. diff --git a/www/web/license.html b/www/web/license.html index b33edbd..4983bef 100644 --- a/www/web/license.html +++ b/www/web/license.html @@ -3,8 +3,8 @@ - - + + @@ -74,7 +74,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
- |
-
- This document is updated frequently (last updated on 21-Jul-2008). -Check back often. - |
-
llvm-py provides Python bindings for LLVM. This document explains how you can setup and use it. A working knowledge of Python and a basic idea of LLVM is assumed.
@@ -75,9 +64,9 @@ open source licenses. llvm-py uses the new BSD license. More information is available here.Currently, llvm-py has been built and tested only on Linux/x86. However, -it should be trivial to build it on other unices. Windows is not -supported, for a variety of reasons.
+Currently, llvm-py has been built and tested only on Linux (i386, amd64) +and OpenBSD (i386, amd64). However, it should be trivial to build it on +other unices. Windows is not supported, for a variety of reasons.
llvm-py requires verion 2.3 of LLVM. It will not work with previous versions.
@@ -120,13 +109,25 @@ llvm-g++ or any other); llvm-py can be built with any compiler. It has been tried only with gcc/g++ though.Tip: If LLVM 2.3 does not install cleanly, try installing “ocamldoc” first.
+The result of an LLVM build is a set of static libraries and object +files. The llvm-py contains an extension package that is built into a +shared object (_core.so) which links to these static libraries and +object files. It is therefore required that the LLVM libraries and +object files be built with the -fPIC option (generate position +independent code). Be sure to use the —enable-pic option while +configuring LLVM (default is no PIC), like this:
+~/llvm-2.3$ ./configure --enable-pic --enable-optimized+
Inorder to build llvm-py, it's build script needs to know from where to -invoke the llvm helper program, llvm-config. If you've installed LLVM, -then this will be available in your PATH, and nothing further needs to -be done. If you've built LLVM yourself, or for any reason llvm-config -is not in your PATH, you'll need to pass the full path of -llvm-config to the build script.
+Inorder to build llvm-py, it's build script needs to know from where it +can invoke the llvm helper program, llvm-config. If you've installed +LLVM, then this will be available in your PATH, and nothing further +needs to be done. If you've built LLVM yourself, or for any reason +llvm-config is not in your PATH, you'll need to pass the full path +of llvm-config to the build script.
You'll need to be root to install llvm-py. Remember that your PATH is different from that of root, so even if llvm-config is in your PATH, it may not be available when you do sudo.
@@ -785,7 +786,7 @@ attributes of the Module class is:- create a new Module instance with given module_id. The module_id + Create a new Module instance with given module_id. The module_id should be a string.
- a string representing the ABI of the platform + A string representing the ABI of the platform.
- a string like i386-pc-linux-gnu or i386-pc-solaris2.8 + A string like i386-pc-linux-gnu or i386-pc-solaris2.8.
- TODO + An iterable that yields GlobalVariable objects, that represent + the global variables of the module.
- TODO + An iterable that yields Function objects, that represent functions + in the module.
- TODO + Add an alias (typedef) for the type ty with the name name.
- TODO + Delete an alias with the name name.
- TODO + Add a global variable of the type ty with the name name. + Returns a GlobalVariable object.
- TODO + Get a GlobalVariable object corresponding to the global + variable with the name name. Raises LLVMException if such a + variable does not exist.
- TODO + Add a function named name with the function type ty. ty must + of an object of type FunctionType.
- TODO + Get a Function object corresponding to the function with the name + name. Raises LLVMException if such a function does not exist.
- Verifies the correctness of the module. Raises LLVMException on + Verify the correctness of the module. Raises LLVMException on errors.
- Create a pointer to type pty (which should be a `Type). (TODO - addrspc). + Create a pointer to type pty (which should be a Type). `addrspc + is an integer that represents the address space of the pointer (see + LLVM docs / ask on llvm-dev for more info).
which gives the output: