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