blob: e77cd03e0590013609548133f39f5f3e66706612 [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 Glushenkove25b8452009-06-30 00:16:43 +000040<li><a class="reference internal" href="#conditioning-on-the-executable-name" id="id20">Conditioning on the executable name</a></li>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000041</ul>
42</li>
43</ul>
44</div>
45<div class="doc_author">
46<p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000047</div><div class="section" id="introduction">
48<h1><a class="toc-backref" href="#id4">Introduction</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000049<p>LLVMC is a generic compiler driver, designed to be customizable and
50extensible. It plays the same role for LLVM as the <tt class="docutils literal"><span class="pre">gcc</span></tt> program
51does for GCC - LLVMC's job is essentially to transform a set of input
52files into a set of targets depending on configuration rules and user
53options. What makes LLVMC different is that these transformation rules
54are completely customizable - in fact, LLVMC knows nothing about the
55specifics of transformation (even the command-line options are mostly
56not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000057abstract graph. The structure of this graph is completely determined
58by plugins, which can be either statically or dynamically linked. This
59makes it possible to easily adapt LLVMC for other purposes - for
60example, as a build tool for game resources.</p>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +000061<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 +000062need to be familiar with it to customize LLVMC.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +000063</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +000064<div class="section" id="compiling-with-llvmc">
65<h1><a class="toc-backref" href="#id5">Compiling with LLVMC</a></h1>
Anton Korobeynikov74120312008-06-09 04:15:49 +000066<p>LLVMC tries hard to be as compatible with <tt class="docutils literal"><span class="pre">gcc</span></tt> as possible,
67although there are some small differences. Most of the time, however,
68you shouldn't be able to notice them:</p>
69<pre class="literal-block">
70$ # This works as expected:
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000071$ llvmc -O3 -Wall hello.cpp
Anton Korobeynikov74120312008-06-09 04:15:49 +000072$ ./a.out
73hello
74</pre>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +000075<p>One nice feature of LLVMC is that one doesn't have to distinguish between
76different 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
77right toolchain is chosen automatically based on input language names (which
78are, in turn, determined from file extensions). If you want to force files
79ending 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
80do it with <tt class="docutils literal"><span class="pre">gcc</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +000081<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000082$ # hello.c is really a C++ file
83$ llvmc -x c++ hello.c
Anton Korobeynikov74120312008-06-09 04:15:49 +000084$ ./a.out
85hello
86</pre>
87<p>On the other hand, when using LLVMC as a linker to combine several C++
88object files you should provide the <tt class="docutils literal"><span class="pre">--linker</span></tt> option since it's
89impossible for LLVMC to choose the right linker in that case:</p>
90<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000091$ llvmc -c hello.cpp
92$ llvmc hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000093[A lot of link-time errors skipped]
Mikhail Glushenkov68319f82008-12-11 23:24:40 +000094$ llvmc --linker=c++ hello.o
Anton Korobeynikov74120312008-06-09 04:15:49 +000095$ ./a.out
96hello
97</pre>
Mikhail Glushenkove25b8452009-06-30 00:16:43 +000098<p>By default, LLVMC uses <tt class="docutils literal"><span class="pre">llvm-gcc</span></tt> to compile the source code. It is also
99possible 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 +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 Glushenkovd7acf732009-06-25 18:21:10 +0000112<li><tt class="docutils literal"><span class="pre">--save-temps</span></tt> - Write temporary files to the current directory and do not
113delete them on exit. This option can also take an argument: the
114<tt class="docutils literal"><span class="pre">--save-temps=obj</span></tt> switch will write files into the directory specified with
115the <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
116both synonyms for the default behaviour.</li>
Mikhail Glushenkov98a39612009-07-09 19:39:49 +0000117<li><tt class="docutils literal"><span class="pre">--temp-dir</span></tt> - Write temporary files to the specified directory. This option
118overrides <tt class="docutils literal"><span class="pre">--save-temps</span></tt>.</li>
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000119<li><tt class="docutils literal"><span class="pre">--check-graph</span></tt> - Check the compilation for common errors like mismatched
120output/input language names, multiple default edges and cycles. Because of
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000121plugins, these checks can't be performed at compile-time. Exit with code zero
122if no errors were found, and return the number of found errors
123otherwise. Hidden option, useful for debugging LLVMC plugins.</li>
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000124<li><tt class="docutils literal"><span class="pre">--view-graph</span></tt> - Show a graphical representation of the compilation graph
125and 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
126option, useful for debugging LLVMC plugins.</li>
127<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
128directory with the compilation graph description in Graphviz format (identical
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000129to 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
130used to set the output file name. Hidden option, useful for debugging LLVMC
131plugins.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000132<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
133their standard meaning.</li>
134</ul>
Reid Spencerb1254a12004-08-09 03:08:29 +0000135</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000136<div class="section" id="compiling-llvmc-plugins">
137<h1><a class="toc-backref" href="#id7">Compiling LLVMC plugins</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000138<p>It's easiest to start working on your own LLVMC plugin by copying the
139skeleton 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 +0000140<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000141$ cd $LLVMC_DIR/plugins
142$ cp -r Simple MyPlugin
143$ cd MyPlugin
144$ ls
145Makefile PluginMain.cpp Simple.td
Anton Korobeynikov74120312008-06-09 04:15:49 +0000146</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000147<p>As you can see, our basic plugin consists of only two files (not
148counting the build script). <tt class="docutils literal"><span class="pre">Simple.td</span></tt> contains TableGen
149description of the compilation graph; its format is documented in the
150following sections. <tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> is just a helper file used to
151compile the auto-generated C++ code produced from TableGen source. It
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000152can also contain hook definitions (see <a class="reference internal" href="#hooks">below</a>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000153<p>The first thing that you should do is to change the <tt class="docutils literal"><span class="pre">LLVMC_PLUGIN</span></tt>
154variable in the <tt class="docutils literal"><span class="pre">Makefile</span></tt> to avoid conflicts (since this variable
155is used to name the resulting library):</p>
156<pre class="literal-block">
157LLVMC_PLUGIN=MyPlugin
158</pre>
159<p>It is also a good idea to rename <tt class="docutils literal"><span class="pre">Simple.td</span></tt> to something less
160generic:</p>
161<pre class="literal-block">
162$ mv Simple.td MyPlugin.td
163</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000164<p>To build your plugin as a dynamic library, just <tt class="docutils literal"><span class="pre">cd</span></tt> to its source
165directory 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 +0000166<tt class="docutils literal"><span class="pre">plugin_llvmc_$(LLVMC_PLUGIN).$(DLL_EXTENSION)</span></tt> (in our case,
167<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 +0000168<tt class="docutils literal"><span class="pre">-load</span></tt> option. Example:</p>
169<pre class="literal-block">
170$ cd $LLVMC_DIR/plugins/Simple
171$ make
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000172$ llvmc -load $LLVM_DIR/Release/lib/plugin_llvmc_Simple.so
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000173</pre>
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000174</div>
175<div class="section" id="compiling-standalone-llvmc-based-drivers">
176<h1><a class="toc-backref" href="#id8">Compiling standalone LLVMC-based drivers</a></h1>
177<p>By default, the <tt class="docutils literal"><span class="pre">llvmc</span></tt> executable consists of a driver core plus several
178statically 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
179produce a standalone LLVMC-based driver executable by linking the core with your
180own plugins. The recommended way to do this is by starting with the provided
181<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>
182<pre class="literal-block">
183$ cd $LLVMC_DIR/example/
184$ cp -r Skeleton mydriver
185$ cd mydriver
186$ vim Makefile
187[...]
188$ make
189</pre>
190<p>If you're compiling LLVM with different source and object directories, then you
191must perform the following additional steps before running <tt class="docutils literal"><span class="pre">make</span></tt>:</p>
192<pre class="literal-block">
193# LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/
194# LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/
195$ cp $LLVMC_SRC_DIR/example/mydriver/Makefile \
196 $LLVMC_OBJ_DIR/example/mydriver/
197$ cd $LLVMC_OBJ_DIR/example/mydriver
198$ make
199</pre>
200<p>Another way to do the same thing is by using the following command:</p>
201<pre class="literal-block">
202$ cd $LLVMC_DIR
203$ make LLVMC_BUILTIN_PLUGINS=MyPlugin LLVMC_BASED_DRIVER_NAME=mydriver
204</pre>
Mikhail Glushenkov63dc3182009-06-17 02:56:48 +0000205<p>This works with both srcdir == objdir and srcdir != objdir, but assumes that the
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000206plugin 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 +0000207<p>Sometimes, you will want a 'bare-bones' version of LLVMC that has no
208built-in plugins. It can be compiled with the following command:</p>
209<pre class="literal-block">
210$ cd $LLVMC_DIR
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000211$ make LLVMC_BUILTIN_PLUGINS=&quot;&quot;
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000212</pre>
213</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000214<div class="section" id="customizing-llvmc-the-compilation-graph">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000215<h1><a class="toc-backref" href="#id9">Customizing LLVMC: the compilation graph</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000216<p>Each TableGen configuration file should include the common
217definitions:</p>
218<pre class="literal-block">
219include &quot;llvm/CompilerDriver/Common.td&quot;
220</pre>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000221<p>Internally, LLVMC stores information about possible source
222transformations in form of a graph. Nodes in this graph represent
223tools, and edges between two nodes represent a transformation path. A
224special &quot;root&quot; node is used to mark entry points for the
225transformations. LLVMC also assigns a weight to each edge (more on
226this later) to choose between several alternative edges.</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000227<p>The definition of the compilation graph (see file
228<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 +0000229<pre class="literal-block">
230def CompilationGraph : CompilationGraph&lt;[
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000231 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_c&quot;&gt;,
232 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_assembler&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000233 ...
234
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000235 Edge&lt;&quot;llvm_gcc_c&quot;, &quot;llc&quot;&gt;,
236 Edge&lt;&quot;llvm_gcc_cpp&quot;, &quot;llc&quot;&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000237 ...
238
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000239 OptionalEdge&lt;&quot;llvm_gcc_c&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
240 (inc_weight))&gt;,
241 OptionalEdge&lt;&quot;llvm_gcc_cpp&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
242 (inc_weight))&gt;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000243 ...
244
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000245 OptionalEdge&lt;&quot;llvm_gcc_assembler&quot;, &quot;llvm_gcc_cpp_linker&quot;,
Anton Korobeynikov74120312008-06-09 04:15:49 +0000246 (case (input_languages_contain &quot;c++&quot;), (inc_weight),
247 (or (parameter_equals &quot;linker&quot;, &quot;g++&quot;),
248 (parameter_equals &quot;linker&quot;, &quot;c++&quot;)), (inc_weight))&gt;,
249 ...
250
251 ]&gt;;
252</pre>
253<p>As you can see, the edges can be either default or optional, where
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000254optional edges are differentiated by an additional <tt class="docutils literal"><span class="pre">case</span></tt> expression
255used to calculate the weight of this edge. Notice also that we refer
256to tools via their names (as strings). This makes it possible to add
257edges to an existing compilation graph in plugins without having to
258know about all tool definitions used in the graph.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000259<p>The default edges are assigned a weight of 1, and optional edges get a
260weight of 0 + 2*N where N is the number of tests that evaluated to
261true in the <tt class="docutils literal"><span class="pre">case</span></tt> expression. It is also possible to provide an
262integer 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,
263the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000264of the default 2. It is also possible to change the default weight of
265an 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>
266construct.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000267<p>When passing an input file through the graph, LLVMC picks the edge
268with the maximum weight. To avoid ambiguity, there should be only one
269default edge between two nodes (with the exception of the root node,
270which gets a special treatment - there you are allowed to specify one
271default edge <em>per language</em>).</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000272<p>When multiple plugins are loaded, their compilation graphs are merged
273together. Since multiple edges that have the same end nodes are not
274allowed (i.e. the graph is not a multigraph), an edge defined in
275several plugins will be replaced by the definition from the plugin
276that was loaded last. Plugin load order can be controlled by using the
277plugin priority feature described above.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000278<p>To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000279debugging), 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 +0000280<tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000281</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000282<div class="section" id="describing-options">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000283<h1><a class="toc-backref" href="#id10">Describing options</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000284<p>Command-line options that the plugin supports are defined by using an
285<tt class="docutils literal"><span class="pre">OptionList</span></tt>:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000286<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000287def Options : OptionList&lt;[
288(switch_option &quot;E&quot;, (help &quot;Help string&quot;)),
289(alias_option &quot;quiet&quot;, &quot;q&quot;)
290...
291]&gt;;
Anton Korobeynikov74120312008-06-09 04:15:49 +0000292</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000293<p>As you can see, the option list is just a list of DAGs, where each DAG
294is an option description consisting of the option name and some
295properties. A plugin can define more than one option list (they are
296all merged together in the end), which can be handy if one wants to
297separate option groups syntactically.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000298<ul>
299<li><p class="first">Possible option types:</p>
300<blockquote>
301<ul class="simple">
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000302<li><tt class="docutils literal"><span class="pre">switch_option</span></tt> - a simple boolean switch without arguments, for example
303<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>
304<li><tt class="docutils literal"><span class="pre">parameter_option</span></tt> - option that takes one argument, for example
305<tt class="docutils literal"><span class="pre">-std=c99</span></tt>. It is also allowed to use spaces instead of the equality
306sign: <tt class="docutils literal"><span class="pre">-std</span> <span class="pre">c99</span></tt>. At most one occurrence is allowed.</li>
307<li><tt class="docutils literal"><span class="pre">parameter_list_option</span></tt> - same as the above, but more than one option
308occurence is allowed.</li>
309<li><tt class="docutils literal"><span class="pre">prefix_option</span></tt> - same as the parameter_option, but the option name and
310argument do not have to be separated. Example: <tt class="docutils literal"><span class="pre">-ofile</span></tt>. This can be also
311specified 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
312(<tt class="docutils literal"><span class="pre">=file</span></tt> will be interpreted as option value). At most one occurrence is
313allowed.</li>
314<li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one occurence of
315the option is allowed; example: <tt class="docutils literal"><span class="pre">-lm</span> <span class="pre">-lpthread</span></tt>.</li>
316<li><tt class="docutils literal"><span class="pre">alias_option</span></tt> - a special option type for creating aliases. Unlike other
317option types, aliases are not allowed to have any properties besides the
318aliased 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 +0000319</ul>
320</blockquote>
321</li>
322<li><p class="first">Possible option properties:</p>
323<blockquote>
324<ul class="simple">
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000325<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>
326output.</li>
327<li><tt class="docutils literal"><span class="pre">required</span></tt> - this option must be specified exactly once (or, in case of
328the list options without the <tt class="docutils literal"><span class="pre">multi_val</span></tt> property, at least
329once). 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>
330<li><tt class="docutils literal"><span class="pre">one_or_more</span></tt> - the option must be specified at least one time. Useful
331only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>; for ordinary lists
332it 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
333<tt class="docutils literal"><span class="pre">zero_or_one</span></tt>.</li>
334<li><tt class="docutils literal"><span class="pre">zero_or_one</span></tt> - the option can be specified zero or one times. Useful
335only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>. Incompatible with
336<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 +0000337<li><tt class="docutils literal"><span class="pre">hidden</span></tt> - the description of this option will not appear in
338the <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>
339output).</li>
340<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 +0000341output.</li>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000342<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
343special 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>
344<span class="pre">3))</span></tt>. Only list options can have this attribute; you can, however, use
345the <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 Glushenkov640dd262009-07-07 16:09:48 +0000346<li><tt class="docutils literal"><span class="pre">init</span></tt> - this option has a default value, either a string (if it is a
347parameter), or a boolean (if it is a switch; boolean constants are called
348<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
349examples: <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>
350<span class="pre">(init</span> <span class="pre">&quot;baz&quot;))</span></tt>.</li>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000351<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 +0000352</ul>
353</blockquote>
354</li>
355</ul>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000356<div class="section" id="external-options">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000357<h2><a class="toc-backref" href="#id11">External options</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000358<p>Sometimes, when linking several plugins together, one plugin needs to
359access options defined in some other plugin. Because of the way
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000360options are implemented, such options must be marked as
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000361<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
362for. Example:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000363<pre class="literal-block">
Anton Korobeynikov74120312008-06-09 04:15:49 +0000364...
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000365(switch_option &quot;E&quot;, (extern))
366...
Anton Korobeynikov74120312008-06-09 04:15:49 +0000367</pre>
Mikhail Glushenkov15367df2009-07-07 16:44:20 +0000368<p>If an external option has additional attributes besides 'extern', they are
369ignored. See also the section on plugin <a class="reference internal" href="#priorities">priorities</a>.</p>
Reid Spencerb1254a12004-08-09 03:08:29 +0000370</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000371</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000372<div class="section" id="conditional-evaluation">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000373<span id="case"></span><h1><a class="toc-backref" href="#id12">Conditional evaluation</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000374<p>The 'case' construct is the main means by which programmability is
375achieved in LLVMC. It can be used to calculate edge weights, program
376actions and modify the shell commands to be executed. The 'case'
377expression is designed after the similarly-named construct in
378functional 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>
379<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
380are evaluated only if the corresponding tests evaluate to true.</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000381<p>Examples:</p>
382<pre class="literal-block">
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000383// Edge weight calculation
384
Anton Korobeynikov74120312008-06-09 04:15:49 +0000385// Increases edge weight by 5 if &quot;-A&quot; is provided on the
386// command-line, and by 5 more if &quot;-B&quot; is also provided.
387(case
388 (switch_on &quot;A&quot;), (inc_weight 5),
389 (switch_on &quot;B&quot;), (inc_weight 5))
390
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000391
392// Tool command line specification
393
394// Evaluates to &quot;cmdline1&quot; if the option &quot;-A&quot; is provided on the
395// command line; to &quot;cmdline2&quot; if &quot;-B&quot; is provided;
396// otherwise to &quot;cmdline3&quot;.
397
Anton Korobeynikov74120312008-06-09 04:15:49 +0000398(case
399 (switch_on &quot;A&quot;), &quot;cmdline1&quot;,
400 (switch_on &quot;B&quot;), &quot;cmdline2&quot;,
401 (default), &quot;cmdline3&quot;)
402</pre>
403<p>Note the slight difference in 'case' expression handling in contexts
404of edge weights and command line specification - in the second example
405the 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
406enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">&quot;cmdline1&quot;</span></tt> in
407that case.</p>
408<p>Case expressions can also be nested, i.e. the following is legal:</p>
409<pre class="literal-block">
410(case (switch_on &quot;E&quot;), (case (switch_on &quot;o&quot;), ..., (default), ...)
411 (default), ...)
412</pre>
413<p>You should, however, try to avoid doing that because it hurts
414readability. It is usually better to split tool descriptions and/or
415use TableGen inheritance instead.</p>
416<ul class="simple">
417<li>Possible tests are:<ul>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000418<li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is
419provided 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 +0000420<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 +0000421a given value.
422Example: <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>
423<li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter
424list contains a given value.
425Example: <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 +0000426<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 +0000427belongs to the current input language set.
428Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
429<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language
430equals to the argument. At the moment works only with <tt class="docutils literal"><span class="pre">cmd_line</span></tt>
431and <tt class="docutils literal"><span class="pre">actions</span></tt> (on non-join nodes).
432Example: <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 +0000433<li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be
434either a parameter or a parameter list) is set by the
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000435user.
436Example: <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 +0000437<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>
438<span class="pre">X))</span></tt>. Provided for convenience.</li>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000439<li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last
440test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li>
441<li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard logical combinator that returns true iff all
442of its arguments return true. Used like this: <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(test1),</span>
443<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,
444but not encouraged.</li>
445<li><tt class="docutils literal"><span class="pre">or</span></tt> - Another logical combinator that returns true only if any
446one of its arguments returns true. Example: <tt class="docutils literal"><span class="pre">(or</span> <span class="pre">(test1),</span>
447<span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>.</li>
448</ul>
449</li>
450</ul>
Reid Spencer46d21922004-08-22 18:06:59 +0000451</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000452<div class="section" id="writing-a-tool-description">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000453<h1><a class="toc-backref" href="#id13">Writing a tool description</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000454<p>As was said earlier, nodes in the compilation graph represent tools,
455which are described separately. A tool definition looks like this
456(taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p>
457<pre class="literal-block">
458def llvm_gcc_cpp : Tool&lt;[
459 (in_language &quot;c++&quot;),
460 (out_language &quot;llvm-assembler&quot;),
461 (output_suffix &quot;bc&quot;),
462 (cmd_line &quot;llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm&quot;),
463 (sink)
464 ]&gt;;
465</pre>
466<p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for
467<tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of
468properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt>
469property means that this tool should be passed all command-line
470options that aren't mentioned in the option list.</p>
471<p>The complete list of all currently implemented tool properties follows.</p>
472<ul class="simple">
473<li>Possible tool properties:<ul>
474<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a
475list, in case the tool supports multiple input languages.</li>
476<li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Tools are not allowed to
477have multiple output languages.</li>
478<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed
479dynamically, see documentation on actions.</li>
480<li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
481use <tt class="docutils literal"><span class="pre">$INFILE</span></tt> and <tt class="docutils literal"><span class="pre">$OUTFILE</span></tt> variables, output redirection
482with <tt class="docutils literal"><span class="pre">&gt;</span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables
483(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>
484<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
485list of input files and joins them together. Used for linkers.</li>
486<li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other
487tools are passed to this tool.</li>
488<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
489this tool reacts on command-line options (described in more detail
490below).</li>
491</ul>
492</li>
493</ul>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000494<div class="section" id="actions">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000495<h2><a class="toc-backref" href="#id14">Actions</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000496<p>A tool often needs to react to command-line options, and this is
497precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example
498illustrates this feature:</p>
499<pre class="literal-block">
500def llvm_gcc_linker : Tool&lt;[
501 (in_language &quot;object-code&quot;),
502 (out_language &quot;executable&quot;),
503 (output_suffix &quot;out&quot;),
504 (cmd_line &quot;llvm-gcc $INFILE -o $OUTFILE&quot;),
505 (join),
506 (actions (case (not_empty &quot;L&quot;), (forward &quot;L&quot;),
507 (not_empty &quot;l&quot;), (forward &quot;l&quot;),
508 (not_empty &quot;dummy&quot;),
509 [(append_cmd &quot;-dummy1&quot;), (append_cmd &quot;-dummy2&quot;)])
510 ]&gt;;
511</pre>
512<p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent
513<tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em>
514with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>,
515which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which
516appends a given string to the tool execution command. Multiple actions
517can be associated with a single condition by using a list of actions
518(used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt>
519construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the
520tool command line.</p>
521<p>The &quot;join&quot; property used in the example means that this tool behaves
522like a linker.</p>
523<p>The list of all possible actions follows.</p>
524<ul>
525<li><p class="first">Possible actions:</p>
526<blockquote>
527<ul class="simple">
528<li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - append a string to the tool invocation
529command.
Mikhail Glushenkov2d0484c2008-12-17 02:47:30 +0000530Example: <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>
531<span class="pre">&quot;-lpthread&quot;))</span></tt></li>
532<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>
533<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 +0000534<li><tt class="docutils literal"><span class="pre">forward</span></tt> - forward an option unchanged.
535Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">&quot;Wall&quot;)</span></tt>.</li>
536<li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the name of an option, but forward the
537argument unchanged.
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000538Example: <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 +0000539<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - modify the output suffix of this
540tool.
541Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">&quot;i&quot;)</span></tt>.</li>
542<li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - stop compilation after this tool processes
543its input. Used without arguments.</li>
544<li><tt class="docutils literal"><span class="pre">unpack_values</span></tt> - used for for splitting and forwarding
545comma-separated lists of options, e.g. <tt class="docutils literal"><span class="pre">-Wa,-foo=bar,-baz</span></tt> is
546converted to <tt class="docutils literal"><span class="pre">-foo=bar</span> <span class="pre">-baz</span></tt> and appended to the tool invocation
547command.
548Example: <tt class="docutils literal"><span class="pre">(unpack_values</span> <span class="pre">&quot;Wa,&quot;)</span></tt>.</li>
549</ul>
550</blockquote>
551</li>
552</ul>
553</div>
554</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000555<div class="section" id="language-map">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000556<h1><a class="toc-backref" href="#id15">Language map</a></h1>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000557<p>If you are adding support for a new language to LLVMC, you'll need to
558modify the language map, which defines mappings from file extensions
559to language names. It is used to choose the proper toolchain(s) for a
560given input file set. Language map definition looks like this:</p>
Anton Korobeynikov74120312008-06-09 04:15:49 +0000561<pre class="literal-block">
562def LanguageMap : LanguageMap&lt;
563 [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;,
564 LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;,
565 ...
566 ]&gt;;
567</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000568<p>For example, without those definitions the following command wouldn't work:</p>
569<pre class="literal-block">
570$ llvmc hello.cpp
571llvmc: Unknown suffix: cpp
572</pre>
573<p>The language map entries should be added only for tools that are
574linked with the root node. Since tools are not allowed to have
575multiple output languages, for nodes &quot;inside&quot; the graph the input and
576output languages should match. This is enforced at compile-time.</p>
Reid Spencer46d21922004-08-22 18:06:59 +0000577</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000578<div class="section" id="more-advanced-topics">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000579<h1><a class="toc-backref" href="#id16">More advanced topics</a></h1>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000580<div class="section" id="hooks-and-environment-variables">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000581<span id="hooks"></span><h2><a class="toc-backref" href="#id17">Hooks and environment variables</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000582<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 +0000583this is not sufficient: for example, we may want to specify tool paths
584or names in the configuration file. This can be easily achieved via
585the hooks mechanism. To write your own hooks, just add their
586definitions 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
587your plugin directory. Hooks should live in the <tt class="docutils literal"><span class="pre">hooks</span></tt> namespace
588and 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>
589<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
590<tt class="docutils literal"><span class="pre">cmd_line</span></tt> tool property:</p>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000591<pre class="literal-block">
592(cmd_line &quot;$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)&quot;)
593</pre>
Mikhail Glushenkov5303c752009-01-28 03:47:38 +0000594<p>To pass arguments to hooks, use the following syntax:</p>
595<pre class="literal-block">
596(cmd_line &quot;$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2&quot;)
597</pre>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000598<p>It is also possible to use environment variables in the same manner:</p>
599<pre class="literal-block">
600(cmd_line &quot;$ENV(VAR1)/path/to/file -o $ENV(VAR2)&quot;)
601</pre>
602<p>To change the command line string based on user-provided options use
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000603the <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 +0000604<pre class="literal-block">
605(cmd_line
606 (case
607 (switch_on &quot;E&quot;),
608 &quot;llvm-g++ -E -x c $INFILE -o $OUTFILE&quot;,
609 (default),
610 &quot;llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm&quot;))
611</pre>
612</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000613<div class="section" id="how-plugins-are-loaded">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000614<span id="priorities"></span><h2><a class="toc-backref" href="#id18">How plugins are loaded</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000615<p>It is possible for LLVMC plugins to depend on each other. For example,
616one can create edges between nodes defined in some other plugin. To
617make this work, however, that plugin should be loaded first. To
618achieve this, the concept of plugin priority was introduced. By
619default, every plugin has priority zero; to specify the priority
620explicitly, put the following line in your plugin's TableGen file:</p>
621<pre class="literal-block">
622def Priority : PluginPriority&lt;$PRIORITY_VALUE&gt;;
623# Where PRIORITY_VALUE is some integer &gt; 0
624</pre>
625<p>Plugins are loaded in order of their (increasing) priority, starting
626with 0. Therefore, the plugin with the highest priority value will be
627loaded last.</p>
628</div>
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000629<div class="section" id="debugging">
Mikhail Glushenkov502106a2009-06-16 00:14:20 +0000630<h2><a class="toc-backref" href="#id19">Debugging</a></h2>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000631<p>When writing LLVMC plugins, it can be useful to get a visual view of
632the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd6d2efc2009-05-06 01:41:47 +0000633line 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
634<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 +0000635creates 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 +0000636current directory.</p>
Mikhail Glushenkovf9b1d792009-01-15 02:42:40 +0000637<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
638compilation graph for common errors like mismatched output/input
639language names, multiple default edges and cycles. These checks can't
640be performed at compile-time because the plugins can load code
641dynamically. 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
642perform any compilation tasks and returns the number of encountered
643errors as its status code.</p>
Mikhail Glushenkove25b8452009-06-30 00:16:43 +0000644</div>
645<div class="section" id="conditioning-on-the-executable-name">
646<h2><a class="toc-backref" href="#id20">Conditioning on the executable name</a></h2>
647<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
648accessible only in the C++ code (i.e. hooks). Use the following code:</p>
649<pre class="literal-block">
650namespace llvmc {
651extern const char* ProgramName;
652}
653
654std::string MyHook() {
655//...
656if (strcmp(ProgramName, &quot;mydriver&quot;) == 0) {
657 //...
658
659}
660</pre>
661<p>In general, you're encouraged not to make the behaviour dependent on the
662executable file name, and use command-line switches instead. See for example how
663the <tt class="docutils literal"><span class="pre">Base</span></tt> plugin behaves when it needs to choose the correct linker options
664(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 +0000665<hr />
Anton Korobeynikov28b66702008-06-09 04:17:51 +0000666<address>
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000667<a href="http://jigsaw.w3.org/css-validator/check/referer">
668<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
669 alt="Valid CSS" /></a>
670<a href="http://validator.w3.org/check?uri=referer">
671<img src="http://www.w3.org/Icons/valid-xhtml10-blue"
672 alt="Valid XHTML 1.0 Transitional"/></a>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000673
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000674<a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
675<a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000676
Mikhail Glushenkov4ad0d572009-03-27 12:58:29 +0000677Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000678</address></div>
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000679</div>
680</div>
Reid Spencerb1254a12004-08-09 03:08:29 +0000681</body>
682</html>