blob: f498e29534994f56c2b432fc7349da4f2b48e07c [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 Glushenkovd6d2efc2009-05-06 01:41:47 +000020<li><a class="reference internal" href="#introduction" id="id4">Introduction</a></li>
21<li><a class="reference internal" href="#compiling-with-llvmc" id="id5">Compiling with LLVMC</a></li>
22<li><a class="reference internal" href="#predefined-options" id="id6">Predefined options</a></li>
23<li><a class="reference internal" href="#compiling-llvmc-plugins" id="id7">Compiling LLVMC plugins</a></li>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +000024<li><a class="reference internal" href="#compiling-standalone-llvmc-based-drivers" id="id8">Compiling standalone LLVMC-based drivers</a></li>
25<li><a class="reference internal" href="#customizing-llvmc-the-compilation-graph" id="id9">Customizing LLVMC: the compilation graph</a></li>
26<li><a class="reference internal" href="#describing-options" id="id10">Describing options</a><ul>
27<li><a class="reference internal" href="#external-options" id="id11">External options</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000028</ul>
29</li>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +000030<li><a class="reference internal" href="#conditional-evaluation" id="id12">Conditional evaluation</a></li>
31<li><a class="reference internal" href="#writing-a-tool-description" id="id13">Writing a tool description</a><ul>
32<li><a class="reference internal" href="#actions" id="id14">Actions</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000033</ul>
34</li>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +000035<li><a class="reference internal" href="#language-map" id="id15">Language map</a></li>
36<li><a class="reference internal" href="#more-advanced-topics" id="id16">More advanced topics</a><ul>
37<li><a class="reference internal" href="#hooks-and-environment-variables" id="id17">Hooks and environment variables</a></li>
38<li><a class="reference internal" href="#how-plugins-are-loaded" id="id18">How plugins are loaded</a></li>
39<li><a class="reference internal" href="#debugging" id="id19">Debugging</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000040</ul>
41</li>
42</ul>
43</div>
44<div class="doc_author">
45<p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000046</div><div class="section" id="introduction">
47<h1><a class="toc-backref" href="#id4">Introduction</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000048<p>LLVMC is a generic compiler driver, designed to be customizable and
49extensible. It plays the same role for LLVM as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program
50does for GCC - LLVMC's job is essentially to transform a set of input
51files into a set of targets depending on configuration rules and user
52options. What makes LLVMC different is that these transformation rules
53are completely customizable - in fact, LLVMC knows nothing about the
54specifics of transformation (even the command-line options are mostly
55not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000056abstract graph. The structure of this graph is completely determined
57by plugins, which can be either statically or dynamically linked. This
58makes it possible to easily adapt LLVMC for other purposes - for
59example, as a build tool for game resources.</p>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +000060<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 +000061need to be familiar with it to customize LLVMC.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +000062</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000063<div class="section" id="compiling-with-llvmc">
64<h1><a class="toc-backref" href="#id5">Compiling with LLVMC</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000065<p>LLVMC tries hard to be as compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as possible,
66although there are some small differences. Most of the time, however,
67you shouldn't be able to notice them:</p>
68<pre class="literal-block">
69$ # This works as expected:
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000070$ llvmc -O3 -Wall hello.cpp
Anton Korobeynikov74120312008-06-09 04:15:49 +000071$ ./a.out
72hello
73</pre>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +000074<p>One nice feature of LLVMC is that one doesn't have to distinguish between
75different 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
76right toolchain is chosen automatically based on input language names (which
77are, in turn, determined from file extensions). If you want to force files
78ending 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
79do it with <tt class="docutils literal"><span class="pre">gcc</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +000080<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000081$ # hello.c is really a C++ file
82$ llvmc -x c++ hello.c
Anton Korobeynikov74120312008-06-09 04:15:49 +000083$ ./a.out
84hello
85</pre>
86<p>On the other hand, when using LLVMC as a linker to combine several C++
87object files you should provide the <tt class="docutils literal"><span class="pre">--linker</span></tt> option since it's
88impossible for LLVMC to choose the right linker in that case:</p>
89<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000090$ llvmc -c hello.cpp
91$ llvmc hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000092[A lot of link-time errors skipped]
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000093$ llvmc --linker=c++ hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000094$ ./a.out
95hello
96</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000097<p>By default, LLVMC uses <tt class="docutils literal"><span class="pre">llvm-gcc</span></tt> to compile the source code. It is
98also possible to choose the work-in-progress <tt class="docutils literal"><span class="pre">clang</span></tt> compiler with
99the <tt class="docutils literal"><span class="pre">-clang</span></tt> option.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000100</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000101<div class="section" id="predefined-options">
102<h1><a class="toc-backref" href="#id6">Predefined options</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000103<p>LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000104configuration libraries:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000105<ul class="simple">
106<li><tt class="docutils literal"><span class="pre">-o</span> <span class="pre">FILE</span></tt> - Output file name.</li>
107<li><tt class="docutils literal"><span class="pre">-x</span> <span class="pre">LANGUAGE</span></tt> - Specify the language of the following input files
108until the next -x option.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000109<li><tt class="docutils literal"><span class="pre">-load</span> <span class="pre">PLUGIN_NAME</span></tt> - Load the specified plugin DLL. Example:
110<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 +0000111<li><tt class="docutils literal"><span class="pre">-v</span></tt> - Enable verbose mode, i.e. print out all executed commands.</li>
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000112<li><tt class="docutils literal"><span class="pre">--check-graph</span></tt> - Check the compilation for common errors like mismatched
113output/input language names, multiple default edges and cycles. Because of
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000114plugins, these checks can't be performed at compile-time. Exit with code zero
115if no errors were found, and return the number of found errors
116otherwise. Hidden option, useful for debugging LLVMC plugins.</li>
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000117<li><tt class="docutils literal"><span class="pre">--view-graph</span></tt> - Show a graphical representation of the compilation graph
118and 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
119option, useful for debugging LLVMC plugins.</li>
120<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
121directory with the compilation graph description in Graphviz format (identical
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000122to 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
123used to set the output file name. Hidden option, useful for debugging LLVMC
124plugins.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000125<li><tt class="docutils literal"><span class="pre">--save-temps</span></tt> - Write temporary files to the current directory
126and do not delete them on exit. Hidden option, useful for debugging.</li>
127<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
128their standard meaning.</li>
129</ul>
Reid Spencerb1254a12004-08-09 03:08:29 +0000130</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000131<div class="section" id="compiling-llvmc-plugins">
132<h1><a class="toc-backref" href="#id7">Compiling LLVMC plugins</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000133<p>It's easiest to start working on your own LLVMC plugin by copying the
134skeleton 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 +0000135<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000136$ cd $LLVMC_DIR/plugins
137$ cp -r Simple MyPlugin
138$ cd MyPlugin
139$ ls
140Makefile PluginMain.cpp Simple.td
Anton Korobeynikov74120312008-06-09 04:15:49 +0000141</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000142<p>As you can see, our basic plugin consists of only two files (not
143counting the build script). <tt class="docutils literal"><span class="pre">Simple.td</span></tt> contains TableGen
144description of the compilation graph; its format is documented in the
145following sections. <tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> is just a helper file used to
146compile the auto-generated C++ code produced from TableGen source. It
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000147can also contain hook definitions (see <a class="reference internal" href="#hooks">below</a>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000148<p>The first thing that you should do is to change the <tt class="docutils literal"><span class="pre">LLVMC_PLUGIN</span></tt>
149variable in the <tt class="docutils literal"><span class="pre">Makefile</span></tt> to avoid conflicts (since this variable
150is used to name the resulting library):</p>
151<pre class="literal-block">
152LLVMC_PLUGIN=MyPlugin
153</pre>
154<p>It is also a good idea to rename <tt class="docutils literal"><span class="pre">Simple.td</span></tt> to something less
155generic:</p>
156<pre class="literal-block">
157$ mv Simple.td MyPlugin.td
158</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000159<p>To build your plugin as a dynamic library, just <tt class="docutils literal"><span class="pre">cd</span></tt> to its source
160directory and run <tt class="docutils literal"><span class="pre">make</span></tt>. The resulting file will be called
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000161<tt class="docutils literal"><span class="pre">plugin_llvmc_$(LLVMC_PLUGIN).$(DLL_EXTENSION)</span></tt> (in our case,
162<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 +0000163<tt class="docutils literal"><span class="pre">-load</span></tt> option. Example:</p>
164<pre class="literal-block">
165$ cd $LLVMC_DIR/plugins/Simple
166$ make
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000167$ llvmc -load $LLVM_DIR/Release/lib/plugin_llvmc_Simple.so
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000168</pre>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000169</div>
170<div class="section" id="compiling-standalone-llvmc-based-drivers">
171<h1><a class="toc-backref" href="#id8">Compiling standalone LLVMC-based drivers</a></h1>
172<p>By default, the <tt class="docutils literal"><span class="pre">llvmc</span></tt> executable consists of a driver core plus several
173statically 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
174produce a standalone LLVMC-based driver executable by linking the core with your
175own plugins. The recommended way to do this is by starting with the provided
176<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>
177<pre class="literal-block">
178$ cd $LLVMC_DIR/example/
179$ cp -r Skeleton mydriver
180$ cd mydriver
181$ vim Makefile
182[...]
183$ make
184</pre>
185<p>If you're compiling LLVM with different source and object directories, then you
186must perform the following additional steps before running <tt class="docutils literal"><span class="pre">make</span></tt>:</p>
187<pre class="literal-block">
188# LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/
189# LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/
190$ cp $LLVMC_SRC_DIR/example/mydriver/Makefile \
191 $LLVMC_OBJ_DIR/example/mydriver/
192$ cd $LLVMC_OBJ_DIR/example/mydriver
193$ make
194</pre>
195<p>Another way to do the same thing is by using the following command:</p>
196<pre class="literal-block">
197$ cd $LLVMC_DIR
198$ make LLVMC_BUILTIN_PLUGINS=MyPlugin LLVMC_BASED_DRIVER_NAME=mydriver
199</pre>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000200<p>This works with both srcdir == objdir and srcdir != objdir, but assumes that the
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000201plugin 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 +0000202<p>Sometimes, you will want a 'bare-bones' version of LLVMC that has no
203built-in plugins. It can be compiled with the following command:</p>
204<pre class="literal-block">
205$ cd $LLVMC_DIR
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000206$ make LLVMC_BUILTIN_PLUGINS=&quot;&quot;
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000207</pre>
208</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000209<div class="section" id="customizing-llvmc-the-compilation-graph">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000210<h1><a class="toc-backref" href="#id9">Customizing LLVMC: the compilation graph</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000211<p>Each TableGen configuration file should include the common
212definitions:</p>
213<pre class="literal-block">
214include &quot;llvm/CompilerDriver/Common.td&quot;
215</pre>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000216<p>Internally, LLVMC stores information about possible source
217transformations in form of a graph. Nodes in this graph represent
218tools, and edges between two nodes represent a transformation path. A
219special &quot;root&quot; node is used to mark entry points for the
220transformations. LLVMC also assigns a weight to each edge (more on
221this later) to choose between several alternative edges.</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000222<p>The definition of the compilation graph (see file
223<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 +0000224<pre class="literal-block">
225def CompilationGraph : CompilationGraph&lt;[
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000226 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_c&quot;&gt;,
227 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_assembler&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000228 ...
229
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000230 Edge&lt;&quot;llvm_gcc_c&quot;, &quot;llc&quot;&gt;,
231 Edge&lt;&quot;llvm_gcc_cpp&quot;, &quot;llc&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000232 ...
233
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000234 OptionalEdge&lt;&quot;llvm_gcc_c&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
235 (inc_weight))&gt;,
236 OptionalEdge&lt;&quot;llvm_gcc_cpp&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
237 (inc_weight))&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000238 ...
239
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000240 OptionalEdge&lt;&quot;llvm_gcc_assembler&quot;, &quot;llvm_gcc_cpp_linker&quot;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000241 (case (input_languages_contain &quot;c++&quot;), (inc_weight),
242 (or (parameter_equals &quot;linker&quot;, &quot;g++&quot;),
243 (parameter_equals &quot;linker&quot;, &quot;c++&quot;)), (inc_weight))&gt;,
244 ...
245
246 ]&gt;;
247</pre>
248<p>As you can see, the edges can be either default or optional, where
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000249optional edges are differentiated by an additional <tt class="docutils literal"><span class="pre">case</span></tt> expression
250used to calculate the weight of this edge. Notice also that we refer
251to tools via their names (as strings). This makes it possible to add
252edges to an existing compilation graph in plugins without having to
253know about all tool definitions used in the graph.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000254<p>The default edges are assigned a weight of 1, and optional edges get a
255weight of 0 + 2*N where N is the number of tests that evaluated to
256true in the <tt class="docutils literal"><span class="pre">case</span></tt> expression. It is also possible to provide an
257integer 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,
258the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000259of the default 2. It is also possible to change the default weight of
260an 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>
261construct.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000262<p>When passing an input file through the graph, LLVMC picks the edge
263with the maximum weight. To avoid ambiguity, there should be only one
264default edge between two nodes (with the exception of the root node,
265which gets a special treatment - there you are allowed to specify one
266default edge <em>per language</em>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000267<p>When multiple plugins are loaded, their compilation graphs are merged
268together. Since multiple edges that have the same end nodes are not
269allowed (i.e. the graph is not a multigraph), an edge defined in
270several plugins will be replaced by the definition from the plugin
271that was loaded last. Plugin load order can be controlled by using the
272plugin priority feature described above.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000273<p>To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000274debugging), 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 +0000275<tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000276</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000277<div class="section" id="describing-options">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000278<h1><a class="toc-backref" href="#id10">Describing options</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000279<p>Command-line options that the plugin supports are defined by using an
280<tt class="docutils literal"><span class="pre">OptionList</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000281<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000282def Options : OptionList&lt;[
283(switch_option &quot;E&quot;, (help &quot;Help string&quot;)),
284(alias_option &quot;quiet&quot;, &quot;q&quot;)
285...
286]&gt;;
Anton Korobeynikov74120312008-06-09 04:15:49 +0000287</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000288<p>As you can see, the option list is just a list of DAGs, where each DAG
289is an option description consisting of the option name and some
290properties. A plugin can define more than one option list (they are
291all merged together in the end), which can be handy if one wants to
292separate option groups syntactically.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000293<ul>
294<li><p class="first">Possible option types:</p>
295<blockquote>
296<ul class="simple">
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000297<li><tt class="docutils literal"><span class="pre">switch_option</span></tt> - a simple boolean switch without arguments, for example
298<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>
299<li><tt class="docutils literal"><span class="pre">parameter_option</span></tt> - option that takes one argument, for example
300<tt class="docutils literal"><span class="pre">-std=c99</span></tt>. It is also allowed to use spaces instead of the equality
301sign: <tt class="docutils literal"><span class="pre">-std</span> <span class="pre">c99</span></tt>. At most one occurrence is allowed.</li>
302<li><tt class="docutils literal"><span class="pre">parameter_list_option</span></tt> - same as the above, but more than one option
303occurence is allowed.</li>
304<li><tt class="docutils literal"><span class="pre">prefix_option</span></tt> - same as the parameter_option, but the option name and
305argument do not have to be separated. Example: <tt class="docutils literal"><span class="pre">-ofile</span></tt>. This can be also
306specified 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
307(<tt class="docutils literal"><span class="pre">=file</span></tt> will be interpreted as option value). At most one occurrence is
308allowed.</li>
309<li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one occurence of
310the option is allowed; example: <tt class="docutils literal"><span class="pre">-lm</span> <span class="pre">-lpthread</span></tt>.</li>
311<li><tt class="docutils literal"><span class="pre">alias_option</span></tt> - a special option type for creating aliases. Unlike other
312option types, aliases are not allowed to have any properties besides the
313aliased 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 +0000314</ul>
315</blockquote>
316</li>
317<li><p class="first">Possible option properties:</p>
318<blockquote>
319<ul class="simple">
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000320<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>
321output.</li>
322<li><tt class="docutils literal"><span class="pre">required</span></tt> - this option must be specified exactly once (or, in case of
323the list options without the <tt class="docutils literal"><span class="pre">multi_val</span></tt> property, at least
324once). 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>
325<li><tt class="docutils literal"><span class="pre">one_or_more</span></tt> - the option must be specified at least one time. Useful
326only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>; for ordinary lists
327it 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
328<tt class="docutils literal"><span class="pre">zero_or_one</span></tt>.</li>
329<li><tt class="docutils literal"><span class="pre">zero_or_one</span></tt> - the option can be specified zero or one times. Useful
330only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>. Incompatible with
331<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 +0000332<li><tt class="docutils literal"><span class="pre">hidden</span></tt> - the description of this option will not appear in
333the <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>
334output).</li>
335<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 +0000336output.</li>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000337<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
338special 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>
339<span class="pre">3))</span></tt>. Only list options can have this attribute; you can, however, use
340the <tt class="docutils literal"><span class="pre">one_or_more</span></tt> and <tt class="docutils literal"><span class="pre">zero_or_one</span></tt> properties.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000341<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 +0000342</ul>
343</blockquote>
344</li>
345</ul>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000346<div class="section" id="external-options">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000347<h2><a class="toc-backref" href="#id11">External options</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000348<p>Sometimes, when linking several plugins together, one plugin needs to
349access options defined in some other plugin. Because of the way
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000350options are implemented, such options must be marked as
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000351<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
352for. Example:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000353<pre class="literal-block">
Anton Korobeynikov74120312008-06-09 04:15:49 +0000354...
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000355(switch_option &quot;E&quot;, (extern))
356...
Anton Korobeynikov74120312008-06-09 04:15:49 +0000357</pre>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000358<p>See also the section on plugin <a class="reference internal" href="#priorities">priorities</a>.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000359</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000360</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000361<div class="section" id="conditional-evaluation">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000362<span id="case"></span><h1><a class="toc-backref" href="#id12">Conditional evaluation</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000363<p>The 'case' construct is the main means by which programmability is
364achieved in LLVMC. It can be used to calculate edge weights, program
365actions and modify the shell commands to be executed. The 'case'
366expression is designed after the similarly-named construct in
367functional 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>
368<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
369are evaluated only if the corresponding tests evaluate to true.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000370<p>Examples:</p>
371<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000372// Edge weight calculation
373
Anton Korobeynikov74120312008-06-09 04:15:49 +0000374// Increases edge weight by 5 if &quot;-A&quot; is provided on the
375// command-line, and by 5 more if &quot;-B&quot; is also provided.
376(case
377 (switch_on &quot;A&quot;), (inc_weight 5),
378 (switch_on &quot;B&quot;), (inc_weight 5))
379
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000380
381// Tool command line specification
382
383// Evaluates to &quot;cmdline1&quot; if the option &quot;-A&quot; is provided on the
384// command line; to &quot;cmdline2&quot; if &quot;-B&quot; is provided;
385// otherwise to &quot;cmdline3&quot;.
386
Anton Korobeynikov74120312008-06-09 04:15:49 +0000387(case
388 (switch_on &quot;A&quot;), &quot;cmdline1&quot;,
389 (switch_on &quot;B&quot;), &quot;cmdline2&quot;,
390 (default), &quot;cmdline3&quot;)
391</pre>
392<p>Note the slight difference in 'case' expression handling in contexts
393of edge weights and command line specification - in the second example
394the 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
395enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">&quot;cmdline1&quot;</span></tt> in
396that case.</p>
397<p>Case expressions can also be nested, i.e. the following is legal:</p>
398<pre class="literal-block">
399(case (switch_on &quot;E&quot;), (case (switch_on &quot;o&quot;), ..., (default), ...)
400 (default), ...)
401</pre>
402<p>You should, however, try to avoid doing that because it hurts
403readability. It is usually better to split tool descriptions and/or
404use TableGen inheritance instead.</p>
405<ul class="simple">
406<li>Possible tests are:<ul>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000407<li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is
408provided 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 +0000409<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 +0000410a given value.
411Example: <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>
412<li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter
413list contains a given value.
414Example: <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 +0000415<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 +0000416belongs to the current input language set.
417Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
418<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language
419equals to the argument. At the moment works only with <tt class="docutils literal"><span class="pre">cmd_line</span></tt>
420and <tt class="docutils literal"><span class="pre">actions</span></tt> (on non-join nodes).
421Example: <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 +0000422<li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be
423either a parameter or a parameter list) is set by the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000424user.
425Example: <tt class="docutils literal"><span class="pre">(not_empty</span> <span class="pre">&quot;o&quot;)</span></tt>.</li>
Mikhail Glushenkov2d0484c2008-12-17 02:47:30 +0000426<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>
427<span class="pre">X))</span></tt>. Provided for convenience.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000428<li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last
429test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li>
430<li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard logical combinator that returns true iff all
431of its arguments return true. Used like this: <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(test1),</span>
432<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,
433but not encouraged.</li>
434<li><tt class="docutils literal"><span class="pre">or</span></tt> - Another logical combinator that returns true only if any
435one of its arguments returns true. Example: <tt class="docutils literal"><span class="pre">(or</span> <span class="pre">(test1),</span>
436<span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>.</li>
437</ul>
438</li>
439</ul>
Reid Spencer46d21922004-08-22 18:06:59 +0000440</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000441<div class="section" id="writing-a-tool-description">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000442<h1><a class="toc-backref" href="#id13">Writing a tool description</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000443<p>As was said earlier, nodes in the compilation graph represent tools,
444which are described separately. A tool definition looks like this
445(taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p>
446<pre class="literal-block">
447def llvm_gcc_cpp : Tool&lt;[
448 (in_language &quot;c++&quot;),
449 (out_language &quot;llvm-assembler&quot;),
450 (output_suffix &quot;bc&quot;),
451 (cmd_line &quot;llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm&quot;),
452 (sink)
453 ]&gt;;
454</pre>
455<p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for
456<tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of
457properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt>
458property means that this tool should be passed all command-line
459options that aren't mentioned in the option list.</p>
460<p>The complete list of all currently implemented tool properties follows.</p>
461<ul class="simple">
462<li>Possible tool properties:<ul>
463<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a
464list, in case the tool supports multiple input languages.</li>
465<li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Tools are not allowed to
466have multiple output languages.</li>
467<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed
468dynamically, see documentation on actions.</li>
469<li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
470use <tt class="docutils literal"><span class="pre">$INFILE</span></tt> and <tt class="docutils literal"><span class="pre">$OUTFILE</span></tt> variables, output redirection
471with <tt class="docutils literal"><span class="pre">&gt;</span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables
472(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>
473<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
474list of input files and joins them together. Used for linkers.</li>
475<li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other
476tools are passed to this tool.</li>
477<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
478this tool reacts on command-line options (described in more detail
479below).</li>
480</ul>
481</li>
482</ul>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000483<div class="section" id="actions">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000484<h2><a class="toc-backref" href="#id14">Actions</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000485<p>A tool often needs to react to command-line options, and this is
486precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example
487illustrates this feature:</p>
488<pre class="literal-block">
489def llvm_gcc_linker : Tool&lt;[
490 (in_language &quot;object-code&quot;),
491 (out_language &quot;executable&quot;),
492 (output_suffix &quot;out&quot;),
493 (cmd_line &quot;llvm-gcc $INFILE -o $OUTFILE&quot;),
494 (join),
495 (actions (case (not_empty &quot;L&quot;), (forward &quot;L&quot;),
496 (not_empty &quot;l&quot;), (forward &quot;l&quot;),
497 (not_empty &quot;dummy&quot;),
498 [(append_cmd &quot;-dummy1&quot;), (append_cmd &quot;-dummy2&quot;)])
499 ]&gt;;
500</pre>
501<p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent
502<tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em>
503with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>,
504which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which
505appends a given string to the tool execution command. Multiple actions
506can be associated with a single condition by using a list of actions
507(used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt>
508construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the
509tool command line.</p>
510<p>The &quot;join&quot; property used in the example means that this tool behaves
511like a linker.</p>
512<p>The list of all possible actions follows.</p>
513<ul>
514<li><p class="first">Possible actions:</p>
515<blockquote>
516<ul class="simple">
517<li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - append a string to the tool invocation
518command.
Mikhail Glushenkov2d0484c2008-12-17 02:47:30 +0000519Example: <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>
520<span class="pre">&quot;-lpthread&quot;))</span></tt></li>
521<li><tt class="docutils literal"><span class="pre">error`</span> <span class="pre">-</span> <span class="pre">exit</span> <span class="pre">with</span> <span class="pre">error.</span>
522<span class="pre">Example:</span> <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 Glushenkov68319f82008-12-11 23:24:40 +0000523<li><tt class="docutils literal"><span class="pre">forward</span></tt> - forward an option unchanged.
524Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">&quot;Wall&quot;)</span></tt>.</li>
525<li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the name of an option, but forward the
526argument unchanged.
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000527Example: <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 Glushenkov68319f82008-12-11 23:24:40 +0000528<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - modify the output suffix of this
529tool.
530Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">&quot;i&quot;)</span></tt>.</li>
531<li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - stop compilation after this tool processes
532its input. Used without arguments.</li>
533<li><tt class="docutils literal"><span class="pre">unpack_values</span></tt> - used for for splitting and forwarding
534comma-separated lists of options, e.g. <tt class="docutils literal"><span class="pre">-Wa,-foo=bar,-baz</span></tt> is
535converted to <tt class="docutils literal"><span class="pre">-foo=bar</span> <span class="pre">-baz</span></tt> and appended to the tool invocation
536command.
537Example: <tt class="docutils literal"><span class="pre">(unpack_values</span> <span class="pre">&quot;Wa,&quot;)</span></tt>.</li>
538</ul>
539</blockquote>
540</li>
541</ul>
542</div>
543</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000544<div class="section" id="language-map">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000545<h1><a class="toc-backref" href="#id15">Language map</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000546<p>If you are adding support for a new language to LLVMC, you'll need to
547modify the language map, which defines mappings from file extensions
548to language names. It is used to choose the proper toolchain(s) for a
549given input file set. Language map definition looks like this:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000550<pre class="literal-block">
551def LanguageMap : LanguageMap&lt;
552 [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;,
553 LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;,
554 ...
555 ]&gt;;
556</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000557<p>For example, without those definitions the following command wouldn't work:</p>
558<pre class="literal-block">
559$ llvmc hello.cpp
560llvmc: Unknown suffix: cpp
561</pre>
562<p>The language map entries should be added only for tools that are
563linked with the root node. Since tools are not allowed to have
564multiple output languages, for nodes &quot;inside&quot; the graph the input and
565output languages should match. This is enforced at compile-time.</p>
Reid Spencer46d21922004-08-22 18:06:59 +0000566</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000567<div class="section" id="more-advanced-topics">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000568<h1><a class="toc-backref" href="#id16">More advanced topics</a></h1>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000569<div class="section" id="hooks-and-environment-variables">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000570<span id="hooks"></span><h2><a class="toc-backref" href="#id17">Hooks and environment variables</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000571<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 +0000572this is not sufficient: for example, we may want to specify tool paths
573or names in the configuration file. This can be easily achieved via
574the hooks mechanism. To write your own hooks, just add their
575definitions 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
576your plugin directory. Hooks should live in the <tt class="docutils literal"><span class="pre">hooks</span></tt> namespace
577and 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>
578<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
579<tt class="docutils literal"><span class="pre">cmd_line</span></tt> tool property:</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000580<pre class="literal-block">
581(cmd_line &quot;$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)&quot;)
582</pre>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000583<p>To pass arguments to hooks, use the following syntax:</p>
584<pre class="literal-block">
585(cmd_line &quot;$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2&quot;)
586</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000587<p>It is also possible to use environment variables in the same manner:</p>
588<pre class="literal-block">
589(cmd_line &quot;$ENV(VAR1)/path/to/file -o $ENV(VAR2)&quot;)
590</pre>
591<p>To change the command line string based on user-provided options use
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000592the <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 +0000593<pre class="literal-block">
594(cmd_line
595 (case
596 (switch_on &quot;E&quot;),
597 &quot;llvm-g++ -E -x c $INFILE -o $OUTFILE&quot;,
598 (default),
599 &quot;llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm&quot;))
600</pre>
601</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000602<div class="section" id="how-plugins-are-loaded">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000603<span id="priorities"></span><h2><a class="toc-backref" href="#id18">How plugins are loaded</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000604<p>It is possible for LLVMC plugins to depend on each other. For example,
605one can create edges between nodes defined in some other plugin. To
606make this work, however, that plugin should be loaded first. To
607achieve this, the concept of plugin priority was introduced. By
608default, every plugin has priority zero; to specify the priority
609explicitly, put the following line in your plugin's TableGen file:</p>
610<pre class="literal-block">
611def Priority : PluginPriority&lt;$PRIORITY_VALUE&gt;;
612# Where PRIORITY_VALUE is some integer &gt; 0
613</pre>
614<p>Plugins are loaded in order of their (increasing) priority, starting
615with 0. Therefore, the plugin with the highest priority value will be
616loaded last.</p>
617</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000618<div class="section" id="debugging">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000619<h2><a class="toc-backref" href="#id19">Debugging</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000620<p>When writing LLVMC plugins, it can be useful to get a visual view of
621the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000622line 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
623<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 +0000624creates 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 +0000625current directory.</p>
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000626<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
627compilation graph for common errors like mismatched output/input
628language names, multiple default edges and cycles. These checks can't
629be performed at compile-time because the plugins can load code
630dynamically. 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
631perform any compilation tasks and returns the number of encountered
632errors as its status code.</p>
Mikhail Glushenkov90531542008-12-11 23:43:14 +0000633<hr />
Anton Korobeynikov28b66702008-06-09 04:17:51 +0000634<address>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000635<a href="http://jigsaw.w3.org/css-validator/check/referer">
636<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
637 alt="Valid CSS" /></a>
638<a href="http://validator.w3.org/check?uri=referer">
639<img src="http://www.w3.org/Icons/valid-xhtml10-blue"
640 alt="Valid XHTML 1.0 Transitional"/></a>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000641
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000642<a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
643<a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000644
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000645Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000646</address></div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000647</div>
648</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000649</body>
650</html>