blob: d0bb72396c48bb2600ef5add5615d95038179e02 [file] [log] [blame]
Anton Korobeynikov4167db92008-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 Spencerd5c0ccb2004-08-09 03:08:29 +00004<head>
Anton Korobeynikov4167db92008-06-09 04:15:49 +00005<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +00006<meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" />
Anton Korobeynikov4167db92008-06-09 04:15:49 +00007<title>Customizing LLVMC: Reference Manual</title>
Mikhail Glushenkov60279922008-12-13 17:50:58 +00008<link rel="stylesheet" href="llvm.css" type="text/css" />
Reid Spencerd5c0ccb2004-08-09 03:08:29 +00009</head>
10<body>
Anton Korobeynikov4167db92008-06-09 04:15:49 +000011<div class="document" id="customizing-llvmc-reference-manual">
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +000012<h1 class="title">Customizing LLVMC: Reference Manual</h1>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +000013
Mikhail Glushenkove04b5bf2008-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 Glushenkov4707bf42009-05-06 01:41:47 +000017<div class="contents topic" id="contents">
18<p class="topic-title first">Contents</p>
Mikhail Glushenkovc0363fc2008-12-13 02:28:58 +000019<ul class="simple">
Mikhail Glushenkov4707bf42009-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 Glushenkovb4b44ea2009-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 Glushenkovc0363fc2008-12-13 02:28:58 +000028</ul>
29</li>
Mikhail Glushenkovb4b44ea2009-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 Glushenkovc0363fc2008-12-13 02:28:58 +000033</ul>
34</li>
Mikhail Glushenkovb4b44ea2009-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 Glushenkovc0363fc2008-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 Glushenkov4707bf42009-05-06 01:41:47 +000046</div><div class="section" id="introduction">
47<h1><a class="toc-backref" href="#id4">Introduction</a></h1>
Anton Korobeynikov4167db92008-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 Glushenkov6fd55132008-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 Glushenkov7af1d242009-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 Korobeynikov4167db92008-06-09 04:15:49 +000061need to be familiar with it to customize LLVMC.</p>
Reid Spencerd5c0ccb2004-08-09 03:08:29 +000062</div>
Mikhail Glushenkov4707bf42009-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 Korobeynikov4167db92008-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 Glushenkov6fd55132008-12-11 23:24:40 +000070$ llvmc -O3 -Wall hello.cpp
Anton Korobeynikov4167db92008-06-09 04:15:49 +000071$ ./a.out
72hello
73</pre>
Mikhail Glushenkov7af1d242009-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 Korobeynikov4167db92008-06-09 04:15:49 +000080<pre class="literal-block">
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +000081$ # hello.c is really a C++ file
82$ llvmc -x c++ hello.c
Anton Korobeynikov4167db92008-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 Glushenkov6fd55132008-12-11 23:24:40 +000090$ llvmc -c hello.cpp
91$ llvmc hello.o
Anton Korobeynikov4167db92008-06-09 04:15:49 +000092[A lot of link-time errors skipped]
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +000093$ llvmc --linker=c++ hello.o
Anton Korobeynikov4167db92008-06-09 04:15:49 +000094$ ./a.out
95hello
96</pre>
Mikhail Glushenkov6fd55132008-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 Spencerd5c0ccb2004-08-09 03:08:29 +0000100</div>
Mikhail Glushenkov4707bf42009-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 Korobeynikov4167db92008-06-09 04:15:49 +0000103<p>LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000104configuration libraries:</p>
Anton Korobeynikov4167db92008-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 Glushenkov6fd55132008-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 Korobeynikov4167db92008-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 Glushenkov2b4a7dc2009-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 Glushenkov0f78c272009-03-27 12:58:29 +0000117<li><tt class="docutils literal"><span class="pre">--check-graph</span></tt> - Check the compilation for common errors like mismatched
118output/input language names, multiple default edges and cycles. Because of
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000119plugins, these checks can't be performed at compile-time. Exit with code zero
120if no errors were found, and return the number of found errors
121otherwise. Hidden option, useful for debugging LLVMC plugins.</li>
Mikhail Glushenkov0f78c272009-03-27 12:58:29 +0000122<li><tt class="docutils literal"><span class="pre">--view-graph</span></tt> - Show a graphical representation of the compilation graph
123and 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
124option, useful for debugging LLVMC plugins.</li>
125<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
126directory with the compilation graph description in Graphviz format (identical
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000127to 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
128used to set the output file name. Hidden option, useful for debugging LLVMC
129plugins.</li>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000130<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
131their standard meaning.</li>
132</ul>
Reid Spencerd5c0ccb2004-08-09 03:08:29 +0000133</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000134<div class="section" id="compiling-llvmc-plugins">
135<h1><a class="toc-backref" href="#id7">Compiling LLVMC plugins</a></h1>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000136<p>It's easiest to start working on your own LLVMC plugin by copying the
137skeleton project which lives under <tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins/Simple</span></tt>:</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000138<pre class="literal-block">
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000139$ cd $LLVMC_DIR/plugins
140$ cp -r Simple MyPlugin
141$ cd MyPlugin
142$ ls
143Makefile PluginMain.cpp Simple.td
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000144</pre>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000145<p>As you can see, our basic plugin consists of only two files (not
146counting the build script). <tt class="docutils literal"><span class="pre">Simple.td</span></tt> contains TableGen
147description of the compilation graph; its format is documented in the
148following sections. <tt class="docutils literal"><span class="pre">PluginMain.cpp</span></tt> is just a helper file used to
149compile the auto-generated C++ code produced from TableGen source. It
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000150can also contain hook definitions (see <a class="reference internal" href="#hooks">below</a>).</p>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000151<p>The first thing that you should do is to change the <tt class="docutils literal"><span class="pre">LLVMC_PLUGIN</span></tt>
152variable in the <tt class="docutils literal"><span class="pre">Makefile</span></tt> to avoid conflicts (since this variable
153is used to name the resulting library):</p>
154<pre class="literal-block">
155LLVMC_PLUGIN=MyPlugin
156</pre>
157<p>It is also a good idea to rename <tt class="docutils literal"><span class="pre">Simple.td</span></tt> to something less
158generic:</p>
159<pre class="literal-block">
160$ mv Simple.td MyPlugin.td
161</pre>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000162<p>To build your plugin as a dynamic library, just <tt class="docutils literal"><span class="pre">cd</span></tt> to its source
163directory and run <tt class="docutils literal"><span class="pre">make</span></tt>. The resulting file will be called
Mikhail Glushenkov7af1d242009-06-17 02:56:48 +0000164<tt class="docutils literal"><span class="pre">plugin_llvmc_$(LLVMC_PLUGIN).$(DLL_EXTENSION)</span></tt> (in our case,
165<tt class="docutils literal"><span class="pre">plugin_llvmc_MyPlugin.so</span></tt>). This library can be then loaded in with the
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000166<tt class="docutils literal"><span class="pre">-load</span></tt> option. Example:</p>
167<pre class="literal-block">
168$ cd $LLVMC_DIR/plugins/Simple
169$ make
Mikhail Glushenkov7af1d242009-06-17 02:56:48 +0000170$ llvmc -load $LLVM_DIR/Release/lib/plugin_llvmc_Simple.so
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000171</pre>
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000172</div>
173<div class="section" id="compiling-standalone-llvmc-based-drivers">
174<h1><a class="toc-backref" href="#id8">Compiling standalone LLVMC-based drivers</a></h1>
175<p>By default, the <tt class="docutils literal"><span class="pre">llvmc</span></tt> executable consists of a driver core plus several
176statically 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
177produce a standalone LLVMC-based driver executable by linking the core with your
178own plugins. The recommended way to do this is by starting with the provided
179<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>
180<pre class="literal-block">
181$ cd $LLVMC_DIR/example/
182$ cp -r Skeleton mydriver
183$ cd mydriver
184$ vim Makefile
185[...]
186$ make
187</pre>
188<p>If you're compiling LLVM with different source and object directories, then you
189must perform the following additional steps before running <tt class="docutils literal"><span class="pre">make</span></tt>:</p>
190<pre class="literal-block">
191# LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/
192# LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/
193$ cp $LLVMC_SRC_DIR/example/mydriver/Makefile \
194 $LLVMC_OBJ_DIR/example/mydriver/
195$ cd $LLVMC_OBJ_DIR/example/mydriver
196$ make
197</pre>
198<p>Another way to do the same thing is by using the following command:</p>
199<pre class="literal-block">
200$ cd $LLVMC_DIR
201$ make LLVMC_BUILTIN_PLUGINS=MyPlugin LLVMC_BASED_DRIVER_NAME=mydriver
202</pre>
Mikhail Glushenkov7af1d242009-06-17 02:56:48 +0000203<p>This works with both srcdir == objdir and srcdir != objdir, but assumes that the
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000204plugin source directory was placed under <tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins</span></tt>.</p>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000205<p>Sometimes, you will want a 'bare-bones' version of LLVMC that has no
206built-in plugins. It can be compiled with the following command:</p>
207<pre class="literal-block">
208$ cd $LLVMC_DIR
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000209$ make LLVMC_BUILTIN_PLUGINS=&quot;&quot;
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000210</pre>
211</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000212<div class="section" id="customizing-llvmc-the-compilation-graph">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000213<h1><a class="toc-backref" href="#id9">Customizing LLVMC: the compilation graph</a></h1>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000214<p>Each TableGen configuration file should include the common
215definitions:</p>
216<pre class="literal-block">
217include &quot;llvm/CompilerDriver/Common.td&quot;
218</pre>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000219<p>Internally, LLVMC stores information about possible source
220transformations in form of a graph. Nodes in this graph represent
221tools, and edges between two nodes represent a transformation path. A
222special &quot;root&quot; node is used to mark entry points for the
223transformations. LLVMC also assigns a weight to each edge (more on
224this later) to choose between several alternative edges.</p>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000225<p>The definition of the compilation graph (see file
226<tt class="docutils literal"><span class="pre">plugins/Base/Base.td</span></tt> for an example) is just a list of edges:</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000227<pre class="literal-block">
228def CompilationGraph : CompilationGraph&lt;[
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000229 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_c&quot;&gt;,
230 Edge&lt;&quot;root&quot;, &quot;llvm_gcc_assembler&quot;&gt;,
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000231 ...
232
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000233 Edge&lt;&quot;llvm_gcc_c&quot;, &quot;llc&quot;&gt;,
234 Edge&lt;&quot;llvm_gcc_cpp&quot;, &quot;llc&quot;&gt;,
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000235 ...
236
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000237 OptionalEdge&lt;&quot;llvm_gcc_c&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
238 (inc_weight))&gt;,
239 OptionalEdge&lt;&quot;llvm_gcc_cpp&quot;, &quot;opt&quot;, (case (switch_on &quot;opt&quot;),
240 (inc_weight))&gt;,
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000241 ...
242
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000243 OptionalEdge&lt;&quot;llvm_gcc_assembler&quot;, &quot;llvm_gcc_cpp_linker&quot;,
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000244 (case (input_languages_contain &quot;c++&quot;), (inc_weight),
245 (or (parameter_equals &quot;linker&quot;, &quot;g++&quot;),
246 (parameter_equals &quot;linker&quot;, &quot;c++&quot;)), (inc_weight))&gt;,
247 ...
248
249 ]&gt;;
250</pre>
251<p>As you can see, the edges can be either default or optional, where
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000252optional edges are differentiated by an additional <tt class="docutils literal"><span class="pre">case</span></tt> expression
253used to calculate the weight of this edge. Notice also that we refer
254to tools via their names (as strings). This makes it possible to add
255edges to an existing compilation graph in plugins without having to
256know about all tool definitions used in the graph.</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000257<p>The default edges are assigned a weight of 1, and optional edges get a
258weight of 0 + 2*N where N is the number of tests that evaluated to
259true in the <tt class="docutils literal"><span class="pre">case</span></tt> expression. It is also possible to provide an
260integer 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,
261the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000262of the default 2. It is also possible to change the default weight of
263an 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>
264construct.</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000265<p>When passing an input file through the graph, LLVMC picks the edge
266with the maximum weight. To avoid ambiguity, there should be only one
267default edge between two nodes (with the exception of the root node,
268which gets a special treatment - there you are allowed to specify one
269default edge <em>per language</em>).</p>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000270<p>When multiple plugins are loaded, their compilation graphs are merged
271together. Since multiple edges that have the same end nodes are not
272allowed (i.e. the graph is not a multigraph), an edge defined in
273several plugins will be replaced by the definition from the plugin
274that was loaded last. Plugin load order can be controlled by using the
275plugin priority feature described above.</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000276<p>To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000277debugging), 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 Korobeynikov4167db92008-06-09 04:15:49 +0000278<tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p>
Reid Spencerd5c0ccb2004-08-09 03:08:29 +0000279</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000280<div class="section" id="describing-options">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000281<h1><a class="toc-backref" href="#id10">Describing options</a></h1>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000282<p>Command-line options that the plugin supports are defined by using an
283<tt class="docutils literal"><span class="pre">OptionList</span></tt>:</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000284<pre class="literal-block">
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000285def Options : OptionList&lt;[
286(switch_option &quot;E&quot;, (help &quot;Help string&quot;)),
287(alias_option &quot;quiet&quot;, &quot;q&quot;)
288...
289]&gt;;
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000290</pre>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000291<p>As you can see, the option list is just a list of DAGs, where each DAG
292is an option description consisting of the option name and some
293properties. A plugin can define more than one option list (they are
294all merged together in the end), which can be handy if one wants to
295separate option groups syntactically.</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000296<ul>
297<li><p class="first">Possible option types:</p>
298<blockquote>
299<ul class="simple">
Mikhail Glushenkov84172f72009-01-28 03:47:38 +0000300<li><tt class="docutils literal"><span class="pre">switch_option</span></tt> - a simple boolean switch without arguments, for example
301<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>
302<li><tt class="docutils literal"><span class="pre">parameter_option</span></tt> - option that takes one argument, for example
303<tt class="docutils literal"><span class="pre">-std=c99</span></tt>. It is also allowed to use spaces instead of the equality
304sign: <tt class="docutils literal"><span class="pre">-std</span> <span class="pre">c99</span></tt>. At most one occurrence is allowed.</li>
305<li><tt class="docutils literal"><span class="pre">parameter_list_option</span></tt> - same as the above, but more than one option
306occurence is allowed.</li>
307<li><tt class="docutils literal"><span class="pre">prefix_option</span></tt> - same as the parameter_option, but the option name and
308argument do not have to be separated. Example: <tt class="docutils literal"><span class="pre">-ofile</span></tt>. This can be also
309specified 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
310(<tt class="docutils literal"><span class="pre">=file</span></tt> will be interpreted as option value). At most one occurrence is
311allowed.</li>
312<li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one occurence of
313the option is allowed; example: <tt class="docutils literal"><span class="pre">-lm</span> <span class="pre">-lpthread</span></tt>.</li>
314<li><tt class="docutils literal"><span class="pre">alias_option</span></tt> - a special option type for creating aliases. Unlike other
315option types, aliases are not allowed to have any properties besides the
316aliased 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 Korobeynikov4167db92008-06-09 04:15:49 +0000317</ul>
318</blockquote>
319</li>
320<li><p class="first">Possible option properties:</p>
321<blockquote>
322<ul class="simple">
Mikhail Glushenkov84172f72009-01-28 03:47:38 +0000323<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>
324output.</li>
325<li><tt class="docutils literal"><span class="pre">required</span></tt> - this option must be specified exactly once (or, in case of
326the list options without the <tt class="docutils literal"><span class="pre">multi_val</span></tt> property, at least
327once). 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>
328<li><tt class="docutils literal"><span class="pre">one_or_more</span></tt> - the option must be specified at least one time. Useful
329only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>; for ordinary lists
330it 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
331<tt class="docutils literal"><span class="pre">zero_or_one</span></tt>.</li>
332<li><tt class="docutils literal"><span class="pre">zero_or_one</span></tt> - the option can be specified zero or one times. Useful
333only for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>. Incompatible with
334<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 Glushenkovbc39dff2009-01-15 02:42:40 +0000335<li><tt class="docutils literal"><span class="pre">hidden</span></tt> - the description of this option will not appear in
336the <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>
337output).</li>
338<li><tt class="docutils literal"><span class="pre">really_hidden</span></tt> - the option will not be mentioned in any help
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000339output.</li>
Mikhail Glushenkov84172f72009-01-28 03:47:38 +0000340<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
341special 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>
342<span class="pre">3))</span></tt>. Only list options can have this attribute; you can, however, use
343the <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 Glushenkov6fd55132008-12-11 23:24:40 +0000344<li><tt class="docutils literal"><span class="pre">extern</span></tt> - this option is defined in some other plugin, see below.</li>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000345</ul>
346</blockquote>
347</li>
348</ul>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000349<div class="section" id="external-options">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000350<h2><a class="toc-backref" href="#id11">External options</a></h2>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000351<p>Sometimes, when linking several plugins together, one plugin needs to
352access options defined in some other plugin. Because of the way
Mikhail Glushenkovbc39dff2009-01-15 02:42:40 +0000353options are implemented, such options must be marked as
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000354<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
355for. Example:</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000356<pre class="literal-block">
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000357...
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000358(switch_option &quot;E&quot;, (extern))
359...
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000360</pre>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000361<p>See also the section on plugin <a class="reference internal" href="#priorities">priorities</a>.</p>
Reid Spencerd5c0ccb2004-08-09 03:08:29 +0000362</div>
Reid Spencerd5c0ccb2004-08-09 03:08:29 +0000363</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000364<div class="section" id="conditional-evaluation">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000365<span id="case"></span><h1><a class="toc-backref" href="#id12">Conditional evaluation</a></h1>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000366<p>The 'case' construct is the main means by which programmability is
367achieved in LLVMC. It can be used to calculate edge weights, program
368actions and modify the shell commands to be executed. The 'case'
369expression is designed after the similarly-named construct in
370functional 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>
371<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
372are evaluated only if the corresponding tests evaluate to true.</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000373<p>Examples:</p>
374<pre class="literal-block">
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000375// Edge weight calculation
376
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000377// Increases edge weight by 5 if &quot;-A&quot; is provided on the
378// command-line, and by 5 more if &quot;-B&quot; is also provided.
379(case
380 (switch_on &quot;A&quot;), (inc_weight 5),
381 (switch_on &quot;B&quot;), (inc_weight 5))
382
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000383
384// Tool command line specification
385
386// Evaluates to &quot;cmdline1&quot; if the option &quot;-A&quot; is provided on the
387// command line; to &quot;cmdline2&quot; if &quot;-B&quot; is provided;
388// otherwise to &quot;cmdline3&quot;.
389
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000390(case
391 (switch_on &quot;A&quot;), &quot;cmdline1&quot;,
392 (switch_on &quot;B&quot;), &quot;cmdline2&quot;,
393 (default), &quot;cmdline3&quot;)
394</pre>
395<p>Note the slight difference in 'case' expression handling in contexts
396of edge weights and command line specification - in the second example
397the 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
398enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">&quot;cmdline1&quot;</span></tt> in
399that case.</p>
400<p>Case expressions can also be nested, i.e. the following is legal:</p>
401<pre class="literal-block">
402(case (switch_on &quot;E&quot;), (case (switch_on &quot;o&quot;), ..., (default), ...)
403 (default), ...)
404</pre>
405<p>You should, however, try to avoid doing that because it hurts
406readability. It is usually better to split tool descriptions and/or
407use TableGen inheritance instead.</p>
408<ul class="simple">
409<li>Possible tests are:<ul>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000410<li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is
411provided by the user. Example: <tt class="docutils literal"><span class="pre">(switch_on</span> <span class="pre">&quot;opt&quot;)</span></tt>.</li>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000412<li><tt class="docutils literal"><span class="pre">parameter_equals</span></tt> - Returns true if a command-line parameter equals
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000413a given value.
414Example: <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>
415<li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter
416list contains a given value.
417Example: <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 Korobeynikov4167db92008-06-09 04:15:49 +0000418<li><tt class="docutils literal"><span class="pre">input_languages_contain</span></tt> - Returns true if a given language
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000419belongs to the current input language set.
420Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
421<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language
422equals to the argument. At the moment works only with <tt class="docutils literal"><span class="pre">cmd_line</span></tt>
423and <tt class="docutils literal"><span class="pre">actions</span></tt> (on non-join nodes).
424Example: <tt class="docutils literal"><span class="pre">(in_language</span> <span class="pre">&quot;c++&quot;)</span></tt>.</li>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000425<li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be
426either a parameter or a parameter list) is set by the
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000427user.
428Example: <tt class="docutils literal"><span class="pre">(not_empty</span> <span class="pre">&quot;o&quot;)</span></tt>.</li>
Mikhail Glushenkov0b9d8f82008-12-17 02:47:30 +0000429<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>
430<span class="pre">X))</span></tt>. Provided for convenience.</li>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000431<li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last
432test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li>
433<li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard logical combinator that returns true iff all
434of its arguments return true. Used like this: <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(test1),</span>
435<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,
436but not encouraged.</li>
437<li><tt class="docutils literal"><span class="pre">or</span></tt> - Another logical combinator that returns true only if any
438one of its arguments returns true. Example: <tt class="docutils literal"><span class="pre">(or</span> <span class="pre">(test1),</span>
439<span class="pre">(test2),</span> <span class="pre">...</span> <span class="pre">(testN))</span></tt>.</li>
440</ul>
441</li>
442</ul>
Reid Spencera6288562004-08-22 18:06:59 +0000443</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000444<div class="section" id="writing-a-tool-description">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000445<h1><a class="toc-backref" href="#id13">Writing a tool description</a></h1>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000446<p>As was said earlier, nodes in the compilation graph represent tools,
447which are described separately. A tool definition looks like this
448(taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p>
449<pre class="literal-block">
450def llvm_gcc_cpp : Tool&lt;[
451 (in_language &quot;c++&quot;),
452 (out_language &quot;llvm-assembler&quot;),
453 (output_suffix &quot;bc&quot;),
454 (cmd_line &quot;llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm&quot;),
455 (sink)
456 ]&gt;;
457</pre>
458<p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for
459<tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of
460properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt>
461property means that this tool should be passed all command-line
462options that aren't mentioned in the option list.</p>
463<p>The complete list of all currently implemented tool properties follows.</p>
464<ul class="simple">
465<li>Possible tool properties:<ul>
466<li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a
467list, in case the tool supports multiple input languages.</li>
468<li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Tools are not allowed to
469have multiple output languages.</li>
470<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed
471dynamically, see documentation on actions.</li>
472<li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
473use <tt class="docutils literal"><span class="pre">$INFILE</span></tt> and <tt class="docutils literal"><span class="pre">$OUTFILE</span></tt> variables, output redirection
474with <tt class="docutils literal"><span class="pre">&gt;</span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables
475(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>
476<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
477list of input files and joins them together. Used for linkers.</li>
478<li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other
479tools are passed to this tool.</li>
480<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
481this tool reacts on command-line options (described in more detail
482below).</li>
483</ul>
484</li>
485</ul>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000486<div class="section" id="actions">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000487<h2><a class="toc-backref" href="#id14">Actions</a></h2>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000488<p>A tool often needs to react to command-line options, and this is
489precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example
490illustrates this feature:</p>
491<pre class="literal-block">
492def llvm_gcc_linker : Tool&lt;[
493 (in_language &quot;object-code&quot;),
494 (out_language &quot;executable&quot;),
495 (output_suffix &quot;out&quot;),
496 (cmd_line &quot;llvm-gcc $INFILE -o $OUTFILE&quot;),
497 (join),
498 (actions (case (not_empty &quot;L&quot;), (forward &quot;L&quot;),
499 (not_empty &quot;l&quot;), (forward &quot;l&quot;),
500 (not_empty &quot;dummy&quot;),
501 [(append_cmd &quot;-dummy1&quot;), (append_cmd &quot;-dummy2&quot;)])
502 ]&gt;;
503</pre>
504<p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent
505<tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em>
506with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>,
507which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which
508appends a given string to the tool execution command. Multiple actions
509can be associated with a single condition by using a list of actions
510(used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt>
511construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the
512tool command line.</p>
513<p>The &quot;join&quot; property used in the example means that this tool behaves
514like a linker.</p>
515<p>The list of all possible actions follows.</p>
516<ul>
517<li><p class="first">Possible actions:</p>
518<blockquote>
519<ul class="simple">
520<li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - append a string to the tool invocation
521command.
Mikhail Glushenkov0b9d8f82008-12-17 02:47:30 +0000522Example: <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>
523<span class="pre">&quot;-lpthread&quot;))</span></tt></li>
524<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>
525<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 Glushenkov6fd55132008-12-11 23:24:40 +0000526<li><tt class="docutils literal"><span class="pre">forward</span></tt> - forward an option unchanged.
527Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">&quot;Wall&quot;)</span></tt>.</li>
528<li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the name of an option, but forward the
529argument unchanged.
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000530Example: <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 Glushenkov6fd55132008-12-11 23:24:40 +0000531<li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - modify the output suffix of this
532tool.
533Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">&quot;i&quot;)</span></tt>.</li>
534<li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - stop compilation after this tool processes
535its input. Used without arguments.</li>
536<li><tt class="docutils literal"><span class="pre">unpack_values</span></tt> - used for for splitting and forwarding
537comma-separated lists of options, e.g. <tt class="docutils literal"><span class="pre">-Wa,-foo=bar,-baz</span></tt> is
538converted to <tt class="docutils literal"><span class="pre">-foo=bar</span> <span class="pre">-baz</span></tt> and appended to the tool invocation
539command.
540Example: <tt class="docutils literal"><span class="pre">(unpack_values</span> <span class="pre">&quot;Wa,&quot;)</span></tt>.</li>
541</ul>
542</blockquote>
543</li>
544</ul>
545</div>
546</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000547<div class="section" id="language-map">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000548<h1><a class="toc-backref" href="#id15">Language map</a></h1>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000549<p>If you are adding support for a new language to LLVMC, you'll need to
550modify the language map, which defines mappings from file extensions
551to language names. It is used to choose the proper toolchain(s) for a
552given input file set. Language map definition looks like this:</p>
Anton Korobeynikov4167db92008-06-09 04:15:49 +0000553<pre class="literal-block">
554def LanguageMap : LanguageMap&lt;
555 [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;,
556 LangToSuffixes&lt;&quot;c&quot;, [&quot;c&quot;]&gt;,
557 ...
558 ]&gt;;
559</pre>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000560<p>For example, without those definitions the following command wouldn't work:</p>
561<pre class="literal-block">
562$ llvmc hello.cpp
563llvmc: Unknown suffix: cpp
564</pre>
565<p>The language map entries should be added only for tools that are
566linked with the root node. Since tools are not allowed to have
567multiple output languages, for nodes &quot;inside&quot; the graph the input and
568output languages should match. This is enforced at compile-time.</p>
Reid Spencera6288562004-08-22 18:06:59 +0000569</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000570<div class="section" id="more-advanced-topics">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000571<h1><a class="toc-backref" href="#id16">More advanced topics</a></h1>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000572<div class="section" id="hooks-and-environment-variables">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000573<span id="hooks"></span><h2><a class="toc-backref" href="#id17">Hooks and environment variables</a></h2>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000574<p>Normally, LLVMC executes programs from the system <tt class="docutils literal"><span class="pre">PATH</span></tt>. Sometimes,
Mikhail Glushenkov84172f72009-01-28 03:47:38 +0000575this is not sufficient: for example, we may want to specify tool paths
576or names in the configuration file. This can be easily achieved via
577the hooks mechanism. To write your own hooks, just add their
578definitions 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
579your plugin directory. Hooks should live in the <tt class="docutils literal"><span class="pre">hooks</span></tt> namespace
580and 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>
581<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
582<tt class="docutils literal"><span class="pre">cmd_line</span></tt> tool property:</p>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000583<pre class="literal-block">
584(cmd_line &quot;$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)&quot;)
585</pre>
Mikhail Glushenkov84172f72009-01-28 03:47:38 +0000586<p>To pass arguments to hooks, use the following syntax:</p>
587<pre class="literal-block">
588(cmd_line &quot;$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2&quot;)
589</pre>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000590<p>It is also possible to use environment variables in the same manner:</p>
591<pre class="literal-block">
592(cmd_line &quot;$ENV(VAR1)/path/to/file -o $ENV(VAR2)&quot;)
593</pre>
594<p>To change the command line string based on user-provided options use
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000595the <tt class="docutils literal"><span class="pre">case</span></tt> expression (documented <a class="reference internal" href="#case">above</a>):</p>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000596<pre class="literal-block">
597(cmd_line
598 (case
599 (switch_on &quot;E&quot;),
600 &quot;llvm-g++ -E -x c $INFILE -o $OUTFILE&quot;,
601 (default),
602 &quot;llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm&quot;))
603</pre>
604</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000605<div class="section" id="how-plugins-are-loaded">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000606<span id="priorities"></span><h2><a class="toc-backref" href="#id18">How plugins are loaded</a></h2>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000607<p>It is possible for LLVMC plugins to depend on each other. For example,
608one can create edges between nodes defined in some other plugin. To
609make this work, however, that plugin should be loaded first. To
610achieve this, the concept of plugin priority was introduced. By
611default, every plugin has priority zero; to specify the priority
612explicitly, put the following line in your plugin's TableGen file:</p>
613<pre class="literal-block">
614def Priority : PluginPriority&lt;$PRIORITY_VALUE&gt;;
615# Where PRIORITY_VALUE is some integer &gt; 0
616</pre>
617<p>Plugins are loaded in order of their (increasing) priority, starting
618with 0. Therefore, the plugin with the highest priority value will be
619loaded last.</p>
620</div>
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000621<div class="section" id="debugging">
Mikhail Glushenkovb4b44ea2009-06-16 00:14:20 +0000622<h2><a class="toc-backref" href="#id19">Debugging</a></h2>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000623<p>When writing LLVMC plugins, it can be useful to get a visual view of
624the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkov4707bf42009-05-06 01:41:47 +0000625line 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
626<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 Glushenkovc0363fc2008-12-13 02:28:58 +0000627creates a Graphviz source file (<tt class="docutils literal"><span class="pre">compilation-graph.dot</span></tt>) in the
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000628current directory.</p>
Mikhail Glushenkovbc39dff2009-01-15 02:42:40 +0000629<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
630compilation graph for common errors like mismatched output/input
631language names, multiple default edges and cycles. These checks can't
632be performed at compile-time because the plugins can load code
633dynamically. 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
634perform any compilation tasks and returns the number of encountered
635errors as its status code.</p>
Mikhail Glushenkov6932e2f2008-12-11 23:43:14 +0000636<hr />
Anton Korobeynikov2a67ecb2008-06-09 04:17:51 +0000637<address>
Mikhail Glushenkovc0363fc2008-12-13 02:28:58 +0000638<a href="http://jigsaw.w3.org/css-validator/check/referer">
639<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
640 alt="Valid CSS" /></a>
641<a href="http://validator.w3.org/check?uri=referer">
642<img src="http://www.w3.org/Icons/valid-xhtml10-blue"
643 alt="Valid XHTML 1.0 Transitional"/></a>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000644
Mikhail Glushenkovc0363fc2008-12-13 02:28:58 +0000645<a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
646<a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000647
Mikhail Glushenkov0f78c272009-03-27 12:58:29 +0000648Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
Mikhail Glushenkovc0363fc2008-12-13 02:28:58 +0000649</address></div>
Mikhail Glushenkov6fd55132008-12-11 23:24:40 +0000650</div>
651</div>
Reid Spencerd5c0ccb2004-08-09 03:08:29 +0000652</body>
653</html>