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