223 lines
No EOL
12 KiB
HTML
223 lines
No EOL
12 KiB
HTML
|
|
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
|
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
|
|
<title>Intrinsic — llvmpy 0.8.2 documentation</title>
|
|
|
|
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
|
|
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
|
|
|
|
<script type="text/javascript">
|
|
var DOCUMENTATION_OPTIONS = {
|
|
URL_ROOT: '../',
|
|
VERSION: '0.8.2',
|
|
COLLAPSE_INDEX: false,
|
|
FILE_SUFFIX: '.html',
|
|
HAS_SOURCE: true
|
|
};
|
|
</script>
|
|
<script type="text/javascript" src="../_static/jquery.js"></script>
|
|
<script type="text/javascript" src="../_static/underscore.js"></script>
|
|
<script type="text/javascript" src="../_static/doctools.js"></script>
|
|
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
|
|
<link rel="top" title="llvmpy 0.8.2 documentation" href="../index.html" />
|
|
</head>
|
|
<body>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
accesskey="I">index</a></li>
|
|
<li><a href="../index.html">llvmpy 0.8.2 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
|
|
<div class="document">
|
|
<div class="documentwrapper">
|
|
<div class="bodywrapper">
|
|
<div class="body">
|
|
|
|
<table border="1" class="docutils">
|
|
<colgroup>
|
|
<col width="100%" />
|
|
</colgroup>
|
|
<tbody valign="top">
|
|
<tr class="row-odd"><td>layout: page</td>
|
|
</tr>
|
|
<tr class="row-even"><td>title: Functions</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p>Functions are represented by
|
|
<a class="reference external" href="llvm.core.Function.html">llvm.core.Function</a> objects. They are
|
|
contained within modules, and can be created either with the method
|
|
<tt class="docutils literal"><span class="pre">module_obj.add_function</span></tt> or the static constructor <tt class="docutils literal"><span class="pre">Function.new</span></tt>.
|
|
References to functions already present in a module can be retrieved via
|
|
<tt class="docutils literal"><span class="pre">module.get_function_named</span></tt> or by the static constructor method
|
|
<tt class="docutils literal"><span class="pre">Function.get</span></tt>. All functions in a module can be enumerated by
|
|
iterating over <tt class="docutils literal"><span class="pre">module_obj.functions</span></tt>.</p>
|
|
<div class="highlight-python"><pre># create a type, representing functions that take
|
|
an integer and return # a floating point value. ft = Type.function(
|
|
Type.float(), [ Type.int() ] )
|
|
|
|
# create a function of this type
|
|
f1 = module_obj.add_function(ft, "func1")
|
|
|
|
# or equivalently, like this:
|
|
f2 = Function.new(module_obj, ft, "func2")
|
|
|
|
# get a reference to an existing function
|
|
f3 = module_obj.get_function_named("func3")
|
|
|
|
# or like this:
|
|
f4 = Function.get(module_obj, "func4")
|
|
|
|
# list all function names in a module
|
|
for f in module_obj.functions: print f.name</pre>
|
|
</div>
|
|
<div class="section" id="intrinsic">
|
|
<h1>Intrinsic<a class="headerlink" href="#intrinsic" title="Permalink to this headline">¶</a></h1>
|
|
<p>References to intrinsic functions can be got via the static constructor
|
|
<tt class="docutils literal"><span class="pre">intrinsic</span></tt>. This returns a <tt class="docutils literal"><span class="pre">Function</span></tt> object, calling which is
|
|
equivalent to invoking the intrinsic. The <tt class="docutils literal"><span class="pre">intrinsic</span></tt> method has to be
|
|
called with a module object, an intrinsic ID (which is a numeric
|
|
constant) and a list of the types of arguments (which LLVM uses to
|
|
resolve overloaded intrinsic functions).</p>
|
|
<div class="highlight-python"><div class="highlight"><pre><span class="c"># get a reference to the llvm.bswap intrinsic</span>
|
|
<span class="n">bswap</span> <span class="o">=</span> <span class="n">Function</span><span class="o">.</span><span class="n">intrinsic</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">INTR_BSWAP</span><span class="p">,</span> <span class="p">[</span><span class="n">Type</span><span class="o">.</span><span class="n">int</span><span class="p">()])</span>
|
|
|
|
<span class="c"># call it</span>
|
|
<span class="n">builder</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">bswap</span><span class="p">,</span> <span class="p">[</span><span class="n">value</span><span class="p">])</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Here, the constant <tt class="docutils literal"><span class="pre">INTR_BSWAP</span></tt>, available from <tt class="docutils literal"><span class="pre">llvm.core</span></tt>,
|
|
represents the LLVM intrinsic
|
|
<a class="reference external" href="http://www.llvm.org/docs/LangRef.html#int_bswap">llvm.bswap</a>. The
|
|
<tt class="docutils literal"><span class="pre">[Type.int()]</span></tt> selects the version of <tt class="docutils literal"><span class="pre">llvm.bswap</span></tt> that has a single
|
|
32-bit integer argument. The list of intrinsic IDs defined as integer
|
|
constants in <tt class="docutils literal"><span class="pre">llvm.core</span></tt>. These are:</p>
|
|
<p>{% include intrinsics.csv %}</p>
|
|
<p>There are also target-specific intrinsics (which correspond to that
|
|
target’s CPU instructions) available, but are omitted here for brevity.
|
|
Full list can be seen from
|
|
[<em>intrinsic_ids.py](https://github.com/numba/llvmpy/blob/master/llvm/</em>intrinsic_ids.py).
|
|
See the <a class="reference external" href="http://www.llvm.org/docs/LangRef.html">LLVM Language
|
|
Reference</a> for more information
|
|
on the intrinsics, and the
|
|
<a class="reference external" href="https://github.com/numba/llvmpy/blob/master/test/intrinsic.py">test</a>
|
|
directory in the source distribution for more examples. The intrinsic ID
|
|
can be retrieved from a function object with the read-only property
|
|
<tt class="docutils literal"><span class="pre">intrinsic_id</span></tt>.</p>
|
|
<blockquote>
|
|
<div><p><strong>Auto-generation of Intrinsic IDs</strong></p>
|
|
<p>A script (tool/intrgen.py in source tree) generates the intrinsic
|
|
IDs automatically. This is necessary when compiling llvmpy with a
|
|
different version of LLVM.</p>
|
|
</div></blockquote>
|
|
</div>
|
|
<div class="section" id="calling-convention-callconv">
|
|
<h1>Calling Convention # {#callconv}<a class="headerlink" href="#calling-convention-callconv" title="Permalink to this headline">¶</a></h1>
|
|
<p>The function’s calling convention can be set using the
|
|
<tt class="docutils literal"><span class="pre">calling_convention</span></tt> property. The following (integer) constants
|
|
defined in <tt class="docutils literal"><span class="pre">llvm.core</span></tt> can be used as values:</p>
|
|
<p>Value | Equivalent LLVM Assembly Keyword |
|
|
——|———————————-| <tt class="docutils literal"><span class="pre">CC_C</span></tt> | <tt class="docutils literal"><span class="pre">ccc</span></tt> |
|
|
<tt class="docutils literal"><span class="pre">CC_FASTCALL</span></tt> | <tt class="docutils literal"><span class="pre">fastcc</span></tt> | <tt class="docutils literal"><span class="pre">CC_COLDCALL</span></tt> | <tt class="docutils literal"><span class="pre">coldcc</span></tt> |
|
|
<tt class="docutils literal"><span class="pre">CC_X86_STDCALL</span></tt> | <tt class="docutils literal"><span class="pre">x86_stdcallcc</span></tt> | <tt class="docutils literal"><span class="pre">CC_X86_FASTCALL</span></tt> |
|
|
<tt class="docutils literal"><span class="pre">x86_fastcallcc</span></tt> |</p>
|
|
<p>See the <a class="reference external" href="http://www.llvm.org/docs/LangRef.html#callingconv">LLVM docs</a>
|
|
for more information on each. Backend-specific numbered conventions can
|
|
be directly passed as integers.</p>
|
|
<p>An arbitrary string identifying which garbage collector to use can be
|
|
set or got with the property <tt class="docutils literal"><span class="pre">collector</span></tt>.</p>
|
|
<p>The value objects corresponding to the arguments of a function can be
|
|
got using the read-only property <tt class="docutils literal"><span class="pre">args</span></tt>. These can be iterated over,
|
|
and also be indexed via integers. An example:</p>
|
|
<div class="highlight-python"><pre># list all argument names and types for arg in
|
|
fn.args: print arg.name, "of type", arg.type
|
|
|
|
# change the name of the first argument
|
|
fn.args[0].name = "objptr"</pre>
|
|
</div>
|
|
<p>Basic blocks (see later) are contained within functions. When newly
|
|
created, a function has no basic blocks. They have to be added
|
|
explicitly, using the <tt class="docutils literal"><span class="pre">append_basic_block</span></tt> method, which adds a new,
|
|
empty basic block as the last one in the function. The first basic block
|
|
of the function can be retrieved using the <tt class="docutils literal"><span class="pre">get_entry_basic_block</span></tt>
|
|
method. The existing basic blocks can be enumerated by iterating over
|
|
using the read-only property <tt class="docutils literal"><span class="pre">basic_blocks</span></tt>. The number of basic
|
|
blocks can be got via <tt class="docutils literal"><span class="pre">basic_block_count</span></tt> method. Note that
|
|
<tt class="docutils literal"><span class="pre">get_entry_basic_block</span></tt> is slightly faster than <tt class="docutils literal"><span class="pre">basic_blocks[0]</span></tt>
|
|
and so is <tt class="docutils literal"><span class="pre">basic_block_count</span></tt>, over <tt class="docutils literal"><span class="pre">len(f.basic_blocks)</span></tt>.</p>
|
|
<div class="highlight-python"><pre># add a basic block b1 =
|
|
fn.append_basic_block("entry")
|
|
|
|
# get the first one
|
|
b2 = fn.get_entry_basic_block() b2 = fn.basic_mdblocks[0] # slower
|
|
than previous method
|
|
|
|
# print names of all basic blocks
|
|
for b in fn.basic_blocks: print b.name
|
|
|
|
# get number of basic blocks
|
|
n = fn.basic_block_count n = len(fn.basic_blocks) # slower than
|
|
previous method</pre>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sphinxsidebar">
|
|
<div class="sphinxsidebarwrapper">
|
|
<h3><a href="../index.html">Table Of Contents</a></h3>
|
|
<ul>
|
|
<li><a class="reference internal" href="#">Intrinsic</a></li>
|
|
<li><a class="reference internal" href="#calling-convention-callconv">Calling Convention # {#callconv}</a></li>
|
|
</ul>
|
|
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../_sources/doc/functions.txt"
|
|
rel="nofollow">Show Source</a></li>
|
|
</ul>
|
|
<div id="searchbox" style="display: none">
|
|
<h3>Quick search</h3>
|
|
<form class="search" action="../search.html" method="get">
|
|
<input type="text" name="q" />
|
|
<input type="submit" value="Go" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
<p class="searchtip" style="font-size: 90%">
|
|
Enter search terms or a module, class or function name.
|
|
</p>
|
|
</div>
|
|
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
</div>
|
|
</div>
|
|
<div class="clearer"></div>
|
|
</div>
|
|
<div class="related">
|
|
<h3>Navigation</h3>
|
|
<ul>
|
|
<li class="right" style="margin-right: 10px">
|
|
<a href="../genindex.html" title="General Index"
|
|
>index</a></li>
|
|
<li><a href="../index.html">llvmpy 0.8.2 documentation</a> »</li>
|
|
</ul>
|
|
</div>
|
|
<div class="footer">
|
|
© Copyright 2012, Mahadevan R (2008-2010), Continuum Analytics (2012).
|
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.2.
|
|
</div>
|
|
</body>
|
|
</html> |