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