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