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