blob: ccd23ebc7331194449d7931ab01b702c930fa8d6 [file] [log] [blame]
Anton Korobeynikov74120312008-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">
Reid Spencerb1254a12004-08-09 03:08:29 +00004<head>
Anton Korobeynikov74120312008-06-09 04:15:49 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Mikhail Glushenkov68319f82008-12-11 23:24:40 +00006<meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
Anton Korobeynikov74120312008-06-09 04:15:49 +00007<title>Customizing LLVMC: Reference Manual</title>
Mikhail Glushenkov8cc82882008-12-13 17:50:58 +00008<link rel="stylesheet" href="llvm.css" type="text/css" />
Reid Spencerb1254a12004-08-09 03:08:29 +00009</head>
10<body>
Anton Korobeynikov74120312008-06-09 04:15:49 +000011<div class="document" id="customizing-llvmc-reference-manual">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000012<h1 class="title">Customizing LLVMC: Reference Manual</h1>
Mikhail Glushenkov23f522a2008-12-13 17:51:47 +000013<!-- This file was automatically generated by rst2html.
14Please do not edit directly!
15The ReST source lives in the directory 'tools/llvmc/doc'. -->
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000016<div class="contents topic">
17<p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
18<ul class="simple">
19<li><a class="reference" href="#introduction" id="id4" name="id4">Introduction</a></li>
20<li><a class="reference" href="#compiling-with-llvmc" id="id5" name="id5">Compiling with LLVMC</a></li>
21<li><a class="reference" href="#predefined-options" id="id6" name="id6">Predefined options</a></li>
22<li><a class="reference" href="#compiling-llvmc-plugins" id="id7" name="id7">Compiling LLVMC plugins</a></li>
23<li><a class="reference" href="#customizing-llvmc-the-compilation-graph" id="id8" name="id8">Customizing LLVMC: the compilation graph</a></li>
24<li><a class="reference" href="#describing-options" id="id9" name="id9">Describing options</a><ul>
25<li><a class="reference" href="#external-options" id="id10" name="id10">External options</a></li>
26</ul>
27</li>
28<li><a class="reference" href="#conditional-evaluation" id="id11" name="id11">Conditional evaluation</a></li>
29<li><a class="reference" href="#writing-a-tool-description" id="id12" name="id12">Writing a tool description</a><ul>
30<li><a class="reference" href="#actions" id="id13" name="id13">Actions</a></li>
31</ul>
32</li>
33<li><a class="reference" href="#language-map" id="id14" name="id14">Language map</a></li>
34<li><a class="reference" href="#more-advanced-topics" id="id15" name="id15">More advanced topics</a><ul>
35<li><a class="reference" href="#hooks-and-environment-variables" id="id16" name="id16">Hooks and environment variables</a></li>
36<li><a class="reference" href="#how-plugins-are-loaded" id="id17" name="id17">How plugins are loaded</a></li>
37<li><a class="reference" href="#debugging" id="id18" name="id18">Debugging</a></li>
38</ul>
39</li>
40</ul>
41</div>
42<div class="doc_author">
43<p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
44</div><div class="section">
45<h1><a class="toc-backref" href="#id4" id="introduction" name="introduction">Introduction</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000046<p>LLVMC is a generic compiler driver, designed to be customizable and
47extensible. It plays the same role for LLVM as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program
48does for GCC - LLVMC's job is essentially to transform a set of input
49files into a set of targets depending on configuration rules and user
50options. What makes LLVMC different is that these transformation rules
51are completely customizable - in fact, LLVMC knows nothing about the
52specifics of transformation (even the command-line options are mostly
53not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000054abstract graph. The structure of this graph is completely determined
55by plugins, which can be either statically or dynamically linked. This
56makes it possible to easily adapt LLVMC for other purposes - for
57example, as a build tool for game resources.</p>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000058<p>Because LLVMC employs <a class="reference" href="http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html">TableGen</a> as its configuration language, you
Anton Korobeynikov74120312008-06-09 04:15:49 +000059need to be familiar with it to customize LLVMC.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +000060</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000061<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000062<h1><a class="toc-backref" href="#id5" id="compiling-with-llvmc" name="compiling-with-llvmc">Compiling with LLVMC</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000063<p>LLVMC tries hard to be as compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as possible,
64although there are some small differences. Most of the time, however,
65you shouldn't be able to notice them:</p>
66<pre class="literal-block">
67$ # This works as expected:
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000068$ llvmc -O3 -Wall hello.cpp
Anton Korobeynikov74120312008-06-09 04:15:49 +000069$ ./a.out
70hello
71</pre>
72<p>One nice feature of LLVMC is that one doesn't have to distinguish
73between different compilers for different languages (think <tt class="docutils literal"><span class="pre">g++</span></tt> and
74<tt class="docutils literal"><span class="pre">gcc</span></tt>) - the right toolchain is chosen automatically based on input
75language names (which are, in turn, determined from file
76extensions). If you want to force files ending with &quot;.c&quot; to compile as
77C++, 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>
78<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000079$ # hello.c is really a C++ file
80$ llvmc -x c++ hello.c
Anton Korobeynikov74120312008-06-09 04:15:49 +000081$ ./a.out
82hello
83</pre>
84<p>On the other hand, when using LLVMC as a linker to combine several C++
85object files you should provide the <tt class="docutils literal"><span class="pre">--linker</span></tt> option since it's
86impossible for LLVMC to choose the right linker in that case:</p>
87<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000088$ llvmc -c hello.cpp
89$ llvmc hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000090[A lot of link-time errors skipped]
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000091$ llvmc --linker=c++ hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000092$ ./a.out
93hello
94</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000095<p>By default, LLVMC uses <tt class="docutils literal"><span class="pre">llvm-gcc</span></tt> to compile the source code. It is
96also possible to choose the work-in-progress <tt class="docutils literal"><span class="pre">clang</span></tt> compiler with
97the <tt class="docutils literal"><span class="pre">-clang</span></tt> option.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +000098</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000099<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000100<h1><a class="toc-backref" href="#id6" id="predefined-options" name="predefined-options">Predefined options</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000101<p>LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000102configuration libraries:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000103<ul class="simple">
104<li><tt class="docutils literal"><span class="pre">-o</span> <span class="pre">FILE</span></tt> - Output file name.</li>
105<li><tt class="docutils literal"><span class="pre">-x</span> <span class="pre">LANGUAGE</span></tt> - Specify the language of the following input files
106until the next -x option.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000107<li><tt class="docutils literal"><span class="pre">-load</span> <span class="pre">PLUGIN_NAME</span></tt> - Load the specified plugin DLL. Example:
108<tt class="docutils literal"><span class="pre">-load</span> <span class="pre">$LLVM_DIR/Release/lib/LLVMCSimple.so</span></tt>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000109<li><tt class="docutils literal"><span class="pre">-v</span></tt> - Enable verbose mode, i.e. print out all executed commands.</li>
110<li><tt class="docutils literal"><span class="pre">--view-graph</span></tt> - Show a graphical representation of the compilation
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000111graph. 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 Korobeynikov74120312008-06-09 04:15:49 +0000112installed. Hidden option, useful for debugging.</li>
113<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
114current directory with the compilation graph description in the
115Graphviz format. Hidden option, useful for debugging.</li>
116<li><tt class="docutils literal"><span class="pre">--save-temps</span></tt> - Write temporary files to the current directory
117and do not delete them on exit. Hidden option, useful for debugging.</li>
118<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
119their standard meaning.</li>
120</ul>
Reid Spencerb1254a12004-08-09 03:08:29 +0000121</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000122<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000123<h1><a class="toc-backref" href="#id7" id="compiling-llvmc-plugins" name="compiling-llvmc-plugins">Compiling LLVMC plugins</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000124<p>It's easiest to start working on your own LLVMC plugin by copying the
125skeleton project which lives under <tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins/Simple</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000126<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000127$ cd $LLVMC_DIR/plugins
128$ cp -r Simple MyPlugin
129$ cd MyPlugin
130$ ls
131Makefile PluginMain.cpp Simple.td
Anton Korobeynikov74120312008-06-09 04:15:49 +0000132</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000133<p>As you can see, our basic plugin consists of only two files (not
134counting the build script). <tt class="docutils literal"><span class="pre">Simple.td</span></tt> contains TableGen
135description of the compilation graph; its format is documented in the
136following sections. <tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> is just a helper file used to
137compile the auto-generated C++ code produced from TableGen source. It
138can also contain hook definitions (see <a class="reference" href="#hooks">below</a>).</p>
139<p>The first thing that you should do is to change the <tt class="docutils literal"><span class="pre">LLVMC_PLUGIN</span></tt>
140variable in the <tt class="docutils literal"><span class="pre">Makefile</span></tt> to avoid conflicts (since this variable
141is used to name the resulting library):</p>
142<pre class="literal-block">
143LLVMC_PLUGIN=MyPlugin
144</pre>
145<p>It is also a good idea to rename <tt class="docutils literal"><span class="pre">Simple.td</span></tt> to something less
146generic:</p>
147<pre class="literal-block">
148$ mv Simple.td MyPlugin.td
149</pre>
150<p>Note that the plugin source directory must be placed under
151<tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins</span></tt> to make use of the existing build
152infrastructure. To build a version of the LLVMC executable called
153<tt class="docutils literal"><span class="pre">mydriver</span></tt> with your plugin compiled in, use the following command:</p>
154<pre class="literal-block">
155$ cd $LLVMC_DIR
156$ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
157</pre>
158<p>To build your plugin as a dynamic library, just <tt class="docutils literal"><span class="pre">cd</span></tt> to its source
159directory and run <tt class="docutils literal"><span class="pre">make</span></tt>. The resulting file will be called
160<tt class="docutils literal"><span class="pre">LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)</span></tt> (in our case,
161<tt class="docutils literal"><span class="pre">LLVMCMyPlugin.so</span></tt>). This library can be then loaded in with the
162<tt class="docutils literal"><span class="pre">-load</span></tt> option. Example:</p>
163<pre class="literal-block">
164$ cd $LLVMC_DIR/plugins/Simple
165$ make
166$ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
167</pre>
168<p>Sometimes, you will want a 'bare-bones' version of LLVMC that has no
169built-in plugins. It can be compiled with the following command:</p>
170<pre class="literal-block">
171$ cd $LLVMC_DIR
172$ make BUILTIN_PLUGINS=&quot;&quot;
173</pre>
174</div>
175<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000176<h1><a class="toc-backref" href="#id8" id="customizing-llvmc-the-compilation-graph" name="customizing-llvmc-the-compilation-graph">Customizing LLVMC: the compilation graph</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000177<p>Each TableGen configuration file should include the common
178definitions:</p>
179<pre class="literal-block">
180include &quot;llvm/CompilerDriver/Common.td&quot;
181</pre>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000182<p>Internally, LLVMC stores information about possible source
183transformations in form of a graph. Nodes in this graph represent
184tools, and edges between two nodes represent a transformation path. A
185special &quot;root&quot; node is used to mark entry points for the
186transformations. LLVMC also assigns a weight to each edge (more on
187this later) to choose between several alternative edges.</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000188<p>The definition of the compilation graph (see file
189<tt class="docutils literal"><span class="pre">plugins/Base/Base.td</span></tt> for an example) is just a list of edges:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000190<pre class="literal-block">
191def CompilationGraph : CompilationGraph&lt;[
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000192 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_c&quot;&gt;,
193 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_assembler&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000194 ...
195
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000196 Edge&lt;&quot;llvm_gcc_c&quot;, &quot;llc&quot;&gt;,
197 Edge&lt;&quot;llvm_gcc_cpp&quot;, &quot;llc&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000198 ...
199
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000200 OptionalEdge&lt;&quot;llvm_gcc_c&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
201 (inc_weight))&gt;,
202 OptionalEdge&lt;&quot;llvm_gcc_cpp&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
203 (inc_weight))&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000204 ...
205
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000206 OptionalEdge&lt;&quot;llvm_gcc_assembler&quot;, &quot;llvm_gcc_cpp_linker&quot;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000207 (case (input_languages_contain &quot;c++&quot;), (inc_weight),
208 (or (parameter_equals &quot;linker&quot;, &quot;g++&quot;),
209 (parameter_equals &quot;linker&quot;, &quot;c++&quot;)), (inc_weight))&gt;,
210 ...
211
212 ]&gt;;
213</pre>
214<p>As you can see, the edges can be either default or optional, where
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000215optional edges are differentiated by an additional <tt class="docutils literal"><span class="pre">case</span></tt> expression
216used to calculate the weight of this edge. Notice also that we refer
217to tools via their names (as strings). This makes it possible to add
218edges to an existing compilation graph in plugins without having to
219know about all tool definitions used in the graph.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000220<p>The default edges are assigned a weight of 1, and optional edges get a
221weight of 0 + 2*N where N is the number of tests that evaluated to
222true in the <tt class="docutils literal"><span class="pre">case</span></tt> expression. It is also possible to provide an
223integer 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,
224the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000225of the default 2. It is also possible to change the default weight of
226an 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>
227construct.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000228<p>When passing an input file through the graph, LLVMC picks the edge
229with the maximum weight. To avoid ambiguity, there should be only one
230default edge between two nodes (with the exception of the root node,
231which gets a special treatment - there you are allowed to specify one
232default edge <em>per language</em>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000233<p>When multiple plugins are loaded, their compilation graphs are merged
234together. Since multiple edges that have the same end nodes are not
235allowed (i.e. the graph is not a multigraph), an edge defined in
236several plugins will be replaced by the definition from the plugin
237that was loaded last. Plugin load order can be controlled by using the
238plugin priority feature described above.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000239<p>To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000240debugging), 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 Korobeynikov74120312008-06-09 04:15:49 +0000241<tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000242</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000243<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000244<h1><a class="toc-backref" href="#id9" id="describing-options" name="describing-options">Describing options</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000245<p>Command-line options that the plugin supports are defined by using an
246<tt class="docutils literal"><span class="pre">OptionList</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000247<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000248def Options : OptionList&lt;[
249(switch_option &quot;E&quot;, (help &quot;Help string&quot;)),
250(alias_option &quot;quiet&quot;, &quot;q&quot;)
251...
252]&gt;;
Anton Korobeynikov74120312008-06-09 04:15:49 +0000253</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000254<p>As you can see, the option list is just a list of DAGs, where each DAG
255is an option description consisting of the option name and some
256properties. A plugin can define more than one option list (they are
257all merged together in the end), which can be handy if one wants to
258separate option groups syntactically.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000259<ul>
260<li><p class="first">Possible option types:</p>
261<blockquote>
262<ul class="simple">
263<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>
264<li><tt class="docutils literal"><span class="pre">parameter_option</span></tt> - option that takes an argument, for example
265<tt class="docutils literal"><span class="pre">-std=c99</span></tt>;</li>
266<li><tt class="docutils literal"><span class="pre">parameter_list_option</span></tt> - same as the above, but more than one
267occurence of the option is allowed.</li>
268<li><tt class="docutils literal"><span class="pre">prefix_option</span></tt> - same as the parameter_option, but the option name
269and parameter value are not separated.</li>
270<li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one
271occurence of the option is allowed; example: <tt class="docutils literal"><span class="pre">-lm</span> <span class="pre">-lpthread</span></tt>.</li>
272<li><tt class="docutils literal"><span class="pre">alias_option</span></tt> - a special option type for creating
273aliases. Unlike other option types, aliases are not allowed to
274have any properties besides the aliased option name. Usage
275example: <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>
276</ul>
277</blockquote>
278</li>
279<li><p class="first">Possible option properties:</p>
280<blockquote>
281<ul class="simple">
Anton Korobeynikov74120312008-06-09 04:15:49 +0000282<li><tt class="docutils literal"><span class="pre">help</span></tt> - help string associated with this option. Used for
283<tt class="docutils literal"><span class="pre">--help</span></tt> output.</li>
284<li><tt class="docutils literal"><span class="pre">required</span></tt> - this option is obligatory.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000285<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>
286output (but should appear in the <tt class="docutils literal"><span class="pre">--help-hidden</span></tt> output).</li>
287<li><tt class="docutils literal"><span class="pre">really_hidden</span></tt> - the option should not appear in any help
288output.</li>
289<li><tt class="docutils literal"><span class="pre">extern</span></tt> - this option is defined in some other plugin, see below.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000290</ul>
291</blockquote>
292</li>
293</ul>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000294<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000295<h2><a class="toc-backref" href="#id10" id="external-options" name="external-options">External options</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000296<p>Sometimes, when linking several plugins together, one plugin needs to
297access options defined in some other plugin. Because of the way
298options are implemented, such options should be marked as
299<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
300for. Example:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000301<pre class="literal-block">
Anton Korobeynikov74120312008-06-09 04:15:49 +0000302...
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000303(switch_option &quot;E&quot;, (extern))
304...
Anton Korobeynikov74120312008-06-09 04:15:49 +0000305</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000306<p>See also the section on plugin <a class="reference" href="#priorities">priorities</a>.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000307</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000308</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000309<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000310<h1><a class="toc-backref" href="#id11" id="conditional-evaluation" name="conditional-evaluation"><span id="case"></span>Conditional evaluation</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000311<p>The 'case' construct is the main means by which programmability is
312achieved in LLVMC. It can be used to calculate edge weights, program
313actions and modify the shell commands to be executed. The 'case'
314expression is designed after the similarly-named construct in
315functional 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>
316<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
317are evaluated only if the corresponding tests evaluate to true.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000318<p>Examples:</p>
319<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000320// Edge weight calculation
321
Anton Korobeynikov74120312008-06-09 04:15:49 +0000322// Increases edge weight by 5 if &quot;-A&quot; is provided on the
323// command-line, and by 5 more if &quot;-B&quot; is also provided.
324(case
325 (switch_on &quot;A&quot;), (inc_weight 5),
326 (switch_on &quot;B&quot;), (inc_weight 5))
327
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000328
329// Tool command line specification
330
331// Evaluates to &quot;cmdline1&quot; if the option &quot;-A&quot; is provided on the
332// command line; to &quot;cmdline2&quot; if &quot;-B&quot; is provided;
333// otherwise to &quot;cmdline3&quot;.
334
Anton Korobeynikov74120312008-06-09 04:15:49 +0000335(case
336 (switch_on &quot;A&quot;), &quot;cmdline1&quot;,
337 (switch_on &quot;B&quot;), &quot;cmdline2&quot;,
338 (default), &quot;cmdline3&quot;)
339</pre>
340<p>Note the slight difference in 'case' expression handling in contexts
341of edge weights and command line specification - in the second example
342the 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
343enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">&quot;cmdline1&quot;</span></tt> in
344that case.</p>
345<p>Case expressions can also be nested, i.e. the following is legal:</p>
346<pre class="literal-block">
347(case (switch_on &quot;E&quot;), (case (switch_on &quot;o&quot;), ..., (default), ...)
348 (default), ...)
349</pre>
350<p>You should, however, try to avoid doing that because it hurts
351readability. It is usually better to split tool descriptions and/or
352use TableGen inheritance instead.</p>
353<ul class="simple">
354<li>Possible tests are:<ul>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000355<li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is
356provided by the user. Example: <tt class="docutils literal"><span class="pre">(switch_on</span> <span class="pre">&quot;opt&quot;)</span></tt>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000357<li><tt class="docutils literal"><span class="pre">parameter_equals</span></tt> - Returns true if a command-line parameter equals
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000358a given value.
359Example: <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>
360<li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter
361list contains a given value.
362Example: <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 Korobeynikov74120312008-06-09 04:15:49 +0000363<li><tt class="docutils literal"><span class="pre">input_languages_contain</span></tt> - Returns true if a given language
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000364belongs to the current input language set.
365Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
366<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language
367equals to the argument. At the moment works only with <tt class="docutils literal"><span class="pre">cmd_line</span></tt>
368and <tt class="docutils literal"><span class="pre">actions</span></tt> (on non-join nodes).
369Example: <tt class="docutils literal"><span class="pre">(in_language</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000370<li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be
371either a parameter or a parameter list) is set by the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000372user.
373Example: <tt class="docutils literal"><span class="pre">(not_empty</span> <span class="pre">&quot;o&quot;)</span></tt>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000374<li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last
375test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li>
376<li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard logical combinator that returns true iff all
377of its arguments return true. Used like this: <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(test1),</span>
378<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,
379but not encouraged.</li>
380<li><tt class="docutils literal"><span class="pre">or</span></tt> - Another logical combinator that returns true only if any
381one of its arguments returns true. Example: <tt class="docutils literal"><span class="pre">(or</span> <span class="pre">(test1),</span>
382<span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>.</li>
383</ul>
384</li>
385</ul>
Reid Spencer46d21922004-08-22 18:06:59 +0000386</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000387<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000388<h1><a class="toc-backref" href="#id12" id="writing-a-tool-description" name="writing-a-tool-description">Writing a tool description</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000389<p>As was said earlier, nodes in the compilation graph represent tools,
390which are described separately. A tool definition looks like this
391(taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p>
392<pre class="literal-block">
393def llvm_gcc_cpp : Tool&lt;[
394 (in_language &quot;c++&quot;),
395 (out_language &quot;llvm-assembler&quot;),
396 (output_suffix &quot;bc&quot;),
397 (cmd_line &quot;llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm&quot;),
398 (sink)
399 ]&gt;;
400</pre>
401<p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for
402<tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of
403properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt>
404property means that this tool should be passed all command-line
405options that aren't mentioned in the option list.</p>
406<p>The complete list of all currently implemented tool properties follows.</p>
407<ul class="simple">
408<li>Possible tool properties:<ul>
409<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a
410list, in case the tool supports multiple input languages.</li>
411<li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Tools are not allowed to
412have multiple output languages.</li>
413<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed
414dynamically, see documentation on actions.</li>
415<li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
416use <tt class="docutils literal"><span class="pre">$INFILE</span></tt> and <tt class="docutils literal"><span class="pre">$OUTFILE</span></tt> variables, output redirection
417with <tt class="docutils literal"><span class="pre">&gt;</span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables
418(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>
419<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
420list of input files and joins them together. Used for linkers.</li>
421<li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other
422tools are passed to this tool.</li>
423<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
424this tool reacts on command-line options (described in more detail
425below).</li>
426</ul>
427</li>
428</ul>
429<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000430<h2><a class="toc-backref" href="#id13" id="actions" name="actions">Actions</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000431<p>A tool often needs to react to command-line options, and this is
432precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example
433illustrates this feature:</p>
434<pre class="literal-block">
435def llvm_gcc_linker : Tool&lt;[
436 (in_language &quot;object-code&quot;),
437 (out_language &quot;executable&quot;),
438 (output_suffix &quot;out&quot;),
439 (cmd_line &quot;llvm-gcc $INFILE -o $OUTFILE&quot;),
440 (join),
441 (actions (case (not_empty &quot;L&quot;), (forward &quot;L&quot;),
442 (not_empty &quot;l&quot;), (forward &quot;l&quot;),
443 (not_empty &quot;dummy&quot;),
444 [(append_cmd &quot;-dummy1&quot;), (append_cmd &quot;-dummy2&quot;)])
445 ]&gt;;
446</pre>
447<p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent
448<tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em>
449with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>,
450which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which
451appends a given string to the tool execution command. Multiple actions
452can be associated with a single condition by using a list of actions
453(used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt>
454construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the
455tool command line.</p>
456<p>The &quot;join&quot; property used in the example means that this tool behaves
457like a linker.</p>
458<p>The list of all possible actions follows.</p>
459<ul>
460<li><p class="first">Possible actions:</p>
461<blockquote>
462<ul class="simple">
463<li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - append a string to the tool invocation
464command.
465Example: <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>
466<li><tt class="docutils literal"><span class="pre">forward</span></tt> - forward an option unchanged.
467Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">&quot;Wall&quot;)</span></tt>.</li>
468<li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the name of an option, but forward the
469argument unchanged.
470Example: <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>
471<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - modify the output suffix of this
472tool.
473Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">&quot;i&quot;)</span></tt>.</li>
474<li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - stop compilation after this tool processes
475its input. Used without arguments.</li>
476<li><tt class="docutils literal"><span class="pre">unpack_values</span></tt> - used for for splitting and forwarding
477comma-separated lists of options, e.g. <tt class="docutils literal"><span class="pre">-Wa,-foo=bar,-baz</span></tt> is
478converted to <tt class="docutils literal"><span class="pre">-foo=bar</span> <span class="pre">-baz</span></tt> and appended to the tool invocation
479command.
480Example: <tt class="docutils literal"><span class="pre">(unpack_values</span> <span class="pre">&quot;Wa,&quot;)</span></tt>.</li>
481</ul>
482</blockquote>
483</li>
484</ul>
485</div>
486</div>
487<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000488<h1><a class="toc-backref" href="#id14" id="language-map" name="language-map">Language map</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000489<p>If you are adding support for a new language to LLVMC, you'll need to
490modify the language map, which defines mappings from file extensions
491to language names. It is used to choose the proper toolchain(s) for a
492given input file set. Language map definition looks like this:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000493<pre class="literal-block">
494def LanguageMap : LanguageMap&lt;
495 [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;,
496 LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;,
497 ...
498 ]&gt;;
499</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000500<p>For example, without those definitions the following command wouldn't work:</p>
501<pre class="literal-block">
502$ llvmc hello.cpp
503llvmc: Unknown suffix: cpp
504</pre>
505<p>The language map entries should be added only for tools that are
506linked with the root node. Since tools are not allowed to have
507multiple output languages, for nodes &quot;inside&quot; the graph the input and
508output languages should match. This is enforced at compile-time.</p>
Reid Spencer46d21922004-08-22 18:06:59 +0000509</div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000510<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000511<h1><a class="toc-backref" href="#id15" id="more-advanced-topics" name="more-advanced-topics">More advanced topics</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000512<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000513<h2><a class="toc-backref" href="#id16" id="hooks-and-environment-variables" name="hooks-and-environment-variables"><span id="hooks"></span>Hooks and environment variables</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000514<p>Normally, LLVMC executes programs from the system <tt class="docutils literal"><span class="pre">PATH</span></tt>. Sometimes,
515this is not sufficient: for example, we may want to specify tool names
516in the configuration file. This can be achieved via the mechanism of
517hooks - to write your own hooks, just add their definitions to the
518<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
519<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>
520namespace and have the signature <tt class="docutils literal"><span class="pre">std::string</span> <span class="pre">hooks::MyHookName</span>
521<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>
522<pre class="literal-block">
523(cmd_line &quot;$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)&quot;)
524</pre>
525<p>It is also possible to use environment variables in the same manner:</p>
526<pre class="literal-block">
527(cmd_line &quot;$ENV(VAR1)/path/to/file -o $ENV(VAR2)&quot;)
528</pre>
529<p>To change the command line string based on user-provided options use
530the <tt class="docutils literal"><span class="pre">case</span></tt> expression (documented <a class="reference" href="#case">above</a>):</p>
531<pre class="literal-block">
532(cmd_line
533 (case
534 (switch_on &quot;E&quot;),
535 &quot;llvm-g++ -E -x c $INFILE -o $OUTFILE&quot;,
536 (default),
537 &quot;llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm&quot;))
538</pre>
539</div>
540<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000541<h2><a class="toc-backref" href="#id17" id="how-plugins-are-loaded" name="how-plugins-are-loaded"><span id="priorities"></span>How plugins are loaded</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000542<p>It is possible for LLVMC plugins to depend on each other. For example,
543one can create edges between nodes defined in some other plugin. To
544make this work, however, that plugin should be loaded first. To
545achieve this, the concept of plugin priority was introduced. By
546default, every plugin has priority zero; to specify the priority
547explicitly, put the following line in your plugin's TableGen file:</p>
548<pre class="literal-block">
549def Priority : PluginPriority&lt;$PRIORITY_VALUE&gt;;
550# Where PRIORITY_VALUE is some integer &gt; 0
551</pre>
552<p>Plugins are loaded in order of their (increasing) priority, starting
553with 0. Therefore, the plugin with the highest priority value will be
554loaded last.</p>
555</div>
556<div class="section">
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000557<h2><a class="toc-backref" href="#id18" id="debugging" name="debugging">Debugging</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000558<p>When writing LLVMC plugins, it can be useful to get a visual view of
559the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000560line option <tt class="docutils literal"><span class="pre">--view-graph</span></tt>. This command assumes that <a class="reference" href="http://www.graphviz.org/">Graphviz</a> and
561<a class="reference" href="http://pages.cs.wisc.edu/~ghost/">Ghostview</a> are installed. There is also a <tt class="docutils literal"><span class="pre">--dump-graph</span></tt> option that
562creates a Graphviz source file (<tt class="docutils literal"><span class="pre">compilation-graph.dot</span></tt>) in the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000563current directory.</p>
Mikhail Glushenkov90531542008-12-11 23:43:14 +0000564<hr />
Anton Korobeynikov28b66702008-06-09 04:17:51 +0000565<address>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000566<a href="http://jigsaw.w3.org/css-validator/check/referer">
567<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
568 alt="Valid CSS" /></a>
569<a href="http://validator.w3.org/check?uri=referer">
570<img src="http://www.w3.org/Icons/valid-xhtml10-blue"
571 alt="Valid XHTML 1.0 Transitional"/></a>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000572
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000573<a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
574<a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000575
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000576Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
577</address></div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000578</div>
579</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000580</body>
581</html>