blob: 2eb452af0fe352579f7fb2602cbb43352f6ef951 [file] [log] [blame]
Anton Korobeynikove31f1472008-06-09 04:15:49 +00001<?xml version="1.0" encoding="utf-8" ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Dan Gohmanf17a25c2007-07-18 16:29:46 +00004<head>
Anton Korobeynikove31f1472008-06-09 04:15:49 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Mikhail Glushenkovac251f22008-12-11 23:24:40 +00006<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
Anton Korobeynikove31f1472008-06-09 04:15:49 +00007<title>Customizing LLVMC: Reference Manual</title>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +00008<meta name="author" content="Mikhail Glushenkov &lt;foldr&#64;codedegers.com&gt;" />
9<link rel="stylesheet" href="llvm-rst.css" type="text/css" />
Dan Gohmanf17a25c2007-07-18 16:29:46 +000010</head>
11<body>
Anton Korobeynikove31f1472008-06-09 04:15:49 +000012<div class="document" id="customizing-llvmc-reference-manual">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000013<h1 class="title">Customizing LLVMC: Reference Manual</h1>
14<table class="docinfo" frame="void" rules="none">
15<col class="docinfo-name" />
16<col class="docinfo-content" />
17<tbody valign="top">
18<tr><th class="docinfo-name">Author:</th>
19<td>Mikhail Glushenkov &lt;<a class="reference" href="mailto:foldr&#64;codedegers.com">foldr&#64;codedegers.com</a>&gt;</td></tr>
20</tbody>
21</table>
Anton Korobeynikove31f1472008-06-09 04:15:49 +000022<p>LLVMC is a generic compiler driver, designed to be customizable and
23extensible. It plays the same role for LLVM as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program
24does for GCC - LLVMC's job is essentially to transform a set of input
25files into a set of targets depending on configuration rules and user
26options. What makes LLVMC different is that these transformation rules
27are completely customizable - in fact, LLVMC knows nothing about the
28specifics of transformation (even the command-line options are mostly
29not hard-coded) and regards the transformation structure as an
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000030abstract graph. The structure of this graph is completely determined
31by plugins, which can be either statically or dynamically linked. This
32makes it possible to easily adapt LLVMC for other purposes - for
33example, as a build tool for game resources.</p>
34<p>Because LLVMC employs TableGen <a class="footnote-reference" href="#id7" id="id1" name="id1">[1]</a> as its configuration language, you
Anton Korobeynikove31f1472008-06-09 04:15:49 +000035need to be familiar with it to customize LLVMC.</p>
36<div class="contents topic">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000037<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +000038<ul class="simple">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000039<li><a class="reference" href="#compiling-with-llvmc" id="id10" name="id10">Compiling with LLVMC</a></li>
40<li><a class="reference" href="#predefined-options" id="id11" name="id11">Predefined options</a></li>
41<li><a class="reference" href="#compiling-llvmc-plugins" id="id12" name="id12">Compiling LLVMC plugins</a></li>
42<li><a class="reference" href="#customizing-llvmc-the-compilation-graph" id="id13" name="id13">Customizing LLVMC: the compilation graph</a></li>
43<li><a class="reference" href="#describing-options" id="id14" name="id14">Describing options</a><ul>
44<li><a class="reference" href="#external-options" id="id15" name="id15">External options</a></li>
45</ul>
46</li>
47<li><a class="reference" href="#conditional-evaluation" id="id16" name="id16">Conditional evaluation</a></li>
48<li><a class="reference" href="#writing-a-tool-description" id="id17" name="id17">Writing a tool description</a><ul>
49<li><a class="reference" href="#actions" id="id18" name="id18">Actions</a></li>
50</ul>
51</li>
52<li><a class="reference" href="#language-map" id="id19" name="id19">Language map</a></li>
53<li><a class="reference" href="#more-advanced-topics" id="id20" name="id20">More advanced topics</a><ul>
54<li><a class="reference" href="#hooks-and-environment-variables" id="id21" name="id21">Hooks and environment variables</a></li>
55<li><a class="reference" href="#how-plugins-are-loaded" id="id22" name="id22">How plugins are loaded</a></li>
56<li><a class="reference" href="#debugging" id="id23" name="id23">Debugging</a></li>
57</ul>
58</li>
59<li><a class="reference" href="#references" id="id24" name="id24">References</a></li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +000060</ul>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000062<div class="section">
63<h1><a class="toc-backref" href="#id10" id="compiling-with-llvmc" name="compiling-with-llvmc">Compiling with LLVMC</a></h1>
Anton Korobeynikove31f1472008-06-09 04:15:49 +000064<p>LLVMC tries hard to be as compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as possible,
65although there are some small differences. Most of the time, however,
66you shouldn't be able to notice them:</p>
67<pre class="literal-block">
68$ # This works as expected:
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000069$ llvmc -O3 -Wall hello.cpp
Anton Korobeynikove31f1472008-06-09 04:15:49 +000070$ ./a.out
71hello
72</pre>
73<p>One nice feature of LLVMC is that one doesn't have to distinguish
74between different compilers for different languages (think <tt class="docutils literal"><span class="pre">g++</span></tt> and
75<tt class="docutils literal"><span class="pre">gcc</span></tt>) - the right toolchain is chosen automatically based on input
76language names (which are, in turn, determined from file
77extensions). If you want to force files ending with &quot;.c&quot; to compile as
78C++, use the <tt class="docutils literal"><span class="pre">-x</span></tt> option, just like you would do it with <tt class="docutils literal"><span class="pre">gcc</span></tt>:</p>
79<pre class="literal-block">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000080$ # hello.c is really a C++ file
81$ llvmc -x c++ hello.c
Anton Korobeynikove31f1472008-06-09 04:15:49 +000082$ ./a.out
83hello
84</pre>
85<p>On the other hand, when using LLVMC as a linker to combine several C++
86object files you should provide the <tt class="docutils literal"><span class="pre">--linker</span></tt> option since it's
87impossible for LLVMC to choose the right linker in that case:</p>
88<pre class="literal-block">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000089$ llvmc -c hello.cpp
90$ llvmc hello.o
Anton Korobeynikove31f1472008-06-09 04:15:49 +000091[A lot of link-time errors skipped]
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000092$ llvmc --linker=c++ hello.o
Anton Korobeynikove31f1472008-06-09 04:15:49 +000093$ ./a.out
94hello
95</pre>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +000096<p>By default, LLVMC uses <tt class="docutils literal"><span class="pre">llvm-gcc</span></tt> to compile the source code. It is
97also possible to choose the work-in-progress <tt class="docutils literal"><span class="pre">clang</span></tt> compiler with
98the <tt class="docutils literal"><span class="pre">-clang</span></tt> option.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000100<div class="section">
101<h1><a class="toc-backref" href="#id11" id="predefined-options" name="predefined-options">Predefined options</a></h1>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000102<p>LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000103configuration libraries:</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000104<ul class="simple">
105<li><tt class="docutils literal"><span class="pre">-o</span> <span class="pre">FILE</span></tt> - Output file name.</li>
106<li><tt class="docutils literal"><span class="pre">-x</span> <span class="pre">LANGUAGE</span></tt> - Specify the language of the following input files
107until the next -x option.</li>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000108<li><tt class="docutils literal"><span class="pre">-load</span> <span class="pre">PLUGIN_NAME</span></tt> - Load the specified plugin DLL. Example:
109<tt class="docutils literal"><span class="pre">-load</span> <span class="pre">$LLVM_DIR/Release/lib/LLVMCSimple.so</span></tt>.</li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000110<li><tt class="docutils literal"><span class="pre">-v</span></tt> - Enable verbose mode, i.e. print out all executed commands.</li>
111<li><tt class="docutils literal"><span class="pre">--view-graph</span></tt> - Show a graphical representation of the compilation
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000112graph. Requires that you have <tt class="docutils literal"><span class="pre">dot</span></tt> and <tt class="docutils literal"><span class="pre">gv</span></tt> programs
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000113installed. Hidden option, useful for debugging.</li>
114<li><tt class="docutils literal"><span class="pre">--write-graph</span></tt> - Write a <tt class="docutils literal"><span class="pre">compilation-graph.dot</span></tt> file in the
115current directory with the compilation graph description in the
116Graphviz format. Hidden option, useful for debugging.</li>
117<li><tt class="docutils literal"><span class="pre">--save-temps</span></tt> - Write temporary files to the current directory
118and do not delete them on exit. Hidden option, useful for debugging.</li>
119<li><tt class="docutils literal"><span class="pre">--help</span></tt>, <tt class="docutils literal"><span class="pre">--help-hidden</span></tt>, <tt class="docutils literal"><span class="pre">--version</span></tt> - These options have
120their standard meaning.</li>
121</ul>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000123<div class="section">
124<h1><a class="toc-backref" href="#id12" id="compiling-llvmc-plugins" name="compiling-llvmc-plugins">Compiling LLVMC plugins</a></h1>
125<p>It's easiest to start working on your own LLVMC plugin by copying the
126skeleton project which lives under <tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins/Simple</span></tt>:</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000127<pre class="literal-block">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000128$ cd $LLVMC_DIR/plugins
129$ cp -r Simple MyPlugin
130$ cd MyPlugin
131$ ls
132Makefile PluginMain.cpp Simple.td
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000133</pre>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000134<p>As you can see, our basic plugin consists of only two files (not
135counting the build script). <tt class="docutils literal"><span class="pre">Simple.td</span></tt> contains TableGen
136description of the compilation graph; its format is documented in the
137following sections. <tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> is just a helper file used to
138compile the auto-generated C++ code produced from TableGen source. It
139can also contain hook definitions (see <a class="reference" href="#hooks">below</a>).</p>
140<p>The first thing that you should do is to change the <tt class="docutils literal"><span class="pre">LLVMC_PLUGIN</span></tt>
141variable in the <tt class="docutils literal"><span class="pre">Makefile</span></tt> to avoid conflicts (since this variable
142is used to name the resulting library):</p>
143<pre class="literal-block">
144LLVMC_PLUGIN=MyPlugin
145</pre>
146<p>It is also a good idea to rename <tt class="docutils literal"><span class="pre">Simple.td</span></tt> to something less
147generic:</p>
148<pre class="literal-block">
149$ mv Simple.td MyPlugin.td
150</pre>
151<p>Note that the plugin source directory must be placed under
152<tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins</span></tt> to make use of the existing build
153infrastructure. To build a version of the LLVMC executable called
154<tt class="docutils literal"><span class="pre">mydriver</span></tt> with your plugin compiled in, use the following command:</p>
155<pre class="literal-block">
156$ cd $LLVMC_DIR
157$ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
158</pre>
159<p>To build your plugin as a dynamic library, just <tt class="docutils literal"><span class="pre">cd</span></tt> to its source
160directory and run <tt class="docutils literal"><span class="pre">make</span></tt>. The resulting file will be called
161<tt class="docutils literal"><span class="pre">LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)</span></tt> (in our case,
162<tt class="docutils literal"><span class="pre">LLVMCMyPlugin.so</span></tt>). This library can be then loaded in with the
163<tt class="docutils literal"><span class="pre">-load</span></tt> option. Example:</p>
164<pre class="literal-block">
165$ cd $LLVMC_DIR/plugins/Simple
166$ make
167$ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
168</pre>
169<p>Sometimes, you will want a 'bare-bones' version of LLVMC that has no
170built-in plugins. It can be compiled with the following command:</p>
171<pre class="literal-block">
172$ cd $LLVMC_DIR
173$ make BUILTIN_PLUGINS=&quot;&quot;
174</pre>
175</div>
176<div class="section">
177<h1><a class="toc-backref" href="#id13" id="customizing-llvmc-the-compilation-graph" name="customizing-llvmc-the-compilation-graph">Customizing LLVMC: the compilation graph</a></h1>
178<p>Each TableGen configuration file should include the common
179definitions:</p>
180<pre class="literal-block">
181include &quot;llvm/CompilerDriver/Common.td&quot;
182</pre>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000183<p>Internally, LLVMC stores information about possible source
184transformations in form of a graph. Nodes in this graph represent
185tools, and edges between two nodes represent a transformation path. A
186special &quot;root&quot; node is used to mark entry points for the
187transformations. LLVMC also assigns a weight to each edge (more on
188this later) to choose between several alternative edges.</p>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000189<p>The definition of the compilation graph (see file
190<tt class="docutils literal"><span class="pre">plugins/Base/Base.td</span></tt> for an example) is just a list of edges:</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000191<pre class="literal-block">
192def CompilationGraph : CompilationGraph&lt;[
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000193 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_c&quot;&gt;,
194 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_assembler&quot;&gt;,
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000195 ...
196
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000197 Edge&lt;&quot;llvm_gcc_c&quot;, &quot;llc&quot;&gt;,
198 Edge&lt;&quot;llvm_gcc_cpp&quot;, &quot;llc&quot;&gt;,
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000199 ...
200
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000201 OptionalEdge&lt;&quot;llvm_gcc_c&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
202 (inc_weight))&gt;,
203 OptionalEdge&lt;&quot;llvm_gcc_cpp&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
204 (inc_weight))&gt;,
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000205 ...
206
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000207 OptionalEdge&lt;&quot;llvm_gcc_assembler&quot;, &quot;llvm_gcc_cpp_linker&quot;,
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000208 (case (input_languages_contain &quot;c++&quot;), (inc_weight),
209 (or (parameter_equals &quot;linker&quot;, &quot;g++&quot;),
210 (parameter_equals &quot;linker&quot;, &quot;c++&quot;)), (inc_weight))&gt;,
211 ...
212
213 ]&gt;;
214</pre>
215<p>As you can see, the edges can be either default or optional, where
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000216optional edges are differentiated by an additional <tt class="docutils literal"><span class="pre">case</span></tt> expression
217used to calculate the weight of this edge. Notice also that we refer
218to tools via their names (as strings). This makes it possible to add
219edges to an existing compilation graph in plugins without having to
220know about all tool definitions used in the graph.</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000221<p>The default edges are assigned a weight of 1, and optional edges get a
222weight of 0 + 2*N where N is the number of tests that evaluated to
223true in the <tt class="docutils literal"><span class="pre">case</span></tt> expression. It is also possible to provide an
224integer parameter to <tt class="docutils literal"><span class="pre">inc_weight</span></tt> and <tt class="docutils literal"><span class="pre">dec_weight</span></tt> - in this case,
225the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000226of the default 2. It is also possible to change the default weight of
227an optional edge by using the <tt class="docutils literal"><span class="pre">default</span></tt> clause of the <tt class="docutils literal"><span class="pre">case</span></tt>
228construct.</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000229<p>When passing an input file through the graph, LLVMC picks the edge
230with the maximum weight. To avoid ambiguity, there should be only one
231default edge between two nodes (with the exception of the root node,
232which gets a special treatment - there you are allowed to specify one
233default edge <em>per language</em>).</p>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000234<p>When multiple plugins are loaded, their compilation graphs are merged
235together. Since multiple edges that have the same end nodes are not
236allowed (i.e. the graph is not a multigraph), an edge defined in
237several plugins will be replaced by the definition from the plugin
238that was loaded last. Plugin load order can be controlled by using the
239plugin priority feature described above.</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000240<p>To get a visual representation of the compilation graph (useful for
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000241debugging), run <tt class="docutils literal"><span class="pre">llvmc</span> <span class="pre">--view-graph</span></tt>. You will need <tt class="docutils literal"><span class="pre">dot</span></tt> and
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000242<tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000244<div class="section">
245<h1><a class="toc-backref" href="#id14" id="describing-options" name="describing-options">Describing options</a></h1>
246<p>Command-line options that the plugin supports are defined by using an
247<tt class="docutils literal"><span class="pre">OptionList</span></tt>:</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000248<pre class="literal-block">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000249def Options : OptionList&lt;[
250(switch_option &quot;E&quot;, (help &quot;Help string&quot;)),
251(alias_option &quot;quiet&quot;, &quot;q&quot;)
252...
253]&gt;;
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000254</pre>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000255<p>As you can see, the option list is just a list of DAGs, where each DAG
256is an option description consisting of the option name and some
257properties. A plugin can define more than one option list (they are
258all merged together in the end), which can be handy if one wants to
259separate option groups syntactically.</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000260<ul>
261<li><p class="first">Possible option types:</p>
262<blockquote>
263<ul class="simple">
264<li><tt class="docutils literal"><span class="pre">switch_option</span></tt> - a simple boolean switch, for example <tt class="docutils literal"><span class="pre">-time</span></tt>.</li>
265<li><tt class="docutils literal"><span class="pre">parameter_option</span></tt> - option that takes an argument, for example
266<tt class="docutils literal"><span class="pre">-std=c99</span></tt>;</li>
267<li><tt class="docutils literal"><span class="pre">parameter_list_option</span></tt> - same as the above, but more than one
268occurence of the option is allowed.</li>
269<li><tt class="docutils literal"><span class="pre">prefix_option</span></tt> - same as the parameter_option, but the option name
270and parameter value are not separated.</li>
271<li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one
272occurence of the option is allowed; example: <tt class="docutils literal"><span class="pre">-lm</span> <span class="pre">-lpthread</span></tt>.</li>
273<li><tt class="docutils literal"><span class="pre">alias_option</span></tt> - a special option type for creating
274aliases. Unlike other option types, aliases are not allowed to
275have any properties besides the aliased option name. Usage
276example: <tt class="docutils literal"><span class="pre">(alias_option</span> <span class="pre">&quot;preprocess&quot;,</span> <span class="pre">&quot;E&quot;)</span></tt></li>
277</ul>
278</blockquote>
279</li>
280<li><p class="first">Possible option properties:</p>
281<blockquote>
282<ul class="simple">
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000283<li><tt class="docutils literal"><span class="pre">help</span></tt> - help string associated with this option. Used for
284<tt class="docutils literal"><span class="pre">--help</span></tt> output.</li>
285<li><tt class="docutils literal"><span class="pre">required</span></tt> - this option is obligatory.</li>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000286<li><tt class="docutils literal"><span class="pre">hidden</span></tt> - this option should not appear in the <tt class="docutils literal"><span class="pre">--help</span></tt>
287output (but should appear in the <tt class="docutils literal"><span class="pre">--help-hidden</span></tt> output).</li>
288<li><tt class="docutils literal"><span class="pre">really_hidden</span></tt> - the option should not appear in any help
289output.</li>
290<li><tt class="docutils literal"><span class="pre">extern</span></tt> - this option is defined in some other plugin, see below.</li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000291</ul>
292</blockquote>
293</li>
294</ul>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000295<div class="section">
296<h2><a class="toc-backref" href="#id15" id="external-options" name="external-options">External options</a></h2>
297<p>Sometimes, when linking several plugins together, one plugin needs to
298access options defined in some other plugin. Because of the way
299options are implemented, such options should be marked as
300<tt class="docutils literal"><span class="pre">extern</span></tt>. This is what the <tt class="docutils literal"><span class="pre">extern</span></tt> option property is
301for. Example:</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000302<pre class="literal-block">
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000303...
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000304(switch_option &quot;E&quot;, (extern))
305...
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000306</pre>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000307<p>See also the section on plugin <a class="reference" href="#priorities">priorities</a>.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308</div>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000310<div class="section">
311<h1><a class="toc-backref" href="#id16" id="conditional-evaluation" name="conditional-evaluation"><span id="case"></span>Conditional evaluation</a></h1>
312<p>The 'case' construct is the main means by which programmability is
313achieved in LLVMC. It can be used to calculate edge weights, program
314actions and modify the shell commands to be executed. The 'case'
315expression is designed after the similarly-named construct in
316functional languages and takes the form <tt class="docutils literal"><span class="pre">(case</span> <span class="pre">(test_1),</span> <span class="pre">statement_1,</span>
317<span class="pre">(test_2),</span> <span class="pre">statement_2,</span> <span class="pre">...</span> <span class="pre">(test_N),</span> <span class="pre">statement_N)</span></tt>. The statements
318are evaluated only if the corresponding tests evaluate to true.</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000319<p>Examples:</p>
320<pre class="literal-block">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000321// Edge weight calculation
322
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000323// Increases edge weight by 5 if &quot;-A&quot; is provided on the
324// command-line, and by 5 more if &quot;-B&quot; is also provided.
325(case
326 (switch_on &quot;A&quot;), (inc_weight 5),
327 (switch_on &quot;B&quot;), (inc_weight 5))
328
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000329
330// Tool command line specification
331
332// Evaluates to &quot;cmdline1&quot; if the option &quot;-A&quot; is provided on the
333// command line; to &quot;cmdline2&quot; if &quot;-B&quot; is provided;
334// otherwise to &quot;cmdline3&quot;.
335
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000336(case
337 (switch_on &quot;A&quot;), &quot;cmdline1&quot;,
338 (switch_on &quot;B&quot;), &quot;cmdline2&quot;,
339 (default), &quot;cmdline3&quot;)
340</pre>
341<p>Note the slight difference in 'case' expression handling in contexts
342of edge weights and command line specification - in the second example
343the value of the <tt class="docutils literal"><span class="pre">&quot;B&quot;</span></tt> switch is never checked when switch <tt class="docutils literal"><span class="pre">&quot;A&quot;</span></tt> is
344enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">&quot;cmdline1&quot;</span></tt> in
345that case.</p>
346<p>Case expressions can also be nested, i.e. the following is legal:</p>
347<pre class="literal-block">
348(case (switch_on &quot;E&quot;), (case (switch_on &quot;o&quot;), ..., (default), ...)
349 (default), ...)
350</pre>
351<p>You should, however, try to avoid doing that because it hurts
352readability. It is usually better to split tool descriptions and/or
353use TableGen inheritance instead.</p>
354<ul class="simple">
355<li>Possible tests are:<ul>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000356<li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is
357provided by the user. Example: <tt class="docutils literal"><span class="pre">(switch_on</span> <span class="pre">&quot;opt&quot;)</span></tt>.</li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000358<li><tt class="docutils literal"><span class="pre">parameter_equals</span></tt> - Returns true if a command-line parameter equals
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000359a given value.
360Example: <tt class="docutils literal"><span class="pre">(parameter_equals</span> <span class="pre">&quot;W&quot;,</span> <span class="pre">&quot;all&quot;)</span></tt>.</li>
361<li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter
362list contains a given value.
363Example: <tt class="docutils literal"><span class="pre">(parameter_in_list</span> <span class="pre">&quot;l&quot;,</span> <span class="pre">&quot;pthread&quot;)</span></tt>.</li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000364<li><tt class="docutils literal"><span class="pre">input_languages_contain</span></tt> - Returns true if a given language
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000365belongs to the current input language set.
366Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
367<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language
368equals to the argument. At the moment works only with <tt class="docutils literal"><span class="pre">cmd_line</span></tt>
369and <tt class="docutils literal"><span class="pre">actions</span></tt> (on non-join nodes).
370Example: <tt class="docutils literal"><span class="pre">(in_language</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000371<li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be
372either a parameter or a parameter list) is set by the
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000373user.
374Example: <tt class="docutils literal"><span class="pre">(not_empty</span> <span class="pre">&quot;o&quot;)</span></tt>.</li>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000375<li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last
376test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li>
377<li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard logical combinator that returns true iff all
378of its arguments return true. Used like this: <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(test1),</span>
379<span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>. Nesting of <tt class="docutils literal"><span class="pre">and</span></tt> and <tt class="docutils literal"><span class="pre">or</span></tt> is allowed,
380but not encouraged.</li>
381<li><tt class="docutils literal"><span class="pre">or</span></tt> - Another logical combinator that returns true only if any
382one of its arguments returns true. Example: <tt class="docutils literal"><span class="pre">(or</span> <span class="pre">(test1),</span>
383<span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>.</li>
384</ul>
385</li>
386</ul>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000388<div class="section">
389<h1><a class="toc-backref" href="#id17" id="writing-a-tool-description" name="writing-a-tool-description">Writing a tool description</a></h1>
390<p>As was said earlier, nodes in the compilation graph represent tools,
391which are described separately. A tool definition looks like this
392(taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p>
393<pre class="literal-block">
394def llvm_gcc_cpp : Tool&lt;[
395 (in_language &quot;c++&quot;),
396 (out_language &quot;llvm-assembler&quot;),
397 (output_suffix &quot;bc&quot;),
398 (cmd_line &quot;llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm&quot;),
399 (sink)
400 ]&gt;;
401</pre>
402<p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for
403<tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of
404properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt>
405property means that this tool should be passed all command-line
406options that aren't mentioned in the option list.</p>
407<p>The complete list of all currently implemented tool properties follows.</p>
408<ul class="simple">
409<li>Possible tool properties:<ul>
410<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a
411list, in case the tool supports multiple input languages.</li>
412<li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Tools are not allowed to
413have multiple output languages.</li>
414<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed
415dynamically, see documentation on actions.</li>
416<li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
417use <tt class="docutils literal"><span class="pre">$INFILE</span></tt> and <tt class="docutils literal"><span class="pre">$OUTFILE</span></tt> variables, output redirection
418with <tt class="docutils literal"><span class="pre">&gt;</span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables
419(via <tt class="docutils literal"><span class="pre">$ENV</span></tt>) and the <tt class="docutils literal"><span class="pre">case</span></tt> construct.</li>
420<li><tt class="docutils literal"><span class="pre">join</span></tt> - this tool is a &quot;join node&quot; in the graph, i.e. it gets a
421list of input files and joins them together. Used for linkers.</li>
422<li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other
423tools are passed to this tool.</li>
424<li><tt class="docutils literal"><span class="pre">actions</span></tt> - A single big <tt class="docutils literal"><span class="pre">case</span></tt> expression that specifies how
425this tool reacts on command-line options (described in more detail
426below).</li>
427</ul>
428</li>
429</ul>
430<div class="section">
431<h2><a class="toc-backref" href="#id18" id="actions" name="actions">Actions</a></h2>
432<p>A tool often needs to react to command-line options, and this is
433precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example
434illustrates this feature:</p>
435<pre class="literal-block">
436def llvm_gcc_linker : Tool&lt;[
437 (in_language &quot;object-code&quot;),
438 (out_language &quot;executable&quot;),
439 (output_suffix &quot;out&quot;),
440 (cmd_line &quot;llvm-gcc $INFILE -o $OUTFILE&quot;),
441 (join),
442 (actions (case (not_empty &quot;L&quot;), (forward &quot;L&quot;),
443 (not_empty &quot;l&quot;), (forward &quot;l&quot;),
444 (not_empty &quot;dummy&quot;),
445 [(append_cmd &quot;-dummy1&quot;), (append_cmd &quot;-dummy2&quot;)])
446 ]&gt;;
447</pre>
448<p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent
449<tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em>
450with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>,
451which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which
452appends a given string to the tool execution command. Multiple actions
453can be associated with a single condition by using a list of actions
454(used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt>
455construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the
456tool command line.</p>
457<p>The &quot;join&quot; property used in the example means that this tool behaves
458like a linker.</p>
459<p>The list of all possible actions follows.</p>
460<ul>
461<li><p class="first">Possible actions:</p>
462<blockquote>
463<ul class="simple">
464<li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - append a string to the tool invocation
465command.
466Example: <tt class="docutils literal"><span class="pre">(case</span> <span class="pre">(switch_on</span> <span class="pre">&quot;pthread&quot;),</span> <span class="pre">(append_cmd</span> <span class="pre">&quot;-lpthread&quot;))</span></tt></li>
467<li><tt class="docutils literal"><span class="pre">forward</span></tt> - forward an option unchanged.
468Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">&quot;Wall&quot;)</span></tt>.</li>
469<li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the name of an option, but forward the
470argument unchanged.
471Example: <tt class="docutils literal"><span class="pre">(forward_as</span> <span class="pre">&quot;O0&quot;</span> <span class="pre">&quot;--disable-optimization&quot;)</span></tt>.</li>
472<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - modify the output suffix of this
473tool.
474Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">&quot;i&quot;)</span></tt>.</li>
475<li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - stop compilation after this tool processes
476its input. Used without arguments.</li>
477<li><tt class="docutils literal"><span class="pre">unpack_values</span></tt> - used for for splitting and forwarding
478comma-separated lists of options, e.g. <tt class="docutils literal"><span class="pre">-Wa,-foo=bar,-baz</span></tt> is
479converted to <tt class="docutils literal"><span class="pre">-foo=bar</span> <span class="pre">-baz</span></tt> and appended to the tool invocation
480command.
481Example: <tt class="docutils literal"><span class="pre">(unpack_values</span> <span class="pre">&quot;Wa,&quot;)</span></tt>.</li>
482</ul>
483</blockquote>
484</li>
485</ul>
486</div>
487</div>
488<div class="section">
489<h1><a class="toc-backref" href="#id19" id="language-map" name="language-map">Language map</a></h1>
490<p>If you are adding support for a new language to LLVMC, you'll need to
491modify the language map, which defines mappings from file extensions
492to language names. It is used to choose the proper toolchain(s) for a
493given input file set. Language map definition looks like this:</p>
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000494<pre class="literal-block">
495def LanguageMap : LanguageMap&lt;
496 [LangToSuffixes&lt;&quot;c++&quot;, [&quot;cc&quot;, &quot;cp&quot;, &quot;cxx&quot;, &quot;cpp&quot;, &quot;CPP&quot;, &quot;c++&quot;, &quot;C&quot;]&gt;,
497 LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;,
498 ...
499 ]&gt;;
500</pre>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000501<p>For example, without those definitions the following command wouldn't work:</p>
502<pre class="literal-block">
503$ llvmc hello.cpp
504llvmc: Unknown suffix: cpp
505</pre>
506<p>The language map entries should be added only for tools that are
507linked with the root node. Since tools are not allowed to have
508multiple output languages, for nodes &quot;inside&quot; the graph the input and
509output languages should match. This is enforced at compile-time.</p>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510</div>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000511<div class="section">
512<h1><a class="toc-backref" href="#id20" id="more-advanced-topics" name="more-advanced-topics">More advanced topics</a></h1>
513<div class="section">
514<h2><a class="toc-backref" href="#id21" id="hooks-and-environment-variables" name="hooks-and-environment-variables"><span id="hooks"></span>Hooks and environment variables</a></h2>
515<p>Normally, LLVMC executes programs from the system <tt class="docutils literal"><span class="pre">PATH</span></tt>. Sometimes,
516this is not sufficient: for example, we may want to specify tool names
517in the configuration file. This can be achieved via the mechanism of
518hooks - to write your own hooks, just add their definitions to the
519<tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> or drop a <tt class="docutils literal"><span class="pre">.cpp</span></tt> file into the
520<tt class="docutils literal"><span class="pre">$LLVMC_DIR/driver</span></tt> directory. Hooks should live in the <tt class="docutils literal"><span class="pre">hooks</span></tt>
521namespace and have the signature <tt class="docutils literal"><span class="pre">std::string</span> <span class="pre">hooks::MyHookName</span>
522<span class="pre">(void)</span></tt>. They can be used from the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> tool property:</p>
523<pre class="literal-block">
524(cmd_line &quot;$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)&quot;)
525</pre>
526<p>It is also possible to use environment variables in the same manner:</p>
527<pre class="literal-block">
528(cmd_line &quot;$ENV(VAR1)/path/to/file -o $ENV(VAR2)&quot;)
529</pre>
530<p>To change the command line string based on user-provided options use
531the <tt class="docutils literal"><span class="pre">case</span></tt> expression (documented <a class="reference" href="#case">above</a>):</p>
532<pre class="literal-block">
533(cmd_line
534 (case
535 (switch_on &quot;E&quot;),
536 &quot;llvm-g++ -E -x c $INFILE -o $OUTFILE&quot;,
537 (default),
538 &quot;llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm&quot;))
539</pre>
540</div>
541<div class="section">
542<h2><a class="toc-backref" href="#id22" id="how-plugins-are-loaded" name="how-plugins-are-loaded"><span id="priorities"></span>How plugins are loaded</a></h2>
543<p>It is possible for LLVMC plugins to depend on each other. For example,
544one can create edges between nodes defined in some other plugin. To
545make this work, however, that plugin should be loaded first. To
546achieve this, the concept of plugin priority was introduced. By
547default, every plugin has priority zero; to specify the priority
548explicitly, put the following line in your plugin's TableGen file:</p>
549<pre class="literal-block">
550def Priority : PluginPriority&lt;$PRIORITY_VALUE&gt;;
551# Where PRIORITY_VALUE is some integer &gt; 0
552</pre>
553<p>Plugins are loaded in order of their (increasing) priority, starting
554with 0. Therefore, the plugin with the highest priority value will be
555loaded last.</p>
556</div>
557<div class="section">
558<h2><a class="toc-backref" href="#id23" id="debugging" name="debugging">Debugging</a></h2>
559<p>When writing LLVMC plugins, it can be useful to get a visual view of
560the resulting compilation graph. This can be achieved via the command
561line option <tt class="docutils literal"><span class="pre">--view-graph</span></tt>. This command assumes that Graphviz <a class="footnote-reference" href="#id8" id="id5" name="id5">[2]</a> and
562Ghostview <a class="footnote-reference" href="#id9" id="id6" name="id6">[3]</a> are installed. There is also a <tt class="docutils literal"><span class="pre">--dump-graph</span></tt> option that
563creates a Graphviz source file(<tt class="docutils literal"><span class="pre">compilation-graph.dot</span></tt>) in the
564current directory.</p>
565</div>
566</div>
567<div class="section">
568<h1><a class="toc-backref" href="#id24" id="references" name="references">References</a></h1>
569<table class="docutils footnote" frame="void" id="id7" rules="none">
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000570<colgroup><col class="label" /><col /></colgroup>
571<tbody valign="top">
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000572<tr><td class="label"><a class="fn-backref" href="#id1" name="id7">[1]</a></td><td>TableGen Fundamentals
Anton Korobeynikove31f1472008-06-09 04:15:49 +0000573<a class="reference" href="http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html">http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html</a></td></tr>
574</tbody>
575</table>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000576<table class="docutils footnote" frame="void" id="id8" rules="none">
577<colgroup><col class="label" /><col /></colgroup>
578<tbody valign="top">
579<tr><td class="label"><a class="fn-backref" href="#id5" name="id8">[2]</a></td><td>Graphviz
580<a class="reference" href="http://www.graphviz.org/">http://www.graphviz.org/</a></td></tr>
581</tbody>
582</table>
583<table class="docutils footnote" frame="void" id="id9" rules="none">
584<colgroup><col class="label" /><col /></colgroup>
585<tbody valign="top">
586<tr><td class="label"><a class="fn-backref" href="#id6" name="id9">[3]</a></td><td>Ghostview
587<a class="reference" href="http://pages.cs.wisc.edu/~ghost/">http://pages.cs.wisc.edu/~ghost/</a></td></tr>
588</tbody>
589</table>
590<hr>
Anton Korobeynikovf8a29d32008-06-09 04:17:51 +0000591<address>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000592 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS"></a>
593 <a href="http://validator.w3.org/check/referer"><img src="http://www.w3.org/Icons/valid-html401-blue" alt="Valid HTML 4.01"></a>
594
595 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br>
596 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br>
597
598 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
Anton Korobeynikovf8a29d32008-06-09 04:17:51 +0000599</address>
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000600</div>
601</div>
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000602</body>
603</html>