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