Updated documentation.
git-svn-id: http://llvm-py.googlecode.com/svn/trunk@22 8d1e9007-1d4e-0410-b67e-1979fd6579aa
This commit is contained in:
parent
23027df611
commit
fe03572bba
12 changed files with 548 additions and 208 deletions
|
|
@ -6,7 +6,7 @@ from string import Template
|
||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
|
|
||||||
# files in src dir that should not be copied to web dir
|
# 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 command line
|
||||||
ASCIIDOC = 'asciidoc --unsafe --conf-file=${srcdir}/layout.conf -a icons -o ${outfile} ${infile}'
|
ASCIIDOC = 'asciidoc --unsafe --conf-file=${srcdir}/layout.conf -a icons -o ${outfile} ${infile}'
|
||||||
|
|
|
||||||
132
www/src/example.inc
Normal file
132
www/src/example.inc
Normal 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?!
|
||||||
|
|
||||||
|
|
@ -1,25 +1,5 @@
|
||||||
Examples
|
Examples
|
||||||
========
|
========
|
||||||
|
|
||||||
Here's an example:
|
include::example.inc[]
|
||||||
|
|
||||||
[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
|
|
||||||
}
|
|
||||||
-----------------------------------------------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,16 +68,16 @@ endif::toc[]
|
||||||
<div>»<a href="about.html">About</a></div>
|
<div>»<a href="about.html">About</a></div>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<div id="layout-content">
|
|
||||||
<div id="header">
|
|
||||||
<h1>{doctitle}</h1>
|
|
||||||
</div>
|
|
||||||
ifdef::toc[]
|
ifdef::toc[]
|
||||||
<div id="toc">
|
<div id="toc" style="float: right">
|
||||||
<div id="toctitle">Table of Contents</div>
|
<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>
|
<noscript><p><b>JavaScript must be enabled in your browser to display the table of contents.</b></p></noscript>
|
||||||
</div>
|
</div>
|
||||||
endif::toc[]
|
endif::toc[]
|
||||||
|
<div id="layout-content">
|
||||||
|
<div id="header">
|
||||||
|
<h1>{doctitle}</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
[footer]
|
[footer]
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,15 @@
|
||||||
llvm-py User Guide
|
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.
|
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
|
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
|
you can setup and use it. A working knowledge of Python and a basic idea
|
||||||
of LLVM is assumed.
|
of LLVM is assumed.
|
||||||
|
|
@ -55,7 +61,7 @@ required for this:
|
||||||
- LLVM, either installed or built
|
- LLVM, either installed or built
|
||||||
|
|
||||||
On debian-based systems, the first three can be installed with the
|
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
|
ubuntu repository has an old version of llvm (1.8) which will not work
|
||||||
with llvm-py.
|
with llvm-py.
|
||||||
|
|
||||||
|
|
@ -296,7 +302,7 @@ in llvm-py.
|
||||||
Modules
|
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:
|
file (.c file). A module contains:
|
||||||
|
|
||||||
- functions (declarations and definitions)
|
- 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
|
the flexibility to choose transformations and optimizations that are
|
||||||
most suitable for the code in the module.
|
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
|
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
|
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
|
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
|
compiler/VM backend in pure Python. If you're come this far, you
|
||||||
probably know why this is a good idea.
|
probably know why this is a good idea.
|
||||||
|
|
||||||
Out of the 6 modules, one is an "extension" module (i.e., it's written
|
Out of the 6 modules, one is an ``extension'' module (i.e., it is
|
||||||
in C), and another one is a private utility module, which leaves 4
|
written in C), and another one is a small private utility module, which
|
||||||
public modules. These are:
|
leaves 4 public modules. These are:
|
||||||
|
|
||||||
- +llvm+ -- top-level package, common classes (like exceptions)
|
- +llvm+ -- top-level package, common classes (like exceptions)
|
||||||
- +llvm.core+ -- IR-related APIs
|
- +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://docs.python.org/lib/built-in-funcs.html[property()] and
|
||||||
http://wiki.python.org/moin/PythonDecoratorLibrary[property
|
http://wiki.python.org/moin/PythonDecoratorLibrary[property
|
||||||
decorators] are probably the most exotic animals around. All classes are
|
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
|
guessable!) once you know a few conventions. These conventions are
|
||||||
highlighted in the sections below.
|
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
|
.A note on the 'import'ing of these modules
|
||||||
Pythonically, modules are imported with the statement +"import
|
Pythonically, modules are imported with the statement +"import
|
||||||
llvm.core"+ and not +"from llvm.core import *"+. However, you might find
|
llvm.core"+. However, you might find it more convenient to import
|
||||||
it more convenient to import llvm-py modules thus:
|
llvm-py modules thus:
|
||||||
|
|
||||||
[python]
|
[python]
|
||||||
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
@ -436,7 +442,7 @@ source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
This avoids quite some typing. Both conventions work, however.
|
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
|
llvm-py. You can use the +help()+ of the interactive Python
|
||||||
interpreter or the +object?+ of http://ipython.scipy.org/moin/[IPython]
|
interpreter or the +object?+ of http://ipython.scipy.org/moin/[IPython]
|
||||||
to get online help. (Note: not complete yet!)
|
to get online help. (Note: not complete yet!)
|
||||||
|
|
@ -460,7 +466,7 @@ from llvm.core import *
|
||||||
my_module = Module.new('my_module')
|
my_module = Module.new('my_module')
|
||||||
source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
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.
|
a Module object. This is a common feature for all llvm-py classes.
|
||||||
|
|
||||||
[TIP]
|
[TIP]
|
||||||
|
|
@ -903,6 +909,7 @@ source~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
The following constructors (static methods) can be used to create
|
The following constructors (static methods) can be used to create
|
||||||
constants:
|
constants:
|
||||||
|
|
||||||
|
[[constctors]]
|
||||||
[frame="all",grid="all"]
|
[frame="all",grid="all"]
|
||||||
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
Constructor Method, What It Creates
|
Constructor Method, What It Creates
|
||||||
|
|
@ -924,6 +931,7 @@ Constructor Method, What It Creates
|
||||||
|
|
||||||
The following operations are available:
|
The following operations are available:
|
||||||
|
|
||||||
|
[[constops]]
|
||||||
[frame="all",grid="all"]
|
[frame="all",grid="all"]
|
||||||
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
Method, Operation
|
Method, Operation
|
||||||
|
|
@ -944,13 +952,13 @@ Method, Operation
|
||||||
`k.xor(k2)`, "Bitwise exclusive-or of `k` and `k2`."
|
`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.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."
|
"`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
|
`k.shl(k2)`, "Shift `k` left by `k2` bits."
|
||||||
lshr, TODO
|
`k.lshr(k2)`, "Shift `k` logically right by `k2` bits (new bits are 0s)."
|
||||||
ashr, TODO
|
`k.ashr(k2)`, "Shift `k` arithmetically right by `k2` bits (new bits are same as previous sign bit)."
|
||||||
gep, TODO
|
`k.gep(indices)`, "TODO"
|
||||||
trunc, TODO
|
`k.trunc(ty)`, "Truncate `k` to a type `ty` of lower bitwidth."
|
||||||
sext, TODO
|
`k.sext(ty)`, "Sign extend `k` to a type `ty` of higher bitwidth, while extending the sign bit."
|
||||||
zext, TODO
|
`k.zext(ty)`, "Sign extend `k` to a type `ty` of higher bitwidth, all new bits are 0s."
|
||||||
fptrunc, TODO
|
fptrunc, TODO
|
||||||
fpext, TODO
|
fpext, TODO
|
||||||
uitofp, TODO
|
uitofp, TODO
|
||||||
|
|
@ -994,24 +1002,37 @@ of these are integer constants defined in the `llvm.core` module.
|
||||||
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
`25`75~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
Value, Meaning
|
Value, Meaning
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
`RPRED_FALSE`,
|
`RPRED_FALSE`, Always false
|
||||||
`RPRED_OEQ`,
|
`RPRED_OEQ`, True if ordered and equal
|
||||||
`RPRED_OGT`,
|
`RPRED_OGT`, True if ordered and greater than
|
||||||
`RPRED_OGE`,
|
`RPRED_OGE`, True if ordered and greater than or equal
|
||||||
`RPRED_OLT`,
|
`RPRED_OLT`, True if ordered and less than
|
||||||
`RPRED_OLE`,
|
`RPRED_OLE`, True if ordered and less than or equal
|
||||||
`RPRED_ONE`,
|
`RPRED_ONE`, True if ordered and operands are unequal
|
||||||
`RPRED_ORD`,
|
`RPRED_ORD`, True if ordered (no NaNs)
|
||||||
`RPRED_UNO`,
|
`RPRED_UNO`, True if unordered: `isnan(X) | isnan(Y)`
|
||||||
`RPRED_UEQ`,
|
`RPRED_UEQ`, True if unordered or equal
|
||||||
`RPRED_UGT`,
|
`RPRED_UGT`, True if unordered or greater than
|
||||||
`RPRED_UGE`,
|
`RPRED_UGE`, "True if unordered, greater than or equal"
|
||||||
`RPRED_ULT`,
|
`RPRED_ULT`, "True if unordered, or less than"
|
||||||
`RPRED_ULE`,
|
`RPRED_ULE`, "True if unordered, less than or equal"
|
||||||
`RPRED_UNE`,
|
`RPRED_UNE`, True if unordered or not equal
|
||||||
`RPRED_TRUE `,
|
`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)
|
TypeHandle (llvm.core)
|
||||||
|
|
@ -1062,10 +1083,11 @@ Pass Managers and Passes (llvm.passes)
|
||||||
TODO
|
TODO
|
||||||
|
|
||||||
|
|
||||||
|
[[examples]]
|
||||||
Annotated Examples
|
Annotated Examples
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
TODO
|
include::example.inc[]
|
||||||
|
|
||||||
|
|
||||||
About the llvm-py Project
|
About the llvm-py Project
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@ llvm-dev mailing list and irc.oftc.net#llvm (mdevan).</p>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -124,7 +124,7 @@ Improve tests.
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -149,7 +149,7 @@ package.</p>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,20 @@
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<h1>Examples</h1>
|
<h1>Examples</h1>
|
||||||
</div>
|
</div>
|
||||||
<div id="preamble">
|
<h3>A Simple Function</h3>
|
||||||
<div class="sectionbody">
|
<p>Let's create a module containing a single function, corresponding to the
|
||||||
<p>Here's an example:</p>
|
<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="listingblock">
|
||||||
<div class="content"><!-- Generator: GNU source-highlight 2.4
|
<div class="content"><!-- Generator: GNU source-highlight 2.4
|
||||||
by Lorenzo Bettini
|
by Lorenzo Bettini
|
||||||
|
|
@ -41,66 +52,115 @@ http://www.lorenzobettini.it
|
||||||
http://www.gnu.org/software/src-highlite -->
|
http://www.gnu.org/software/src-highlite -->
|
||||||
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
|
<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>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>
|
<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>
|
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>
|
<span style="font-style: italic"><span style="color: #9A1900"># All the types involved here are "int"s. This type is represented</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>
|
<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="font-weight: bold"><span style="color: #000000">int</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="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="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">## create a function of this type</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>
|
||||||
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"># 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>
|
<span style="font-style: italic"><span style="color: #9A1900"># Now we need a function named 'sum' of this type. Functions are not</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>
|
<span style="font-style: italic"><span style="color: #9A1900"># free-standing (in llvm-py); it needs to be contained in a module.</span></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>
|
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>
|
<span style="font-style: italic"><span style="color: #9A1900"># Our function needs a "basic block" -- a set of instructions that</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"># 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>
|
<span style="font-style: italic"><span style="color: #9A1900"># Let's add instructions into the block. For this, we need an</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"># 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>
|
<span style="font-style: italic"><span style="color: #9A1900"># OK, now for the instructions themselves. We'll create an add</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"># 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>
|
<span style="font-style: italic"><span style="color: #9A1900"># We've completed the definition now! Let's see the LLVM assembly</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>
|
<span style="font-style: italic"><span style="color: #9A1900"># language representation of what we've created:</span></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-weight: bold"><span style="color: #0000FF">print</span></span> my<span style="color: #009900">_</span>module
|
||||||
|
|
||||||
<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
|
|
||||||
</tt></pre></div></div>
|
</tt></pre></div></div>
|
||||||
<p>which gives this output:</p>
|
<p>Here is the output:</p>
|
||||||
<div class="listingblock">
|
<div class="listingblock">
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<pre><tt>; ModuleID = 'my_module'
|
<pre><tt>; ModuleID = 'my_module'
|
||||||
|
|
||||||
define i32 @foobar(double %arg1, double %arg2) {
|
define i32 @sum(i32 %a, i32 %b) {
|
||||||
entry:
|
entry:
|
||||||
%temp1 = add double %arg1, %arg2 ; <double> [#uses=1]
|
%tmp = add i32 %a, %b ; <i32> [#uses=1]
|
||||||
%temp2 = sub double %temp1, 1.000000e+00 ; <double> [#uses=1]
|
ret i32 %tmp
|
||||||
%temp3 = fptoui double %temp2 to i32 ; <i32> [#uses=1]
|
|
||||||
ret i32 %temp3
|
|
||||||
}</tt></pre>
|
}</tt></pre>
|
||||||
</div></div>
|
</div></div>
|
||||||
</div>
|
<h3>Adding JIT Compilation</h3>
|
||||||
</div>
|
<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">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ miss any specific LLVM API.</p>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</tt></pre>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,14 @@ window.onload = function(){generateToc(2)}
|
||||||
<div>»<a href="about.html">About</a></div>
|
<div>»<a href="about.html">About</a></div>
|
||||||
</td>
|
</td>
|
||||||
<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="layout-content">
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<h1>llvm-py User Guide</h1>
|
<h1>llvm-py User Guide</h1>
|
||||||
</div>
|
</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 id="preamble">
|
||||||
<div class="sectionbody">
|
<div class="sectionbody">
|
||||||
<div class="admonitionblock">
|
<div class="admonitionblock">
|
||||||
|
|
@ -48,8 +48,11 @@ window.onload = function(){generateToc(2)}
|
||||||
<td class="icon">
|
<td class="icon">
|
||||||
<img src="./images/icons/note.png" alt="Note" />
|
<img src="./images/icons/note.png" alt="Note" />
|
||||||
</td>
|
</td>
|
||||||
<td class="content">This document is updated frequently (last updated on 24-Jun-2008).
|
<td class="content">
|
||||||
Check back often.</td>
|
<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>
|
</tr></table>
|
||||||
</div>
|
</div>
|
||||||
<p>llvm-py provides Python bindings for LLVM. This document explains how
|
<p>llvm-py provides Python bindings for LLVM. This document explains how
|
||||||
|
|
@ -110,7 +113,7 @@ LLVM, either installed or built
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
<p>On debian-based systems, the first three can be installed with the
|
<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
|
ubuntu repository has an old version of llvm (1.8) which will not work
|
||||||
with llvm-py.</p>
|
with llvm-py.</p>
|
||||||
<p>It does not matter which compiler LLVM itself was built with (g++,
|
<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.lorenzobettini.it
|
||||||
http://www.gnu.org/software/src-highlite -->
|
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>
|
<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="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">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">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">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>
|
<span style="color: #FF0000">}</span>
|
||||||
</tt></pre></div></div>
|
</tt></pre></div></div>
|
||||||
<p>The corresponding LLVM assembly:</p>
|
<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
|
<p>Intrinsics (instructions that start with <tt>llvm.</tt>) are not yet available
|
||||||
in llvm-py.</p>
|
in llvm-py.</p>
|
||||||
<h3>Modules</h3>
|
<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>
|
file (.c file). A module contains:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<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
|
have to be explicitly selected and run on each module. This gives you
|
||||||
the flexibility to choose transformations and optimizations that are
|
the flexibility to choose transformations and optimizations that are
|
||||||
most suitable for the code in the module.</p>
|
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
|
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
|
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
|
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
|
over enough LLVM APIs to allow the implementation of your own
|
||||||
compiler/VM backend in pure Python. If you're come this far, you
|
compiler/VM backend in pure Python. If you're come this far, you
|
||||||
probably know why this is a good idea.</p>
|
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
|
<p>Out of the 6 modules, one is an “extension” module (i.e., it is
|
||||||
in C), and another one is a private utility module, which leaves 4
|
written in C), and another one is a small private utility module, which
|
||||||
public modules. These are:</p>
|
leaves 4 public modules. These are:</p>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<p>
|
<p>
|
||||||
|
|
@ -587,7 +590,7 @@ Python constructs are used (deliberately) —
|
||||||
<a href="http://docs.python.org/lib/built-in-funcs.html">property()</a> and
|
<a href="http://docs.python.org/lib/built-in-funcs.html">property()</a> and
|
||||||
<a href="http://wiki.python.org/moin/PythonDecoratorLibrary">property
|
<a href="http://wiki.python.org/moin/PythonDecoratorLibrary">property
|
||||||
decorators</a> are probably the most exotic animals around. All classes are
|
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
|
guessable!) once you know a few conventions. These conventions are
|
||||||
highlighted in the sections below.</p>
|
highlighted in the sections below.</p>
|
||||||
<p>Here is a quick overview of the contents of each package:</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>
|
</ul>
|
||||||
<div class="title">A note on the 'import'ing of these modules</div>
|
<div class="title">A note on the 'import'ing of these modules</div>
|
||||||
<p>Pythonically, modules are imported with the statement <tt>"import
|
<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
|
llvm.core"</tt>. However, you might find it more convenient to import
|
||||||
it more convenient to import llvm-py modules thus:</p>
|
llvm-py modules thus:</p>
|
||||||
<div class="listingblock">
|
<div class="listingblock">
|
||||||
<div class="content"><!-- Generator: GNU source-highlight 2.4
|
<div class="content"><!-- Generator: GNU source-highlight 2.4
|
||||||
by Lorenzo Bettini
|
by Lorenzo Bettini
|
||||||
|
|
@ -735,7 +738,7 @@ http://www.gnu.org/software/src-highlite -->
|
||||||
<td class="icon">
|
<td class="icon">
|
||||||
<img src="./images/icons/tip.png" alt="Tip" />
|
<img src="./images/icons/tip.png" alt="Tip" />
|
||||||
</td>
|
</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
|
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>
|
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>
|
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-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>
|
<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>
|
</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>
|
a Module object. This is a common feature for all llvm-py classes.</p>
|
||||||
<div class="admonitionblock">
|
<div class="admonitionblock">
|
||||||
<table><tr>
|
<table><tr>
|
||||||
|
|
@ -1290,8 +1293,8 @@ http://www.gnu.org/software/src-highlite -->
|
||||||
by Lorenzo Bettini
|
by Lorenzo Bettini
|
||||||
http://www.lorenzobettini.it
|
http://www.lorenzobettini.it
|
||||||
http://www.gnu.org/software/src-highlite -->
|
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>
|
<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="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>
|
<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>
|
</tt></pre></div></div>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
|
@ -1390,11 +1393,11 @@ http://www.gnu.org/software/src-highlite -->
|
||||||
by Lorenzo Bettini
|
by Lorenzo Bettini
|
||||||
http://www.lorenzobettini.it
|
http://www.lorenzobettini.it
|
||||||
http://www.gnu.org/software/src-highlite -->
|
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">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>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> 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="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> 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>
|
</tt></pre></div></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt>
|
<dt>
|
||||||
|
|
@ -1440,11 +1443,11 @@ http://www.gnu.org/software/src-highlite -->
|
||||||
by Lorenzo Bettini
|
by Lorenzo Bettini
|
||||||
http://www.lorenzobettini.it
|
http://www.lorenzobettini.it
|
||||||
http://www.gnu.org/software/src-highlite -->
|
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">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>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> 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="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> 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>
|
</tt></pre></div></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt>
|
<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>
|
<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>
|
<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>
|
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="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>
|
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="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>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>
|
<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>
|
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="font-weight: bold"><span style="color: #000000">double</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="color: #000000">double</span><span style="color: #990000">()</span>
|
||||||
|
|
||||||
<span style="font-style: italic"><span style="color: #9A1900"># arrays and vectors</span></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>
|
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="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>
|
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="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>
|
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>
|
<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"># "struct s1_ty { int v1; float v2; };"</span></span>
|
||||||
|
|
||||||
<span style="font-style: italic"><span style="color: #9A1900"># pointers</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>
|
<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>
|
<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>
|
<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>
|
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="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>
|
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>
|
<span style="font-style: italic"><span style="color: #9A1900"># variadic function</span></span>
|
||||||
</tt></pre></div></div>
|
</tt></pre></div></div>
|
||||||
<h3>Values (llvm.core)</h3>
|
<h3>Values (llvm.core)</h3>
|
||||||
|
|
@ -1671,19 +1674,20 @@ http://www.lorenzobettini.it
|
||||||
http://www.gnu.org/software/src-highlite -->
|
http://www.gnu.org/software/src-highlite -->
|
||||||
<pre><tt><span style="font-style: italic"><span style="color: #9A1900">#!/usr/bin/env python</span></span>
|
<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>
|
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="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>
|
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>
|
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="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>
|
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>
|
</tt></pre></div></div>
|
||||||
<p>The following constructors (static methods) can be used to create
|
<p>The following constructors (static methods) can be used to create
|
||||||
constants:</p>
|
constants:</p>
|
||||||
<div class="tableblock">
|
<div class="tableblock">
|
||||||
|
<a id="constctors"></a>
|
||||||
<table rules="all"
|
<table rules="all"
|
||||||
frame="border"
|
frame="border"
|
||||||
cellspacing="0" cellpadding="4">
|
cellspacing="0" cellpadding="4">
|
||||||
|
|
@ -1809,6 +1813,7 @@ cellspacing="0" cellpadding="4">
|
||||||
</div>
|
</div>
|
||||||
<p>The following operations are available:</p>
|
<p>The following operations are available:</p>
|
||||||
<div class="tableblock">
|
<div class="tableblock">
|
||||||
|
<a id="constops"></a>
|
||||||
<table rules="all"
|
<table rules="all"
|
||||||
frame="border"
|
frame="border"
|
||||||
cellspacing="0" cellpadding="4">
|
cellspacing="0" cellpadding="4">
|
||||||
|
|
@ -1955,7 +1960,31 @@ cellspacing="0" cellpadding="4">
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="left">
|
<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>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
TODO
|
TODO
|
||||||
|
|
@ -1963,50 +1992,26 @@ cellspacing="0" cellpadding="4">
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
lshr
|
<tt>k.trunc(ty)</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
TODO
|
Truncate <tt>k</tt> to a type <tt>ty</tt> of lower bitwidth.
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
ashr
|
<tt>k.sext(ty)</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
TODO
|
Sign extend <tt>k</tt> to a type <tt>ty</tt> of higher bitwidth, while extending the sign bit.
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
gep
|
<tt>k.zext(ty)</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<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>
|
|
||||||
<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
|
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2242,7 +2247,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_FALSE</tt>
|
<tt>RPRED_FALSE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
Always false
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2250,7 +2255,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_OEQ</tt>
|
<tt>RPRED_OEQ</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered and equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2258,7 +2263,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_OGT</tt>
|
<tt>RPRED_OGT</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered and greater than
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2266,7 +2271,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_OGE</tt>
|
<tt>RPRED_OGE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered and greater than or equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2274,7 +2279,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_OLT</tt>
|
<tt>RPRED_OLT</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered and less than
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2282,7 +2287,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_OLE</tt>
|
<tt>RPRED_OLE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered and less than or equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2290,7 +2295,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_ONE</tt>
|
<tt>RPRED_ONE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered and operands are unequal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2298,7 +2303,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_ORD</tt>
|
<tt>RPRED_ORD</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if ordered (no NaNs)
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2306,7 +2311,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_UNO</tt>
|
<tt>RPRED_UNO</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered: <tt>isnan(X) | isnan(Y)</tt>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2314,7 +2319,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_UEQ</tt>
|
<tt>RPRED_UEQ</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered or equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2322,7 +2327,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_UGT</tt>
|
<tt>RPRED_UGT</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered or greater than
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2330,7 +2335,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_UGE</tt>
|
<tt>RPRED_UGE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered, greater than or equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2338,7 +2343,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_ULT</tt>
|
<tt>RPRED_ULT</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered, or less than
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2346,7 +2351,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_ULE</tt>
|
<tt>RPRED_ULE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered, less than or equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2354,7 +2359,7 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_UNE</tt>
|
<tt>RPRED_UNE</tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
True if unordered or not equal
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
|
|
@ -2362,12 +2367,28 @@ cellspacing="0" cellpadding="4">
|
||||||
<tt>RPRED_TRUE </tt>
|
<tt>RPRED_TRUE </tt>
|
||||||
</td>
|
</td>
|
||||||
<td align="left">
|
<td align="left">
|
||||||
|
Always true
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</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>
|
<h3>TypeHandle (llvm.core)</h3>
|
||||||
<p>TODO</p>
|
<p>TODO</p>
|
||||||
<h3>Instructions (llvm.core)</h3>
|
<h3>Instructions (llvm.core)</h3>
|
||||||
|
|
@ -2385,9 +2406,134 @@ cellspacing="0" cellpadding="4">
|
||||||
<h3>Pass Managers and Passes (llvm.passes)</h3>
|
<h3>Pass Managers and Passes (llvm.passes)</h3>
|
||||||
<p>TODO</p>
|
<p>TODO</p>
|
||||||
</div>
|
</div>
|
||||||
<h2>Annotated Examples</h2>
|
<h2><a id="examples"></a>Annotated Examples</h2>
|
||||||
<div class="sectionbody">
|
<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 ; <i32> [#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>
|
</div>
|
||||||
<h2>About the llvm-py Project</h2>
|
<h2>About the llvm-py Project</h2>
|
||||||
<div class="sectionbody">
|
<div class="sectionbody">
|
||||||
|
|
@ -2413,7 +2559,7 @@ reached at <em>mdevan.foobar@gmail.com</em>.</p>
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<div id="footer-text">
|
<div id="footer-text">
|
||||||
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
Web pages © 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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue