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