blob: 91f7dda6a883b3f7c1003a2693ef2ca70d3e463a [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001===================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00004
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +00005LLVMC is a generic compiler driver, designed to be customizable and
6extensible. It plays the same role for LLVM as the ``gcc`` program
7does for GCC - LLVMC's job is essentially to transform a set of input
8files into a set of targets depending on configuration rules and user
9options. What makes LLVMC different is that these transformation rules
10are completely customizable - in fact, LLVMC knows nothing about the
11specifics of transformation (even the command-line options are mostly
12not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov83237482008-10-15 09:29:13 +000013abstract graph. The structure of this graph is completely determined
14by plugins, which can be either statically or dynamically linked. This
15makes it possible to easily adapt LLVMC for other purposes - for
16example, as a build tool for game resources.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000017
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000018Because LLVMC employs TableGen [1]_ as its configuration language, you
19need to be familiar with it to customize LLVMC.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000020
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000021
22.. contents::
23
24
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000025Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000026====================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000027
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000028LLVMC tries hard to be as compatible with ``gcc`` as possible,
29although there are some small differences. Most of the time, however,
30you shouldn't be able to notice them::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000032 $ # This works as expected:
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000033 $ llvmc2 -O3 -Wall hello.cpp
34 $ ./a.out
35 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000036
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000037One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000038between different compilers for different languages (think ``g++`` and
39``gcc``) - the right toolchain is chosen automatically based on input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000040language names (which are, in turn, determined from file
41extensions). If you want to force files ending with ".c" to compile as
42C++, use the ``-x`` option, just like you would do it with ``gcc``::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000043
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000044 $ # hello.c is really a C++ file
45 $ llvmc2 -x c++ hello.c
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000046 $ ./a.out
47 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000048
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000049On the other hand, when using LLVMC as a linker to combine several C++
50object files you should provide the ``--linker`` option since it's
51impossible for LLVMC to choose the right linker in that case::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000052
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000053 $ llvmc2 -c hello.cpp
54 $ llvmc2 hello.o
55 [A lot of link-time errors skipped]
56 $ llvmc2 --linker=c++ hello.o
57 $ ./a.out
58 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000059
Mikhail Glushenkov83237482008-10-15 09:29:13 +000060
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000061Predefined options
62==================
63
64LLVMC has some built-in options that can't be overridden in the
65configuration files:
66
67* ``-o FILE`` - Output file name.
68
69* ``-x LANGUAGE`` - Specify the language of the following input files
70 until the next -x option.
71
Mikhail Glushenkov83237482008-10-15 09:29:13 +000072* ``-load PLUGIN_NAME`` - Load the specified plugin DLL. Example:
73 ``-load $LLVM_DIR/Release/lib/LLVMCSimple.so``.
74
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000075* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
76
77* ``--view-graph`` - Show a graphical representation of the compilation
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +000078 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000079 installed. Hidden option, useful for debugging.
80
81* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
82 current directory with the compilation graph description in the
83 Graphviz format. Hidden option, useful for debugging.
84
Mikhail Glushenkov73296102008-05-30 06:29:17 +000085* ``--save-temps`` - Write temporary files to the current directory
86 and do not delete them on exit. Hidden option, useful for debugging.
87
88* ``--help``, ``--help-hidden``, ``--version`` - These options have
89 their standard meaning.
90
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000091
Mikhail Glushenkov83237482008-10-15 09:29:13 +000092Compiling LLVMC plugins
93=======================
94
95It's easiest to start working on your own LLVMC plugin by copying the
96skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
97
98 $ cd $LLVMC_DIR/plugins
99 $ cp -r Simple MyPlugin
100 $ cd MyPlugin
101 $ ls
102 Makefile PluginMain.cpp Simple.td
103
104As you can see, our basic plugin consists of only two files (not
105counting the build script). ``Simple.td`` contains TableGen
106description of the compilation graph; its format is documented in the
107following sections. ``PluginMain.cpp`` is just a helper file used to
108compile the auto-generated C++ code produced from TableGen source. It
109can also contain hook definitions (see `below`__).
110
111__ hooks_
112
113The first thing that you should do is to change the ``LLVMC_PLUGIN``
114variable in the ``Makefile`` to avoid conflicts (since this variable
115is used to name the resulting library)::
116
117 LLVMC_PLUGIN=MyPlugin
118
119It is also a good idea to rename ``Simple.td`` to something less
120generic::
121
122 $ mv Simple.td MyPlugin.td
123
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000124Note that the plugin source directory must be placed under
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000125``$LLVMC_DIR/plugins`` to make use of the existing build
126infrastructure. To build a version of the LLVMC executable called
127``mydriver`` with your plugin compiled in, use the following command::
128
129 $ cd $LLVMC_DIR
130 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
131
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000132To build your plugin as a dynamic library, just ``cd`` to its source
133directory and run ``make``. The resulting file will be called
134``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
135``LLVMCMyPlugin.so``). This library can be then loaded in with the
136``-load`` option. Example::
137
138 $ cd $LLVMC_DIR/plugins/Simple
139 $ make
140 $ llvmc2 -load $LLVM_DIR/Release/lib/LLVMCSimple.so
141
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000142Sometimes, you will want a 'bare-bones' version of LLVMC that has no
143built-in plugins. It can be compiled with the following command::
144
145 $ cd $LLVMC_DIR
146 $ make BUILTIN_PLUGINS=""
147
148How plugins are loaded
149======================
150
151It is possible for LLVMC plugins to depend on each other. For example,
152one can create edges between nodes defined in some other plugin. To
153make this work, however, that plugin should be loaded first. To
154achieve this, the concept of plugin priority was introduced. By
155default, every plugin has priority zero; to specify the priority
156explicitly, put the following line in your ``.td`` file::
157
158 def Priority : PluginPriority<$PRIORITY_VALUE>;
159 # Where PRIORITY_VALUE is some integer > 0
160
161Plugins are loaded in order of their (increasing) priority, starting
162with 0. Therefore, the plugin with the highest priority value will be
163loaded last.
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000164
165
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000166Customizing LLVMC: the compilation graph
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000167========================================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000168
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000169Each TableGen configuration file should include the common
170definitions::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000171
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000172 include "llvm/CompilerDriver/Common.td"
173 // And optionally:
174 // include "llvm/CompilerDriver/Tools.td"
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000175 // which contains some useful tool definitions.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000176
177Internally, LLVMC stores information about possible source
178transformations in form of a graph. Nodes in this graph represent
179tools, and edges between two nodes represent a transformation path. A
180special "root" node is used to mark entry points for the
181transformations. LLVMC also assigns a weight to each edge (more on
182this later) to choose between several alternative edges.
183
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000184The definition of the compilation graph (see file
185``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000186
187 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000188 Edge<"root", "llvm_gcc_c">,
189 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000190 ...
191
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000192 Edge<"llvm_gcc_c", "llc">,
193 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000194 ...
195
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000196 OptionalEdge<"llvm_gcc_c", "opt", [(switch_on "opt")]>,
197 OptionalEdge<"llvm_gcc_cpp", "opt", [(switch_on "opt")]>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000198 ...
199
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000200 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000201 (case (input_languages_contain "c++"), (inc_weight),
202 (or (parameter_equals "linker", "g++"),
203 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000204 ...
205
206 ]>;
207
208As you can see, the edges can be either default or optional, where
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000209optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000210used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000211to tools via their names (as strings). This makes it possible to add
212edges to an existing compilation graph in plugins without having to
213know about all tool definitions used in the graph.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000214
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000215The default edges are assigned a weight of 1, and optional edges get a
216weight of 0 + 2*N where N is the number of tests that evaluated to
217true in the ``case`` expression. It is also possible to provide an
218integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
219the weight is increased (or decreased) by the provided value instead
220of the default 2.
221
222When passing an input file through the graph, LLVMC picks the edge
223with the maximum weight. To avoid ambiguity, there should be only one
224default edge between two nodes (with the exception of the root node,
225which gets a special treatment - there you are allowed to specify one
226default edge *per language*).
227
228To get a visual representation of the compilation graph (useful for
229debugging), run ``llvmc2 --view-graph``. You will need ``dot`` and
230``gsview`` installed for this to work properly.
231
232
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000233Writing a tool description
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000234==========================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000235
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000236As was said earlier, nodes in the compilation graph represent tools,
237which are described separately. A tool definition looks like this
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000238(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000239
240 def llvm_gcc_cpp : Tool<[
241 (in_language "c++"),
242 (out_language "llvm-assembler"),
243 (output_suffix "bc"),
244 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
245 (sink)
246 ]>;
247
248This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
249``llvm-g++``. As you can see, a tool definition is just a list of
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000250properties; most of them should be self-explanatory. The ``sink``
251property means that this tool should be passed all command-line
252options that lack explicit descriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000253
254The complete list of the currently implemented tool properties follows:
255
256* Possible tool properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257
Mikhail Glushenkov5ccf28f2008-09-22 20:45:17 +0000258 - ``in_language`` - input language name. Can be either a string or a
259 list, in case the tool supports multiple input languages.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000260
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000261 - ``out_language`` - output language name.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000262
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000263 - ``output_suffix`` - output file suffix.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000264
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000265 - ``cmd_line`` - the actual command used to run the tool. You can
266 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
267 with ``>``, hook invocations (``$CALL``), environment variables
268 (via ``$ENV``) and the ``case`` construct (more on this below).
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000269
270 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000271 list of input files and joins them together. Used for linkers.
272
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000273 - ``sink`` - all command-line options that are not handled by other
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000274 tools are passed to this tool.
275
276The next tool definition is slightly more complex::
277
278 def llvm_gcc_linker : Tool<[
279 (in_language "object-code"),
280 (out_language "executable"),
281 (output_suffix "out"),
282 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
283 (join),
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000284 (prefix_list_option "L", (forward),
285 (help "add a directory to link path")),
286 (prefix_list_option "l", (forward),
287 (help "search a library when linking")),
288 (prefix_list_option "Wl", (unpack_values),
289 (help "pass options to linker"))
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000290 ]>;
291
292This tool has a "join" property, which means that it behaves like a
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000293linker. This tool also defines several command-line options: ``-l``,
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000294``-L`` and ``-Wl`` which have their usual meaning. An option has two
295attributes: a name and a (possibly empty) list of properties. All
296currently implemented option types and properties are described below:
297
298* Possible option types:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000299
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000300 - ``switch_option`` - a simple boolean switch, for example ``-time``.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000301
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000302 - ``parameter_option`` - option that takes an argument, for example
303 ``-std=c99``;
304
305 - ``parameter_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000306 occurence of the option is allowed.
307
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000308 - ``prefix_option`` - same as the parameter_option, but the option name
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000309 and parameter value are not separated.
310
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000311 - ``prefix_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000312 occurence of the option is allowed; example: ``-lm -lpthread``.
313
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000314 - ``alias_option`` - a special option type for creating
315 aliases. Unlike other option types, aliases are not allowed to
316 have any properties besides the aliased option name. Usage
317 example: ``(alias_option "preprocess", "E")``
318
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000319
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000320* Possible option properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000321
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000322 - ``append_cmd`` - append a string to the tool invocation command.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000323
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000324 - ``forward`` - forward this option unchanged.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000325
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000326 - ``forward_as`` - Change the name of this option, but forward the
327 argument unchanged. Example: ``(forward_as "--disable-optimize")``.
328
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000329 - ``output_suffix`` - modify the output suffix of this
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000330 tool. Example: ``(switch "E", (output_suffix "i")``.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000331
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000332 - ``stop_compilation`` - stop compilation after this phase.
333
334 - ``unpack_values`` - used for for splitting and forwarding
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000335 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
336 converted to ``-foo=bar -baz`` and appended to the tool invocation
337 command.
338
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000339 - ``help`` - help string associated with this option. Used for
340 ``--help`` output.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000341
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000342 - ``required`` - this option is obligatory.
343
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000344
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000345Option list - specifying all options in a single place
346======================================================
347
348It can be handy to have all information about options gathered in a
349single place to provide an overview. This can be achieved by using a
350so-called ``OptionList``::
351
352 def Options : OptionList<[
353 (switch_option "E", (help "Help string")),
354 (alias_option "quiet", "q")
355 ...
356 ]>;
357
358``OptionList`` is also a good place to specify option aliases.
359
360Tool-specific option properties like ``append_cmd`` have (obviously)
361no meaning in the context of ``OptionList``, so the only properties
362allowed there are ``help`` and ``required``.
363
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +0000364Option lists are used at file scope. See the file
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000365``plugins/Clang/Clang.td`` for an example of ``OptionList`` usage.
366
367.. _hooks:
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000368
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000369Using hooks and environment variables in the ``cmd_line`` property
370==================================================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000371
372Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
373this is not sufficient: for example, we may want to specify tool names
374in the configuration file. This can be achieved via the mechanism of
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000375hooks - to write your own hooks, just add their definitions to the
376``PluginMain.cpp`` or drop a ``.cpp`` file into the
377``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000378namespace and have the signature ``std::string hooks::MyHookName
379(void)``. They can be used from the ``cmd_line`` tool property::
380
381 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
382
383It is also possible to use environment variables in the same manner::
384
385 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
386
387To change the command line string based on user-provided options use
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000388the ``case`` expression (documented below)::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000389
390 (cmd_line
391 (case
392 (switch_on "E"),
393 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
394 (default),
395 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
396
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000397Conditional evaluation: the ``case`` expression
398===============================================
399
400The 'case' construct can be used to calculate weights of the optional
401edges and to choose between several alternative command line strings
402in the ``cmd_line`` tool property. It is designed after the
403similarly-named construct in functional languages and takes the form
404``(case (test_1), statement_1, (test_2), statement_2, ... (test_N),
405statement_N)``. The statements are evaluated only if the corresponding
406tests evaluate to true.
407
408Examples::
409
410 // Increases edge weight by 5 if "-A" is provided on the
411 // command-line, and by 5 more if "-B" is also provided.
412 (case
413 (switch_on "A"), (inc_weight 5),
414 (switch_on "B"), (inc_weight 5))
415
416 // Evaluates to "cmdline1" if option "-A" is provided on the
417 // command line, otherwise to "cmdline2"
418 (case
419 (switch_on "A"), "cmdline1",
420 (switch_on "B"), "cmdline2",
421 (default), "cmdline3")
422
423Note the slight difference in 'case' expression handling in contexts
424of edge weights and command line specification - in the second example
425the value of the ``"B"`` switch is never checked when switch ``"A"`` is
426enabled, and the whole expression always evaluates to ``"cmdline1"`` in
427that case.
428
429Case expressions can also be nested, i.e. the following is legal::
430
431 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
432 (default), ...)
433
434You should, however, try to avoid doing that because it hurts
435readability. It is usually better to split tool descriptions and/or
436use TableGen inheritance instead.
437
438* Possible tests are:
439
440 - ``switch_on`` - Returns true if a given command-line option is
441 provided by the user. Example: ``(switch_on "opt")``. Note that
442 you have to define all possible command-line options separately in
443 the tool descriptions. See the next section for the discussion of
444 different kinds of command-line options.
445
446 - ``parameter_equals`` - Returns true if a command-line parameter equals
447 a given value. Example: ``(parameter_equals "W", "all")``.
448
449 - ``element_in_list`` - Returns true if a command-line parameter list
450 includes a given value. Example: ``(parameter_in_list "l", "pthread")``.
451
452 - ``input_languages_contain`` - Returns true if a given language
453 belongs to the current input language set. Example:
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000454 ``(input_languages_contain "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000455
456 - ``in_language`` - Evaluates to true if the language of the input
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000457 file equals to the argument. At the moment works only with
458 ``cmd_line`` property on non-join nodes. Example: ``(in_language
459 "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000460
461 - ``not_empty`` - Returns true if a given option (which should be
462 either a parameter or a parameter list) is set by the
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000463 user. Example: ``(not_empty "o")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000464
465 - ``default`` - Always evaluates to true. Should always be the last
466 test in the ``case`` expression.
467
468 - ``and`` - A standard logical combinator that returns true iff all
469 of its arguments return true. Used like this: ``(and (test1),
470 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
471 but not encouraged.
472
473 - ``or`` - Another logical combinator that returns true only if any
474 one of its arguments returns true. Example: ``(or (test1),
475 (test2), ... (testN))``.
476
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000477
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000478Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000479============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000480
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000481One last thing that you will need to modify when adding support for a
482new language to LLVMC is the language map, which defines mappings from
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000483file extensions to language names. It is used to choose the proper
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000484toolchain(s) for a given input file set. Language map definition is
485located in the file ``Tools.td`` and looks like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000486
487 def LanguageMap : LanguageMap<
488 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
489 LangToSuffixes<"c", ["c"]>,
490 ...
491 ]>;
492
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000493Debugging
494=========
495
496When writing LLVMC plugins, it can be useful to get a visual view of
497the resulting compilation graph. This can be achieved via the command
498line option ``--view-graph``. This command assumes that Graphviz [2]_ and
499Ghostview [3]_ are installed. There is also a ``--dump-graph`` option that
500creates a Graphviz source file(``compilation-graph.dot``) in the
501current directory.
502
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000503
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000504References
505==========
506
507.. [1] TableGen Fundamentals
508 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000509
510.. [2] Graphviz
511 http://www.graphviz.org/
512
513.. [3] Ghostview
514 http://pages.cs.wisc.edu/~ghost/