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