| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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"> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 4 | <head> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 6 | <meta name="generator" content="Docutils 0.5: http://docutils.sourceforge.net/" /> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 7 | <title>Customizing LLVMC: Reference Manual</title> | 
| Mikhail Glushenkov | 8cc8288 | 2008-12-13 17:50:58 +0000 | [diff] [blame] | 8 | <link rel="stylesheet" href="llvm.css" type="text/css" /> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 9 | </head> | 
 | 10 | <body> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 11 | <div class="document" id="customizing-llvmc-reference-manual"> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 12 | <h1 class="title">Customizing LLVMC: Reference Manual</h1> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 13 |  | 
| Mikhail Glushenkov | 23f522a | 2008-12-13 17:51:47 +0000 | [diff] [blame] | 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'. --> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 17 | <div class="contents topic" id="contents"> | 
 | 18 | <p class="topic-title first">Contents</p> | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 19 | <ul class="simple"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 20 | <li><a class="reference internal" href="#introduction" id="id8">Introduction</a></li> | 
 | 21 | <li><a class="reference internal" href="#compiling-with-llvmc" id="id9">Compiling with LLVMC</a></li> | 
 | 22 | <li><a class="reference internal" href="#predefined-options" id="id10">Predefined options</a></li> | 
 | 23 | <li><a class="reference internal" href="#compiling-llvmc-plugins" id="id11">Compiling LLVMC plugins</a></li> | 
 | 24 | <li><a class="reference internal" href="#compiling-standalone-llvmc-based-drivers" id="id12">Compiling standalone LLVMC-based drivers</a></li> | 
 | 25 | <li><a class="reference internal" href="#customizing-llvmc-the-compilation-graph" id="id13">Customizing LLVMC: the compilation graph</a></li> | 
 | 26 | <li><a class="reference internal" href="#describing-options" id="id14">Describing options</a><ul> | 
 | 27 | <li><a class="reference internal" href="#external-options" id="id15">External options</a></li> | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 28 | </ul> | 
 | 29 | </li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 30 | <li><a class="reference internal" href="#conditional-evaluation" id="id16">Conditional evaluation</a></li> | 
 | 31 | <li><a class="reference internal" href="#writing-a-tool-description" id="id17">Writing a tool description</a><ul> | 
 | 32 | <li><a class="reference internal" href="#id5" id="id18">Actions</a></li> | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 33 | </ul> | 
 | 34 | </li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 35 | <li><a class="reference internal" href="#language-map" id="id19">Language map</a></li> | 
 | 36 | <li><a class="reference internal" href="#option-preprocessor" id="id20">Option preprocessor</a></li> | 
 | 37 | <li><a class="reference internal" href="#more-advanced-topics" id="id21">More advanced topics</a><ul> | 
 | 38 | <li><a class="reference internal" href="#hooks-and-environment-variables" id="id22">Hooks and environment variables</a></li> | 
 | 39 | <li><a class="reference internal" href="#how-plugins-are-loaded" id="id23">How plugins are loaded</a></li> | 
 | 40 | <li><a class="reference internal" href="#debugging" id="id24">Debugging</a></li> | 
 | 41 | <li><a class="reference internal" href="#conditioning-on-the-executable-name" id="id25">Conditioning on the executable name</a></li> | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 48 | </div><div class="section" id="introduction"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 49 | <h1><a class="toc-backref" href="#id8">Introduction</a></h1> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 63dc318 | 2009-06-17 02:56:48 +0000 | [diff] [blame] | 62 | <p>Because LLVMC employs <a class="reference external" href="http://llvm.org/docs/TableGenFundamentals.html">TableGen</a> as its configuration language, you | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 63 | need to be familiar with it to customize LLVMC.</p> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 64 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 65 | <div class="section" id="compiling-with-llvmc"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 66 | <h1><a class="toc-backref" href="#id9">Compiling with LLVMC</a></h1> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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: | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 72 | $ llvmc -O3 -Wall hello.cpp | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 73 | $ ./a.out | 
 | 74 | hello | 
 | 75 | </pre> | 
| Mikhail Glushenkov | 63dc318 | 2009-06-17 02:56:48 +0000 | [diff] [blame] | 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 ".c" 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 82 | <pre class="literal-block"> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 83 | $ # hello.c is really a C++ file | 
 | 84 | $ llvmc -x c++ hello.c | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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"> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 92 | $ llvmc -c hello.cpp | 
 | 93 | $ llvmc hello.o | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 94 | [A lot of link-time errors skipped] | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 95 | $ llvmc --linker=c++ hello.o | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 96 | $ ./a.out | 
 | 97 | hello | 
 | 98 | </pre> | 
| Mikhail Glushenkov | e25b845 | 2009-06-30 00:16:43 +0000 | [diff] [blame] | 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> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 101 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 102 | <div class="section" id="predefined-options"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 103 | <h1><a class="toc-backref" href="#id10">Predefined options</a></h1> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 104 | <p>LLVMC has some built-in options that can't be overridden in the | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 105 | configuration libraries:</p> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 112 | <li><tt class="docutils literal"><span class="pre">-v</span></tt> - Enable verbose mode, i.e. print out all executed commands.</li> | 
| Mikhail Glushenkov | d7acf73 | 2009-06-25 18:21:10 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 4429aaf | 2009-07-11 19:28:00 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 4ad0d57 | 2009-03-27 12:58:29 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 502106a | 2009-06-16 00:14:20 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 4ad0d57 | 2009-03-27 12:58:29 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 502106a | 2009-06-16 00:14:20 +0000 | [diff] [blame] | 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> | 
| Duncan Sands | 7e7ae5a | 2010-02-18 14:08:13 +0000 | [diff] [blame] | 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 | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 136 | their standard meaning.</li> | 
 | 137 | </ul> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 138 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 139 | <div class="section" id="compiling-llvmc-plugins"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 140 | <h1><a class="toc-backref" href="#id11">Compiling LLVMC plugins</a></h1> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 143 | <pre class="literal-block"> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 144 | $ cd $LLVMC_DIR/plugins | 
 | 145 | $ cp -r Simple MyPlugin | 
 | 146 | $ cd MyPlugin | 
 | 147 | $ ls | 
 | 148 | Makefile PluginMain.cpp Simple.td | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 149 | </pre> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 155 | can also contain hook definitions (see <a class="reference internal" href="#hooks">below</a>).</p> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 63dc318 | 2009-06-17 02:56:48 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 63dc318 | 2009-06-17 02:56:48 +0000 | [diff] [blame] | 175 | $ llvmc -load $LLVM_DIR/Release/lib/plugin_llvmc_Simple.so | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 176 | </pre> | 
| Mikhail Glushenkov | 502106a | 2009-06-16 00:14:20 +0000 | [diff] [blame] | 177 | </div> | 
 | 178 | <div class="section" id="compiling-standalone-llvmc-based-drivers"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 179 | <h1><a class="toc-backref" href="#id12">Compiling standalone LLVMC-based drivers</a></h1> | 
| Mikhail Glushenkov | 502106a | 2009-06-16 00:14:20 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 63dc318 | 2009-06-17 02:56:48 +0000 | [diff] [blame] | 208 | <p>This works with both srcdir == objdir and srcdir != objdir, but assumes that the | 
| Mikhail Glushenkov | 502106a | 2009-06-16 00:14:20 +0000 | [diff] [blame] | 209 | plugin source directory was placed under <tt class="docutils literal"><span class="pre">$LLVMC_DIR/plugins</span></tt>.</p> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 502106a | 2009-06-16 00:14:20 +0000 | [diff] [blame] | 214 | $ make LLVMC_BUILTIN_PLUGINS="" | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 215 | </pre> | 
 | 216 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 217 | <div class="section" id="customizing-llvmc-the-compilation-graph"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 218 | <h1><a class="toc-backref" href="#id13">Customizing LLVMC: the compilation graph</a></h1> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 219 | <p>Each TableGen configuration file should include the common | 
 | 220 | definitions:</p> | 
 | 221 | <pre class="literal-block"> | 
 | 222 | include "llvm/CompilerDriver/Common.td" | 
 | 223 | </pre> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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 "root" 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> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 232 | <pre class="literal-block"> | 
 | 233 | def CompilationGraph : CompilationGraph<[ | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 234 |     Edge<"root", "llvm_gcc_c">, | 
 | 235 |     Edge<"root", "llvm_gcc_assembler">, | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 236 |     ... | 
 | 237 |  | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 238 |     Edge<"llvm_gcc_c", "llc">, | 
 | 239 |     Edge<"llvm_gcc_cpp", "llc">, | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 240 |     ... | 
 | 241 |  | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 242 |     OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"), | 
 | 243 |                                       (inc_weight))>, | 
 | 244 |     OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"), | 
 | 245 |                                               (inc_weight))>, | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 246 |     ... | 
 | 247 |  | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 248 |     OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker", | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 249 |         (case (input_languages_contain "c++"), (inc_weight), | 
 | 250 |               (or (parameter_equals "linker", "g++"), | 
 | 251 |                   (parameter_equals "linker", "c++")), (inc_weight))>, | 
 | 252 |     ... | 
 | 253 |  | 
 | 254 |     ]>; | 
 | 255 | </pre> | 
 | 256 | <p>As you can see, the edges can be either default or optional, where | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 281 | <p>To get a visual representation of the compilation graph (useful for | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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 | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 283 | <tt class="docutils literal"><span class="pre">gsview</span></tt> installed for this to work properly.</p> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 284 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 285 | <div class="section" id="describing-options"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 286 | <h1><a class="toc-backref" href="#id14">Describing options</a></h1> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 289 | <pre class="literal-block"> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 290 | def Options : OptionList<[ | 
 | 291 | (switch_option "E", (help "Help string")), | 
 | 292 | (alias_option "quiet", "q") | 
 | 293 | ... | 
 | 294 | ]>; | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 295 | </pre> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 301 | <ul> | 
 | 302 | <li><p class="first">Possible option types:</p> | 
 | 303 | <blockquote> | 
 | 304 | <ul class="simple"> | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 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 | 
| Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame^] | 311 | occurrence is allowed.</li> | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 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> | 
| Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame^] | 317 | <li><tt class="docutils literal"><span class="pre">prefix_list_option</span></tt> - same as the above, but more than one occurrence of | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 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">"preprocess",</span> <span class="pre">"E")</span></tt></li> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 322 | </ul> | 
 | 323 | </blockquote> | 
 | 324 | </li> | 
 | 325 | <li><p class="first">Possible option properties:</p> | 
 | 326 | <blockquote> | 
 | 327 | <ul class="simple"> | 
| Duncan Sands | 7e7ae5a | 2010-02-18 14:08:13 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 337 | <li><tt class="docutils literal"><span class="pre">optional</span></tt> - the option can be specified zero or one times. Useful only | 
 | 338 | for list options in conjunction with <tt class="docutils literal"><span class="pre">multi_val</span></tt>. Incompatible with | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | f9b1d79 | 2009-01-15 02:42:40 +0000 | [diff] [blame] | 340 | <li><tt class="docutils literal"><span class="pre">hidden</span></tt> - the description of this option will not appear in | 
| Duncan Sands | 7e7ae5a | 2010-02-18 14:08:13 +0000 | [diff] [blame] | 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> | 
| Mikhail Glushenkov | f9b1d79 | 2009-01-15 02:42:40 +0000 | [diff] [blame] | 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 | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 344 | output.</li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 345 | <li><tt class="docutils literal"><span class="pre">comma_separated</span></tt> - Indicates that any commas specified for an option's | 
 | 346 | value should be used to split the value up into multiple values for the | 
 | 347 | option. This property is valid only for list options. In conjunction with | 
 | 348 | <tt class="docutils literal"><span class="pre">forward_value</span></tt> can be used to implement option forwarding in style of | 
 | 349 | gcc's <tt class="docutils literal"><span class="pre">-Wa,</span></tt>.</li> | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 350 | <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 | 
 | 351 | special cases). Usage example: <tt class="docutils literal"><span class="pre">(parameter_list_option</span> <span class="pre">"foo",</span> <span class="pre">(multi_val</span> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 352 | <span class="pre">3))</span></tt>; the command-line syntax is '-foo a b c'. Only list options can have | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 353 | 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">optional</span></tt> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 354 | and <tt class="docutils literal"><span class="pre">required</span></tt> properties.</li> | 
| Mikhail Glushenkov | 640dd26 | 2009-07-07 16:09:48 +0000 | [diff] [blame] | 355 | <li><tt class="docutils literal"><span class="pre">init</span></tt> - this option has a default value, either a string (if it is a | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 356 | parameter), or a boolean (if it is a switch; as in C++, boolean constants | 
 | 357 | are called <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 <tt class="docutils literal"><span class="pre">init</span></tt> | 
 | 358 | attribute. | 
 | 359 | Usage examples: <tt class="docutils literal"><span class="pre">(switch_option</span> <span class="pre">"foo",</span> <span class="pre">(init</span> <span class="pre">true))</span></tt>; <tt class="docutils literal"><span class="pre">(prefix_option</span> | 
 | 360 | <span class="pre">"bar",</span> <span class="pre">(init</span> <span class="pre">"baz"))</span></tt>.</li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 361 | <li><tt class="docutils literal"><span class="pre">extern</span></tt> - this option is defined in some other plugin, see <a class="reference internal" href="#extern">below</a>.</li> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 362 | </ul> | 
 | 363 | </blockquote> | 
 | 364 | </li> | 
 | 365 | </ul> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 366 | <div class="section" id="external-options"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 367 | <span id="extern"></span><h2><a class="toc-backref" href="#id15">External options</a></h2> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 368 | <p>Sometimes, when linking several plugins together, one plugin needs to | 
 | 369 | access options defined in some other plugin. Because of the way | 
| Mikhail Glushenkov | f9b1d79 | 2009-01-15 02:42:40 +0000 | [diff] [blame] | 370 | options are implemented, such options must be marked as | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 371 | <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 | 
 | 372 | for. Example:</p> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 373 | <pre class="literal-block"> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 374 | ... | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 375 | (switch_option "E", (extern)) | 
 | 376 | ... | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 377 | </pre> | 
| Mikhail Glushenkov | 15367df | 2009-07-07 16:44:20 +0000 | [diff] [blame] | 378 | <p>If an external option has additional attributes besides 'extern', they are | 
 | 379 | ignored. See also the section on plugin <a class="reference internal" href="#priorities">priorities</a>.</p> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 380 | </div> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 381 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 382 | <div class="section" id="conditional-evaluation"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 383 | <span id="case"></span><h1><a class="toc-backref" href="#id16">Conditional evaluation</a></h1> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 384 | <p>The 'case' construct is the main means by which programmability is | 
 | 385 | achieved in LLVMC. It can be used to calculate edge weights, program | 
 | 386 | actions and modify the shell commands to be executed. The 'case' | 
 | 387 | expression is designed after the similarly-named construct in | 
 | 388 | 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> | 
 | 389 | <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 | 
 | 390 | are evaluated only if the corresponding tests evaluate to true.</p> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 391 | <p>Examples:</p> | 
 | 392 | <pre class="literal-block"> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 393 | // Edge weight calculation | 
 | 394 |  | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 395 | // Increases edge weight by 5 if "-A" is provided on the | 
 | 396 | // command-line, and by 5 more if "-B" is also provided. | 
 | 397 | (case | 
 | 398 |     (switch_on "A"), (inc_weight 5), | 
 | 399 |     (switch_on "B"), (inc_weight 5)) | 
 | 400 |  | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 401 |  | 
 | 402 | // Tool command line specification | 
 | 403 |  | 
 | 404 | // Evaluates to "cmdline1" if the option "-A" is provided on the | 
 | 405 | // command line; to "cmdline2" if "-B" is provided; | 
 | 406 | // otherwise to "cmdline3". | 
 | 407 |  | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 408 | (case | 
 | 409 |     (switch_on "A"), "cmdline1", | 
 | 410 |     (switch_on "B"), "cmdline2", | 
 | 411 |     (default), "cmdline3") | 
 | 412 | </pre> | 
 | 413 | <p>Note the slight difference in 'case' expression handling in contexts | 
 | 414 | of edge weights and command line specification - in the second example | 
 | 415 | the value of the <tt class="docutils literal"><span class="pre">"B"</span></tt> switch is never checked when switch <tt class="docutils literal"><span class="pre">"A"</span></tt> is | 
 | 416 | enabled, and the whole expression always evaluates to <tt class="docutils literal"><span class="pre">"cmdline1"</span></tt> in | 
 | 417 | that case.</p> | 
 | 418 | <p>Case expressions can also be nested, i.e. the following is legal:</p> | 
 | 419 | <pre class="literal-block"> | 
 | 420 | (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...) | 
 | 421 |       (default), ...) | 
 | 422 | </pre> | 
 | 423 | <p>You should, however, try to avoid doing that because it hurts | 
 | 424 | readability. It is usually better to split tool descriptions and/or | 
 | 425 | use TableGen inheritance instead.</p> | 
 | 426 | <ul class="simple"> | 
 | 427 | <li>Possible tests are:<ul> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 428 | <li><tt class="docutils literal"><span class="pre">switch_on</span></tt> - Returns true if a given command-line switch is provided by | 
 | 429 | 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">["foo",</span> | 
 | 430 | <span class="pre">"bar",</span> <span class="pre">"baz"])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(and</span> <span class="pre">(switch_on</span> <span class="pre">"foo"),</span> <span class="pre">(switch_on</span> | 
 | 431 | <span class="pre">"bar"),</span> <span class="pre">(switch_on</span> <span class="pre">"baz"))</span></tt>. | 
 | 432 | Example: <tt class="docutils literal"><span class="pre">(switch_on</span> <span class="pre">"opt")</span></tt>.</li> | 
 | 433 | <li><tt class="docutils literal"><span class="pre">any_switch_on</span></tt> - Given a list of switch options, returns true if any of | 
 | 434 | the switches is turned on. | 
 | 435 | Example: <tt class="docutils literal"><span class="pre">(any_switch_on</span> <span class="pre">["foo",</span> <span class="pre">"bar",</span> <span class="pre">"baz"])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(or</span> | 
 | 436 | <span class="pre">(switch_on</span> <span class="pre">"foo"),</span> <span class="pre">(switch_on</span> <span class="pre">"bar"),</span> <span class="pre">(switch_on</span> <span class="pre">"baz"))</span></tt>.</li> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 437 | <li><tt class="docutils literal"><span class="pre">parameter_equals</span></tt> - Returns true if a command-line parameter equals | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 438 | a given value. | 
 | 439 | Example: <tt class="docutils literal"><span class="pre">(parameter_equals</span> <span class="pre">"W",</span> <span class="pre">"all")</span></tt>.</li> | 
 | 440 | <li><tt class="docutils literal"><span class="pre">element_in_list</span></tt> - Returns true if a command-line parameter | 
 | 441 | list contains a given value. | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 442 | Example: <tt class="docutils literal"><span class="pre">(element_in_list</span> <span class="pre">"l",</span> <span class="pre">"pthread")</span></tt>.</li> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 443 | <li><tt class="docutils literal"><span class="pre">input_languages_contain</span></tt> - Returns true if a given language | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 444 | belongs to the current input language set. | 
 | 445 | Example: <tt class="docutils literal"><span class="pre">(input_languages_contain</span> <span class="pre">"c++")</span></tt>.</li> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 446 | <li><tt class="docutils literal"><span class="pre">in_language</span></tt> - Evaluates to true if the input file language is equal to | 
 | 447 | 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 | 
 | 448 | non-join nodes). | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 449 | Example: <tt class="docutils literal"><span class="pre">(in_language</span> <span class="pre">"c++")</span></tt>.</li> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 450 | <li><tt class="docutils literal"><span class="pre">not_empty</span></tt> - Returns true if a given option (which should be either a | 
 | 451 | parameter or a parameter list) is set by the user. Like <tt class="docutils literal"><span class="pre">switch_on</span></tt>, can | 
 | 452 | be also given a list as argument. | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 453 | Example: <tt class="docutils literal"><span class="pre">(not_empty</span> <span class="pre">"o")</span></tt>.</li> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 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_not_empty</span> <span class="pre">["foo",</span> <span class="pre">"bar",</span> <span class="pre">"baz"])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(or</span> | 
 | 457 | <span class="pre">(not_empty</span> <span class="pre">"foo"),</span> <span class="pre">(not_empty</span> <span class="pre">"bar"),</span> <span class="pre">(not_empty</span> <span class="pre">"baz"))</span></tt>.</li> | 
| Mikhail Glushenkov | 2d0484c | 2008-12-17 02:47:30 +0000 | [diff] [blame] | 458 | <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> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 459 | <span class="pre">X))</span></tt>. Provided for convenience. Can be given a list as argument.</li> | 
 | 460 | <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 | 
 | 461 | the options in the list. | 
 | 462 | Example: <tt class="docutils literal"><span class="pre">(any_empty</span> <span class="pre">["foo",</span> <span class="pre">"bar",</span> <span class="pre">"baz"])</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">(not</span> <span class="pre">(and</span> | 
 | 463 | <span class="pre">(not_empty</span> <span class="pre">"foo"),</span> <span class="pre">(not_empty</span> <span class="pre">"bar"),</span> <span class="pre">(not_empty</span> <span class="pre">"baz")))</span></tt>.</li> | 
| Mikhail Glushenkov | 3128015 | 2009-09-28 01:28:26 +0000 | [diff] [blame] | 464 | <li><tt class="docutils literal"><span class="pre">single_input_file</span></tt> - Returns true if there was only one input file | 
 | 465 | provided on the command-line. Used without arguments: | 
 | 466 | <tt class="docutils literal"><span class="pre">(single_input_file)</span></tt>.</li> | 
 | 467 | <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 | 
 | 468 | case of zero input files is considered an error).</li> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 469 | <li><tt class="docutils literal"><span class="pre">default</span></tt> - Always evaluates to true. Should always be the last | 
 | 470 | test in the <tt class="docutils literal"><span class="pre">case</span></tt> expression.</li> | 
| Mikhail Glushenkov | 3128015 | 2009-09-28 01:28:26 +0000 | [diff] [blame] | 471 | <li><tt class="docutils literal"><span class="pre">and</span></tt> - A standard binary logical combinator that returns true iff all of | 
 | 472 | 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> | 
 | 473 | <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 | 
 | 474 | encouraged.</li> | 
 | 475 | <li><tt class="docutils literal"><span class="pre">or</span></tt> - A binary logical combinator that returns true iff any of its | 
 | 476 | 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> | 
 | 477 | <li><tt class="docutils literal"><span class="pre">not</span></tt> - Standard unary logical combinator that negates its | 
 | 478 | 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> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 479 | </ul> | 
 | 480 | </li> | 
 | 481 | </ul> | 
| Reid Spencer | 46d2192 | 2004-08-22 18:06:59 +0000 | [diff] [blame] | 482 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 483 | <div class="section" id="writing-a-tool-description"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 484 | <h1><a class="toc-backref" href="#id17">Writing a tool description</a></h1> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 485 | <p>As was said earlier, nodes in the compilation graph represent tools, | 
 | 486 | which are described separately. A tool definition looks like this | 
 | 487 | (taken from the <tt class="docutils literal"><span class="pre">include/llvm/CompilerDriver/Tools.td</span></tt> file):</p> | 
 | 488 | <pre class="literal-block"> | 
 | 489 | def llvm_gcc_cpp : Tool<[ | 
 | 490 |     (in_language "c++"), | 
 | 491 |     (out_language "llvm-assembler"), | 
 | 492 |     (output_suffix "bc"), | 
 | 493 |     (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"), | 
 | 494 |     (sink) | 
 | 495 |     ]>; | 
 | 496 | </pre> | 
 | 497 | <p>This defines a new tool called <tt class="docutils literal"><span class="pre">llvm_gcc_cpp</span></tt>, which is an alias for | 
 | 498 | <tt class="docutils literal"><span class="pre">llvm-g++</span></tt>. As you can see, a tool definition is just a list of | 
 | 499 | properties; most of them should be self-explanatory. The <tt class="docutils literal"><span class="pre">sink</span></tt> | 
 | 500 | property means that this tool should be passed all command-line | 
 | 501 | options that aren't mentioned in the option list.</p> | 
 | 502 | <p>The complete list of all currently implemented tool properties follows.</p> | 
 | 503 | <ul class="simple"> | 
 | 504 | <li>Possible tool properties:<ul> | 
 | 505 | <li><tt class="docutils literal"><span class="pre">in_language</span></tt> - input language name. Can be either a string or a | 
 | 506 | list, in case the tool supports multiple input languages.</li> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 507 | <li><tt class="docutils literal"><span class="pre">out_language</span></tt> - output language name. Multiple output languages are not | 
 | 508 | allowed.</li> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 509 | <li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - output file suffix. Can also be changed | 
 | 510 | dynamically, see documentation on actions.</li> | 
 | 511 | <li><tt class="docutils literal"><span class="pre">cmd_line</span></tt> - the actual command used to run the tool. You can | 
 | 512 | 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 | 
 | 513 | with <tt class="docutils literal"><span class="pre">></span></tt>, hook invocations (<tt class="docutils literal"><span class="pre">$CALL</span></tt>), environment variables | 
 | 514 | (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> | 
 | 515 | <li><tt class="docutils literal"><span class="pre">join</span></tt> - this tool is a "join node" in the graph, i.e. it gets a | 
 | 516 | list of input files and joins them together. Used for linkers.</li> | 
 | 517 | <li><tt class="docutils literal"><span class="pre">sink</span></tt> - all command-line options that are not handled by other | 
 | 518 | tools are passed to this tool.</li> | 
 | 519 | <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 | 
 | 520 | this tool reacts on command-line options (described in more detail | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 521 | <a class="reference internal" href="#actions">below</a>).</li> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 522 | </ul> | 
 | 523 | </li> | 
 | 524 | </ul> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 525 | <div class="section" id="id5"> | 
 | 526 | <span id="actions"></span><h2><a class="toc-backref" href="#id18">Actions</a></h2> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 527 | <p>A tool often needs to react to command-line options, and this is | 
 | 528 | precisely what the <tt class="docutils literal"><span class="pre">actions</span></tt> property is for. The next example | 
 | 529 | illustrates this feature:</p> | 
 | 530 | <pre class="literal-block"> | 
 | 531 | def llvm_gcc_linker : Tool<[ | 
 | 532 |     (in_language "object-code"), | 
 | 533 |     (out_language "executable"), | 
 | 534 |     (output_suffix "out"), | 
 | 535 |     (cmd_line "llvm-gcc $INFILE -o $OUTFILE"), | 
 | 536 |     (join), | 
 | 537 |     (actions (case (not_empty "L"), (forward "L"), | 
 | 538 |                    (not_empty "l"), (forward "l"), | 
 | 539 |                    (not_empty "dummy"), | 
 | 540 |                              [(append_cmd "-dummy1"), (append_cmd "-dummy2")]) | 
 | 541 |     ]>; | 
 | 542 | </pre> | 
 | 543 | <p>The <tt class="docutils literal"><span class="pre">actions</span></tt> tool property is implemented on top of the omnipresent | 
 | 544 | <tt class="docutils literal"><span class="pre">case</span></tt> expression. It associates one or more different <em>actions</em> | 
 | 545 | with given conditions - in the example, the actions are <tt class="docutils literal"><span class="pre">forward</span></tt>, | 
 | 546 | which forwards a given option unchanged, and <tt class="docutils literal"><span class="pre">append_cmd</span></tt>, which | 
 | 547 | appends a given string to the tool execution command. Multiple actions | 
 | 548 | can be associated with a single condition by using a list of actions | 
 | 549 | (used in the example to append some dummy options). The same <tt class="docutils literal"><span class="pre">case</span></tt> | 
 | 550 | construct can also be used in the <tt class="docutils literal"><span class="pre">cmd_line</span></tt> property to modify the | 
 | 551 | tool command line.</p> | 
 | 552 | <p>The "join" property used in the example means that this tool behaves | 
 | 553 | like a linker.</p> | 
 | 554 | <p>The list of all possible actions follows.</p> | 
 | 555 | <ul> | 
 | 556 | <li><p class="first">Possible actions:</p> | 
 | 557 | <blockquote> | 
 | 558 | <ul class="simple"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 559 | <li><tt class="docutils literal"><span class="pre">append_cmd</span></tt> - Append a string to the tool invocation command. | 
 | 560 | Example: <tt class="docutils literal"><span class="pre">(case</span> <span class="pre">(switch_on</span> <span class="pre">"pthread"),</span> <span class="pre">(append_cmd</span> <span class="pre">"-lpthread"))</span></tt>.</li> | 
 | 561 | <li><tt class="docutils literal"><span class="pre">error</span></tt> - Exit with error. | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 562 | Example: <tt class="docutils literal"><span class="pre">(error</span> <span class="pre">"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!")</span></tt>.</li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 563 | <li><tt class="docutils literal"><span class="pre">warning</span></tt> - Print a warning. | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 564 | Example: <tt class="docutils literal"><span class="pre">(warning</span> <span class="pre">"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!")</span></tt>.</li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 565 | <li><tt class="docutils literal"><span class="pre">forward</span></tt> - Forward the option unchanged. | 
 | 566 | Example: <tt class="docutils literal"><span class="pre">(forward</span> <span class="pre">"Wall")</span></tt>.</li> | 
 | 567 | <li><tt class="docutils literal"><span class="pre">forward_as</span></tt> - Change the option's name, but forward the argument | 
 | 568 | unchanged. | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 569 | Example: <tt class="docutils literal"><span class="pre">(forward_as</span> <span class="pre">"O0",</span> <span class="pre">"--disable-optimization")</span></tt>.</li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 570 | <li><tt class="docutils literal"><span class="pre">forward_value</span></tt> - Forward only option's value. Cannot be used with switch | 
 | 571 | options (since they don't have values), but works fine with lists. | 
 | 572 | Example: <tt class="docutils literal"><span class="pre">(forward_value</span> <span class="pre">"Wa,")</span></tt>.</li> | 
 | 573 | <li><tt class="docutils literal"><span class="pre">forward_transformed_value</span></tt> - As above, but applies a hook to the | 
 | 574 | option's value before forwarding (see <a class="reference internal" href="#hooks">below</a>). When | 
 | 575 | <tt class="docutils literal"><span class="pre">forward_transformed_value</span></tt> is applied to a list | 
 | 576 | option, the hook must have signature | 
 | 577 | <tt class="docutils literal"><span class="pre">std::string</span> <span class="pre">hooks::HookName</span> <span class="pre">(const</span> <span class="pre">std::vector<std::string>&)</span></tt>. | 
 | 578 | Example: <tt class="docutils literal"><span class="pre">(forward_transformed_value</span> <span class="pre">"m",</span> <span class="pre">"ConvertToMAttr")</span></tt>.</li> | 
 | 579 | <li><tt class="docutils literal"><span class="pre">output_suffix</span></tt> - Modify the output suffix of this tool. | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 580 | Example: <tt class="docutils literal"><span class="pre">(output_suffix</span> <span class="pre">"i")</span></tt>.</li> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 581 | <li><tt class="docutils literal"><span class="pre">stop_compilation</span></tt> - Stop compilation after this tool processes its | 
 | 582 | input. Used without arguments. | 
 | 583 | Example: <tt class="docutils literal"><span class="pre">(stop_compilation)</span></tt>.</li> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 584 | </ul> | 
 | 585 | </blockquote> | 
 | 586 | </li> | 
 | 587 | </ul> | 
 | 588 | </div> | 
 | 589 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 590 | <div class="section" id="language-map"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 591 | <h1><a class="toc-backref" href="#id19">Language map</a></h1> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 592 | <p>If you are adding support for a new language to LLVMC, you'll need to | 
 | 593 | modify the language map, which defines mappings from file extensions | 
 | 594 | to language names. It is used to choose the proper toolchain(s) for a | 
 | 595 | given input file set. Language map definition looks like this:</p> | 
| Anton Korobeynikov | 7412031 | 2008-06-09 04:15:49 +0000 | [diff] [blame] | 596 | <pre class="literal-block"> | 
 | 597 | def LanguageMap : LanguageMap< | 
 | 598 |     [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>, | 
 | 599 |      LangToSuffixes<"c", ["c"]>, | 
 | 600 |      ... | 
 | 601 |     ]>; | 
 | 602 | </pre> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 603 | <p>For example, without those definitions the following command wouldn't work:</p> | 
 | 604 | <pre class="literal-block"> | 
 | 605 | $ llvmc hello.cpp | 
 | 606 | llvmc: Unknown suffix: cpp | 
 | 607 | </pre> | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 608 | <p>The language map entries are needed only for the tools that are linked from the | 
 | 609 | root node. Since a tool can't have multiple output languages, for inner nodes of | 
 | 610 | the graph the input and output languages should match. This is enforced at | 
 | 611 | compile-time.</p> | 
| Reid Spencer | 46d2192 | 2004-08-22 18:06:59 +0000 | [diff] [blame] | 612 | </div> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 613 | <div class="section" id="option-preprocessor"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 614 | <h1><a class="toc-backref" href="#id20">Option preprocessor</a></h1> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 615 | <p>It is sometimes useful to run error-checking code before processing the | 
 | 616 | compilation graph. For example, if optimization options "-O1" and "-O2" are | 
 | 617 | implemented as switches, we might want to output a warning if the user invokes | 
 | 618 | the driver with both of these options enabled.</p> | 
 | 619 | <p>The <tt class="docutils literal"><span class="pre">OptionPreprocessor</span></tt> feature is reserved specially for these | 
 | 620 | occasions. Example (adapted from the built-in Base plugin):</p> | 
 | 621 | <pre class="literal-block"> | 
 | 622 | def Preprocess : OptionPreprocessor< | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 623 | (case (not (any_switch_on ["O0", "O1", "O2", "O3"])), | 
 | 624 |            (set_option "O2"), | 
 | 625 |       (and (switch_on "O3"), (any_switch_on ["O0", "O1", "O2"])), | 
 | 626 |            (unset_option ["O0", "O1", "O2"]), | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 627 |       (and (switch_on "O2"), (any_switch_on ["O0", "O1"])), | 
 | 628 |            (unset_option ["O0", "O1"]), | 
 | 629 |       (and (switch_on "O1"), (switch_on "O0")), | 
 | 630 |            (unset_option "O0")) | 
 | 631 | >; | 
 | 632 | </pre> | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 633 | <p>Here, <tt class="docutils literal"><span class="pre">OptionPreprocessor</span></tt> is used to unset all spurious <tt class="docutils literal"><span class="pre">-O</span></tt> options so | 
 | 634 | that they are not forwarded to the compiler. If no optimization options are | 
 | 635 | specified, <tt class="docutils literal"><span class="pre">-O2</span></tt> is enabled.</p> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 636 | <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 | 
 | 637 | evaluated only once right after the plugin is loaded. The only allowed actions | 
| Mikhail Glushenkov | 0a9ff4b | 2009-12-23 12:49:51 +0000 | [diff] [blame] | 638 | 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 two special actions: | 
| Mikhail Glushenkov | 90b5d04 | 2009-12-17 07:49:26 +0000 | [diff] [blame] | 639 | <tt class="docutils literal"><span class="pre">unset_option</span></tt> and <tt class="docutils literal"><span class="pre">set_option</span></tt>. As their names suggest, they can be used to | 
| Mikhail Glushenkov | 0a9ff4b | 2009-12-23 12:49:51 +0000 | [diff] [blame] | 640 | set or unset a given option. To set an option with <tt class="docutils literal"><span class="pre">set_option</span></tt>, use the | 
 | 641 | two-argument form: <tt class="docutils literal"><span class="pre">(set_option</span> <span class="pre">"parameter",</span> <span class="pre">VALUE)</span></tt>. Here, <tt class="docutils literal"><span class="pre">VALUE</span></tt> can be | 
 | 642 | either a string, a string list, or a boolean constant.</p> | 
 | 643 | <p>For convenience, <tt class="docutils literal"><span class="pre">set_option</span></tt> and <tt class="docutils literal"><span class="pre">unset_option</span></tt> also work on lists. That | 
 | 644 | is, instead of <tt class="docutils literal"><span class="pre">[(unset_option</span> <span class="pre">"A"),</span> <span class="pre">(unset_option</span> <span class="pre">"B")]</span></tt> you can use | 
 | 645 | <tt class="docutils literal"><span class="pre">(unset_option</span> <span class="pre">["A",</span> <span class="pre">"B"])</span></tt>. Obviously, <tt class="docutils literal"><span class="pre">(set_option</span> <span class="pre">["A",</span> <span class="pre">"B"])</span></tt> is valid | 
 | 646 | only if both <tt class="docutils literal"><span class="pre">A</span></tt> and <tt class="docutils literal"><span class="pre">B</span></tt> are switches.</p> | 
| Mikhail Glushenkov | 0a018d3 | 2009-10-25 01:44:24 +0000 | [diff] [blame] | 647 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 648 | <div class="section" id="more-advanced-topics"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 649 | <h1><a class="toc-backref" href="#id21">More advanced topics</a></h1> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 650 | <div class="section" id="hooks-and-environment-variables"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 651 | <span id="hooks"></span><h2><a class="toc-backref" href="#id22">Hooks and environment variables</a></h2> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 652 | <p>Normally, LLVMC executes programs from the system <tt class="docutils literal"><span class="pre">PATH</span></tt>. Sometimes, | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 653 | this is not sufficient: for example, we may want to specify tool paths | 
 | 654 | or names in the configuration file. This can be easily achieved via | 
 | 655 | the hooks mechanism. To write your own hooks, just add their | 
 | 656 | 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 | 
 | 657 | your plugin directory. Hooks should live in the <tt class="docutils literal"><span class="pre">hooks</span></tt> namespace | 
 | 658 | 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> | 
 | 659 | <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 | 
 | 660 | <tt class="docutils literal"><span class="pre">cmd_line</span></tt> tool property:</p> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 661 | <pre class="literal-block"> | 
 | 662 | (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)") | 
 | 663 | </pre> | 
| Mikhail Glushenkov | 5303c75 | 2009-01-28 03:47:38 +0000 | [diff] [blame] | 664 | <p>To pass arguments to hooks, use the following syntax:</p> | 
 | 665 | <pre class="literal-block"> | 
 | 666 | (cmd_line "$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2") | 
 | 667 | </pre> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 668 | <p>It is also possible to use environment variables in the same manner:</p> | 
 | 669 | <pre class="literal-block"> | 
 | 670 | (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)") | 
 | 671 | </pre> | 
 | 672 | <p>To change the command line string based on user-provided options use | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 673 | the <tt class="docutils literal"><span class="pre">case</span></tt> expression (documented <a class="reference internal" href="#case">above</a>):</p> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 674 | <pre class="literal-block"> | 
 | 675 | (cmd_line | 
 | 676 |   (case | 
 | 677 |     (switch_on "E"), | 
 | 678 |        "llvm-g++ -E -x c $INFILE -o $OUTFILE", | 
 | 679 |     (default), | 
 | 680 |        "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm")) | 
 | 681 | </pre> | 
 | 682 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 683 | <div class="section" id="how-plugins-are-loaded"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 684 | <span id="priorities"></span><h2><a class="toc-backref" href="#id23">How plugins are loaded</a></h2> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 685 | <p>It is possible for LLVMC plugins to depend on each other. For example, | 
 | 686 | one can create edges between nodes defined in some other plugin. To | 
 | 687 | make this work, however, that plugin should be loaded first. To | 
 | 688 | achieve this, the concept of plugin priority was introduced. By | 
 | 689 | default, every plugin has priority zero; to specify the priority | 
 | 690 | explicitly, put the following line in your plugin's TableGen file:</p> | 
 | 691 | <pre class="literal-block"> | 
 | 692 | def Priority : PluginPriority<$PRIORITY_VALUE>; | 
 | 693 | # Where PRIORITY_VALUE is some integer > 0 | 
 | 694 | </pre> | 
 | 695 | <p>Plugins are loaded in order of their (increasing) priority, starting | 
 | 696 | with 0. Therefore, the plugin with the highest priority value will be | 
 | 697 | loaded last.</p> | 
 | 698 | </div> | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 699 | <div class="section" id="debugging"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 700 | <h2><a class="toc-backref" href="#id24">Debugging</a></h2> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 701 | <p>When writing LLVMC plugins, it can be useful to get a visual view of | 
 | 702 | the resulting compilation graph. This can be achieved via the command | 
| Mikhail Glushenkov | d6d2efc | 2009-05-06 01:41:47 +0000 | [diff] [blame] | 703 | 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 | 
 | 704 | <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 | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 705 | creates a Graphviz source file (<tt class="docutils literal"><span class="pre">compilation-graph.dot</span></tt>) in the | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 706 | current directory.</p> | 
| Mikhail Glushenkov | f9b1d79 | 2009-01-15 02:42:40 +0000 | [diff] [blame] | 707 | <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 | 
 | 708 | compilation graph for common errors like mismatched output/input | 
 | 709 | language names, multiple default edges and cycles. These checks can't | 
 | 710 | be performed at compile-time because the plugins can load code | 
 | 711 | 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 | 
 | 712 | perform any compilation tasks and returns the number of encountered | 
 | 713 | errors as its status code.</p> | 
| Mikhail Glushenkov | e25b845 | 2009-06-30 00:16:43 +0000 | [diff] [blame] | 714 | </div> | 
 | 715 | <div class="section" id="conditioning-on-the-executable-name"> | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 716 | <h2><a class="toc-backref" href="#id25">Conditioning on the executable name</a></h2> | 
| Mikhail Glushenkov | e25b845 | 2009-06-30 00:16:43 +0000 | [diff] [blame] | 717 | <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 | 
 | 718 | accessible only in the C++ code (i.e. hooks). Use the following code:</p> | 
 | 719 | <pre class="literal-block"> | 
 | 720 | namespace llvmc { | 
 | 721 | extern const char* ProgramName; | 
 | 722 | } | 
 | 723 |  | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 724 | namespace hooks { | 
 | 725 |  | 
| Mikhail Glushenkov | e25b845 | 2009-06-30 00:16:43 +0000 | [diff] [blame] | 726 | std::string MyHook() { | 
 | 727 | //... | 
 | 728 | if (strcmp(ProgramName, "mydriver") == 0) { | 
 | 729 |    //... | 
 | 730 |  | 
 | 731 | } | 
| Mikhail Glushenkov | abf31b9 | 2009-12-07 18:26:24 +0000 | [diff] [blame] | 732 |  | 
 | 733 | } // end namespace hooks | 
| Mikhail Glushenkov | e25b845 | 2009-06-30 00:16:43 +0000 | [diff] [blame] | 734 | </pre> | 
 | 735 | <p>In general, you're encouraged not to make the behaviour dependent on the | 
 | 736 | executable file name, and use command-line switches instead. See for example how | 
 | 737 | the <tt class="docutils literal"><span class="pre">Base</span></tt> plugin behaves when it needs to choose the correct linker options | 
 | 738 | (think <tt class="docutils literal"><span class="pre">g++</span></tt> vs. <tt class="docutils literal"><span class="pre">gcc</span></tt>).</p> | 
| Mikhail Glushenkov | 9053154 | 2008-12-11 23:43:14 +0000 | [diff] [blame] | 739 | <hr /> | 
| Anton Korobeynikov | 28b6670 | 2008-06-09 04:17:51 +0000 | [diff] [blame] | 740 | <address> | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 741 | <a href="http://jigsaw.w3.org/css-validator/check/referer"> | 
 | 742 | <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" | 
 | 743 |    alt="Valid CSS" /></a> | 
 | 744 | <a href="http://validator.w3.org/check?uri=referer"> | 
 | 745 | <img src="http://www.w3.org/Icons/valid-xhtml10-blue" | 
 | 746 |    alt="Valid XHTML 1.0 Transitional"/></a> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 747 |  | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 748 | <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br /> | 
| NAKAMURA Takumi | b9a3363 | 2011-04-09 02:13:37 +0000 | [diff] [blame] | 749 | <a href="http://llvm.org/">LLVM Compiler Infrastructure</a><br /> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 750 |  | 
| Duncan Sands | 7e7ae5a | 2010-02-18 14:08:13 +0000 | [diff] [blame] | 751 | Last modified: $Date$ | 
| Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 752 | </address></div> | 
| Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 753 | </div> | 
 | 754 | </div> | 
| Reid Spencer | b1254a1 | 2004-08-09 03:08:29 +0000 | [diff] [blame] | 755 | </body> | 
 | 756 | </html> |