DOC: Add first version of updated documentation.
This commit is contained in:
parent
75ca0289b5
commit
415c01f745
19 changed files with 474 additions and 595 deletions
|
|
@ -1,45 +1,36 @@
|
|||
+-------------------------+
|
||||
| layout: page |
|
||||
+-------------------------+
|
||||
| title: JIT Tutorial 1 |
|
||||
+-------------------------+
|
||||
A First Function
|
||||
==================
|
||||
|
||||
{% highlight python %} #!/usr/bin/env python
|
||||
.. code-block:: python
|
||||
|
||||
from llvm.core import \*
|
||||
#!/usr/bin/env python
|
||||
|
||||
from llvm.core import *
|
||||
|
||||
create a module
|
||||
===============
|
||||
#create a module
|
||||
module = Module.new("tut1")
|
||||
|
||||
module = Module.new ("tut1")
|
||||
#create a function type taking 3 32-bit integers, return a 32-bit integer
|
||||
ty_int = Type.int(32)
|
||||
func_type = Type.function(ty_int, (ty_int,)*3)
|
||||
|
||||
create a function type taking 3 32-bit integers, return a 32-bit integer
|
||||
========================================================================
|
||||
#create a function of that type
|
||||
mul_add = Function.new (module, func_type, "mul_add")
|
||||
mul_add.calling_convention = CC_C
|
||||
x = mul_add.args[0]; x.name = "x"
|
||||
y = mul_add.args[1]; y.name = "y"
|
||||
z = mul_add.args[2]; z.name = "z"
|
||||
|
||||
ty\_int = Type.int (32) func\_type = Type.function (ty\_int,
|
||||
(ty\_int,)\*3)
|
||||
#implement the function
|
||||
|
||||
create a function of that type
|
||||
==============================
|
||||
#new block
|
||||
blk = mul_add.append_basic_block("entry")
|
||||
|
||||
mul\_add = Function.new (module, func\_type, "mul\_add")
|
||||
mul\_add.calling\_convention = CC\_C x = mul\_add.args[0]; x.name = "x"
|
||||
y = mul\_add.args[1]; y.name = "y" z = mul\_add.args[2]; z.name = "z"
|
||||
#IR builder
|
||||
bldr = Builder.new(blk)
|
||||
tmp_1 = bldr.mul(x, y, "tmp_1")
|
||||
tmp_2 = bldr.add(tmp_1, z, "tmp_2")
|
||||
|
||||
implement the function
|
||||
======================
|
||||
bldr.ret(tmp_2)
|
||||
|
||||
new block
|
||||
=========
|
||||
|
||||
blk = mul\_add.append\_basic\_block ("entry")
|
||||
|
||||
IR builder
|
||||
==========
|
||||
|
||||
bldr = Builder.new (blk) tmp\_1 = bldr.mul (x, y, "tmp\_1") tmp\_2 =
|
||||
bldr.add (tmp\_1, z, "tmp\_2")
|
||||
|
||||
bldr.ret (tmp\_2)
|
||||
|
||||
print(module) {% endhighlight %}
|
||||
print module
|
||||
|
|
|
|||
|
|
@ -1,62 +1,55 @@
|
|||
+-------------------------+
|
||||
| layout: page |
|
||||
+-------------------------+
|
||||
| title: JIT Tutorial 2 |
|
||||
+-------------------------+
|
||||
A More Complicated Function
|
||||
====================
|
||||
|
||||
{% highlight python %} #!/usr/bin/env python
|
||||
.. code-block:: python
|
||||
|
||||
from llvm.core import \*
|
||||
#!/usr/bin/env python
|
||||
|
||||
create a module
|
||||
===============
|
||||
from llvm.core import *
|
||||
|
||||
module = Module.new ("tut2")
|
||||
#create a module
|
||||
module = Module.new("tut2")
|
||||
|
||||
create a function type taking 2 integers, return a 32-bit integer
|
||||
=================================================================
|
||||
#create a function type taking 2 integers, return a 32-bit integer
|
||||
ty_int = Type.int(32)
|
||||
func_type = Type.function(ty_int, (ty_int, ty_int))
|
||||
|
||||
ty\_int = Type.int (32) func\_type = Type.function (ty\_int, (ty\_int,
|
||||
ty\_int))
|
||||
#create a function of that type
|
||||
gcd = Function.new(module, func_type, "gcd")
|
||||
|
||||
create a function of that type
|
||||
==============================
|
||||
#name function args
|
||||
x = gcd.args[0]; x.name = "x"
|
||||
y = gcd.args[1]; y.name = "y"
|
||||
|
||||
gcd = Function.new (module, func\_type, "gcd")
|
||||
#implement the function
|
||||
|
||||
name function args
|
||||
==================
|
||||
#blocks...
|
||||
entry = gcd.append_basic_block("entry")
|
||||
ret = gcd.append_basic_block("return")
|
||||
cond_false = gcd.append_basic_block("cond_false")
|
||||
cond_true = gcd.append_basic_block("cond_true")
|
||||
cond_false_2 = gcd.append_basic_block("cond_false_2")
|
||||
|
||||
x = gcd.args[0]; x.name = "x" y = gcd.args[1]; y.name = "y"
|
||||
#create a llvm::IRBuilder
|
||||
bldr = Builder.new(entry)
|
||||
x_eq_y = bldr.icmp(IPRED_EQ, x, y, "tmp")
|
||||
bldr.cbranch(x_eq_y, ret, cond_false)
|
||||
|
||||
implement the function
|
||||
======================
|
||||
bldr.position_at_end (ret)
|
||||
bldr.ret(x)
|
||||
|
||||
blocks...
|
||||
=========
|
||||
bldr.position_at_end(cond_false)
|
||||
x_lt_y = bldr.icmp(IPRED_ULT, x, y, "tmp")
|
||||
bldr.cbranch(x_lt_y, cond_true, cond_false_2)
|
||||
|
||||
entry = gcd.append\_basic\_block ("entry") ret =
|
||||
gcd.append\_basic\_block ("return") cond\_false =
|
||||
gcd.append\_basic\_block ("cond\_false") cond\_true =
|
||||
gcd.append\_basic\_block ("cond\_true") cond\_false\_2 =
|
||||
gcd.append\_basic\_block ("cond\_false\_2")
|
||||
bldr.position_at_end(cond_true)
|
||||
y_sub_x = bldr.sub(y, x, "tmp")
|
||||
recur_1 = bldr.call(gcd, (x, y_sub_x,), "tmp")
|
||||
bldr.ret(recur_1)
|
||||
|
||||
create a llvm::IRBuilder
|
||||
========================
|
||||
bldr.position_at_end(cond_false_2)
|
||||
x_sub_y = bldr.sub(x, y, "x_sub_y")
|
||||
recur_2 = bldr.call(gcd, (x_sub_y, y,), "tmp")
|
||||
bldr.ret(recur_2)
|
||||
|
||||
bldr = Builder.new (entry) x\_eq\_y = bldr.icmp (IPRED\_EQ, x, y, "tmp")
|
||||
bldr.cbranch (x\_eq\_y, ret, cond\_false)
|
||||
|
||||
bldr.position\_at\_end (ret) bldr.ret(x)
|
||||
|
||||
bldr.position\_at\_end (cond\_false) x\_lt\_y = bldr.icmp (IPRED\_ULT,
|
||||
x, y, "tmp") bldr.cbranch (x\_lt\_y, cond\_true, cond\_false\_2)
|
||||
|
||||
bldr.position\_at\_end (cond\_true) y\_sub\_x = bldr.sub (y, x, "tmp")
|
||||
recur\_1 = bldr.call (gcd, (x, y\_sub\_x,), "tmp") bldr.ret (recur\_1)
|
||||
|
||||
bldr.position\_at\_end (cond\_false\_2) x\_sub\_y = bldr.sub (x, y,
|
||||
"x\_sub\_y") recur\_2 = bldr.call (gcd, (x\_sub\_y, y,), "tmp") bldr.ret
|
||||
(recur\_2)
|
||||
|
||||
print(module) {% endhighlight %}
|
||||
print module
|
||||
|
|
|
|||
10
docs/source/doc/examples/index.rst
Normal file
10
docs/source/doc/examples/index.rst
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
LLVM Tutorials
|
||||
==============
|
||||
|
||||
The following JIT tutorials were contributed by Sebastien Binet.
|
||||
|
||||
.. toctree::
|
||||
:titlesonly:
|
||||
|
||||
JITTutorial1.rst
|
||||
JITTutorial2.rst
|
||||
Loading…
Add table
Add a link
Reference in a new issue