Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 1 | =================================== |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 2 | Customizing LLVMC: Reference Manual |
| 3 | =================================== |
Mikhail Glushenkov | 23f522a | 2008-12-13 17:51:47 +0000 | [diff] [blame] | 4 | .. |
| 5 | This file was automatically generated by rst2html. |
| 6 | Please do not edit directly! |
| 7 | The ReST source lives in the directory 'tools/llvmc/doc'. |
Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 8 | |
| 9 | .. contents:: |
| 10 | |
| 11 | .. raw:: html |
| 12 | |
| 13 | <div class="doc_author"> |
| 14 | <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p> |
| 15 | </div> |
| 16 | |
| 17 | Introduction |
| 18 | ============ |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 19 | |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 20 | LLVMC is a generic compiler driver, designed to be customizable and |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 21 | extensible. It plays the same role for LLVM as the ``gcc`` program does for |
| 22 | GCC - LLVMC's job is essentially to transform a set of input files into a set of |
| 23 | targets depending on configuration rules and user options. What makes LLVMC |
| 24 | different is that these transformation rules are completely customizable - in |
| 25 | fact, LLVMC knows nothing about the specifics of transformation (even the |
| 26 | command-line options are mostly not hard-coded) and regards the transformation |
| 27 | structure as an abstract graph. The structure of this graph is described in |
| 28 | high-level TableGen code, from which an efficient C++ representation is |
| 29 | automatically derived. This makes it possible to adapt LLVMC for other |
| 30 | purposes - for example, as a build tool for game resources. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 31 | |
Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 32 | Because LLVMC employs TableGen_ as its configuration language, you |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 33 | need to be familiar with it to customize LLVMC. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 34 | |
Mikhail Glushenkov | 4aecec1 | 2009-06-17 02:56:08 +0000 | [diff] [blame] | 35 | .. _TableGen: http://llvm.org/docs/TableGenFundamentals.html |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 36 | |
| 37 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 38 | Compiling with ``llvmc`` |
| 39 | ======================== |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 40 | |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 41 | LLVMC tries hard to be as compatible with ``gcc`` as possible, |
| 42 | although there are some small differences. Most of the time, however, |
| 43 | you shouldn't be able to notice them:: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 44 | |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 45 | $ # This works as expected: |
Mikhail Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 46 | $ llvmc -O3 -Wall hello.cpp |
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 | 4aecec1 | 2009-06-17 02:56:08 +0000 | [diff] [blame] | 50 | One nice feature of LLVMC is that one doesn't have to distinguish between |
| 51 | different compilers for different languages (think ``g++`` vs. ``gcc``) - the |
| 52 | right toolchain is chosen automatically based on input language names (which |
| 53 | are, in turn, determined from file extensions). If you want to force files |
| 54 | ending with ".c" to compile as C++, use the ``-x`` option, just like you would |
| 55 | do it with ``gcc``:: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 56 | |
Mikhail Glushenkov | ebdeca7 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 57 | $ # hello.c is really a C++ file |
Mikhail Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 58 | $ llvmc -x c++ hello.c |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 59 | $ ./a.out |
| 60 | hello |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 61 | |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 62 | On the other hand, when using LLVMC as a linker to combine several C++ |
| 63 | object files you should provide the ``--linker`` option since it's |
| 64 | impossible for LLVMC to choose the right linker in that case:: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 65 | |
Mikhail Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 66 | $ llvmc -c hello.cpp |
| 67 | $ llvmc hello.o |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 68 | [A lot of link-time errors skipped] |
Mikhail Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 69 | $ llvmc --linker=c++ hello.o |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 70 | $ ./a.out |
| 71 | hello |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 72 | |
Mikhail Glushenkov | e8e4d58 | 2009-06-30 00:16:22 +0000 | [diff] [blame] | 73 | By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is also |
| 74 | possible to choose the ``clang`` compiler with the ``-clang`` option. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 75 | |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 76 | |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 77 | Predefined options |
| 78 | ================== |
| 79 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 80 | LLVMC has some built-in options that can't be overridden in the TableGen code: |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 81 | |
| 82 | * ``-o FILE`` - Output file name. |
| 83 | |
| 84 | * ``-x LANGUAGE`` - Specify the language of the following input files |
| 85 | until the next -x option. |
| 86 | |
| 87 | * ``-v`` - Enable verbose mode, i.e. print out all executed commands. |
| 88 | |
Mikhail Glushenkov | 294f507 | 2009-06-25 18:20:44 +0000 | [diff] [blame] | 89 | * ``--save-temps`` - Write temporary files to the current directory and do not |
| 90 | delete them on exit. This option can also take an argument: the |
| 91 | ``--save-temps=obj`` switch will write files into the directory specified with |
| 92 | the ``-o`` option. The ``--save-temps=cwd`` and ``--save-temps`` switches are |
| 93 | both synonyms for the default behaviour. |
| 94 | |
Mikhail Glushenkov | 09826e3 | 2009-07-11 19:27:40 +0000 | [diff] [blame] | 95 | * ``--temp-dir DIRECTORY`` - Store temporary files in the given directory. This |
| 96 | directory is deleted on exit unless ``--save-temps`` is specified. If |
| 97 | ``--save-temps=obj`` is also specified, ``--temp-dir`` is given the |
| 98 | precedence. |
Mikhail Glushenkov | 792f182 | 2009-07-09 19:39:16 +0000 | [diff] [blame] | 99 | |
Mikhail Glushenkov | 4ad0d57 | 2009-03-27 12:58:29 +0000 | [diff] [blame] | 100 | * ``--check-graph`` - Check the compilation for common errors like mismatched |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 101 | output/input language names, multiple default edges and cycles. Exit with code |
| 102 | zero if no errors were found, and return the number of found errors |
| 103 | otherwise. Hidden option, useful for debugging. |
Mikhail Glushenkov | f8c430b | 2009-01-09 16:16:27 +0000 | [diff] [blame] | 104 | |
Mikhail Glushenkov | 4ad0d57 | 2009-03-27 12:58:29 +0000 | [diff] [blame] | 105 | * ``--view-graph`` - Show a graphical representation of the compilation graph |
| 106 | and exit. Requires that you have ``dot`` and ``gv`` programs installed. Hidden |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 107 | option, useful for debugging. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 108 | |
Mikhail Glushenkov | 4ad0d57 | 2009-03-27 12:58:29 +0000 | [diff] [blame] | 109 | * ``--write-graph`` - Write a ``compilation-graph.dot`` file in the current |
| 110 | directory with the compilation graph description in Graphviz format (identical |
Mikhail Glushenkov | 530f399 | 2009-06-16 00:13:52 +0000 | [diff] [blame] | 111 | to the file used by the ``--view-graph`` option). The ``-o`` option can be |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 112 | used to set the output file name. Hidden option, useful for debugging. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 113 | |
Mikhail Glushenkov | 7329610 | 2008-05-30 06:29:17 +0000 | [diff] [blame] | 114 | * ``--help``, ``--help-hidden``, ``--version`` - These options have |
| 115 | their standard meaning. |
| 116 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 117 | Compiling LLVMC-based drivers |
| 118 | ============================= |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 119 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 120 | It's easiest to start working on your own LLVMC driver by copying the skeleton |
| 121 | project which lives under ``$LLVMC_DIR/examples/Skeleton``:: |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 122 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 123 | $ cd $LLVMC_DIR/examples |
| 124 | $ cp -r Skeleton MyDriver |
| 125 | $ cd MyDriver |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 126 | $ ls |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 127 | AutoGenerated.td Hooks.cpp Main.cpp Makefile |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 128 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 129 | As you can see, our basic driver consists of only three files (not counting the |
| 130 | build script). ``AutoGenerated.td`` contains TableGen description of the |
| 131 | compilation graph; its format is documented in the following |
| 132 | sections. ``Hooks.cpp`` is an empty file that should be used for hook |
| 133 | definitions (see `below`__). ``Main.cpp`` is just a helper used to compile the |
| 134 | auto-generated C++ code produced from TableGen source. |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 135 | |
| 136 | __ hooks_ |
| 137 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 138 | The first thing that you should do is to change the ``LLVMC_BASED_DRIVER`` |
| 139 | variable in the ``Makefile``:: |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 140 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 141 | LLVMC_BASED_DRIVER=MyDriver |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 142 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 143 | It can also be a good idea to put your TableGen code into a file with a less |
| 144 | generic name:: |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 145 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 146 | $ touch MyDriver.td |
| 147 | $ vim AutoGenerated.td |
| 148 | [...] |
| 149 | include "MyDriver.td" |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 150 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 151 | If you have more than one TableGen source file, they all should be included from |
| 152 | ``AutoGenerated.td``, since this file is used by the build system to generate |
| 153 | C++ code. |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 154 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 155 | To build your driver, just ``cd`` to its source directory and run ``make``. The |
| 156 | resulting executable will be put into ``$LLVM_OBJ_DIR/$(BuildMode)/bin``. |
Mikhail Glushenkov | 530f399 | 2009-06-16 00:13:52 +0000 | [diff] [blame] | 157 | |
| 158 | If you're compiling LLVM with different source and object directories, then you |
| 159 | must perform the following additional steps before running ``make``:: |
| 160 | |
| 161 | # LLVMC_SRC_DIR = $LLVM_SRC_DIR/tools/llvmc/ |
| 162 | # LLVMC_OBJ_DIR = $LLVM_OBJ_DIR/tools/llvmc/ |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 163 | $ mkdir $LLVMC_OBJ_DIR/examples/MyDriver/ |
| 164 | $ cp $LLVMC_SRC_DIR/examples/MyDriver/Makefile \ |
| 165 | $LLVMC_OBJ_DIR/examples/MyDriver/ |
| 166 | $ cd $LLVMC_OBJ_DIR/examples/MyDriver |
Mikhail Glushenkov | 530f399 | 2009-06-16 00:13:52 +0000 | [diff] [blame] | 167 | $ make |
| 168 | |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 169 | |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 170 | Customizing LLVMC: the compilation graph |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 171 | ======================================== |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 172 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 173 | Each TableGen configuration file should include the common definitions:: |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 174 | |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 175 | include "llvm/CompilerDriver/Common.td" |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 176 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 177 | Internally, LLVMC stores information about possible source transformations in |
| 178 | form of a graph. Nodes in this graph represent tools, and edges between two |
| 179 | nodes represent a transformation path. A special "root" node is used to mark |
| 180 | entry points for the transformations. LLVMC also assigns a weight to each edge |
| 181 | (more on this later) to choose between several alternative edges. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 182 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 183 | The definition of the compilation graph (see file ``llvmc/src/Base.td`` for an |
| 184 | example) is just a list of edges:: |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 185 | |
| 186 | def CompilationGraph : CompilationGraph<[ |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 187 | Edge<"root", "llvm_gcc_c">, |
| 188 | Edge<"root", "llvm_gcc_assembler">, |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 189 | ... |
| 190 | |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 191 | Edge<"llvm_gcc_c", "llc">, |
| 192 | Edge<"llvm_gcc_cpp", "llc">, |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 193 | ... |
| 194 | |
Mikhail Glushenkov | 536637f | 2008-11-25 21:34:53 +0000 | [diff] [blame] | 195 | OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"), |
| 196 | (inc_weight))>, |
| 197 | OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"), |
| 198 | (inc_weight))>, |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 199 | ... |
| 200 | |
Mikhail Glushenkov | 0108877 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 201 | OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker", |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 202 | (case (input_languages_contain "c++"), (inc_weight), |
| 203 | (or (parameter_equals "linker", "g++"), |
| 204 | (parameter_equals "linker", "c++")), (inc_weight))>, |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 205 | ... |
| 206 | |
| 207 | ]>; |
| 208 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 209 | As you can see, the edges can be either default or optional, where optional |
| 210 | edges are differentiated by an additional ``case`` expression used to calculate |
| 211 | the weight of this edge. Notice also that we refer to tools via their names (as |
| 212 | strings). This makes it possible to add edges to an existing compilation graph |
| 213 | without having to know about all tool definitions used in the graph. |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 214 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 215 | The default edges are assigned a weight of 1, and optional edges get a weight of |
| 216 | 0 + 2*N where N is the number of tests that evaluated to true in the ``case`` |
| 217 | expression. It is also possible to provide an integer parameter to |
| 218 | ``inc_weight`` and ``dec_weight`` - in this case, the weight is increased (or |
| 219 | decreased) by the provided value instead of the default 2. Default weight of an |
| 220 | optional edge can be changed by using the ``default`` clause of the ``case`` |
Mikhail Glushenkov | 7e6d70a | 2008-11-26 22:59:45 +0000 | [diff] [blame] | 221 | construct. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 222 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 223 | When passing an input file through the graph, LLVMC picks the edge with the |
| 224 | maximum weight. To avoid ambiguity, there should be only one default edge |
| 225 | between two nodes (with the exception of the root node, which gets a special |
| 226 | treatment - there you are allowed to specify one default edge *per language*). |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 227 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 228 | When multiple compilation graphs are defined, they are merged together. Multiple |
| 229 | edges with the same end nodes are not allowed (i.e. the graph is not a |
| 230 | multigraph), and will lead to a compile-time error. |
Mikhail Glushenkov | 7e6d70a | 2008-11-26 22:59:45 +0000 | [diff] [blame] | 231 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 232 | To get a visual representation of the compilation graph (useful for debugging), |
| 233 | run ``llvmc --view-graph``. You will need ``dot`` and ``gsview`` installed for |
| 234 | this to work properly. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 235 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 236 | Describing options |
| 237 | ================== |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 238 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 239 | Command-line options supported by the driver are defined by using an |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 240 | ``OptionList``:: |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 241 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 242 | def Options : OptionList<[ |
| 243 | (switch_option "E", (help "Help string")), |
| 244 | (alias_option "quiet", "q") |
| 245 | ... |
| 246 | ]>; |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 247 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 248 | As you can see, the option list is just a list of DAGs, where each DAG is an |
| 249 | option description consisting of the option name and some properties. More than |
| 250 | one option list can be defined (they are all merged together in the end), which |
| 251 | can be handy if one wants to separate option groups syntactically. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 252 | |
| 253 | * Possible option types: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 254 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 255 | - ``switch_option`` - a simple boolean switch without arguments, for example |
Mikhail Glushenkov | fc97aeb | 2010-07-19 03:16:25 +0000 | [diff] [blame] | 256 | ``-O2`` or ``-time``. At most one occurrence is allowed by default. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 257 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 258 | - ``parameter_option`` - option that takes one argument, for example |
| 259 | ``-std=c99``. It is also allowed to use spaces instead of the equality |
| 260 | sign: ``-std c99``. At most one occurrence is allowed. |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 261 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 262 | - ``parameter_list_option`` - same as the above, but more than one option |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 263 | occurrence is allowed. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 264 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 265 | - ``prefix_option`` - same as the parameter_option, but the option name and |
| 266 | argument do not have to be separated. Example: ``-ofile``. This can be also |
| 267 | specified as ``-o file``; however, ``-o=file`` will be parsed incorrectly |
| 268 | (``=file`` will be interpreted as option value). At most one occurrence is |
| 269 | allowed. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 7a2bdde | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 271 | - ``prefix_list_option`` - same as the above, but more than one occurrence of |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 272 | the option is allowed; example: ``-lm -lpthread``. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 273 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 274 | - ``alias_option`` - a special option type for creating aliases. Unlike other |
| 275 | option types, aliases are not allowed to have any properties besides the |
Mikhail Glushenkov | 4d5a299 | 2010-11-22 17:10:20 +0000 | [diff] [blame] | 276 | aliased option name. |
| 277 | Usage example: ``(alias_option "preprocess", "E")`` |
Mikhail Glushenkov | 0ab8ac3 | 2008-05-30 06:28:00 +0000 | [diff] [blame] | 278 | |
Mikhail Glushenkov | fc97aeb | 2010-07-19 03:16:25 +0000 | [diff] [blame] | 279 | - ``switch_list_option`` - like ``switch_option`` with the ``zero_or_more`` |
| 280 | property, but remembers how many times the switch was turned on. Useful |
| 281 | mostly for forwarding. Example: when ``-foo`` is a switch option (with the |
| 282 | ``zero_or_more`` property), the command ``driver -foo -foo`` is forwarded |
| 283 | as ``some-tool -foo``, but when ``-foo`` is a switch list, the same command |
| 284 | is forwarded as ``some-tool -foo -foo``. |
| 285 | |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 286 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 287 | * Possible option properties: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 288 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 289 | - ``help`` - help string associated with this option. Used for ``--help`` |
| 290 | output. |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 291 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 292 | - ``required`` - this option must be specified exactly once (or, in case of |
| 293 | the list options without the ``multi_val`` property, at least |
Mikhail Glushenkov | b5c4239 | 2010-03-05 04:46:39 +0000 | [diff] [blame] | 294 | once). Incompatible with ``optional`` and ``one_or_more``. |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 295 | |
Mikhail Glushenkov | b5c4239 | 2010-03-05 04:46:39 +0000 | [diff] [blame] | 296 | - ``optional`` - the option can be specified either zero times or exactly |
| 297 | once. The default for switch options. Useful only for list options in |
| 298 | conjunction with ``multi_val``. Incompatible with ``required``, |
| 299 | ``zero_or_more`` and ``one_or_more``. |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 300 | |
Mikhail Glushenkov | b5c4239 | 2010-03-05 04:46:39 +0000 | [diff] [blame] | 301 | - ``one_or_more`` - the option must be specified at least once. Can be useful |
| 302 | to allow switch options be both obligatory and be specified multiple |
| 303 | times. For list options is useful only in conjunction with ``multi_val``; |
| 304 | for ordinary it is synonymous with ``required``. Incompatible with |
| 305 | ``required``, ``optional`` and ``zero_or_more``. |
| 306 | |
| 307 | - ``zero_or_more`` - the option can be specified zero or more times. Useful |
| 308 | to allow a single switch option to be specified more than |
| 309 | once. Incompatible with ``required``, ``optional`` and ``one_or_more``. |
Mikhail Glushenkov | 77ddce9 | 2008-05-06 18:17:19 +0000 | [diff] [blame] | 310 | |
Mikhail Glushenkov | f9b1d79 | 2009-01-15 02:42:40 +0000 | [diff] [blame] | 311 | - ``hidden`` - the description of this option will not appear in |
| 312 | the ``--help`` output (but will appear in the ``--help-hidden`` |
| 313 | output). |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 314 | |
Mikhail Glushenkov | f9b1d79 | 2009-01-15 02:42:40 +0000 | [diff] [blame] | 315 | - ``really_hidden`` - the option will not be mentioned in any help |
Mikhail Glushenkov | 739c720 | 2008-11-28 00:13:25 +0000 | [diff] [blame] | 316 | output. |
| 317 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 318 | - ``comma_separated`` - Indicates that any commas specified for an option's |
| 319 | value should be used to split the value up into multiple values for the |
| 320 | option. This property is valid only for list options. In conjunction with |
| 321 | ``forward_value`` can be used to implement option forwarding in style of |
| 322 | gcc's ``-Wa,``. |
| 323 | |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 324 | - ``multi_val n`` - this option takes *n* arguments (can be useful in some |
| 325 | special cases). Usage example: ``(parameter_list_option "foo", (multi_val |
Mikhail Glushenkov | 4e61387 | 2009-10-21 02:13:52 +0000 | [diff] [blame] | 326 | 3))``; the command-line syntax is '-foo a b c'. Only list options can have |
Mikhail Glushenkov | e4ac23a | 2009-12-15 03:04:52 +0000 | [diff] [blame] | 327 | this attribute; you can, however, use the ``one_or_more``, ``optional`` |
Mikhail Glushenkov | 4e61387 | 2009-10-21 02:13:52 +0000 | [diff] [blame] | 328 | and ``required`` properties. |
Mikhail Glushenkov | 19d3e82 | 2009-01-28 03:47:20 +0000 | [diff] [blame] | 329 | |
Mikhail Glushenkov | dad7820 | 2009-07-07 16:09:29 +0000 | [diff] [blame] | 330 | - ``init`` - this option has a default value, either a string (if it is a |
Mikhail Glushenkov | 68d475a | 2009-12-15 03:03:37 +0000 | [diff] [blame] | 331 | parameter), or a boolean (if it is a switch; as in C++, boolean constants |
| 332 | are called ``true`` and ``false``). List options can't have ``init`` |
| 333 | attribute. |
| 334 | Usage examples: ``(switch_option "foo", (init true))``; ``(prefix_option |
| 335 | "bar", (init "baz"))``. |
Mikhail Glushenkov | dad7820 | 2009-07-07 16:09:29 +0000 | [diff] [blame] | 336 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 337 | .. _case: |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 338 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 339 | Conditional evaluation |
| 340 | ====================== |
Mikhail Glushenkov | 0ab8ac3 | 2008-05-30 06:28:00 +0000 | [diff] [blame] | 341 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 342 | The 'case' construct is the main means by which programmability is achieved in |
| 343 | LLVMC. It can be used to calculate edge weights, program actions and modify the |
| 344 | shell commands to be executed. The 'case' expression is designed after the |
| 345 | similarly-named construct in functional languages and takes the form ``(case |
| 346 | (test_1), statement_1, (test_2), statement_2, ... (test_N), statement_N)``. The |
| 347 | statements are evaluated only if the corresponding tests evaluate to true. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 348 | |
| 349 | Examples:: |
| 350 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 351 | // Edge weight calculation |
| 352 | |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 353 | // Increases edge weight by 5 if "-A" is provided on the |
| 354 | // command-line, and by 5 more if "-B" is also provided. |
| 355 | (case |
| 356 | (switch_on "A"), (inc_weight 5), |
| 357 | (switch_on "B"), (inc_weight 5)) |
| 358 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 359 | |
| 360 | // Tool command line specification |
| 361 | |
| 362 | // Evaluates to "cmdline1" if the option "-A" is provided on the |
| 363 | // command line; to "cmdline2" if "-B" is provided; |
| 364 | // otherwise to "cmdline3". |
| 365 | |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 366 | (case |
| 367 | (switch_on "A"), "cmdline1", |
| 368 | (switch_on "B"), "cmdline2", |
| 369 | (default), "cmdline3") |
| 370 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 371 | Note the slight difference in 'case' expression handling in contexts of edge |
| 372 | weights and command line specification - in the second example the value of the |
| 373 | ``"B"`` switch is never checked when switch ``"A"`` is enabled, and the whole |
| 374 | expression always evaluates to ``"cmdline1"`` in that case. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 375 | |
| 376 | Case expressions can also be nested, i.e. the following is legal:: |
| 377 | |
| 378 | (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...) |
| 379 | (default), ...) |
| 380 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 381 | You should, however, try to avoid doing that because it hurts readability. It is |
| 382 | usually better to split tool descriptions and/or use TableGen inheritance |
| 383 | instead. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 384 | |
| 385 | * Possible tests are: |
| 386 | |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 387 | - ``switch_on`` - Returns true if a given command-line switch is provided by |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 388 | the user. Can be given multiple arguments, in that case ``(switch_on "foo", |
| 389 | "bar", "baz")`` is equivalent to ``(and (switch_on "foo"), (switch_on |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 390 | "bar"), (switch_on "baz"))``. |
| 391 | Example: ``(switch_on "opt")``. |
| 392 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 393 | - ``any_switch_on`` - Given a number of switch options, returns true if any of |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 394 | the switches is turned on. |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 395 | Example: ``(any_switch_on "foo", "bar", "baz")`` is equivalent to ``(or |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 396 | (switch_on "foo"), (switch_on "bar"), (switch_on "baz"))``. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 397 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 398 | - ``parameter_equals`` - Returns true if a command-line parameter (first |
| 399 | argument) equals a given value (second argument). |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 400 | Example: ``(parameter_equals "W", "all")``. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 401 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 402 | - ``element_in_list`` - Returns true if a command-line parameter list (first |
| 403 | argument) contains a given value (second argument). |
Mikhail Glushenkov | 4227c0f | 2009-12-01 09:19:09 +0000 | [diff] [blame] | 404 | Example: ``(element_in_list "l", "pthread")``. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 405 | |
| 406 | - ``input_languages_contain`` - Returns true if a given language |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 407 | belongs to the current input language set. |
| 408 | Example: ``(input_languages_contain "c++")``. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 409 | |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 410 | - ``in_language`` - Evaluates to true if the input file language is equal to |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 411 | the argument. At the moment works only with ``command`` and ``actions`` (on |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 412 | non-join nodes). |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 413 | Example: ``(in_language "c++")``. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 414 | |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 415 | - ``not_empty`` - Returns true if a given option (which should be either a |
| 416 | parameter or a parameter list) is set by the user. Like ``switch_on``, can |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 417 | be also given multiple arguments. |
| 418 | Examples: ``(not_empty "o")``, ``(not_empty "o", "l")``. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 419 | |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 420 | - ``any_not_empty`` - Returns true if ``not_empty`` returns true for any of |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 421 | the provided options. |
| 422 | Example: ``(any_not_empty "foo", "bar", "baz")`` is equivalent to ``(or |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 423 | (not_empty "foo"), (not_empty "bar"), (not_empty "baz"))``. |
| 424 | |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 425 | - ``empty`` - The opposite of ``not_empty``. Equivalent to ``(not (not_empty |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 426 | X))``. Can be given multiple arguments. |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 427 | |
| 428 | - ``any_not_empty`` - Returns true if ``not_empty`` returns true for any of |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 429 | the provided options. |
| 430 | Example: ``(any_empty "foo", "bar", "baz")`` is equivalent to ``(or |
| 431 | (not_empty "foo"), (not_empty "bar"), (not_empty "baz"))``. |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 432 | |
Mikhail Glushenkov | ad981bf | 2009-09-28 01:16:42 +0000 | [diff] [blame] | 433 | - ``single_input_file`` - Returns true if there was only one input file |
| 434 | provided on the command-line. Used without arguments: |
| 435 | ``(single_input_file)``. |
| 436 | |
| 437 | - ``multiple_input_files`` - Equivalent to ``(not (single_input_file))`` (the |
| 438 | case of zero input files is considered an error). |
| 439 | |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 440 | - ``default`` - Always evaluates to true. Should always be the last |
| 441 | test in the ``case`` expression. |
| 442 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 443 | - ``and`` - A standard logical combinator that returns true iff all of |
Mikhail Glushenkov | d66e8de | 2009-09-28 01:16:07 +0000 | [diff] [blame] | 444 | its arguments return true. Used like this: ``(and (test1), (test2), |
| 445 | ... (testN))``. Nesting of ``and`` and ``or`` is allowed, but not |
| 446 | encouraged. |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 447 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 448 | - ``or`` - A logical combinator that returns true iff any of its arguments |
| 449 | return true. |
| 450 | Example: ``(or (test1), (test2), ... (testN))``. |
Mikhail Glushenkov | d66e8de | 2009-09-28 01:16:07 +0000 | [diff] [blame] | 451 | |
| 452 | - ``not`` - Standard unary logical combinator that negates its |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 453 | argument. |
| 454 | Example: ``(not (or (test1), (test2), ... (testN)))``. |
Mikhail Glushenkov | d66e8de | 2009-09-28 01:16:07 +0000 | [diff] [blame] | 455 | |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 456 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 457 | Writing a tool description |
| 458 | ========================== |
| 459 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 460 | As was said earlier, nodes in the compilation graph represent tools, which are |
| 461 | described separately. A tool definition looks like this (taken from the |
| 462 | ``llvmc/src/Base.td`` file):: |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 463 | |
| 464 | def llvm_gcc_cpp : Tool<[ |
| 465 | (in_language "c++"), |
| 466 | (out_language "llvm-assembler"), |
| 467 | (output_suffix "bc"), |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 468 | (command "llvm-g++ -c -emit-llvm"), |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 469 | (sink) |
| 470 | ]>; |
| 471 | |
| 472 | This defines a new tool called ``llvm_gcc_cpp``, which is an alias for |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 473 | ``llvm-g++``. As you can see, a tool definition is just a list of properties; |
| 474 | most of them should be self-explanatory. The ``sink`` property means that this |
| 475 | tool should be passed all command-line options that aren't mentioned in the |
| 476 | option list. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 477 | |
| 478 | The complete list of all currently implemented tool properties follows. |
| 479 | |
| 480 | * Possible tool properties: |
| 481 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 482 | - ``in_language`` - input language name. Can be given multiple arguments, in |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 483 | case the tool supports multiple input languages. Used for typechecking and |
| 484 | mapping file extensions to tools. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 485 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 486 | - ``out_language`` - output language name. Multiple output languages are |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 487 | allowed. Used for typechecking the compilation graph. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 488 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 489 | - ``output_suffix`` - output file suffix. Can also be changed dynamically, see |
| 490 | documentation on `actions`__. |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 491 | |
| 492 | __ actions_ |
| 493 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 494 | - ``command`` - the actual command used to run the tool. You can use output |
| 495 | redirection with ``>``, hook invocations (``$CALL``), environment variables |
| 496 | (via ``$ENV``) and the ``case`` construct. |
| 497 | |
| 498 | - ``join`` - this tool is a "join node" in the graph, i.e. it gets a list of |
| 499 | input files and joins them together. Used for linkers. |
| 500 | |
| 501 | - ``sink`` - all command-line options that are not handled by other tools are |
| 502 | passed to this tool. |
| 503 | |
| 504 | - ``actions`` - A single big ``case`` expression that specifies how this tool |
| 505 | reacts on command-line options (described in more detail `below`__). |
| 506 | |
| 507 | __ actions_ |
| 508 | |
| 509 | - ``out_file_option``, ``in_file_option`` - Options appended to the |
| 510 | ``command`` string to designate output and input files. Default values are |
| 511 | ``"-o"`` and ``""``, respectively. |
| 512 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 513 | .. _actions: |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 514 | |
| 515 | Actions |
| 516 | ------- |
| 517 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 518 | A tool often needs to react to command-line options, and this is precisely what |
| 519 | the ``actions`` property is for. The next example illustrates this feature:: |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 520 | |
| 521 | def llvm_gcc_linker : Tool<[ |
| 522 | (in_language "object-code"), |
| 523 | (out_language "executable"), |
| 524 | (output_suffix "out"), |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 525 | (command "llvm-gcc"), |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 526 | (join), |
| 527 | (actions (case (not_empty "L"), (forward "L"), |
| 528 | (not_empty "l"), (forward "l"), |
| 529 | (not_empty "dummy"), |
| 530 | [(append_cmd "-dummy1"), (append_cmd "-dummy2")]) |
| 531 | ]>; |
| 532 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 533 | The ``actions`` tool property is implemented on top of the omnipresent ``case`` |
| 534 | expression. It associates one or more different *actions* with given |
| 535 | conditions - in the example, the actions are ``forward``, which forwards a given |
| 536 | option unchanged, and ``append_cmd``, which appends a given string to the tool |
| 537 | execution command. Multiple actions can be associated with a single condition by |
| 538 | using a list of actions (used in the example to append some dummy options). The |
| 539 | same ``case`` construct can also be used in the ``cmd_line`` property to modify |
| 540 | the tool command line. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 541 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 542 | The "join" property used in the example means that this tool behaves like a |
| 543 | linker. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 544 | |
| 545 | The list of all possible actions follows. |
| 546 | |
| 547 | * Possible actions: |
| 548 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 549 | - ``append_cmd`` - Append a string to the tool invocation command. |
| 550 | Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``. |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 551 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 552 | - ``error`` - Exit with error. |
Mikhail Glushenkov | 5c2b6b2 | 2008-12-17 02:47:01 +0000 | [diff] [blame] | 553 | Example: ``(error "Mixing -c and -S is not allowed!")``. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 554 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 555 | - ``warning`` - Print a warning. |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 556 | Example: ``(warning "Specifying both -O1 and -O2 is meaningless!")``. |
| 557 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 558 | - ``forward`` - Forward the option unchanged. |
| 559 | Example: ``(forward "Wall")``. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 560 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 561 | - ``forward_as`` - Change the option's name, but forward the argument |
| 562 | unchanged. |
Mikhail Glushenkov | e89331b | 2009-05-06 01:41:19 +0000 | [diff] [blame] | 563 | Example: ``(forward_as "O0", "--disable-optimization")``. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 564 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 565 | - ``forward_value`` - Forward only option's value. Cannot be used with switch |
| 566 | options (since they don't have values), but works fine with lists. |
| 567 | Example: ``(forward_value "Wa,")``. |
| 568 | |
| 569 | - ``forward_transformed_value`` - As above, but applies a hook to the |
| 570 | option's value before forwarding (see `below`__). When |
| 571 | ``forward_transformed_value`` is applied to a list |
| 572 | option, the hook must have signature |
| 573 | ``std::string hooks::HookName (const std::vector<std::string>&)``. |
| 574 | Example: ``(forward_transformed_value "m", "ConvertToMAttr")``. |
| 575 | |
| 576 | __ hooks_ |
| 577 | |
| 578 | - ``output_suffix`` - Modify the output suffix of this tool. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 579 | Example: ``(output_suffix "i")``. |
| 580 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 581 | - ``stop_compilation`` - Stop compilation after this tool processes its |
| 582 | input. Used without arguments. |
| 583 | Example: ``(stop_compilation)``. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 584 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 585 | |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 586 | Language map |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 587 | ============ |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 588 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 589 | If you are adding support for a new language to LLVMC, you'll need to modify the |
| 590 | language map, which defines mappings from file extensions to language names. It |
| 591 | is used to choose the proper toolchain(s) for a given input file set. Language |
| 592 | map definition looks like this:: |
Anton Korobeynikov | ac67b7e | 2008-03-23 08:57:20 +0000 | [diff] [blame] | 593 | |
| 594 | def LanguageMap : LanguageMap< |
| 595 | [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>, |
| 596 | LangToSuffixes<"c", ["c"]>, |
| 597 | ... |
| 598 | ]>; |
| 599 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 600 | For example, without those definitions the following command wouldn't work:: |
| 601 | |
| 602 | $ llvmc hello.cpp |
| 603 | llvmc: Unknown suffix: cpp |
| 604 | |
Mikhail Glushenkov | 994dbe0 | 2009-12-17 07:49:16 +0000 | [diff] [blame] | 605 | The language map entries are needed only for the tools that are linked from the |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 606 | root node. A tool can have multiple output languages. |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 607 | |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 608 | Option preprocessor |
| 609 | =================== |
| 610 | |
| 611 | It is sometimes useful to run error-checking code before processing the |
| 612 | compilation graph. For example, if optimization options "-O1" and "-O2" are |
| 613 | implemented as switches, we might want to output a warning if the user invokes |
| 614 | the driver with both of these options enabled. |
| 615 | |
| 616 | The ``OptionPreprocessor`` feature is reserved specially for these |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 617 | occasions. Example (adapted from ``llvm/src/Base.td.in``):: |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 618 | |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 619 | |
Mikhail Glushenkov | 994dbe0 | 2009-12-17 07:49:16 +0000 | [diff] [blame] | 620 | def Preprocess : OptionPreprocessor< |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 621 | (case (not (any_switch_on "O0", "O1", "O2", "O3")), |
Mikhail Glushenkov | 994dbe0 | 2009-12-17 07:49:16 +0000 | [diff] [blame] | 622 | (set_option "O2"), |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 623 | (and (switch_on "O3"), (any_switch_on "O0", "O1", "O2")), |
| 624 | (unset_option "O0", "O1", "O2"), |
| 625 | (and (switch_on "O2"), (any_switch_on "O0", "O1")), |
| 626 | (unset_option "O0", "O1"), |
Mikhail Glushenkov | 994dbe0 | 2009-12-17 07:49:16 +0000 | [diff] [blame] | 627 | (and (switch_on "O1"), (switch_on "O0")), |
| 628 | (unset_option "O0")) |
| 629 | >; |
| 630 | |
| 631 | Here, ``OptionPreprocessor`` is used to unset all spurious ``-O`` options so |
| 632 | that they are not forwarded to the compiler. If no optimization options are |
| 633 | specified, ``-O2`` is enabled. |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 634 | |
| 635 | ``OptionPreprocessor`` is basically a single big ``case`` expression, which is |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 636 | evaluated only once right after the driver is started. The only allowed actions |
Mikhail Glushenkov | e0b6570 | 2009-12-23 12:49:30 +0000 | [diff] [blame] | 637 | in ``OptionPreprocessor`` are ``error``, ``warning``, and two special actions: |
Mikhail Glushenkov | 994dbe0 | 2009-12-17 07:49:16 +0000 | [diff] [blame] | 638 | ``unset_option`` and ``set_option``. As their names suggest, they can be used to |
Mikhail Glushenkov | e0b6570 | 2009-12-23 12:49:30 +0000 | [diff] [blame] | 639 | set or unset a given option. To set an option with ``set_option``, use the |
| 640 | two-argument form: ``(set_option "parameter", VALUE)``. Here, ``VALUE`` can be |
| 641 | either a string, a string list, or a boolean constant. |
| 642 | |
Mikhail Glushenkov | 17ef94f | 2010-10-23 07:32:46 +0000 | [diff] [blame] | 643 | For convenience, ``set_option`` and ``unset_option`` also work with multiple |
| 644 | arguments. That is, instead of ``[(unset_option "A"), (unset_option "B")]`` you |
| 645 | can use ``(unset_option "A", "B")``. Obviously, ``(set_option "A", "B")`` is |
| 646 | only valid if both ``A`` and ``B`` are switches. |
Mikhail Glushenkov | 3e4102e | 2009-10-25 01:44:11 +0000 | [diff] [blame] | 647 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 648 | |
| 649 | More advanced topics |
| 650 | ==================== |
| 651 | |
| 652 | .. _hooks: |
| 653 | |
| 654 | Hooks and environment variables |
| 655 | ------------------------------- |
| 656 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 657 | Normally, LLVMC searches for programs in the system ``PATH``. Sometimes, this is |
| 658 | not sufficient: for example, we may want to specify tool paths or names in the |
| 659 | configuration file. This can be achieved via the hooks mechanism. To write your |
| 660 | own hooks, add their definitions to the ``Hooks.cpp`` or drop a ``.cpp`` file |
| 661 | into your driver directory. Hooks should live in the ``hooks`` namespace and |
| 662 | have the signature ``std::string hooks::MyHookName ([const char* Arg0 [ const |
| 663 | char* Arg2 [, ...]]])``. They can be used from the ``command`` tool property:: |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 664 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 665 | (command "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)") |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 666 | |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 667 | To pass arguments to hooks, use the following syntax:: |
| 668 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 669 | (command "$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2") |
Mikhail Glushenkov | a298bb7 | 2009-01-21 13:04:00 +0000 | [diff] [blame] | 670 | |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 671 | It is also possible to use environment variables in the same manner:: |
| 672 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 673 | (command "$ENV(VAR1)/path/to/file -o $ENV(VAR2)") |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 674 | |
| 675 | To change the command line string based on user-provided options use |
| 676 | the ``case`` expression (documented `above`__):: |
| 677 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 678 | (command |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 679 | (case |
| 680 | (switch_on "E"), |
| 681 | "llvm-g++ -E -x c $INFILE -o $OUTFILE", |
| 682 | (default), |
| 683 | "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm")) |
| 684 | |
| 685 | __ case_ |
| 686 | |
Mikhail Glushenkov | 9ecd30c | 2008-09-22 20:48:48 +0000 | [diff] [blame] | 687 | Debugging |
Mikhail Glushenkov | 4410e32 | 2008-12-07 16:47:42 +0000 | [diff] [blame] | 688 | --------- |
Mikhail Glushenkov | 9ecd30c | 2008-09-22 20:48:48 +0000 | [diff] [blame] | 689 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 690 | When writing LLVMC-based drivers, it can be useful to get a visual view of the |
| 691 | resulting compilation graph. This can be achieved via the command line option |
| 692 | ``--view-graph`` (which assumes that Graphviz_ and Ghostview_ are |
| 693 | installed). There is also a ``--write-graph`` option that creates a Graphviz |
| 694 | source file (``compilation-graph.dot``) in the current directory. |
Mikhail Glushenkov | 9ecd30c | 2008-09-22 20:48:48 +0000 | [diff] [blame] | 695 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 696 | Another useful ``llvmc`` option is ``--check-graph``. It checks the compilation |
| 697 | graph for common errors like mismatched output/input language names, multiple |
| 698 | default edges and cycles. When invoked with ``--check-graph``, ``llvmc`` doesn't |
| 699 | perform any compilation tasks and returns the number of encountered errors as |
| 700 | its status code. In the future, these checks will be performed at compile-time |
| 701 | and this option will disappear. |
Mikhail Glushenkov | f8c430b | 2009-01-09 16:16:27 +0000 | [diff] [blame] | 702 | |
Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 703 | .. _Graphviz: http://www.graphviz.org/ |
| 704 | .. _Ghostview: http://pages.cs.wisc.edu/~ghost/ |
Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 705 | |
Mikhail Glushenkov | 875ace5 | 2009-06-30 00:16:00 +0000 | [diff] [blame] | 706 | Conditioning on the executable name |
| 707 | ----------------------------------- |
| 708 | |
| 709 | For now, the executable name (the value passed to the driver in ``argv[0]``) is |
| 710 | accessible only in the C++ code (i.e. hooks). Use the following code:: |
| 711 | |
| 712 | namespace llvmc { |
| 713 | extern const char* ProgramName; |
| 714 | } |
| 715 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 716 | namespace hooks { |
| 717 | |
Mikhail Glushenkov | 875ace5 | 2009-06-30 00:16:00 +0000 | [diff] [blame] | 718 | std::string MyHook() { |
| 719 | //... |
| 720 | if (strcmp(ProgramName, "mydriver") == 0) { |
| 721 | //... |
| 722 | |
| 723 | } |
| 724 | |
Mikhail Glushenkov | b59b0f8 | 2009-12-07 18:26:11 +0000 | [diff] [blame] | 725 | } // end namespace hooks |
| 726 | |
Mikhail Glushenkov | 875ace5 | 2009-06-30 00:16:00 +0000 | [diff] [blame] | 727 | In general, you're encouraged not to make the behaviour dependent on the |
| 728 | executable file name, and use command-line switches instead. See for example how |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame] | 729 | the ``llvmc`` program behaves when it needs to choose the correct linker options |
Mikhail Glushenkov | 875ace5 | 2009-06-30 00:16:00 +0000 | [diff] [blame] | 730 | (think ``g++`` vs. ``gcc``). |
| 731 | |
Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 732 | .. raw:: html |
Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 733 | |
| 734 | <hr /> |
| 735 | <address> |
| 736 | <a href="http://jigsaw.w3.org/css-validator/check/referer"> |
| 737 | <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" |
| 738 | alt="Valid CSS" /></a> |
| 739 | <a href="http://validator.w3.org/check?uri=referer"> |
| 740 | <img src="http://www.w3.org/Icons/valid-xhtml10-blue" |
| 741 | alt="Valid XHTML 1.0 Transitional"/></a> |
| 742 | |
| 743 | <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br /> |
| 744 | <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br /> |
| 745 | |
| 746 | Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $ |
| 747 | </address> |