Updated documentation.

git-svn-id: http://llvm-py.googlecode.com/svn/trunk@22 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
mdevan.foobar 2008-06-25 15:09:11 +00:00
commit fe03572bba
12 changed files with 548 additions and 208 deletions

View file

@ -6,7 +6,7 @@ from string import Template
from optparse import OptionParser
# files in src dir that should not be copied to web dir
SKIP_FILES = [ 'layout.conf', '.svn', 'instrset.inc' ]
SKIP_FILES = [ 'layout.conf', '.svn', 'instrset.inc', 'example.inc' ]
# asciidoc command line
ASCIIDOC = 'asciidoc --unsafe --conf-file=${srcdir}/layout.conf -a icons -o ${outfile} ${infile}'

132
www/src/example.inc Normal file
View file

@ -0,0 +1,132 @@
A Simple Function
~~~~~~~~~~~~~~~~~
Let's create a module containing a single function, corresponding to the
`C` function:
[C]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int sum(int a, int b)
{
return a + b;
}
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here's how it looks like:
[python]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env python
# Import the llvm-py modules.
from llvm import *
from llvm.core import *
# Create a 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
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the output:
-----------------------------------------------------------------------
; ModuleID = 'my_module'
define i32 @sum(i32 %a, i32 %b) {
entry:
%tmp = add i32 %a, %b ; <i32> [#uses=1]
ret i32 %tmp
}
-----------------------------------------------------------------------
Adding JIT Compilation
~~~~~~~~~~~~~~~~~~~~~~
Let's compile this function in-memory and run it.
[python]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env python
# Import the llvm-py modules.
from llvm import *
from llvm.core import *
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")
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)
# 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)
# Create an execution engine object. This will create a JIT compiler
# on platforms that support it, or an interpreter otherwise.
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)
# Now let's compile and run!
retval = ee.run_function(f_sum, [arg1, arg2])
# The return value is also GenericValue. Let's print it.
print "returned", retval.as_int()
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
And here's the output:
-----------------------------------------------------------------------
returned 142
-----------------------------------------------------------------------
That was easy, right?!

View file

@ -1,25 +1,5 @@
Examples
========
Here's an example:
[python]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include::../../test/example.py[]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
which gives this output:
-----------------------------------------------------------------------
; ModuleID = 'my_module'
define i32 @foobar(double %arg1, double %arg2) {
entry:
%temp1 = add double %arg1, %arg2 ; <double> [#uses=1]
%temp2 = sub double %temp1, 1.000000e+00 ; <double> [#uses=1]
%temp3 = fptoui double %temp2 to i32 ; <i32> [#uses=1]
ret i32 %temp3
}
-----------------------------------------------------------------------
include::example.inc[]

View file

@ -68,16 +68,16 @@ endif::toc[]
<div>&#187;<a href="about.html">About</a></div>
</td>
<td>
<div id="layout-content">
<div id="header">
<h1>{doctitle}</h1>
</div>
ifdef::toc[]
<div id="toc">
<div id="toc" style="float: right">
<div id="toctitle">Table of Contents</div>
<noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
endif::toc[]
<div id="layout-content">
<div id="header">
<h1>{doctitle}</h1>
</div>
[footer]
<div id="footer">

View file

@ -1,9 +1,15 @@
llvm-py User Guide
===================
NOTE: This document is updated frequently (last updated on {localdate}).
[NOTE]
=======================================================================
This document is updated frequently (last updated on {localdate}).
Check back often.
You might wish to look over the link:#examples[examples] first.
=======================================================================
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.
@ -55,7 +61,7 @@ required for this:
- LLVM, either installed or built
On debian-based systems, the first three can be installed with the
command `sudo apt-get install gcc g++ python python-dev'. Note that
command `sudo apt-get install gcc g\+\+ python python-dev`. Note that
ubuntu repository has an old version of llvm (1.8) which will not work
with llvm-py.
@ -296,7 +302,7 @@ in llvm-py.
Modules
~~~~~~~
Modules, in the LLVM IR, are similar to a single _C_ language source
Modules, in the LLVM IR, are similar to a single `C` language source
file (.c file). A module contains:
- functions (declarations and definitions)
@ -325,7 +331,7 @@ have to be explicitly selected and run on each module. This gives you
the flexibility to choose transformations and optimizations that are
most suitable for the code in the module.
There is a LLVM binary called http://www.llvm.org/cmds/opt.html[opt],
There is an LLVM binary called http://www.llvm.org/cmds/opt.html[opt],
which lets you run passes on bitcode files from the command line. You
can write your own passes (in C/C\+\+, as a shared library). This can be
loaded and executed by +opt+. (Although llvm-py does not allow you to
@ -361,9 +367,9 @@ over enough LLVM APIs to allow the implementation of your own
compiler/VM backend in pure Python. If you're come this far, you
probably know why this is a good idea.
Out of the 6 modules, one is an "extension" module (i.e., it's written
in C), and another one is a private utility module, which leaves 4
public modules. These are:
Out of the 6 modules, one is an ``extension'' module (i.e., it is
written in C), and another one is a small private utility module, which
leaves 4 public modules. These are:
- +llvm+ -- top-level package, common classes (like exceptions)
- +llvm.core+ -- IR-related APIs
@ -375,7 +381,7 @@ Python constructs are used (deliberately) --
http://docs.python.org/lib/built-in-funcs.html[property()] and
http://wiki.python.org/moin/PythonDecoratorLibrary[property
decorators] are probably the most exotic animals around. All classes are
the "new style" classes. The APIs are designed to be navigable (and
"new style" classes. The APIs are designed to be navigable (and
guessable!) once you know a few conventions. These conventions are
highlighted in the sections below.
@ -423,8 +429,8 @@ Here is a quick overview of the contents of each package:
.A note on the 'import'ing of these modules
Pythonically, modules are imported with the statement +"import
llvm.core"+ and not +"from llvm.core import *"+. However, you might find
it more convenient to import llvm-py modules thus:
llvm.core"+. However, you might find it more convenient to import
llvm-py modules thus:
[python]
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -436,7 +442,7 @@ source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This avoids quite some typing. Both conventions work, however.
TIP: Python-style documentation strings (+__doc__+) are present in
TIP: Python-style documentation strings (`\_\_doc\_\_`) are present in
llvm-py. You can use the +help()+ of the interactive Python
interpreter or the +object?+ of http://ipython.scipy.org/moin/[IPython]
to get online help. (Note: not complete yet!)
@ -460,7 +466,7 @@ from llvm.core import *
my_module = Module.new('my_module')
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The constructor of the Module class should *not* be used to instantiate
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.
[TIP]
@ -903,6 +909,7 @@ source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following constructors (static methods) can be used to create
constants:
[[constctors]]
[frame="all",grid="all"]
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Constructor Method, What It Creates
@ -924,6 +931,7 @@ Constructor Method, What It Creates
The following operations are available:
[[constops]]
[frame="all",grid="all"]
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Method, Operation
@ -944,13 +952,13 @@ Method, Operation
`k.xor(k2)`, "Bitwise exclusive-or of `k` and `k2`."
"`k.icmp(ipred, k2)`", "Compare `k` with `k2` using the predicate `ipred`. See table link:#ipred[below] for list of predicates for integer operands."
"`k.fcmp(rpred, k2)`", "Compare `k` with `k2` using the predicate `rpred`. See table link:#rpred[below] for list of predicates for real operands."
shl, TODO
lshr, TODO
ashr, TODO
gep, TODO
trunc, TODO
sext, TODO
zext, TODO
`k.shl(k2)`, "Shift `k` left by `k2` bits."
`k.lshr(k2)`, "Shift `k` logically right by `k2` bits (new bits are 0s)."
`k.ashr(k2)`, "Shift `k` arithmetically right by `k2` bits (new bits are same as previous sign bit)."
`k.gep(indices)`, "TODO"
`k.trunc(ty)`, "Truncate `k` to a type `ty` of lower bitwidth."
`k.sext(ty)`, "Sign extend `k` to a type `ty` of higher bitwidth, while extending the sign bit."
`k.zext(ty)`, "Sign extend `k` to a type `ty` of higher bitwidth, all new bits are 0s."
fptrunc, TODO
fpext, TODO
uitofp, TODO
@ -994,24 +1002,37 @@ of these are integer constants defined in the `llvm.core` module.
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Value, Meaning
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`RPRED_FALSE`,
`RPRED_OEQ`,
`RPRED_OGT`,
`RPRED_OGE`,
`RPRED_OLT`,
`RPRED_OLE`,
`RPRED_ONE`,
`RPRED_ORD`,
`RPRED_UNO`,
`RPRED_UEQ`,
`RPRED_UGT`,
`RPRED_UGE`,
`RPRED_ULT`,
`RPRED_ULE`,
`RPRED_UNE`,
`RPRED_TRUE `,
`RPRED_FALSE`, Always false
`RPRED_OEQ`, True if ordered and equal
`RPRED_OGT`, True if ordered and greater than
`RPRED_OGE`, True if ordered and greater than or equal
`RPRED_OLT`, True if ordered and less than
`RPRED_OLE`, True if ordered and less than or equal
`RPRED_ONE`, True if ordered and operands are unequal
`RPRED_ORD`, True if ordered (no NaNs)
`RPRED_UNO`, True if unordered: `isnan(X) | isnan(Y)`
`RPRED_UEQ`, True if unordered or equal
`RPRED_UGT`, True if unordered or greater than
`RPRED_UGE`, "True if unordered, greater than or equal"
`RPRED_ULT`, "True if unordered, or less than"
`RPRED_ULE`, "True if unordered, less than or equal"
`RPRED_UNE`, True if unordered or not equal
`RPRED_TRUE `, Always true
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.llvm.core.Constant
[caption=""]
=======================================================================
.Base Class
- `llvm.core.Value`
.Static Constructors
See table of constructors link:#constctors[above] for full list.
.Methods
See table of operations link:#constops[above] for full list. There are no other
methods.
=======================================================================
TypeHandle (llvm.core)
@ -1062,10 +1083,11 @@ Pass Managers and Passes (llvm.passes)
TODO
[[examples]]
Annotated Examples
------------------
TODO
include::example.inc[]
About the llvm-py Project

View file

@ -42,7 +42,7 @@ llvm-dev mailing list and irc.oftc.net#llvm (mdevan).</p>
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>

View file

@ -124,7 +124,7 @@ Improve tests.
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>

View file

@ -149,7 +149,7 @@ package.</p>
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>

View file

@ -31,9 +31,20 @@
<div id="header">
<h1>Examples</h1>
</div>
<div id="preamble">
<div class="sectionbody">
<p>Here's an example:</p>
<h3>A Simple Function</h3>
<p>Let's create a module containing a single function, corresponding to the
<tt>C</tt> function:</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #009900">int</span> <span style="color: #000000">sum</span><span style="color: #990000">(</span><span style="color: #009900">int</span> a<span style="color: #990000">,</span> <span style="color: #009900">int</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">return</span></span> a <span style="color: #990000">+</span> b<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>
</tt></pre></div></div>
<p>Here's how it looks like:</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
@ -41,66 +52,115 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Import the llvm-py modules.</span></span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>core <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-style: italic"><span style="color: #9A1900">## create a module</span></span>
module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"my_module"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Create a module.</span></span>
my<span style="color: #009900">_</span>module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900">## create a function type taking two doubles and returning a (32-bit) integer</span></span>
ty<span style="color: #009900">_</span>double <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">double</span></span><span style="color: #990000">()</span>
ty<span style="color: #009900">_</span>int <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span>
ty<span style="color: #009900">_</span>func <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span> ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #990000">[</span> ty<span style="color: #009900">_</span>double<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>double <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># All the types involved here are "int"s. This type is represented</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># by an object of the llvm.core.Type class:</span></span>
ty<span style="color: #009900">_</span>int <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="font-style: italic"><span style="color: #9A1900"># by default 32 bits</span></span>
<span style="font-style: italic"><span style="color: #9A1900">## create a function of this type</span></span>
func <span style="color: #990000">=</span> <span style="color: #009900">Function</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">(</span> module<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>func<span style="color: #990000">,</span> <span style="color: #FF0000">"foobar"</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># We need to represent the class of functions that accept two integers</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># and return an integer. This is represented by an object of the</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># function type (llvm.core.FunctionType):</span></span>
ty<span style="color: #009900">_</span>func <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #990000">[</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>int<span style="color: #990000">])</span>
<span style="font-style: italic"><span style="color: #9A1900"># name function args</span></span>
func<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"arg1"</span>
func<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"arg2"</span>
<span style="font-style: italic"><span style="color: #9A1900"># Now we need a function named 'sum' of this type. Functions are not</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># free-standing (in llvm-py); it needs to be contained in a module.</span></span>
f<span style="color: #009900">_</span>sum <span style="color: #990000">=</span> my<span style="color: #009900">_</span>module<span style="color: #990000">.</span><span style="color: #000000">add_function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>func<span style="color: #990000">,</span> <span style="color: #FF0000">"sum"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900">## implement the function</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Let's name the function arguments as 'a' and 'b'.</span></span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"a"</span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"b"</span>
<span style="font-style: italic"><span style="color: #9A1900"># add a basic block</span></span>
entry <span style="color: #990000">=</span> func<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">append_basic_block</span></span><span style="color: #990000">(</span><span style="color: #FF0000">"entry"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Our function needs a "basic block" -- a set of instructions that</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># end with a terminator (like return, branch etc.). By convention</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># the first block is called "entry".</span></span>
bb <span style="color: #990000">=</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span><span style="color: #000000">append_basic_block</span><span style="color: #990000">(</span><span style="color: #FF0000">"entry"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># create an llvm::IRBuilder</span></span>
builder <span style="color: #990000">=</span> <span style="color: #009900">Builder</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">(</span>entry<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Let's add instructions into the block. For this, we need an</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># instruction builder:</span></span>
builder <span style="color: #990000">=</span> <span style="color: #009900">Builder</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>bb<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># add two args into tmp1</span></span>
tmp1 <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span>func<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> func<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #FF0000">"tmp1"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># OK, now for the instructions themselves. We'll create an add</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># instruction that returns the sum as a value, which we'll use</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># a ret instruction to return.</span></span>
tmp <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="color: #000000">add</span><span style="color: #990000">(</span>f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #FF0000">"tmp"</span><span style="color: #990000">)</span>
builder<span style="color: #990000">.</span><span style="color: #000000">ret</span><span style="color: #990000">(</span>tmp<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># sub `1' from that</span></span>
one <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">real</span></span><span style="color: #990000">(</span> ty<span style="color: #009900">_</span>double<span style="color: #990000">,</span> <span style="color: #993399">1.0</span> <span style="color: #990000">)</span>
tmp2 <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">sub</span></span><span style="color: #990000">(</span>tmp1<span style="color: #990000">,</span> one<span style="color: #990000">,</span> <span style="color: #FF0000">"tmp2"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># convert to integer</span></span>
tmp3 <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">fptoui</span></span><span style="color: #990000">(</span>tmp2<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #FF0000">"tmp3"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># return it</span></span>
builder<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">ret</span></span><span style="color: #990000">(</span>tmp3<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># dump the module to see the llvm "assembly" code</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">print</span></span> module
<span style="font-style: italic"><span style="color: #9A1900"># We've completed the definition now! Let's see the LLVM assembly</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># language representation of what we've created:</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">print</span></span> my<span style="color: #009900">_</span>module
</tt></pre></div></div>
<p>which gives this output:</p>
<p>Here is the output:</p>
<div class="listingblock">
<div class="content">
<pre><tt>; ModuleID = 'my_module'
define i32 @foobar(double %arg1, double %arg2) {
define i32 @sum(i32 %a, i32 %b) {
entry:
%temp1 = add double %arg1, %arg2 ; &lt;double&gt; [#uses=1]
%temp2 = sub double %temp1, 1.000000e+00 ; &lt;double&gt; [#uses=1]
%temp3 = fptoui double %temp2 to i32 ; &lt;i32&gt; [#uses=1]
ret i32 %temp3
%tmp = add i32 %a, %b ; &lt;i32&gt; [#uses=1]
ret i32 %tmp
}</tt></pre>
</div></div>
</div>
</div>
<h3>Adding JIT Compilation</h3>
<p>Let's compile this function in-memory and run it.</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Import the llvm-py modules.</span></span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>core <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>ee <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span> <span style="font-style: italic"><span style="color: #9A1900"># new import: ee = Execution Engine</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Create a module, as in the previous example.</span></span>
my<span style="color: #009900">_</span>module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
ty<span style="color: #009900">_</span>int <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="font-style: italic"><span style="color: #9A1900"># by default 32 bits</span></span>
ty<span style="color: #009900">_</span>func <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #990000">[</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>int<span style="color: #990000">])</span>
f<span style="color: #009900">_</span>sum <span style="color: #990000">=</span> my<span style="color: #009900">_</span>module<span style="color: #990000">.</span><span style="color: #000000">add_function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>func<span style="color: #990000">,</span> <span style="color: #FF0000">"sum"</span><span style="color: #990000">)</span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"a"</span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"b"</span>
bb <span style="color: #990000">=</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span><span style="color: #000000">append_basic_block</span><span style="color: #990000">(</span><span style="color: #FF0000">"entry"</span><span style="color: #990000">)</span>
builder <span style="color: #990000">=</span> <span style="color: #009900">Builder</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>bb<span style="color: #990000">)</span>
tmp <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="color: #000000">add</span><span style="color: #990000">(</span>f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #FF0000">"tmp"</span><span style="color: #990000">)</span>
builder<span style="color: #990000">.</span><span style="color: #000000">ret</span><span style="color: #990000">(</span>tmp<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Create a module provider object first. Modules can come from</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># in-memory IRs like what we created now, or from bitcode (.bc)</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># files. The module provider abstracts this detail.</span></span>
mp <span style="color: #990000">=</span> <span style="color: #009900">ModuleProvider</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>my<span style="color: #009900">_</span>module<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Create an execution engine object. This will create a JIT compiler</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># on platforms that support it, or an interpreter otherwise.</span></span>
ee <span style="color: #990000">=</span> <span style="color: #009900">ExecutionEngine</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>mp<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># The arguments needs to be passed as "GenericValue" objects.</span></span>
arg1 <span style="color: #990000">=</span> <span style="color: #009900">GenericValue</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #993399">100</span><span style="color: #990000">)</span>
arg2 <span style="color: #990000">=</span> <span style="color: #009900">GenericValue</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #993399">42</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Now let's compile and run!</span></span>
retval <span style="color: #990000">=</span> ee<span style="color: #990000">.</span><span style="color: #000000">run_function</span><span style="color: #990000">(</span>f<span style="color: #009900">_</span>sum<span style="color: #990000">,</span> <span style="color: #990000">[</span>arg1<span style="color: #990000">,</span> arg2<span style="color: #990000">])</span>
<span style="font-style: italic"><span style="color: #9A1900"># The return value is also GenericValue. Let's print it.</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">print</span></span> <span style="color: #FF0000">"returned"</span><span style="color: #990000">,</span> retval<span style="color: #990000">.</span><span style="color: #000000">as_int</span><span style="color: #990000">()</span>
</tt></pre></div></div>
<p>And here's the output:</p>
<div class="listingblock">
<div class="content">
<pre><tt>returned 142</tt></pre>
</div></div>
<p>That was easy, right?!</p>
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>

View file

@ -76,7 +76,7 @@ miss any specific LLVM API.</p>
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>

View file

@ -74,7 +74,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</tt></pre>
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>

View file

@ -33,14 +33,14 @@ window.onload = function(){generateToc(2)}
<div>&#187;<a href="about.html">About</a></div>
</td>
<td>
<div id="toc" style="float: right">
<div id="toctitle">Table of Contents</div>
<noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
<div id="layout-content">
<div id="header">
<h1>llvm-py User Guide</h1>
</div>
<div id="toc">
<div id="toctitle">Table of Contents</div>
<noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
</div>
<div id="preamble">
<div class="sectionbody">
<div class="admonitionblock">
@ -48,8 +48,11 @@ window.onload = function(){generateToc(2)}
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">This document is updated frequently (last updated on 24-Jun-2008).
Check back often.</td>
<td class="content">
<p>This document is updated frequently (last updated on 25-Jun-2008).
Check back often.</p>
<p>You might wish to look over the <a href="#examples">examples</a> first.</p>
</td>
</tr></table>
</div>
<p>llvm-py provides Python bindings for LLVM. This document explains how
@ -110,7 +113,7 @@ LLVM, either installed or built
</li>
</ul>
<p>On debian-based systems, the first three can be installed with the
command `sudo apt-get install gcc g++ python python-dev'. Note that
command <tt>sudo apt-get install gcc g++ python python-dev</tt>. Note that
ubuntu repository has an old version of llvm (1.8) which will not work
with llvm-py.</p>
<p>It does not matter which compiler LLVM itself was built with (g++,
@ -299,12 +302,12 @@ by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">/* compute sum of 1..n */</span></span>
<span style="color: #009900">unsigned</span> <span style="font-weight: bold"><span style="color: #000000">sum</span></span><span style="color: #990000">(</span><span style="color: #009900">unsigned</span> n<span style="color: #990000">)</span>
<span style="color: #009900">unsigned</span> <span style="color: #000000">sum</span><span style="color: #990000">(</span><span style="color: #009900">unsigned</span> n<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">(</span>n <span style="color: #990000">==</span> <span style="color: #993399">0</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">return</span></span> <span style="color: #993399">0</span><span style="color: #990000">;</span>
<span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">return</span></span> n <span style="color: #990000">+</span> <span style="font-weight: bold"><span style="color: #000000">sum</span></span><span style="color: #990000">(</span>n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
<span style="font-weight: bold"><span style="color: #0000FF">return</span></span> n <span style="color: #990000">+</span> <span style="color: #000000">sum</span><span style="color: #990000">(</span>n<span style="color: #990000">-</span><span style="color: #993399">1</span><span style="color: #990000">);</span>
<span style="color: #FF0000">}</span>
</tt></pre></div></div>
<p>The corresponding LLVM assembly:</p>
@ -503,7 +506,7 @@ cellspacing="0" cellpadding="4">
<p>Intrinsics (instructions that start with <tt>llvm.</tt>) are not yet available
in llvm-py.</p>
<h3>Modules</h3>
<p>Modules, in the LLVM IR, are similar to a single <em>C</em> language source
<p>Modules, in the LLVM IR, are similar to a single <tt>C</tt> language source
file (.c file). A module contains:</p>
<ul>
<li>
@ -537,7 +540,7 @@ describes all the available passes, and what they do.</p>
have to be explicitly selected and run on each module. This gives you
the flexibility to choose transformations and optimizations that are
most suitable for the code in the module.</p>
<p>There is a LLVM binary called <a href="http://www.llvm.org/cmds/opt.html">opt</a>,
<p>There is an LLVM binary called <a href="http://www.llvm.org/cmds/opt.html">opt</a>,
which lets you run passes on bitcode files from the command line. You
can write your own passes (in C/C++, as a shared library). This can be
loaded and executed by <tt>opt</tt>. (Although llvm-py does not allow you to
@ -557,9 +560,9 @@ any stage, and perform any transforms on it as you like.)</p>
over enough LLVM APIs to allow the implementation of your own
compiler/VM backend in pure Python. If you're come this far, you
probably know why this is a good idea.</p>
<p>Out of the 6 modules, one is an "extension" module (i.e., it's written
in C), and another one is a private utility module, which leaves 4
public modules. These are:</p>
<p>Out of the 6 modules, one is an &#8220;extension&#8221; module (i.e., it is
written in C), and another one is a small private utility module, which
leaves 4 public modules. These are:</p>
<ul>
<li>
<p>
@ -587,7 +590,7 @@ Python constructs are used (deliberately) &#8212;
<a href="http://docs.python.org/lib/built-in-funcs.html">property()</a> and
<a href="http://wiki.python.org/moin/PythonDecoratorLibrary">property
decorators</a> are probably the most exotic animals around. All classes are
the "new style" classes. The APIs are designed to be navigable (and
"new style" classes. The APIs are designed to be navigable (and
guessable!) once you know a few conventions. These conventions are
highlighted in the sections below.</p>
<p>Here is a quick overview of the contents of each package:</p>
@ -717,8 +720,8 @@ constants <tt>PASS_*</tt> that represent various passes
</ul>
<div class="title">A note on the 'import'ing of these modules</div>
<p>Pythonically, modules are imported with the statement <tt>"import
llvm.core"</tt> and not <tt>"from llvm.core import *"</tt>. However, you might find
it more convenient to import llvm-py modules thus:</p>
llvm.core"</tt>. However, you might find it more convenient to import
llvm-py modules thus:</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
@ -735,7 +738,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
<td class="content">Python-style documentation strings (<tt><em>doc</em></tt>) are present in
<td class="content">Python-style documentation strings (<tt>__doc__</tt>) are present in
llvm-py. You can use the <tt>help()</tt> of the interactive Python
interpreter or the <tt>object?</tt> of <a href="http://ipython.scipy.org/moin/">IPython</a>
to get online help. (Note: not complete yet!)</td>
@ -756,9 +759,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>core <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-style: italic"><span style="color: #9A1900"># create a module</span></span>
my<span style="color: #009900">_</span>module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
my<span style="color: #009900">_</span>module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
</tt></pre></div></div>
<p>The constructor of the Module class should <strong>not</strong> be used to instantiate
<p>The constructor of the Module class should <em>not</em> be used to instantiate
a Module object. This is a common feature for all llvm-py classes.</p>
<div class="admonitionblock">
<table><tr>
@ -1290,8 +1293,8 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">().</span>kind <span style="color: #990000">==</span> <span style="color: #009900">TYPE_INTEGER</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">void</span></span><span style="color: #990000">().</span>kind <span style="color: #990000">==</span> <span style="color: #009900">TYPE_VOID</span>
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">().</span>kind <span style="color: #990000">==</span> <span style="color: #009900">TYPE_INTEGER</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">void</span><span style="color: #990000">().</span>kind <span style="color: #990000">==</span> <span style="color: #009900">TYPE_VOID</span>
</tt></pre></div></div>
</dd>
</dl>
@ -1390,11 +1393,11 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>func<span style="color: #009900">_</span>type <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(),</span> <span style="color: #990000">[</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(),</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span> <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<pre><tt>func<span style="color: #009900">_</span>type <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(),</span> <span style="color: #990000">[</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(),</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">for</span></span> arg <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> func<span style="color: #009900">_</span>type<span style="color: #990000">.</span>args<span style="color: #990000">:</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> arg<span style="color: #990000">.</span>kind <span style="color: #990000">==</span> <span style="color: #009900">TYPE_INTEGER</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> arg <span style="color: #990000">==</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> func<span style="color: #009900">_</span>type<span style="color: #990000">.</span>arg<span style="color: #009900">_</span>count <span style="color: #990000">==</span> <span style="font-weight: bold"><span style="color: #000000">len</span></span><span style="color: #990000">(</span>func<span style="color: #009900">_</span>type<span style="color: #990000">.</span>args<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> arg <span style="color: #990000">==</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> func<span style="color: #009900">_</span>type<span style="color: #990000">.</span>arg<span style="color: #009900">_</span>count <span style="color: #990000">==</span> <span style="color: #000000">len</span><span style="color: #990000">(</span>func<span style="color: #009900">_</span>type<span style="color: #990000">.</span>args<span style="color: #990000">)</span>
</tt></pre></div></div>
</dd>
<dt>
@ -1440,11 +1443,11 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>struct<span style="color: #009900">_</span>type <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">struct</span></span><span style="color: #990000">(</span> <span style="color: #990000">[</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(),</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span> <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<pre><tt>struct<span style="color: #009900">_</span>type <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">struct</span><span style="color: #990000">(</span> <span style="color: #990000">[</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(),</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">for</span></span> elem <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> struct<span style="color: #009900">_</span>type<span style="color: #990000">.</span>elements<span style="color: #990000">:</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> elem<span style="color: #990000">.</span>kind <span style="color: #990000">==</span> <span style="color: #009900">TYPE_INTEGER</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> elem <span style="color: #990000">==</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> struct<span style="color: #009900">_</span>type<span style="color: #990000">.</span>element<span style="color: #009900">_</span>count <span style="color: #990000">==</span> <span style="font-weight: bold"><span style="color: #000000">len</span></span><span style="color: #990000">(</span>struct<span style="color: #009900">_</span>type<span style="color: #990000">.</span>elements<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> elem <span style="color: #990000">==</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span>
<span style="font-weight: bold"><span style="color: #0000FF">assert</span></span> struct<span style="color: #009900">_</span>type<span style="color: #990000">.</span>element<span style="color: #009900">_</span>count <span style="color: #990000">==</span> <span style="color: #000000">len</span><span style="color: #990000">(</span>struct<span style="color: #009900">_</span>type<span style="color: #990000">.</span>elements<span style="color: #990000">)</span>
</tt></pre></div></div>
</dd>
<dt>
@ -1553,35 +1556,35 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># integers</span></span>
int<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span>
bool<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
int<span style="color: #009900">_</span>64bit <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span><span style="color: #993399">64</span><span style="color: #990000">)</span>
int<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span>
bool<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
int<span style="color: #009900">_</span>64bit <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span><span style="color: #993399">64</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># floats</span></span>
sprec<span style="color: #009900">_</span>real <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">float</span></span><span style="color: #990000">()</span>
dprec<span style="color: #009900">_</span>real <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">double</span></span><span style="color: #990000">()</span>
sprec<span style="color: #009900">_</span>real <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">float</span><span style="color: #990000">()</span>
dprec<span style="color: #009900">_</span>real <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">double</span><span style="color: #990000">()</span>
<span style="font-style: italic"><span style="color: #9A1900"># arrays and vectors</span></span>
intar<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">array</span></span><span style="color: #990000">(</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "typedef int intar_ty[10];"</span></span>
twodim <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">array</span></span><span style="color: #990000">(</span> intar<span style="color: #009900">_</span>ty <span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "typedef int twodim[10][10];"</span></span>
vec <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">array</span></span><span style="color: #990000">(</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span>
intar<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">array</span><span style="color: #990000">(</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "typedef int intar_ty[10];"</span></span>
twodim <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">array</span><span style="color: #990000">(</span> intar<span style="color: #009900">_</span>ty <span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "typedef int twodim[10][10];"</span></span>
vec <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">array</span><span style="color: #990000">(</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># structures</span></span>
s1<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">struct</span></span><span style="color: #990000">(</span> <span style="color: #990000">[</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> sprec<span style="color: #009900">_</span>real <span style="color: #990000">]</span> <span style="color: #990000">)</span>
s1<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">struct</span><span style="color: #990000">(</span> <span style="color: #990000">[</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> sprec<span style="color: #009900">_</span>real <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># "struct s1_ty { int v1; float v2; };"</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># pointers</span></span>
intptr<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pointer</span></span><span style="color: #990000">(</span>int<span style="color: #009900">_</span>ty<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "typedef int *intptr_ty;"</span></span>
intptr<span style="color: #009900">_</span>ty <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">pointer</span><span style="color: #990000">(</span>int<span style="color: #009900">_</span>ty<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "typedef int *intptr_ty;"</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># functions</span></span>
f1 <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> <span style="color: #990000">[</span> int<span style="color: #009900">_</span>ty <span style="color: #990000">]</span> <span style="color: #990000">)</span>
f1 <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span> int<span style="color: #009900">_</span>ty<span style="color: #990000">,</span> <span style="color: #990000">[</span> int<span style="color: #009900">_</span>ty <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># functions that take 1 int_ty and return 1 int_ty</span></span>
f2 <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">void</span></span><span style="color: #990000">(),</span> <span style="color: #990000">[</span> int<span style="color: #009900">_</span>ty <span style="color: #990000">]</span> <span style="color: #990000">)</span>
f2 <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">void</span><span style="color: #990000">(),</span> <span style="color: #990000">[</span> int<span style="color: #009900">_</span>ty <span style="color: #990000">]</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># functions that take 1 int_ty and return nothing</span></span>
fnargs <span style="color: #990000">=</span> <span style="color: #990000">[</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">pointer</span></span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span><span style="color: #993399">8</span><span style="color: #990000">)</span> <span style="color: #990000">)</span> <span style="color: #990000">]</span>
printf <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(),</span> fnargs<span style="color: #990000">,</span> <span style="color: #009900">True</span> <span style="color: #990000">)</span>
fnargs <span style="color: #990000">=</span> <span style="color: #990000">[</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">pointer</span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span><span style="color: #993399">8</span><span style="color: #990000">)</span> <span style="color: #990000">)</span> <span style="color: #990000">]</span>
printf <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(),</span> fnargs<span style="color: #990000">,</span> <span style="color: #009900">True</span> <span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># variadic function</span></span>
</tt></pre></div></div>
<h3>Values (llvm.core)</h3>
@ -1671,19 +1674,20 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
ti <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">()</span> <span style="font-style: italic"><span style="color: #9A1900"># a 32-bit int type</span></span>
ti <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="font-style: italic"><span style="color: #9A1900"># a 32-bit int type</span></span>
k1 <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span>ti<span style="color: #990000">,</span> <span style="color: #993399">42</span><span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "int k1 = 42;"</span></span>
k2 <span style="color: #990000">=</span> k1<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span> ti<span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span> <span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "int k2 = k1 + 10;"</span></span>
k1 <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span>ti<span style="color: #990000">,</span> <span style="color: #993399">42</span><span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "int k1 = 42;"</span></span>
k2 <span style="color: #990000">=</span> k1<span style="color: #990000">.</span><span style="color: #000000">add</span><span style="color: #990000">(</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span> ti<span style="color: #990000">,</span> <span style="color: #993399">10</span> <span style="color: #990000">)</span> <span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># "int k2 = k1 + 10;"</span></span>
tr <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">float</span></span><span style="color: #990000">()</span>
tr <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">float</span><span style="color: #990000">()</span>
r1 <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">real</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> <span style="color: #FF0000">"3.141592"</span><span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># create from a string</span></span>
r2 <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">real</span></span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> <span style="color: #993399">1.61803399</span><span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># create from a Python float</span></span>
r1 <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="color: #000000">real</span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> <span style="color: #FF0000">"3.141592"</span><span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># create from a string</span></span>
r2 <span style="color: #990000">=</span> <span style="color: #009900">Constant</span><span style="color: #990000">.</span><span style="color: #000000">real</span><span style="color: #990000">(</span>tr<span style="color: #990000">,</span> <span style="color: #993399">1.61803399</span><span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># create from a Python float</span></span>
</tt></pre></div></div>
<p>The following constructors (static methods) can be used to create
constants:</p>
<div class="tableblock">
<a id="constctors"></a>
<table rules="all"
frame="border"
cellspacing="0" cellpadding="4">
@ -1809,6 +1813,7 @@ cellspacing="0" cellpadding="4">
</div>
<p>The following operations are available:</p>
<div class="tableblock">
<a id="constops"></a>
<table rules="all"
frame="border"
cellspacing="0" cellpadding="4">
@ -1955,7 +1960,31 @@ cellspacing="0" cellpadding="4">
</tr>
<tr>
<td align="left">
shl
<tt>k.shl(k2)</tt>
</td>
<td align="left">
Shift <tt>k</tt> left by <tt>k2</tt> bits.
</td>
</tr>
<tr>
<td align="left">
<tt>k.lshr(k2)</tt>
</td>
<td align="left">
Shift <tt>k</tt> logically right by <tt>k2</tt> bits (new bits are 0s).
</td>
</tr>
<tr>
<td align="left">
<tt>k.ashr(k2)</tt>
</td>
<td align="left">
Shift <tt>k</tt> arithmetically right by <tt>k2</tt> bits (new bits are same as previous sign bit).
</td>
</tr>
<tr>
<td align="left">
<tt>k.gep(indices)</tt>
</td>
<td align="left">
TODO
@ -1963,50 +1992,26 @@ cellspacing="0" cellpadding="4">
</tr>
<tr>
<td align="left">
lshr
<tt>k.trunc(ty)</tt>
</td>
<td align="left">
TODO
Truncate <tt>k</tt> to a type <tt>ty</tt> of lower bitwidth.
</td>
</tr>
<tr>
<td align="left">
ashr
<tt>k.sext(ty)</tt>
</td>
<td align="left">
TODO
Sign extend <tt>k</tt> to a type <tt>ty</tt> of higher bitwidth, while extending the sign bit.
</td>
</tr>
<tr>
<td align="left">
gep
<tt>k.zext(ty)</tt>
</td>
<td align="left">
TODO
</td>
</tr>
<tr>
<td align="left">
trunc
</td>
<td align="left">
TODO
</td>
</tr>
<tr>
<td align="left">
sext
</td>
<td align="left">
TODO
</td>
</tr>
<tr>
<td align="left">
zext
</td>
<td align="left">
TODO
Sign extend <tt>k</tt> to a type <tt>ty</tt> of higher bitwidth, all new bits are 0s.
</td>
</tr>
<tr>
@ -2242,7 +2247,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_FALSE</tt>
</td>
<td align="left">
Always false
</td>
</tr>
<tr>
@ -2250,7 +2255,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_OEQ</tt>
</td>
<td align="left">
True if ordered and equal
</td>
</tr>
<tr>
@ -2258,7 +2263,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_OGT</tt>
</td>
<td align="left">
True if ordered and greater than
</td>
</tr>
<tr>
@ -2266,7 +2271,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_OGE</tt>
</td>
<td align="left">
True if ordered and greater than or equal
</td>
</tr>
<tr>
@ -2274,7 +2279,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_OLT</tt>
</td>
<td align="left">
True if ordered and less than
</td>
</tr>
<tr>
@ -2282,7 +2287,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_OLE</tt>
</td>
<td align="left">
True if ordered and less than or equal
</td>
</tr>
<tr>
@ -2290,7 +2295,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_ONE</tt>
</td>
<td align="left">
True if ordered and operands are unequal
</td>
</tr>
<tr>
@ -2298,7 +2303,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_ORD</tt>
</td>
<td align="left">
True if ordered (no NaNs)
</td>
</tr>
<tr>
@ -2306,7 +2311,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_UNO</tt>
</td>
<td align="left">
True if unordered: <tt>isnan(X) | isnan(Y)</tt>
</td>
</tr>
<tr>
@ -2314,7 +2319,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_UEQ</tt>
</td>
<td align="left">
True if unordered or equal
</td>
</tr>
<tr>
@ -2322,7 +2327,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_UGT</tt>
</td>
<td align="left">
True if unordered or greater than
</td>
</tr>
<tr>
@ -2330,7 +2335,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_UGE</tt>
</td>
<td align="left">
True if unordered, greater than or equal
</td>
</tr>
<tr>
@ -2338,7 +2343,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_ULT</tt>
</td>
<td align="left">
True if unordered, or less than
</td>
</tr>
<tr>
@ -2346,7 +2351,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_ULE</tt>
</td>
<td align="left">
True if unordered, less than or equal
</td>
</tr>
<tr>
@ -2354,7 +2359,7 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_UNE</tt>
</td>
<td align="left">
True if unordered or not equal
</td>
</tr>
<tr>
@ -2362,12 +2367,28 @@ cellspacing="0" cellpadding="4">
<tt>RPRED_TRUE </tt>
</td>
<td align="left">
Always true
</td>
</tr>
</tbody>
</table>
</div>
<div class="exampleblock">
<div class="title">llvm.core.Constant</div>
<div class="exampleblock-content">
<div class="title">Base Class</div><ul>
<li>
<p>
<tt>llvm.core.Value</tt>
</p>
</li>
</ul>
<div class="title">Static Constructors</div>
<p>See table of constructors <a href="#constctors">above</a> for full list.</p>
<div class="title">Methods</div>
<p>See table of operations <a href="#constops">above</a> for full list. There are no other
methods.</p>
</div></div>
<h3>TypeHandle (llvm.core)</h3>
<p>TODO</p>
<h3>Instructions (llvm.core)</h3>
@ -2385,9 +2406,134 @@ cellspacing="0" cellpadding="4">
<h3>Pass Managers and Passes (llvm.passes)</h3>
<p>TODO</p>
</div>
<h2>Annotated Examples</h2>
<h2><a id="examples"></a>Annotated Examples</h2>
<div class="sectionbody">
<p>TODO</p>
<h3>A Simple Function</h3>
<p>Let's create a module containing a single function, corresponding to the
<tt>C</tt> function:</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #009900">int</span> <span style="color: #000000">sum</span><span style="color: #990000">(</span><span style="color: #009900">int</span> a<span style="color: #990000">,</span> <span style="color: #009900">int</span> b<span style="color: #990000">)</span>
<span style="color: #FF0000">{</span>
<span style="font-weight: bold"><span style="color: #0000FF">return</span></span> a <span style="color: #990000">+</span> b<span style="color: #990000">;</span>
<span style="color: #FF0000">}</span>
</tt></pre></div></div>
<p>Here's how it looks like:</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Import the llvm-py modules.</span></span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>core <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-style: italic"><span style="color: #9A1900"># Create a module.</span></span>
my<span style="color: #009900">_</span>module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># All the types involved here are "int"s. This type is represented</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># by an object of the llvm.core.Type class:</span></span>
ty<span style="color: #009900">_</span>int <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="font-style: italic"><span style="color: #9A1900"># by default 32 bits</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># We need to represent the class of functions that accept two integers</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># and return an integer. This is represented by an object of the</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># function type (llvm.core.FunctionType):</span></span>
ty<span style="color: #009900">_</span>func <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #990000">[</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>int<span style="color: #990000">])</span>
<span style="font-style: italic"><span style="color: #9A1900"># Now we need a function named 'sum' of this type. Functions are not</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># free-standing (in llvm-py); it needs to be contained in a module.</span></span>
f<span style="color: #009900">_</span>sum <span style="color: #990000">=</span> my<span style="color: #009900">_</span>module<span style="color: #990000">.</span><span style="color: #000000">add_function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>func<span style="color: #990000">,</span> <span style="color: #FF0000">"sum"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Let's name the function arguments as 'a' and 'b'.</span></span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"a"</span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"b"</span>
<span style="font-style: italic"><span style="color: #9A1900"># Our function needs a "basic block" -- a set of instructions that</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># end with a terminator (like return, branch etc.). By convention</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># the first block is called "entry".</span></span>
bb <span style="color: #990000">=</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span><span style="color: #000000">append_basic_block</span><span style="color: #990000">(</span><span style="color: #FF0000">"entry"</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Let's add instructions into the block. For this, we need an</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># instruction builder:</span></span>
builder <span style="color: #990000">=</span> <span style="color: #009900">Builder</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>bb<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># OK, now for the instructions themselves. We'll create an add</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># instruction that returns the sum as a value, which we'll use</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># a ret instruction to return.</span></span>
tmp <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="color: #000000">add</span><span style="color: #990000">(</span>f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #FF0000">"tmp"</span><span style="color: #990000">)</span>
builder<span style="color: #990000">.</span><span style="color: #000000">ret</span><span style="color: #990000">(</span>tmp<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># We've completed the definition now! Let's see the LLVM assembly</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># language representation of what we've created:</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">print</span></span> my<span style="color: #009900">_</span>module
</tt></pre></div></div>
<p>Here is the output:</p>
<div class="listingblock">
<div class="content">
<pre><tt>; ModuleID = 'my_module'
define i32 @sum(i32 %a, i32 %b) {
entry:
%tmp = add i32 %a, %b ; &lt;i32&gt; [#uses=1]
ret i32 %tmp
}</tt></pre>
</div></div>
<h3>Adding JIT Compilation</h3>
<p>Let's compile this function in-memory and run it.</p>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.4
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Import the llvm-py modules.</span></span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>core <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span>
<span style="font-weight: bold"><span style="color: #000080">from</span></span> llvm<span style="color: #990000">.</span>ee <span style="font-weight: bold"><span style="color: #000080">import</span></span> <span style="color: #990000">*</span> <span style="font-style: italic"><span style="color: #9A1900"># new import: ee = Execution Engine</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Create a module, as in the previous example.</span></span>
my<span style="color: #009900">_</span>module <span style="color: #990000">=</span> <span style="color: #009900">Module</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
ty<span style="color: #009900">_</span>int <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">()</span> <span style="font-style: italic"><span style="color: #9A1900"># by default 32 bits</span></span>
ty<span style="color: #009900">_</span>func <span style="color: #990000">=</span> <span style="color: #009900">Type</span><span style="color: #990000">.</span><span style="color: #000000">function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #990000">[</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> ty<span style="color: #009900">_</span>int<span style="color: #990000">])</span>
f<span style="color: #009900">_</span>sum <span style="color: #990000">=</span> my<span style="color: #009900">_</span>module<span style="color: #990000">.</span><span style="color: #000000">add_function</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>func<span style="color: #990000">,</span> <span style="color: #FF0000">"sum"</span><span style="color: #990000">)</span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"a"</span>
f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">].</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">"b"</span>
bb <span style="color: #990000">=</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span><span style="color: #000000">append_basic_block</span><span style="color: #990000">(</span><span style="color: #FF0000">"entry"</span><span style="color: #990000">)</span>
builder <span style="color: #990000">=</span> <span style="color: #009900">Builder</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>bb<span style="color: #990000">)</span>
tmp <span style="color: #990000">=</span> builder<span style="color: #990000">.</span><span style="color: #000000">add</span><span style="color: #990000">(</span>f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> f<span style="color: #009900">_</span>sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">],</span> <span style="color: #FF0000">"tmp"</span><span style="color: #990000">)</span>
builder<span style="color: #990000">.</span><span style="color: #000000">ret</span><span style="color: #990000">(</span>tmp<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Create a module provider object first. Modules can come from</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># in-memory IRs like what we created now, or from bitcode (.bc)</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># files. The module provider abstracts this detail.</span></span>
mp <span style="color: #990000">=</span> <span style="color: #009900">ModuleProvider</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>my<span style="color: #009900">_</span>module<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Create an execution engine object. This will create a JIT compiler</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># on platforms that support it, or an interpreter otherwise.</span></span>
ee <span style="color: #990000">=</span> <span style="color: #009900">ExecutionEngine</span><span style="color: #990000">.</span><span style="color: #000000">new</span><span style="color: #990000">(</span>mp<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># The arguments needs to be passed as "GenericValue" objects.</span></span>
arg1 <span style="color: #990000">=</span> <span style="color: #009900">GenericValue</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #993399">100</span><span style="color: #990000">)</span>
arg2 <span style="color: #990000">=</span> <span style="color: #009900">GenericValue</span><span style="color: #990000">.</span><span style="color: #000000">int</span><span style="color: #990000">(</span>ty<span style="color: #009900">_</span>int<span style="color: #990000">,</span> <span style="color: #993399">42</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># Now let's compile and run!</span></span>
retval <span style="color: #990000">=</span> ee<span style="color: #990000">.</span><span style="color: #000000">run_function</span><span style="color: #990000">(</span>f<span style="color: #009900">_</span>sum<span style="color: #990000">,</span> <span style="color: #990000">[</span>arg1<span style="color: #990000">,</span> arg2<span style="color: #990000">])</span>
<span style="font-style: italic"><span style="color: #9A1900"># The return value is also GenericValue. Let's print it.</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">print</span></span> <span style="color: #FF0000">"returned"</span><span style="color: #990000">,</span> retval<span style="color: #990000">.</span><span style="color: #000000">as_int</span><span style="color: #990000">()</span>
</tt></pre></div></div>
<p>And here's the output:</p>
<div class="listingblock">
<div class="content">
<pre><tt>returned 142</tt></pre>
</div></div>
<p>That was easy, right?!</p>
</div>
<h2>About the llvm-py Project</h2>
<div class="sectionbody">
@ -2413,7 +2559,7 @@ reached at <em>mdevan.foobar@gmail.com</em>.</p>
<div id="footer">
<div id="footer-text">
Web pages &copy; Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
Last updated 24-Jun-2008.
Last updated 25-Jun-2008.
</div>
</div>
</div>