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