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[] ifdef::toc[] -
+ diff --git a/www/src/style/xhtml11.css b/www/src/style/xhtml11.css index fd931dd..4c11b48 100644 --- a/www/src/style/xhtml11.css +++ b/www/src/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/src/userguide.txt b/www/src/userguide.txt index f36610a..1a43e41 100644 --- a/www/src/userguide.txt +++ b/www/src/userguide.txt @@ -1,13 +1,6 @@ llvm-py User Guide =================== -[NOTE] -======================================================================= -This document is updated frequently (last updated on {localdate}). -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. @@ -33,9 +26,9 @@ http://opensource.org/licenses/bsd-license.php[new BSD license]. More information is available link:license.html[here]. .Platforms -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. .Versions llvm-py requires verion 2.3 of LLVM. It will not work with previous @@ -70,16 +63,31 @@ been tried only with gcc/g\+\+ though. Tip: If LLVM 2.3 does not install cleanly, try installing ``ocamldoc'' first. +LLVM and `--enable-pic` +~~~~~~~~~~~~~~~~~~~~~~~ + +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 +---- + llvm-config ~~~~~~~~~~~ -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 @@ -482,34 +490,41 @@ attributes of the `Module` class is: ======================================================================= .Static Constructors `new(module_id)`:: - 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. .Properties `data_layout`:: - a string representing the ABI of the platform + A string representing the ABI of the platform. `target`:: - a string like `i386-pc-linux-gnu` or `i386-pc-solaris2.8` + A string like `i386-pc-linux-gnu` or `i386-pc-solaris2.8`. `global_variables` [read-only]:: - TODO + An iterable that yields `GlobalVariable` objects, that represent + the global variables of the module. `functions` [read-only]:: - TODO + An iterable that yields `Function` objects, that represent functions + in the module. .Methods -`add_type_name`:: - TODO -`delete_type_name`:: - TODO -`add_global_variable`:: - TODO -`get_global_variable_named`:: - TODO -`add_function`:: - TODO -`get_function_named`:: - TODO -`verify`:: - Verifies the correctness of the module. Raises `LLVMException` on +`add_type_name(name, ty)`:: + Add an alias (typedef) for the type `ty` with the name `name`. +`delete_type_name(name)`:: + Delete an alias with the name `name`. +`add_global_variable(ty, name)`:: + Add a global variable of the type `ty` with the name `name`. + Returns a `GlobalVariable` object. +`get_global_variable_named(name)`:: + Get a `GlobalVariable` object corresponding to the global + variable with the name `name`. Raises `LLVMException` if such a + variable does not exist. +`add_function(ty, name)`:: + Add a function named `name` with the function type `ty`. `ty` must + of an object of type `FunctionType`. +`get_function_named(name)`:: + Get a `Function` object corresponding to the function with the name + `name`. Raises `LLVMException` if such a function does not exist. +`verify()`:: + Verify the correctness of the module. Raises `LLVMException` on errors. .Special Methods @@ -615,8 +630,9 @@ The class-level documentation follows: Creates an array type, holding `count` elements, each of type `elty` (which should be a `Type`). `pointer(pty, addrspc=0)`:: - 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). `void()`:: Creates a void type. Used for function return types. `label()`:: diff --git a/www/web/about.html b/www/web/about.html index b523b8a..28df233 100644 --- a/www/web/about.html +++ b/www/web/about.html @@ -3,8 +3,8 @@ - - + + @@ -42,7 +42,7 @@ llvm-dev mailing list and irc.oftc.net#llvm (mdevan).

diff --git a/www/web/contribute.html b/www/web/contribute.html index c8c0bb3..2122582 100644 --- a/www/web/contribute.html +++ b/www/web/contribute.html @@ -3,8 +3,8 @@ - - + + @@ -124,7 +124,7 @@ Improve tests. diff --git a/www/web/download.html b/www/web/download.html index 0c29274..99d1023 100644 --- a/www/web/download.html +++ b/www/web/download.html @@ -3,8 +3,8 @@ - - + + @@ -122,7 +122,7 @@ package.

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 @@ + +
#!/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.
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)}
»About
-
+ @@ -43,17 +43,6 @@ window.onload = function(){generateToc(2)}
-
- - - -
-Note - -

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.

Platforms
-

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.

Versions

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.

+

LLVM and —enable-pic

+

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
+

llvm-config

-

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.

@@ -796,7 +797,7 @@ attributes of the Module class is:

- a string representing the ABI of the platform + A string representing the ABI of the platform.

@@ -804,7 +805,7 @@ attributes of the Module class is:

- a string like i386-pc-linux-gnu or i386-pc-solaris2.8 + A string like i386-pc-linux-gnu or i386-pc-solaris2.8.

@@ -812,7 +813,8 @@ attributes of the Module class is:

- TODO + An iterable that yields GlobalVariable objects, that represent + the global variables of the module.

@@ -820,65 +822,71 @@ attributes of the Module class is:

- TODO + An iterable that yields Function objects, that represent functions + in the module.

Methods
-add_type_name +add_type_name(name, ty)

- TODO + Add an alias (typedef) for the type ty with the name name.

-delete_type_name +delete_type_name(name)

- TODO + Delete an alias with the name name.

-add_global_variable +add_global_variable(ty, name)

- TODO + Add a global variable of the type ty with the name name. + Returns a GlobalVariable object.

-get_global_variable_named +get_global_variable_named(name)

- TODO + Get a GlobalVariable object corresponding to the global + variable with the name name. Raises LLVMException if such a + variable does not exist.

-add_function +add_function(ty, name)

- TODO + Add a function named name with the function type ty. ty must + of an object of type FunctionType.

-get_function_named +get_function_named(name)

- TODO + Get a Function object corresponding to the function with the name + name. Raises LLVMException if such a function does not exist.

-verify +verify()

- Verifies the correctness of the module. Raises LLVMException on + Verify the correctness of the module. Raises LLVMException on errors.

@@ -1228,8 +1236,9 @@ cellspacing="0" cellpadding="4">

- 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).

@@ -2425,6 +2434,8 @@ m.add_type_name("struct.node", th.type) # show what we created print m
+
+

which gives the output:

@@ -2633,7 +2644,7 @@ reached at mdevan.foobar@gmail.com.