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