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