<li><aclass="reference"href="#customizing-llvmc-the-compilation-graph"id="id5"name="id5">Customizing LLVMC: the compilation graph</a></li>
<li><aclass="reference"href="#writing-a-tool-description"id="id6"name="id6">Writing a tool description</a></li>
<li><aclass="reference"href="#option-list-specifying-all-options-in-a-single-place"id="id7"name="id7">Option list - specifying all options in a single place</a></li>
<li><aclass="reference"href="#using-hooks-and-environment-variables-in-the-cmd-line-property"id="id8"name="id8">Using hooks and environment variables in the <ttclass="docutils literal"><spanclass="pre">cmd_line</span></tt> property</a></li>
<li><aclass="reference"href="#conditional-evaluation-the-case-expression"id="id9"name="id9">Conditional evaluation: the <ttclass="docutils literal"><spanclass="pre">case</span></tt> expression</a></li>
<divclass="doc_section"><aclass="toc-backref"href="#id3"id="compiling-with-llvmc"name="compiling-with-llvmc">Compiling with LLVMC</a></div>
<p>LLVMC tries hard to be as compatible with <ttclass="docutils literal"><spanclass="pre">gcc</span></tt> as possible,
although there are some small differences. Most of the time, however,
you shouldn't be able to notice them:</p>
<preclass="literal-block">
$ # This works as expected:
$ llvmc2 -O3 -Wall hello.cpp
$ ./a.out
hello
</pre>
<p>One nice feature of LLVMC is that one doesn't have to distinguish
between different compilers for different languages (think <ttclass="docutils literal"><spanclass="pre">g++</span></tt> and
<ttclass="docutils literal"><spanclass="pre">gcc</span></tt>) - the right toolchain is chosen automatically based on input
language names (which are, in turn, determined from file
extensions). If you want to force files ending with ".c" to compile as
C++, use the <ttclass="docutils literal"><spanclass="pre">-x</span></tt> option, just like you would do it with <ttclass="docutils literal"><spanclass="pre">gcc</span></tt>:</p>
<preclass="literal-block">
$ llvmc2 -x c hello.cpp
$ # hello.cpp is really a C file
$ ./a.out
hello
</pre>
<p>On the other hand, when using LLVMC as a linker to combine several C++
object files you should provide the <ttclass="docutils literal"><spanclass="pre">--linker</span></tt> option since it's
impossible for LLVMC to choose the right linker in that case:</p>
<li><ttclass="docutils literal"><spanclass="pre">-x</span><spanclass="pre">LANGUAGE</span></tt> - Specify the language of the following input files
until the next -x option.</li>
<li><ttclass="docutils literal"><spanclass="pre">-v</span></tt> - Enable verbose mode, i.e. print out all executed commands.</li>
<li><ttclass="docutils literal"><spanclass="pre">--view-graph</span></tt> - Show a graphical representation of the compilation
graph. Requires that you have <ttclass="docutils literal"><spanclass="pre">dot</span></tt> and <ttclass="docutils literal"><spanclass="pre">gv</span></tt> commands
installed. Hidden option, useful for debugging.</li>
<li><ttclass="docutils literal"><spanclass="pre">--write-graph</span></tt> - Write a <ttclass="docutils literal"><spanclass="pre">compilation-graph.dot</span></tt> file in the
current directory with the compilation graph description in the
Graphviz format. Hidden option, useful for debugging.</li>
<li><ttclass="docutils literal"><spanclass="pre">--save-temps</span></tt> - Write temporary files to the current directory
and do not delete them on exit. Hidden option, useful for debugging.</li>
<li><ttclass="docutils literal"><spanclass="pre">--help</span></tt>, <ttclass="docutils literal"><spanclass="pre">--help-hidden</span></tt>, <ttclass="docutils literal"><spanclass="pre">--version</span></tt> - These options have
<divclass="doc_section"><aclass="toc-backref"href="#id5"id="customizing-llvmc-the-compilation-graph"name="customizing-llvmc-the-compilation-graph">Customizing LLVMC: the compilation graph</a></div>
<p>At the time of writing LLVMC does not support on-the-fly reloading of
configuration, so to customize LLVMC you'll have to recompile the
source code (which lives under <ttclass="docutils literal"><spanclass="pre">$LLVM_DIR/tools/llvmc2</span></tt>). The
default configuration files are <ttclass="docutils literal"><spanclass="pre">Common.td</span></tt> (contains common
definitions, don't forget to <ttclass="docutils literal"><spanclass="pre">include</span></tt> it in your configuration
files), <ttclass="docutils literal"><spanclass="pre">Tools.td</span></tt> (tool descriptions) and <ttclass="docutils literal"><spanclass="pre">Graph.td</span></tt> (compilation
graph definition).</p>
<p>To compile LLVMC with your own configuration file (say,``MyGraph.td``),
run <ttclass="docutils literal"><spanclass="pre">make</span></tt> like this:</p>
<preclass="literal-block">
$ cd $LLVM_DIR/tools/llvmc2
$ make GRAPH=MyGraph.td TOOLNAME=my_llvmc
</pre>
<p>This will build an executable named <ttclass="docutils literal"><spanclass="pre">my_llvmc</span></tt>. There are also
several sample configuration files in the <ttclass="docutils literal"><spanclass="pre">llvmc2/examples</span></tt>
subdirectory that should help to get you started.</p>
<p>Internally, LLVMC stores information about possible source
transformations in form of a graph. Nodes in this graph represent
tools, and edges between two nodes represent a transformation path. A
special "root" node is used to mark entry points for the
transformations. LLVMC also assigns a weight to each edge (more on
this later) to choose between several alternative edges.</p>
<p>The definition of the compilation graph (see file <ttclass="docutils literal"><spanclass="pre">Graph.td</span></tt>) is
<p>As you can see, the edges can be either default or optional, where
optional edges are differentiated by sporting a <ttclass="docutils literal"><spanclass="pre">case</span></tt> expression
used to calculate the edge's weight.</p>
<p>The default edges are assigned a weight of 1, and optional edges get a
weight of 0 + 2*N where N is the number of tests that evaluated to
true in the <ttclass="docutils literal"><spanclass="pre">case</span></tt> expression. It is also possible to provide an
integer parameter to <ttclass="docutils literal"><spanclass="pre">inc_weight</span></tt> and <ttclass="docutils literal"><spanclass="pre">dec_weight</span></tt> - in this case,
the weight is increased (or decreased) by the provided value instead
of the default 2.</p>
<p>When passing an input file through the graph, LLVMC picks the edge
with the maximum weight. To avoid ambiguity, there should be only one
default edge between two nodes (with the exception of the root node,
which gets a special treatment - there you are allowed to specify one
default edge <em>per language</em>).</p>
<p>To get a visual representation of the compilation graph (useful for
debugging), run <ttclass="docutils literal"><spanclass="pre">llvmc2</span><spanclass="pre">--view-graph</span></tt>. You will need <ttclass="docutils literal"><spanclass="pre">dot</span></tt> and
<ttclass="docutils literal"><spanclass="pre">gsview</span></tt> installed for this to work properly.</p>
<divclass="doc_section"><aclass="toc-backref"href="#id6"id="writing-a-tool-description"name="writing-a-tool-description">Writing a tool description</a></div>
<p>As was said earlier, nodes in the compilation graph represent tools,
which are described separately. A tool definition looks like this
(taken from the <ttclass="docutils literal"><spanclass="pre">Tools.td</span></tt> file):</p>
<li><ttclass="docutils literal"><spanclass="pre">cmd_line</span></tt> - the actual command used to run the tool. You can
use <ttclass="docutils literal"><spanclass="pre">$INFILE</span></tt> and <ttclass="docutils literal"><spanclass="pre">$OUTFILE</span></tt> variables, output redirection
with <ttclass="docutils literal"><spanclass="pre">></span></tt>, hook invocations (<ttclass="docutils literal"><spanclass="pre">$CALL</span></tt>), environment variables
(via <ttclass="docutils literal"><spanclass="pre">$ENV</span></tt>) and the <ttclass="docutils literal"><spanclass="pre">case</span></tt> construct (more on this below).</li>
<li><ttclass="docutils literal"><spanclass="pre">join</span></tt> - this tool is a "join node" in the graph, i.e. it gets a
list of input files and joins them together. Used for linkers.</li>
<li><ttclass="docutils literal"><spanclass="pre">sink</span></tt> - all command-line options that are not handled by other
tools are passed to this tool.</li>
</ul>
</li>
</ul>
<p>The next tool definition is slightly more complex:</p>
<p>This tool has a "join" property, which means that it behaves like a
linker. This tool also defines several command-line options: <ttclass="docutils literal"><spanclass="pre">-l</span></tt>,
<ttclass="docutils literal"><spanclass="pre">-L</span></tt> and <ttclass="docutils literal"><spanclass="pre">-Wl</span></tt> which have their usual meaning. An option has two
attributes: a name and a (possibly empty) list of properties. All
currently implemented option types and properties are described below:</p>
<ul>
<li><pclass="first">Possible option types:</p>
<blockquote>
<ulclass="simple">
<li><ttclass="docutils literal"><spanclass="pre">switch_option</span></tt> - a simple boolean switch, for example <ttclass="docutils literal"><spanclass="pre">-time</span></tt>.</li>
<li><ttclass="docutils literal"><spanclass="pre">parameter_option</span></tt> - option that takes an argument, for example
<li><ttclass="docutils literal"><spanclass="pre">append_cmd</span></tt> - append a string to the tool invocation command.</li>
<li><ttclass="docutils literal"><spanclass="pre">forward</span></tt> - forward this option unchanged.</li>
<li><ttclass="docutils literal"><spanclass="pre">output_suffix</span></tt> - modify the output suffix of this
tool. Example : <ttclass="docutils literal"><spanclass="pre">(switch</span><spanclass="pre">"E",</span><spanclass="pre">(output_suffix</span><spanclass="pre">"i")</span></tt>.</li>
<li><ttclass="docutils literal"><spanclass="pre">stop_compilation</span></tt> - stop compilation after this phase.</li>
<li><ttclass="docutils literal"><spanclass="pre">unpack_values</span></tt> - used for for splitting and forwarding
comma-separated lists of options, e.g. <ttclass="docutils literal"><spanclass="pre">-Wa,-foo=bar,-baz</span></tt> is
converted to <ttclass="docutils literal"><spanclass="pre">-foo=bar</span><spanclass="pre">-baz</span></tt> and appended to the tool invocation
command.</li>
<li><ttclass="docutils literal"><spanclass="pre">help</span></tt> - help string associated with this option. Used for
<divclass="doc_section"><aclass="toc-backref"href="#id7"id="option-list-specifying-all-options-in-a-single-place"name="option-list-specifying-all-options-in-a-single-place">Option list - specifying all options in a single place</a></div>
<p>It can be handy to have all information about options gathered in a
single place to provide an overview. This can be achieved by using a
<p><ttclass="docutils literal"><spanclass="pre">OptionList</span></tt> is also a good place to specify option aliases.</p>
<p>Tool-specific option properties like <ttclass="docutils literal"><spanclass="pre">append_cmd</span></tt> have (obviously)
no meaning in the context of <ttclass="docutils literal"><spanclass="pre">OptionList</span></tt>, so the only properties
allowed there are <ttclass="docutils literal"><spanclass="pre">help</span></tt> and <ttclass="docutils literal"><spanclass="pre">required</span></tt>.</p>
<p>Option lists are used at the file scope. See file
<ttclass="docutils literal"><spanclass="pre">examples/Clang.td</span></tt> for an example of <ttclass="docutils literal"><spanclass="pre">OptionList</span></tt> usage.</p>
<divclass="doc_section"><aclass="toc-backref"href="#id8"id="using-hooks-and-environment-variables-in-the-cmd-line-property"name="using-hooks-and-environment-variables-in-the-cmd-line-property">Using hooks and environment variables in the <ttclass="docutils literal"><spanclass="pre">cmd_line</span></tt> property</a></div>
<p>Normally, LLVMC executes programs from the system <ttclass="docutils literal"><spanclass="pre">PATH</span></tt>. Sometimes,
this is not sufficient: for example, we may want to specify tool names
in the configuration file. This can be achieved via the mechanism of
hooks - to compile LLVMC with your hooks, just drop a .cpp file into
<ttclass="docutils literal"><spanclass="pre">tools/llvmc2</span></tt> directory. Hooks should live in the <ttclass="docutils literal"><spanclass="pre">hooks</span></tt>
namespace and have the signature <ttclass="docutils literal"><spanclass="pre">std::string</span><spanclass="pre">hooks::MyHookName</span>
<spanclass="pre">(void)</span></tt>. They can be used from the <ttclass="docutils literal"><spanclass="pre">cmd_line</span></tt> tool property:</p>
<spanclass="pre">statement_N)</span></tt>. The statements are evaluated only if the corresponding
tests evaluate to true.</p>
<p>Examples:</p>
<preclass="literal-block">
// Increases edge weight by 5 if "-A" is provided on the
// command-line, and by 5 more if "-B" is also provided.
(case
(switch_on "A"), (inc_weight 5),
(switch_on "B"), (inc_weight 5))
// Evaluates to "cmdline1" if option "-A" is provided on the
// command line, otherwise to "cmdline2"
(case
(switch_on "A"), "cmdline1",
(switch_on "B"), "cmdline2",
(default), "cmdline3")
</pre>
<p>Note the slight difference in 'case' expression handling in contexts
of edge weights and command line specification - in the second example
the value of the <ttclass="docutils literal"><spanclass="pre">"B"</span></tt> switch is never checked when switch <ttclass="docutils literal"><spanclass="pre">"A"</span></tt> is
enabled, and the whole expression always evaluates to <ttclass="docutils literal"><spanclass="pre">"cmdline1"</span></tt> in
that case.</p>
<p>Case expressions can also be nested, i.e. the following is legal:</p>
<p>You should, however, try to avoid doing that because it hurts
readability. It is usually better to split tool descriptions and/or
use TableGen inheritance instead.</p>
<ulclass="simple">
<li>Possible tests are:<ul>
<li><ttclass="docutils literal"><spanclass="pre">switch_on</span></tt> - Returns true if a given command-line option is
provided by the user. Example: <ttclass="docutils literal"><spanclass="pre">(switch_on</span><spanclass="pre">"opt")</span></tt>. Note that
you have to define all possible command-line options separately in
the tool descriptions. See the next doc_text for the discussion of
different kinds of command-line options.</li>
<li><ttclass="docutils literal"><spanclass="pre">parameter_equals</span></tt> - Returns true if a command-line parameter equals
a given value. Example: <ttclass="docutils literal"><spanclass="pre">(parameter_equals</span><spanclass="pre">"W",</span><spanclass="pre">"all")</span></tt>.</li>
<li><ttclass="docutils literal"><spanclass="pre">element_in_list</span></tt> - Returns true if a command-line parameter list
includes a given value. Example: <ttclass="docutils literal"><spanclass="pre">(parameter_in_list</span><spanclass="pre">"l",</span><spanclass="pre">"pthread")</span></tt>.</li>
<li><ttclass="docutils literal"><spanclass="pre">input_languages_contain</span></tt> - Returns true if a given language
belongs to the current input language set. Example:
<li><ttclass="docutils literal"><spanclass="pre">default</span></tt> - Always evaluates to true. Should always be the last
test in the <ttclass="docutils literal"><spanclass="pre">case</span></tt> expression.</li>
<li><ttclass="docutils literal"><spanclass="pre">and</span></tt> - A standard logical combinator that returns true iff all
of its arguments return true. Used like this: <ttclass="docutils literal"><spanclass="pre">(and</span><spanclass="pre">(test1),</span>
<spanclass="pre">(test2),</span><spanclass="pre">...</span><spanclass="pre">(testN))</span></tt>. Nesting of <ttclass="docutils literal"><spanclass="pre">and</span></tt> and <ttclass="docutils literal"><spanclass="pre">or</span></tt> is allowed,
but not encouraged.</li>
<li><ttclass="docutils literal"><spanclass="pre">or</span></tt> - Another logical combinator that returns true only if any
one of its arguments returns true. Example: <ttclass="docutils literal"><spanclass="pre">(or</span><spanclass="pre">(test1),</span>