git-svn-id: http://llvm-py.googlecode.com/svn/trunk@105 8d1e9007-1d4e-0410-b67e-1979fd6579aa
232 lines
18 KiB
HTML
232 lines
18 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
|
<meta name="generator" content="AsciiDoc 8.5.2" />
|
|
<meta name="description" content="Python bindings for LLVM" />
|
|
<meta name="keywords" content="llvm python compiler backend bindings" />
|
|
<link rel="stylesheet" href="style/xhtml11.css" type="text/css" />
|
|
<link rel="stylesheet" href="style/xhtml11-quirks.css" type="text/css" />
|
|
<link rel="stylesheet" href="style/layout.css" type="text/css" />
|
|
<title>Examples and LLVM Tutorials - llvm-py</title>
|
|
</head>
|
|
<body>
|
|
<div id="layout-banner">
|
|
<div id="layout-title">llvm-py</div>
|
|
<div id="layout-description">Python Bindings for LLVM</div>
|
|
</div>
|
|
<table>
|
|
<tr valign="top">
|
|
<td id="layout-menu">
|
|
<div>»<a href="index.html">Home</a></div>
|
|
<div>»<a href="examples.html">Examples</a></div>
|
|
<div>»<a href="download.html">Download</a></div>
|
|
<div>»<a href="userguide.html">User Guide</a></div>
|
|
<div>»<a href="contribute.html">Contribute</a></div>
|
|
<div>»<a href="license.html">License</a></div>
|
|
<div>»<a href="about.html">About</a></div>
|
|
</td>
|
|
<td>
|
|
<div id="layout-content">
|
|
<div id="header">
|
|
<h1>Examples and LLVM Tutorials</h1>
|
|
</div>
|
|
<h2 id="_examples">Examples</h2>
|
|
<div class="sectionbody">
|
|
<h3 id="_a_simple_function">A Simple Function</h3><div style="clear:left"></div>
|
|
<div class="paragraph"><p>Let’s create a (LLVM) module containing a single function, corresponding
|
|
to the <tt>C</tt> function:</p></div>
|
|
<div class="listingblock">
|
|
<div class="content"><!-- Generator: GNU source-highlight 3.1.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="font-weight: bold"><span style="color: #000000">sum</span></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>
|
|
<div class="paragraph"><p>Here’s how it looks like:</p></div>
|
|
<div class="listingblock">
|
|
<div class="content"><!-- Generator: GNU source-highlight 3.1.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 an (empty) module.</span></span>
|
|
my_module <span style="color: #990000">=</span> Module<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'my_module'</span><span style="color: #990000">)</span>
|
|
|
|
<span style="font-style: italic"><span style="color: #9A1900"># 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_int <span style="color: #990000">=</span> Type<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"># 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_func <span style="color: #990000">=</span> Type<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span>ty_int<span style="color: #990000">,</span> <span style="color: #990000">[</span>ty_int<span style="color: #990000">,</span> ty_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_sum <span style="color: #990000">=</span> my_module<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_function</span></span><span style="color: #990000">(</span>ty_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_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_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_sum<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"># 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> Builder<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></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="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span>f_sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> f_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="font-weight: bold"><span style="color: #000000">ret</span></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_module</tt></pre></div></div>
|
|
<div class="paragraph"><p>Here is the output:</p></div>
|
|
<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 id="_adding_jit_compilation">Adding JIT Compilation</h3><div style="clear:left"></div>
|
|
<div class="paragraph"><p>Let’s compile this function in-memory and run it.</p></div>
|
|
<div class="listingblock">
|
|
<div class="content"><!-- Generator: GNU source-highlight 3.1.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_module <span style="color: #990000">=</span> Module<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>
|
|
ty_int <span style="color: #990000">=</span> Type<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"># by default 32 bits</span></span>
|
|
ty_func <span style="color: #990000">=</span> Type<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">function</span></span><span style="color: #990000">(</span>ty_int<span style="color: #990000">,</span> <span style="color: #990000">[</span>ty_int<span style="color: #990000">,</span> ty_int<span style="color: #990000">])</span>
|
|
f_sum <span style="color: #990000">=</span> my_module<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">add_function</span></span><span style="color: #990000">(</span>ty_func<span style="color: #990000">,</span> <span style="color: #FF0000">"sum"</span><span style="color: #990000">)</span>
|
|
f_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_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_sum<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>
|
|
builder <span style="color: #990000">=</span> Builder<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></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="font-weight: bold"><span style="color: #000000">add</span></span><span style="color: #990000">(</span>f_sum<span style="color: #990000">.</span>args<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> f_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="font-weight: bold"><span style="color: #000000">ret</span></span><span style="color: #990000">(</span>tmp<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> ExecutionEngine<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">new</span></span><span style="color: #990000">(</span>my_module<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> GenericValue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span>ty_int<span style="color: #990000">,</span> <span style="color: #993399">100</span><span style="color: #990000">)</span>
|
|
arg2 <span style="color: #990000">=</span> GenericValue<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #000000">int</span></span><span style="color: #990000">(</span>ty_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="font-weight: bold"><span style="color: #000000">run_function</span></span><span style="color: #990000">(</span>f_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="font-weight: bold"><span style="color: #000000">as_int</span></span><span style="color: #990000">()</span></tt></pre></div></div>
|
|
<div class="paragraph"><p>And here’s the output:</p></div>
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre><tt>returned 142</tt></pre>
|
|
</div></div>
|
|
</div>
|
|
<h2 id="_llvm_tutorials">LLVM Tutorials</h2>
|
|
<div class="sectionbody">
|
|
<div class="paragraph"><div class="title">Simple JIT Tutorials</div><p>The following JIT tutorials were contributed by Sebastien Binet.</p></div>
|
|
<div class="olist arabic"><ol class="arabic">
|
|
<li>
|
|
<p>
|
|
<a href="examples/JITTutorial1.html">A First Function</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="examples/JITTutorial2.html">A More Complicated Function</a>
|
|
</p>
|
|
</li>
|
|
</ol></div>
|
|
<div class="paragraph" id="kaleidoscope"><div class="title">Kaleidoscope: Implementing a Language with LLVM</div><p>The LLVM <a href="http://www.llvm.org/docs/tutorial/">Kaleidoscope</a> tutorial
|
|
has been ported to llvm-py by Max Shawabkeh.</p></div>
|
|
<div class="olist arabic"><ol class="arabic">
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl1.html">Tutorial Introduction and the Lexer</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl2.html">Implementing a Parser and AST</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl3.html">Implementing Code Generation to LLVM IR</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl4.html">Adding JIT and Optimizer Support</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl5.html">Extending the language: control flow</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl6.html">Extending the language: user-defined operators</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl7.html">Extending the language: mutable variables / SSA construction</a>
|
|
</p>
|
|
</li>
|
|
<li>
|
|
<p>
|
|
<a href="kaleidoscope/PythonLangImpl8.html">Conclusion and other useful LLVM tidbits</a>
|
|
</p>
|
|
</li>
|
|
</ol></div>
|
|
</div>
|
|
<div id="footer">
|
|
<div id="footer-text">
|
|
Web pages © Mahadevan R. Generated with <a href="http://www.methods.co.nz/asciidoc/">asciidoc</a>.
|
|
Last updated 2010-11-05.
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<script type="text/javascript">
|
|
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
|
|
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
|
|
var pageTracker = _gat._getTracker("UA-4519056-2");
|
|
pageTracker._initData();
|
|
pageTracker._trackPageview();
|
|
</script>
|
|
</body>
|
|
</html>
|