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