blob: 0a3f87716235cb91a56e6e7678edc1b852ede6ad [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 Glushenkovd6d2efc2009-05-06 01:41:47 +00006<meta name="generator" content="Docutils 0.5: 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 Glushenkovd6d2efc2009-05-06 01:41:47 +000013
Mikhail Glushenkov23f522a2008-12-13 17:51:47 +000014<!-- This file was automatically generated by rst2html.
15Please do not edit directly!
16The ReST source lives in the directory 'tools/llvmc/doc'. -->
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000017<div class="contents topic" id="contents">
18<p class="topic-title first">Contents</p>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000019<ul class="simple">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +000020<li><a class="reference internal" href="#introduction" id="id8">Introduction</a></li>
21<li><a class="reference internal" href="#compiling-with-llvmc" id="id9">Compiling with LLVMC</a></li>
22<li><a class="reference internal" href="#predefined-options" id="id10">Predefined options</a></li>
23<li><a class="reference internal" href="#compiling-llvmc-plugins" id="id11">Compiling LLVMC plugins</a></li>
24<li><a class="reference internal" href="#compiling-standalone-llvmc-based-drivers" id="id12">Compiling standalone LLVMC-based drivers</a></li>
25<li><a class="reference internal" href="#customizing-llvmc-the-compilation-graph" id="id13">Customizing LLVMC: the compilation graph</a></li>
26<li><a class="reference internal" href="#describing-options" id="id14">Describing options</a><ul>
27<li><a class="reference internal" href="#external-options" id="id15">External options</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000028</ul>
29</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +000030<li><a class="reference internal" href="#conditional-evaluation" id="id16">Conditional evaluation</a></li>
31<li><a class="reference internal" href="#writing-a-tool-description" id="id17">Writing a tool description</a><ul>
32<li><a class="reference internal" href="#id5" id="id18">Actions</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000033</ul>
34</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +000035<li><a class="reference internal" href="#language-map" id="id19">Language map</a></li>
36<li><a class="reference internal" href="#option-preprocessor" id="id20">Option preprocessor</a></li>
37<li><a class="reference internal" href="#more-advanced-topics" id="id21">More advanced topics</a><ul>
38<li><a class="reference internal" href="#hooks-and-environment-variables" id="id22">Hooks and environment variables</a></li>
39<li><a class="reference internal" href="#how-plugins-are-loaded" id="id23">How plugins are loaded</a></li>
40<li><a class="reference internal" href="#debugging" id="id24">Debugging</a></li>
41<li><a class="reference internal" href="#conditioning-on-the-executable-name" id="id25">Conditioning on the executable name</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000042</ul>
43</li>
44</ul>
45</div>
46<div class="doc_author">
47<p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000048</div><div class="section" id="introduction">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +000049<h1><a class="toc-backref" href="#id8">Introduction</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000050<p>LLVMC is a generic compiler driver, designed to be customizable and
51extensible. It plays the same role for LLVM as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program
52does for GCC - LLVMC's job is essentially to transform a set of input
53files into a set of targets depending on configuration rules and user
54options. What makes LLVMC different is that these transformation rules
55are completely customizable - in fact, LLVMC knows nothing about the
56specifics of transformation (even the command-line options are mostly
57not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000058abstract graph. The structure of this graph is completely determined
59by plugins, which can be either statically or dynamically linked. This
60makes it possible to easily adapt LLVMC for other purposes - for
61example, as a build tool for game resources.</p>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +000062<p>Because LLVMC employs <a class="reference external" href="http://llvm.org/docs/TableGenFundamentals.html">TableGen</a> as its configuration language, you
Anton Korobeynikov74120312008-06-09 04:15:49 +000063need to be familiar with it to customize LLVMC.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +000064</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000065<div class="section" id="compiling-with-llvmc">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +000066<h1><a class="toc-backref" href="#id9">Compiling with LLVMC</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000067<p>LLVMC tries hard to be as compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as possible,
68although there are some small differences. Most of the time, however,
69you shouldn't be able to notice them:</p>
70<pre class="literal-block">
71$ # This works as expected:
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000072$ llvmc -O3 -Wall hello.cpp
Anton Korobeynikov74120312008-06-09 04:15:49 +000073$ ./a.out
74hello
75</pre>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +000076<p>One nice feature of LLVMC is that one doesn't have to distinguish between
77different compilers for different languages (think <tt class="docutils literal"><span class="pre">g++</span></tt> vs. <tt class="docutils literal"><span class="pre">gcc</span></tt>) - the
78right toolchain is chosen automatically based on input language names (which
79are, in turn, determined from file extensions). If you want to force files
80ending with &quot;.c&quot; to compile as C++, use the <tt class="docutils literal"><span class="pre">-x</span></tt> option, just like you would
81do it with <tt class="docutils literal"><span class="pre">gcc</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +000082<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000083$ # hello.c is really a C++ file
84$ llvmc -x c++ hello.c
Anton Korobeynikov74120312008-06-09 04:15:49 +000085$ ./a.out
86hello
87</pre>
88<p>On the other hand, when using LLVMC as a linker to combine several C++
89object files you should provide the <tt class="docutils literal"><span class="pre">--linker</span></tt> option since it's
90impossible for LLVMC to choose the right linker in that case:</p>
91<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000092$ llvmc -c hello.cpp
93$ llvmc hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000094[A lot of link-time errors skipped]
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000095$ llvmc --linker=c++ hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000096$ ./a.out
97hello
98</pre>
Mikhail Glushenkove25b8452009-06-30 00:16:43 +000099<p>By default, LLVMC uses <tt class="docutils literal"><span class="pre">llvm-gcc</span></tt> to compile the source code. It is also
100possible to choose the <tt class="docutils literal"><span class="pre">clang</span></tt> compiler with the <tt class="docutils literal"><span class="pre">-clang</span></tt> option.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000101</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000102<div class="section" id="predefined-options">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000103<h1><a class="toc-backref" href="#id10">Predefined options</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000104<p>LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000105configuration libraries:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000106<ul class="simple">
107<li><tt class="docutils literal"><span class="pre">-o</span> <span class="pre">FILE</span></tt> - Output file name.</li>
108<li><tt class="docutils literal"><span class="pre">-x</span> <span class="pre">LANGUAGE</span></tt> - Specify the language of the following input files
109until the next -x option.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000110<li><tt class="docutils literal"><span class="pre">-load</span> <span class="pre">PLUGIN_NAME</span></tt> - Load the specified plugin DLL. Example:
111<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 +0000112<li><tt class="docutils literal"><span class="pre">-v</span></tt> - Enable verbose mode, i.e. print out all executed commands.</li>
Mikhail Glushenkovd7acf732009-06-25 18:21:10 +0000113<li><tt class="docutils literal"><span class="pre">--save-temps</span></tt> - Write temporary files to the current directory and do not
114delete them on exit. This option can also take an argument: the
115<tt class="docutils literal"><span class="pre">--save-temps=obj</span></tt> switch will write files into the directory specified with
116the <tt class="docutils literal"><span class="pre">-o</span></tt> option. The <tt class="docutils literal"><span class="pre">--save-temps=cwd</span></tt> and <tt class="docutils literal"><span class="pre">--save-temps</span></tt> switches are
117both synonyms for the default behaviour.</li>
Mikhail Glushenkov4429aaf2009-07-11 19:28:00 +0000118<li><tt class="docutils literal"><span class="pre">--temp-dir</span> <span class="pre">DIRECTORY</span></tt> - Store temporary files in the given directory. This
119directory is deleted on exit unless <tt class="docutils literal"><span class="pre">--save-temps</span></tt> is specified. If
120<tt class="docutils literal"><span class="pre">--save-temps=obj</span></tt> is also specified, <tt class="docutils literal"><span class="pre">--temp-dir</span></tt> is given the
121precedence.</li>
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000122<li><tt class="docutils literal"><span class="pre">--check-graph</span></tt> - Check the compilation for common errors like mismatched
123output/input language names, multiple default edges and cycles. Because of
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000124plugins, these checks can't be performed at compile-time. Exit with code zero
125if no errors were found, and return the number of found errors
126otherwise. Hidden option, useful for debugging LLVMC plugins.</li>
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000127<li><tt class="docutils literal"><span class="pre">--view-graph</span></tt> - Show a graphical representation of the compilation graph
128and exit. 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 installed. Hidden
129option, useful for debugging LLVMC plugins.</li>
130<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 current
131directory with the compilation graph description in Graphviz format (identical
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000132to the file used by the <tt class="docutils literal"><span class="pre">--view-graph</span></tt> option). The <tt class="docutils literal"><span class="pre">-o</span></tt> option can be
133used to set the output file name. Hidden option, useful for debugging LLVMC
134plugins.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000135<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
136their standard meaning.</li>
137</ul>
Reid Spencerb1254a12004-08-09 03:08:29 +0000138</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000139<div class="section" id="compiling-llvmc-plugins">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000140<h1><a class="toc-backref" href="#id11">Compiling LLVMC plugins</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000141<p>It's easiest to start working on your own LLVMC plugin by copying the
142skeleton 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 +0000143<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000144$ cd $LLVMC_DIR/plugins
145$ cp -r Simple MyPlugin
146$ cd MyPlugin
147$ ls
148Makefile PluginMain.cpp Simple.td
Anton Korobeynikov74120312008-06-09 04:15:49 +0000149</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000150<p>As you can see, our basic plugin consists of only two files (not
151counting the build script). <tt class="docutils literal"><span class="pre">Simple.td</span></tt> contains TableGen
152description of the compilation graph; its format is documented in the
153following sections. <tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> is just a helper file used to
154compile the auto-generated C++ code produced from TableGen source. It
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000155can also contain hook definitions (see <a class="reference internal" href="#hooks">below</a>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000156<p>The first thing that you should do is to change the <tt class="docutils literal"><span class="pre">LLVMC_PLUGIN</span></tt>
157variable in the <tt class="docutils literal"><span class="pre">Makefile</span></tt> to avoid conflicts (since this variable
158is used to name the resulting library):</p>
159<pre class="literal-block">
160LLVMC_PLUGIN=MyPlugin
161</pre>
162<p>It is also a good idea to rename <tt class="docutils literal"><span class="pre">Simple.td</span></tt> to something less
163generic:</p>
164<pre class="literal-block">
165$ mv Simple.td MyPlugin.td
166</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000167<p>To build your plugin as a dynamic library, just <tt class="docutils literal"><span class="pre">cd</span></tt> to its source
168directory and run <tt class="docutils literal"><span class="pre">make</span></tt>. The resulting file will be called
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000169<tt class="docutils literal"><span class="pre">plugin_llvmc_$(LLVMC_PLUGIN).$(DLL_EXTENSION)</span></tt> (in our case,
170<tt class="docutils literal"><span class="pre">plugin_llvmc_MyPlugin.so</span></tt>). This library can be then loaded in with the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000171<tt class="docutils literal"><span class="pre">-load</span></tt> option. Example:</p>
172<pre class="literal-block">
173$ cd $LLVMC_DIR/plugins/Simple
174$ make
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000175$ llvmc -load $LLVM_DIR/Release/lib/plugin_llvmc_Simple.so
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000176</pre>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000177</div>
178<div class="section" id="compiling-standalone-llvmc-based-drivers">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000179<h1><a class="toc-backref" href="#id12">Compiling standalone LLVMC-based drivers</a></h1>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000180<p>By default, the <tt class="docutils literal"><span class="pre">llvmc</span></tt> executable consists of a driver core plus several
181statically linked plugins (<tt class="docutils literal"><span class="pre">Base</span></tt> and <tt class="docutils literal"><span class="pre">Clang</span></tt> at the moment). You can
182produce a standalone LLVMC-based driver executable by linking the core with your
183own plugins. The recommended way to do this is by starting with the provided
184<tt class="docutils literal"><span class="pre">Skeleton</span></tt> example (<tt class="docutils literal"><span class="pre">$LLVMC_DIR/example/Skeleton</span></tt>):</p>
185<pre class="literal-block">
186$ cd $LLVMC_DIR/example/
187$ cp -r Skeleton mydriver
188$ cd mydriver
189$ vim Makefile
190[...]
191$ make
192</pre>
193<p>If you're compiling LLVM with different source and object directories, then you
194must perform the following additional steps before running <tt class="docutils literal"><span class="pre">make</span></tt>:</p>
195<pre class="literal-block">
196# LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/
197# LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/
198$ cp $LLVMC_SRC_DIR/example/mydriver/Makefile \
199 $LLVMC_OBJ_DIR/example/mydriver/
200$ cd $LLVMC_OBJ_DIR/example/mydriver
201$ make
202</pre>
203<p>Another way to do the same thing is by using the following command:</p>
204<pre class="literal-block">
205$ cd $LLVMC_DIR
206$ make LLVMC_BUILTIN_PLUGINS=MyPlugin LLVMC_BASED_DRIVER_NAME=mydriver
207</pre>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000208<p>This works with both srcdir == objdir and srcdir != objdir, but assumes that the
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000209plugin source directory was placed under <tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins</span></tt>.</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000210<p>Sometimes, you will want a 'bare-bones' version of LLVMC that has no
211built-in plugins. It can be compiled with the following command:</p>
212<pre class="literal-block">
213$ cd $LLVMC_DIR
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000214$ make LLVMC_BUILTIN_PLUGINS=&quot;&quot;
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000215</pre>
216</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000217<div class="section" id="customizing-llvmc-the-compilation-graph">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000218<h1><a class="toc-backref" href="#id13">Customizing LLVMC: the compilation graph</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000219<p>Each TableGen configuration file should include the common
220definitions:</p>
221<pre class="literal-block">
222include &quot;llvm/CompilerDriver/Common.td&quot;
223</pre>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000224<p>Internally, LLVMC stores information about possible source
225transformations in form of a graph. Nodes in this graph represent
226tools, and edges between two nodes represent a transformation path. A
227special &quot;root&quot; node is used to mark entry points for the
228transformations. LLVMC also assigns a weight to each edge (more on
229this later) to choose between several alternative edges.</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000230<p>The definition of the compilation graph (see file
231<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 +0000232<pre class="literal-block">
233def CompilationGraph : CompilationGraph&lt;[
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000234 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_c&quot;&gt;,
235 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_assembler&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000236 ...
237
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000238 Edge&lt;&quot;llvm_gcc_c&quot;, &quot;llc&quot;&gt;,
239 Edge&lt;&quot;llvm_gcc_cpp&quot;, &quot;llc&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000240 ...
241
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000242 OptionalEdge&lt;&quot;llvm_gcc_c&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
243 (inc_weight))&gt;,
244 OptionalEdge&lt;&quot;llvm_gcc_cpp&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
245 (inc_weight))&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000246 ...
247
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000248 OptionalEdge&lt;&quot;llvm_gcc_assembler&quot;, &quot;llvm_gcc_cpp_linker&quot;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000249 (case (input_languages_contain &quot;c++&quot;), (inc_weight),
250 (or (parameter_equals &quot;linker&quot;, &quot;g++&quot;),
251 (parameter_equals &quot;linker&quot;, &quot;c++&quot;)), (inc_weight))&gt;,
252 ...
253
254 ]&gt;;
255</pre>
256<p>As you can see, the edges can be either default or optional, where
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000257optional edges are differentiated by an additional <tt class="docutils literal"><span class="pre">case</span></tt> expression
258used to calculate the weight of this edge. Notice also that we refer
259to tools via their names (as strings). This makes it possible to add
260edges to an existing compilation graph in plugins without having to
261know about all tool definitions used in the graph.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000262<p>The default edges are assigned a weight of 1, and optional edges get a
263weight of 0 + 2*N where N is the number of tests that evaluated to
264true in the <tt class="docutils literal"><span class="pre">case</span></tt> expression. It is also possible to provide an
265integer 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,
266the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000267of the default 2. It is also possible to change the default weight of
268an 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>
269construct.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000270<p>When passing an input file through the graph, LLVMC picks the edge
271with the maximum weight. To avoid ambiguity, there should be only one
272default edge between two nodes (with the exception of the root node,
273which gets a special treatment - there you are allowed to specify one
274default edge <em>per language</em>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000275<p>When multiple plugins are loaded, their compilation graphs are merged
276together. Since multiple edges that have the same end nodes are not
277allowed (i.e. the graph is not a multigraph), an edge defined in
278several plugins will be replaced by the definition from the plugin
279that was loaded last. Plugin load order can be controlled by using the
280plugin priority feature described above.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000281<p>To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000282debugging), 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 +0000283<tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000284</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000285<div class="section" id="describing-options">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000286<h1><a class="toc-backref" href="#id14">Describing options</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000287<p>Command-line options that the plugin supports are defined by using an
288<tt class="docutils literal"><span class="pre">OptionList</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000289<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000290def Options : OptionList&lt;[
291(switch_option &quot;E&quot;, (help &quot;Help string&quot;)),
292(alias_option &quot;quiet&quot;, &quot;q&quot;)
293...
294]&gt;;
Anton Korobeynikov74120312008-06-09 04:15:49 +0000295</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000296<p>As you can see, the option list is just a list of DAGs, where each DAG
297is an option description consisting of the option name and some
298properties. A plugin can define more than one option list (they are
299all merged together in the end), which can be handy if one wants to
300separate option groups syntactically.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000301<ul>
302<li><p class="first">Possible option types:</p>
303<blockquote>
304<ul class="simple">
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000305<li><tt class="docutils literal"><span class="pre">switch_option</span></tt> - a simple boolean switch without arguments, for example
306<tt class="docutils literal"><span class="pre">-O2</span></tt> or <tt class="docutils literal"><span class="pre">-time</span></tt>. At most one occurrence is allowed.</li>
307<li><tt class="docutils literal"><span class="pre">parameter_option</span></tt> - option that takes one argument, for example
308<tt class="docutils literal"><span class="pre">-std=c99</span></tt>. It is also allowed to use spaces instead of the equality
309sign: <tt class="docutils literal"><span class="pre">-std</span> <span class="pre">c99</span></tt>. At most one occurrence is allowed.</li>
310<li><tt class="docutils literal"><span class="pre">parameter_list_option</span></tt> - same as the above, but more than one option
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000311occurence is allowed.</li>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000312<li><tt class="docutils literal"><span class="pre">prefix_option</span></tt> - same as the parameter_option, but the option name and
313argument do not have to be separated. Example: <tt class="docutils literal"><span class="pre">-ofile</span></tt>. This can be also
314specified as <tt class="docutils literal"><span class="pre">-o</span> <span class="pre">file</span></tt>; however, <tt class="docutils literal"><span class="pre">-o=file</span></tt> will be parsed incorrectly
315(<tt class="docutils literal"><span class="pre">=file</span></tt> will be interpreted as option value). At most one occurrence is
316allowed.</li>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000317<li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one occurence of
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000318the option is allowed; example: <tt class="docutils literal"><span class="pre">-lm</span> <span class="pre">-lpthread</span></tt>.</li>
319<li><tt class="docutils literal"><span class="pre">alias_option</span></tt> - a special option type for creating aliases. Unlike other
320option types, aliases are not allowed to have any properties besides the
321aliased option name. Usage example: <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>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000322</ul>
323</blockquote>
324</li>
325<li><p class="first">Possible option properties:</p>
326<blockquote>
327<ul class="simple">
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000328<li><tt class="docutils literal"><span class="pre">help</span></tt> - help string associated with this option. Used for <tt class="docutils literal"><span class="pre">--help</span></tt>
329output.</li>
330<li><tt class="docutils literal"><span class="pre">required</span></tt> - this option must be specified exactly once (or, in case of
331the list options without the <tt class="docutils literal"><span class="pre">multi_val</span></tt> property, at least
332once). Incompatible with <tt class="docutils literal"><span class="pre">zero_or_one</span></tt> and <tt class="docutils literal"><span class="pre">one_or_more</span></tt>.</li>
333<li><tt class="docutils literal"><span class="pre">one_or_more</span></tt> - the option must be specified at least one time. Useful
334only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>; for ordinary lists
335it is synonymous with <tt class="docutils literal"><span class="pre">required</span></tt>. Incompatible with <tt class="docutils literal"><span class="pre">required</span></tt> and
336<tt class="docutils literal"><span class="pre">zero_or_one</span></tt>.</li>
337<li><tt class="docutils literal"><span class="pre">zero_or_one</span></tt> - the option can be specified zero or one times. Useful
338only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>. Incompatible with
339<tt class="docutils literal"><span class="pre">required</span></tt> and <tt class="docutils literal"><span class="pre">one_or_more</span></tt>.</li>
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000340<li><tt class="docutils literal"><span class="pre">hidden</span></tt> - the description of this option will not appear in
341the <tt class="docutils literal"><span class="pre">--help</span></tt> output (but will appear in the <tt class="docutils literal"><span class="pre">--help-hidden</span></tt>
342output).</li>
343<li><tt class="docutils literal"><span class="pre">really_hidden</span></tt> - the option will not be mentioned in any help
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000344output.</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000345<li><tt class="docutils literal"><span class="pre">comma_separated</span></tt> - Indicates that any commas specified for an option's
346value should be used to split the value up into multiple values for the
347option. This property is valid only for list options. In conjunction with
348<tt class="docutils literal"><span class="pre">forward_value</span></tt> can be used to implement option forwarding in style of
349gcc's <tt class="docutils literal"><span class="pre">-Wa,</span></tt>.</li>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000350<li><tt class="docutils literal"><span class="pre">multi_val</span> <span class="pre">n</span></tt> - this option takes <em>n</em> arguments (can be useful in some
351special cases). Usage example: <tt class="docutils literal"><span class="pre">(parameter_list_option</span> <span class="pre">&quot;foo&quot;,</span> <span class="pre">(multi_val</span>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000352<span class="pre">3))</span></tt>; the command-line syntax is '-foo a b c'. Only list options can have
353this attribute; you can, however, use the <tt class="docutils literal"><span class="pre">one_or_more</span></tt>, <tt class="docutils literal"><span class="pre">zero_or_one</span></tt>
354and <tt class="docutils literal"><span class="pre">required</span></tt> properties.</li>
Mikhail Glushenkov640dd262009-07-07 16:09:48 +0000355<li><tt class="docutils literal"><span class="pre">init</span></tt> - this option has a default value, either a string (if it is a
356parameter), or a boolean (if it is a switch; boolean constants are called
357<tt class="docutils literal"><span class="pre">true</span></tt> and <tt class="docutils literal"><span class="pre">false</span></tt>). List options can't have this attribute. Usage
358examples: <tt class="docutils literal"><span class="pre">(switch_option</span> <span class="pre">&quot;foo&quot;,</span> <span class="pre">(init</span> <span class="pre">true))</span></tt>; <tt class="docutils literal"><span class="pre">(prefix_option</span> <span class="pre">&quot;bar&quot;,</span>
359<span class="pre">(init</span> <span class="pre">&quot;baz&quot;))</span></tt>.</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000360<li><tt class="docutils literal"><span class="pre">extern</span></tt> - this option is defined in some other plugin, see <a class="reference internal" href="#extern">below</a>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000361</ul>
362</blockquote>
363</li>
364</ul>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000365<div class="section" id="external-options">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000366<span id="extern"></span><h2><a class="toc-backref" href="#id15">External options</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000367<p>Sometimes, when linking several plugins together, one plugin needs to
368access options defined in some other plugin. Because of the way
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000369options are implemented, such options must be marked as
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000370<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
371for. Example:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000372<pre class="literal-block">
Anton Korobeynikov74120312008-06-09 04:15:49 +0000373...
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000374(switch_option &quot;E&quot;, (extern))
375...
Anton Korobeynikov74120312008-06-09 04:15:49 +0000376</pre>
Mikhail Glushenkov15367df2009-07-07 16:44:20 +0000377<p>If an external option has additional attributes besides 'extern', they are
378ignored. See also the section on plugin <a class="reference internal" href="#priorities">priorities</a>.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000379</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000380</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000381<div class="section" id="conditional-evaluation">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000382<span id="case"></span><h1><a class="toc-backref" href="#id16">Conditional evaluation</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000383<p>The 'case' construct is the main means by which programmability is
384achieved in LLVMC. It can be used to calculate edge weights, program
385actions and modify the shell commands to be executed. The 'case'
386expression is designed after the similarly-named construct in
387functional 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>
388<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
389are evaluated only if the corresponding tests evaluate to true.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000390<p>Examples:</p>
391<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000392// Edge weight calculation
393
Anton Korobeynikov74120312008-06-09 04:15:49 +0000394// Increases edge weight by 5 if &quot;-A&quot; is provided on the
395// command-line, and by 5 more if &quot;-B&quot; is also provided.
396(case
397 (switch_on &quot;A&quot;), (inc_weight 5),
398 (switch_on &quot;B&quot;), (inc_weight 5))
399
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000400
401// Tool command line specification
402
403// Evaluates to &quot;cmdline1&quot; if the option &quot;-A&quot; is provided on the
404// command line; to &quot;cmdline2&quot; if &quot;-B&quot; is provided;
405// otherwise to &quot;cmdline3&quot;.
406
Anton Korobeynikov74120312008-06-09 04:15:49 +0000407(case
408 (switch_on &quot;A&quot;), &quot;cmdline1&quot;,
409 (switch_on &quot;B&quot;), &quot;cmdline2&quot;,
410 (default), &quot;cmdline3&quot;)
411</pre>
412<p>Note the slight difference in 'case' expression handling in contexts
413of edge weights and command line specification - in the second example
414the 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
415enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">&quot;cmdline1&quot;</span></tt> in
416that case.</p>
417<p>Case expressions can also be nested, i.e. the following is legal:</p>
418<pre class="literal-block">
419(case (switch_on &quot;E&quot;), (case (switch_on &quot;o&quot;), ..., (default), ...)
420 (default), ...)
421</pre>
422<p>You should, however, try to avoid doing that because it hurts
423readability. It is usually better to split tool descriptions and/or
424use TableGen inheritance instead.</p>
425<ul class="simple">
426<li>Possible tests are:<ul>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000427<li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is provided by
428the user. Can be given a list as argument, in that case <tt class="docutils literal"><span class="pre">(switch_on</span> <span class="pre">[&quot;foo&quot;,</span>
429<span class="pre">&quot;bar&quot;,</span> <span class="pre">&quot;baz&quot;])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(switch_on</span> <span class="pre">&quot;foo&quot;),</span> <span class="pre">(switch_on</span>
430<span class="pre">&quot;bar&quot;),</span> <span class="pre">(switch_on</span> <span class="pre">&quot;baz&quot;))</span></tt>.
431Example: <tt class="docutils literal"><span class="pre">(switch_on</span> <span class="pre">&quot;opt&quot;)</span></tt>.</li>
432<li><tt class="docutils literal"><span class="pre">any_switch_on</span></tt> - Given a list of switch options, returns true if any of
433the switches is turned on.
434Example: <tt class="docutils literal"><span class="pre">(any_switch_on</span> <span class="pre">[&quot;foo&quot;,</span> <span class="pre">&quot;bar&quot;,</span> <span class="pre">&quot;baz&quot;])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(or</span>
435<span class="pre">(switch_on</span> <span class="pre">&quot;foo&quot;),</span> <span class="pre">(switch_on</span> <span class="pre">&quot;bar&quot;),</span> <span class="pre">(switch_on</span> <span class="pre">&quot;baz&quot;))</span></tt>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000436<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 +0000437a given value.
438Example: <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>
439<li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter
440list contains a given value.
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000441Example: <tt class="docutils literal"><span class="pre">(element_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 +0000442<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 +0000443belongs to the current input language set.
444Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000445<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language is equal to
446the argument. At the moment works only with <tt class="docutils literal"><span class="pre">cmd_line</span></tt> and <tt class="docutils literal"><span class="pre">actions</span></tt> (on
447non-join nodes).
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000448Example: <tt class="docutils literal"><span class="pre">(in_language</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000449<li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be either a
450parameter or a parameter list) is set by the user. Like <tt class="docutils literal"><span class="pre">switch_on</span></tt>, can
451be also given a list as argument.
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000452Example: <tt class="docutils literal"><span class="pre">(not_empty</span> <span class="pre">&quot;o&quot;)</span></tt>.</li>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000453<li><tt class="docutils literal"><span class="pre">any_not_empty</span></tt> - Returns true if <tt class="docutils literal"><span class="pre">not_empty</span></tt> returns true for any of
454the options in the list.
455Example: <tt class="docutils literal"><span class="pre">(any_not_empty</span> <span class="pre">[&quot;foo&quot;,</span> <span class="pre">&quot;bar&quot;,</span> <span class="pre">&quot;baz&quot;])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(or</span>
456<span class="pre">(not_empty</span> <span class="pre">&quot;foo&quot;),</span> <span class="pre">(not_empty</span> <span class="pre">&quot;bar&quot;),</span> <span class="pre">(not_empty</span> <span class="pre">&quot;baz&quot;))</span></tt>.</li>
Mikhail Glushenkov2d0484c2008-12-17 02:47:30 +0000457<li><tt class="docutils literal"><span class="pre">empty</span></tt> - The opposite of <tt class="docutils literal"><span class="pre">not_empty</span></tt>. Equivalent to <tt class="docutils literal"><span class="pre">(not</span> <span class="pre">(not_empty</span>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000458<span class="pre">X))</span></tt>. Provided for convenience. Can be given a list as argument.</li>
459<li><tt class="docutils literal"><span class="pre">any_not_empty</span></tt> - Returns true if <tt class="docutils literal"><span class="pre">not_empty</span></tt> returns true for any of
460the options in the list.
461Example: <tt class="docutils literal"><span class="pre">(any_empty</span> <span class="pre">[&quot;foo&quot;,</span> <span class="pre">&quot;bar&quot;,</span> <span class="pre">&quot;baz&quot;])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(not</span> <span class="pre">(and</span>
462<span class="pre">(not_empty</span> <span class="pre">&quot;foo&quot;),</span> <span class="pre">(not_empty</span> <span class="pre">&quot;bar&quot;),</span> <span class="pre">(not_empty</span> <span class="pre">&quot;baz&quot;)))</span></tt>.</li>
Mikhail Glushenkov31280152009-09-28 01:28:26 +0000463<li><tt class="docutils literal"><span class="pre">single_input_file</span></tt> - Returns true if there was only one input file
464provided on the command-line. Used without arguments:
465<tt class="docutils literal"><span class="pre">(single_input_file)</span></tt>.</li>
466<li><tt class="docutils literal"><span class="pre">multiple_input_files</span></tt> - Equivalent to <tt class="docutils literal"><span class="pre">(not</span> <span class="pre">(single_input_file))</span></tt> (the
467case of zero input files is considered an error).</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000468<li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last
469test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li>
Mikhail Glushenkov31280152009-09-28 01:28:26 +0000470<li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard binary logical combinator that returns true iff all of
471its arguments return true. Used like this: <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(test1),</span> <span class="pre">(test2),</span>
472<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, but not
473encouraged.</li>
474<li><tt class="docutils literal"><span class="pre">or</span></tt> - A binary logical combinator that returns true iff any of its
475arguments returns true. Example: <tt class="docutils literal"><span class="pre">(or</span> <span class="pre">(test1),</span> <span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>.</li>
476<li><tt class="docutils literal"><span class="pre">not</span></tt> - Standard unary logical combinator that negates its
477argument. Example: <tt class="docutils literal"><span class="pre">(not</span> <span class="pre">(or</span> <span class="pre">(test1),</span> <span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN)))</span></tt>.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000478</ul>
479</li>
480</ul>
Reid Spencer46d21922004-08-22 18:06:59 +0000481</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000482<div class="section" id="writing-a-tool-description">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000483<h1><a class="toc-backref" href="#id17">Writing a tool description</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000484<p>As was said earlier, nodes in the compilation graph represent tools,
485which are described separately. A tool definition looks like this
486(taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p>
487<pre class="literal-block">
488def llvm_gcc_cpp : Tool&lt;[
489 (in_language &quot;c++&quot;),
490 (out_language &quot;llvm-assembler&quot;),
491 (output_suffix &quot;bc&quot;),
492 (cmd_line &quot;llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm&quot;),
493 (sink)
494 ]&gt;;
495</pre>
496<p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for
497<tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of
498properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt>
499property means that this tool should be passed all command-line
500options that aren't mentioned in the option list.</p>
501<p>The complete list of all currently implemented tool properties follows.</p>
502<ul class="simple">
503<li>Possible tool properties:<ul>
504<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a
505list, in case the tool supports multiple input languages.</li>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000506<li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Multiple output languages are not
507allowed.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000508<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed
509dynamically, see documentation on actions.</li>
510<li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
511use <tt class="docutils literal"><span class="pre">$INFILE</span></tt> and <tt class="docutils literal"><span class="pre">$OUTFILE</span></tt> variables, output redirection
512with <tt class="docutils literal"><span class="pre">&gt;</span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables
513(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>
514<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
515list of input files and joins them together. Used for linkers.</li>
516<li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other
517tools are passed to this tool.</li>
518<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
519this tool reacts on command-line options (described in more detail
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000520<a class="reference internal" href="#actions">below</a>).</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000521</ul>
522</li>
523</ul>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000524<div class="section" id="id5">
525<span id="actions"></span><h2><a class="toc-backref" href="#id18">Actions</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000526<p>A tool often needs to react to command-line options, and this is
527precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example
528illustrates this feature:</p>
529<pre class="literal-block">
530def llvm_gcc_linker : Tool&lt;[
531 (in_language &quot;object-code&quot;),
532 (out_language &quot;executable&quot;),
533 (output_suffix &quot;out&quot;),
534 (cmd_line &quot;llvm-gcc $INFILE -o $OUTFILE&quot;),
535 (join),
536 (actions (case (not_empty &quot;L&quot;), (forward &quot;L&quot;),
537 (not_empty &quot;l&quot;), (forward &quot;l&quot;),
538 (not_empty &quot;dummy&quot;),
539 [(append_cmd &quot;-dummy1&quot;), (append_cmd &quot;-dummy2&quot;)])
540 ]&gt;;
541</pre>
542<p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent
543<tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em>
544with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>,
545which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which
546appends a given string to the tool execution command. Multiple actions
547can be associated with a single condition by using a list of actions
548(used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt>
549construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the
550tool command line.</p>
551<p>The &quot;join&quot; property used in the example means that this tool behaves
552like a linker.</p>
553<p>The list of all possible actions follows.</p>
554<ul>
555<li><p class="first">Possible actions:</p>
556<blockquote>
557<ul class="simple">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000558<li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - Append a string to the tool invocation command.
559Example: <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>
560<li><tt class="docutils literal"><span class="pre">error</span></tt> - Exit with error.
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000561Example: <tt class="docutils literal"><span class="pre">(error</span> <span class="pre">&quot;Mixing</span> <span class="pre">-c</span> <span class="pre">and</span> <span class="pre">-S</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">allowed!&quot;)</span></tt>.</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000562<li><tt class="docutils literal"><span class="pre">warning</span></tt> - Print a warning.
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000563Example: <tt class="docutils literal"><span class="pre">(warning</span> <span class="pre">&quot;Specifying</span> <span class="pre">both</span> <span class="pre">-O1</span> <span class="pre">and</span> <span class="pre">-O2</span> <span class="pre">is</span> <span class="pre">meaningless!&quot;)</span></tt>.</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000564<li><tt class="docutils literal"><span class="pre">forward</span></tt> - Forward the option unchanged.
565Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">&quot;Wall&quot;)</span></tt>.</li>
566<li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the option's name, but forward the argument
567unchanged.
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000568Example: <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>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000569<li><tt class="docutils literal"><span class="pre">forward_value</span></tt> - Forward only option's value. Cannot be used with switch
570options (since they don't have values), but works fine with lists.
571Example: <tt class="docutils literal"><span class="pre">(forward_value</span> <span class="pre">&quot;Wa,&quot;)</span></tt>.</li>
572<li><tt class="docutils literal"><span class="pre">forward_transformed_value</span></tt> - As above, but applies a hook to the
573option's value before forwarding (see <a class="reference internal" href="#hooks">below</a>). When
574<tt class="docutils literal"><span class="pre">forward_transformed_value</span></tt> is applied to a list
575option, the hook must have signature
576<tt class="docutils literal"><span class="pre">std::string</span> <span class="pre">hooks::HookName</span> <span class="pre">(const</span> <span class="pre">std::vector&lt;std::string&gt;&amp;)</span></tt>.
577Example: <tt class="docutils literal"><span class="pre">(forward_transformed_value</span> <span class="pre">&quot;m&quot;,</span> <span class="pre">&quot;ConvertToMAttr&quot;)</span></tt>.</li>
578<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - Modify the output suffix of this tool.
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000579Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">&quot;i&quot;)</span></tt>.</li>
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000580<li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - Stop compilation after this tool processes its
581input. Used without arguments.
582Example: <tt class="docutils literal"><span class="pre">(stop_compilation)</span></tt>.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000583</ul>
584</blockquote>
585</li>
586</ul>
587</div>
588</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000589<div class="section" id="language-map">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000590<h1><a class="toc-backref" href="#id19">Language map</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000591<p>If you are adding support for a new language to LLVMC, you'll need to
592modify the language map, which defines mappings from file extensions
593to language names. It is used to choose the proper toolchain(s) for a
594given input file set. Language map definition looks like this:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000595<pre class="literal-block">
596def LanguageMap : LanguageMap&lt;
597 [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;,
598 LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;,
599 ...
600 ]&gt;;
601</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000602<p>For example, without those definitions the following command wouldn't work:</p>
603<pre class="literal-block">
604$ llvmc hello.cpp
605llvmc: Unknown suffix: cpp
606</pre>
607<p>The language map entries should be added only for tools that are
608linked with the root node. Since tools are not allowed to have
609multiple output languages, for nodes &quot;inside&quot; the graph the input and
610output languages should match. This is enforced at compile-time.</p>
Reid Spencer46d21922004-08-22 18:06:59 +0000611</div>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000612<div class="section" id="option-preprocessor">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000613<h1><a class="toc-backref" href="#id20">Option preprocessor</a></h1>
Mikhail Glushenkov0a018d32009-10-25 01:44:24 +0000614<p>It is sometimes useful to run error-checking code before processing the
615compilation graph. For example, if optimization options &quot;-O1&quot; and &quot;-O2&quot; are
616implemented as switches, we might want to output a warning if the user invokes
617the driver with both of these options enabled.</p>
618<p>The <tt class="docutils literal"><span class="pre">OptionPreprocessor</span></tt> feature is reserved specially for these
619occasions. Example (adapted from the built-in Base plugin):</p>
620<pre class="literal-block">
621def Preprocess : OptionPreprocessor&lt;
622(case (and (switch_on &quot;O3&quot;), (any_switch_on [&quot;O0&quot;, &quot;O1&quot;, &quot;O2&quot;])),
623 [(unset_option [&quot;O0&quot;, &quot;O1&quot;, &quot;O2&quot;]),
624 (warning &quot;Multiple -O options specified, defaulted to -O3.&quot;)],
625 (and (switch_on &quot;O2&quot;), (any_switch_on [&quot;O0&quot;, &quot;O1&quot;])),
626 (unset_option [&quot;O0&quot;, &quot;O1&quot;]),
627 (and (switch_on &quot;O1&quot;), (switch_on &quot;O0&quot;)),
628 (unset_option &quot;O0&quot;))
629&gt;;
630</pre>
631<p>Here, <tt class="docutils literal"><span class="pre">OptionPreprocessor</span></tt> is used to unset all spurious optimization options
632(so that they are not forwarded to the compiler).</p>
633<p><tt class="docutils literal"><span class="pre">OptionPreprocessor</span></tt> is basically a single big <tt class="docutils literal"><span class="pre">case</span></tt> expression, which is
634evaluated only once right after the plugin is loaded. The only allowed actions
635in <tt class="docutils literal"><span class="pre">OptionPreprocessor</span></tt> are <tt class="docutils literal"><span class="pre">error</span></tt>, <tt class="docutils literal"><span class="pre">warning</span></tt> and a special action
636<tt class="docutils literal"><span class="pre">unset_option</span></tt>, which, as the name suggests, unsets a given option. For
637convenience, <tt class="docutils literal"><span class="pre">unset_option</span></tt> also works on lists.</p>
638</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000639<div class="section" id="more-advanced-topics">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000640<h1><a class="toc-backref" href="#id21">More advanced topics</a></h1>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000641<div class="section" id="hooks-and-environment-variables">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000642<span id="hooks"></span><h2><a class="toc-backref" href="#id22">Hooks and environment variables</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000643<p>Normally, LLVMC executes programs from the system <tt class="docutils literal"><span class="pre">PATH</span></tt>. Sometimes,
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000644this is not sufficient: for example, we may want to specify tool paths
645or names in the configuration file. This can be easily achieved via
646the hooks mechanism. To write your own hooks, just add their
647definitions to the <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
648your plugin directory. Hooks should live in the <tt class="docutils literal"><span class="pre">hooks</span></tt> namespace
649and have the signature <tt class="docutils literal"><span class="pre">std::string</span> <span class="pre">hooks::MyHookName</span> <span class="pre">([const</span> <span class="pre">char*</span>
650<span class="pre">Arg0</span> <span class="pre">[</span> <span class="pre">const</span> <span class="pre">char*</span> <span class="pre">Arg2</span> <span class="pre">[,</span> <span class="pre">...]]])</span></tt>. They can be used from the
651<tt class="docutils literal"><span class="pre">cmd_line</span></tt> tool property:</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000652<pre class="literal-block">
653(cmd_line &quot;$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)&quot;)
654</pre>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000655<p>To pass arguments to hooks, use the following syntax:</p>
656<pre class="literal-block">
657(cmd_line &quot;$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2&quot;)
658</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000659<p>It is also possible to use environment variables in the same manner:</p>
660<pre class="literal-block">
661(cmd_line &quot;$ENV(VAR1)/path/to/file -o $ENV(VAR2)&quot;)
662</pre>
663<p>To change the command line string based on user-provided options use
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000664the <tt class="docutils literal"><span class="pre">case</span></tt> expression (documented <a class="reference internal" href="#case">above</a>):</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000665<pre class="literal-block">
666(cmd_line
667 (case
668 (switch_on &quot;E&quot;),
669 &quot;llvm-g++ -E -x c $INFILE -o $OUTFILE&quot;,
670 (default),
671 &quot;llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm&quot;))
672</pre>
673</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000674<div class="section" id="how-plugins-are-loaded">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000675<span id="priorities"></span><h2><a class="toc-backref" href="#id23">How plugins are loaded</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000676<p>It is possible for LLVMC plugins to depend on each other. For example,
677one can create edges between nodes defined in some other plugin. To
678make this work, however, that plugin should be loaded first. To
679achieve this, the concept of plugin priority was introduced. By
680default, every plugin has priority zero; to specify the priority
681explicitly, put the following line in your plugin's TableGen file:</p>
682<pre class="literal-block">
683def Priority : PluginPriority&lt;$PRIORITY_VALUE&gt;;
684# Where PRIORITY_VALUE is some integer &gt; 0
685</pre>
686<p>Plugins are loaded in order of their (increasing) priority, starting
687with 0. Therefore, the plugin with the highest priority value will be
688loaded last.</p>
689</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000690<div class="section" id="debugging">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000691<h2><a class="toc-backref" href="#id24">Debugging</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000692<p>When writing LLVMC plugins, it can be useful to get a visual view of
693the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000694line option <tt class="docutils literal"><span class="pre">--view-graph</span></tt>. This command assumes that <a class="reference external" href="http://www.graphviz.org/">Graphviz</a> and
695<a class="reference external" href="http://pages.cs.wisc.edu/~ghost/">Ghostview</a> are installed. There is also a <tt class="docutils literal"><span class="pre">--write-graph</span></tt> option that
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000696creates 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 +0000697current directory.</p>
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000698<p>Another useful <tt class="docutils literal"><span class="pre">llvmc</span></tt> option is <tt class="docutils literal"><span class="pre">--check-graph</span></tt>. It checks the
699compilation graph for common errors like mismatched output/input
700language names, multiple default edges and cycles. These checks can't
701be performed at compile-time because the plugins can load code
702dynamically. When invoked with <tt class="docutils literal"><span class="pre">--check-graph</span></tt>, <tt class="docutils literal"><span class="pre">llvmc</span></tt> doesn't
703perform any compilation tasks and returns the number of encountered
704errors as its status code.</p>
Mikhail Glushenkove25b8452009-06-30 00:16:43 +0000705</div>
706<div class="section" id="conditioning-on-the-executable-name">
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000707<h2><a class="toc-backref" href="#id25">Conditioning on the executable name</a></h2>
Mikhail Glushenkove25b8452009-06-30 00:16:43 +0000708<p>For now, the executable name (the value passed to the driver in <tt class="docutils literal"><span class="pre">argv[0]</span></tt>) is
709accessible only in the C++ code (i.e. hooks). Use the following code:</p>
710<pre class="literal-block">
711namespace llvmc {
712extern const char* ProgramName;
713}
714
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000715namespace hooks {
716
Mikhail Glushenkove25b8452009-06-30 00:16:43 +0000717std::string MyHook() {
718//...
719if (strcmp(ProgramName, &quot;mydriver&quot;) == 0) {
720 //...
721
722}
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000723
724} // end namespace hooks
Mikhail Glushenkove25b8452009-06-30 00:16:43 +0000725</pre>
726<p>In general, you're encouraged not to make the behaviour dependent on the
727executable file name, and use command-line switches instead. See for example how
728the <tt class="docutils literal"><span class="pre">Base</span></tt> plugin behaves when it needs to choose the correct linker options
729(think <tt class="docutils literal"><span class="pre">g++</span></tt> vs. <tt class="docutils literal"><span class="pre">gcc</span></tt>).</p>
Mikhail Glushenkov90531542008-12-11 23:43:14 +0000730<hr />
Anton Korobeynikov28b66702008-06-09 04:17:51 +0000731<address>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000732<a href="http://jigsaw.w3.org/css-validator/check/referer">
733<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
734 alt="Valid CSS" /></a>
735<a href="http://validator.w3.org/check?uri=referer">
736<img src="http://www.w3.org/Icons/valid-xhtml10-blue"
737 alt="Valid XHTML 1.0 Transitional"/></a>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000738
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000739<a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
740<a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000741
Mikhail Glushenkovabf31b92009-12-07 18:26:24 +0000742Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000743</address></div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000744</div>
745</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000746</body>
747</html>