blob: 01a8be0d20eff3bbe64e7c68267dd229cd720ed9 [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001===================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkov536637f2008-11-25 21:34:53 +00004:Author: Mikhail Glushenkov <foldr@codedegers.com>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00005
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +00006LLVMC is a generic compiler driver, designed to be customizable and
7extensible. It plays the same role for LLVM as the ``gcc`` program
8does for GCC - LLVMC's job is essentially to transform a set of input
9files into a set of targets depending on configuration rules and user
10options. What makes LLVMC different is that these transformation rules
11are completely customizable - in fact, LLVMC knows nothing about the
12specifics of transformation (even the command-line options are mostly
13not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov83237482008-10-15 09:29:13 +000014abstract graph. The structure of this graph is completely determined
15by plugins, which can be either statically or dynamically linked. This
16makes it possible to easily adapt LLVMC for other purposes - for
17example, as a build tool for game resources.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000019Because LLVMC employs TableGen [1]_ as its configuration language, you
20need to be familiar with it to customize LLVMC.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000022
23.. contents::
24
25
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000026Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000027====================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000029LLVMC tries hard to be as compatible with ``gcc`` as possible,
30although there are some small differences. Most of the time, however,
31you shouldn't be able to notice them::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000033 $ # This works as expected:
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000034 $ llvmc2 -O3 -Wall hello.cpp
35 $ ./a.out
36 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000037
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000038One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000039between different compilers for different languages (think ``g++`` and
40``gcc``) - the right toolchain is chosen automatically based on input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000041language names (which are, in turn, determined from file
42extensions). If you want to force files ending with ".c" to compile as
43C++, use the ``-x`` option, just like you would do it with ``gcc``::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000044
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000045 $ # hello.c is really a C++ file
46 $ llvmc2 -x c++ hello.c
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000047 $ ./a.out
48 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000050On the other hand, when using LLVMC as a linker to combine several C++
51object files you should provide the ``--linker`` option since it's
52impossible for LLVMC to choose the right linker in that case::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000054 $ llvmc2 -c hello.cpp
55 $ llvmc2 hello.o
56 [A lot of link-time errors skipped]
57 $ llvmc2 --linker=c++ hello.o
58 $ ./a.out
59 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000060
Mikhail Glushenkov83237482008-10-15 09:29:13 +000061
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000062Predefined options
63==================
64
65LLVMC has some built-in options that can't be overridden in the
66configuration files:
67
68* ``-o FILE`` - Output file name.
69
70* ``-x LANGUAGE`` - Specify the language of the following input files
71 until the next -x option.
72
Mikhail Glushenkov83237482008-10-15 09:29:13 +000073* ``-load PLUGIN_NAME`` - Load the specified plugin DLL. Example:
74 ``-load $LLVM_DIR/Release/lib/LLVMCSimple.so``.
75
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000076* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
77
78* ``--view-graph`` - Show a graphical representation of the compilation
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +000079 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000080 installed. Hidden option, useful for debugging.
81
82* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
83 current directory with the compilation graph description in the
84 Graphviz format. Hidden option, useful for debugging.
85
Mikhail Glushenkov73296102008-05-30 06:29:17 +000086* ``--save-temps`` - Write temporary files to the current directory
87 and do not delete them on exit. Hidden option, useful for debugging.
88
89* ``--help``, ``--help-hidden``, ``--version`` - These options have
90 their standard meaning.
91
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000092
Mikhail Glushenkov83237482008-10-15 09:29:13 +000093Compiling LLVMC plugins
94=======================
95
96It's easiest to start working on your own LLVMC plugin by copying the
97skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
98
99 $ cd $LLVMC_DIR/plugins
100 $ cp -r Simple MyPlugin
101 $ cd MyPlugin
102 $ ls
103 Makefile PluginMain.cpp Simple.td
104
105As you can see, our basic plugin consists of only two files (not
106counting the build script). ``Simple.td`` contains TableGen
107description of the compilation graph; its format is documented in the
108following sections. ``PluginMain.cpp`` is just a helper file used to
109compile the auto-generated C++ code produced from TableGen source. It
110can also contain hook definitions (see `below`__).
111
112__ hooks_
113
114The first thing that you should do is to change the ``LLVMC_PLUGIN``
115variable in the ``Makefile`` to avoid conflicts (since this variable
116is used to name the resulting library)::
117
118 LLVMC_PLUGIN=MyPlugin
119
120It is also a good idea to rename ``Simple.td`` to something less
121generic::
122
123 $ mv Simple.td MyPlugin.td
124
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000125Note that the plugin source directory must be placed under
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000126``$LLVMC_DIR/plugins`` to make use of the existing build
127infrastructure. To build a version of the LLVMC executable called
128``mydriver`` with your plugin compiled in, use the following command::
129
130 $ cd $LLVMC_DIR
131 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
132
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000133To build your plugin as a dynamic library, just ``cd`` to its source
134directory and run ``make``. The resulting file will be called
135``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
136``LLVMCMyPlugin.so``). This library can be then loaded in with the
137``-load`` option. Example::
138
139 $ cd $LLVMC_DIR/plugins/Simple
140 $ make
141 $ llvmc2 -load $LLVM_DIR/Release/lib/LLVMCSimple.so
142
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000143Sometimes, you will want a 'bare-bones' version of LLVMC that has no
144built-in plugins. It can be compiled with the following command::
145
146 $ cd $LLVMC_DIR
147 $ make BUILTIN_PLUGINS=""
148
149How plugins are loaded
150======================
151
152It is possible for LLVMC plugins to depend on each other. For example,
153one can create edges between nodes defined in some other plugin. To
154make this work, however, that plugin should be loaded first. To
155achieve this, the concept of plugin priority was introduced. By
156default, every plugin has priority zero; to specify the priority
157explicitly, put the following line in your ``.td`` file::
158
159 def Priority : PluginPriority<$PRIORITY_VALUE>;
160 # Where PRIORITY_VALUE is some integer > 0
161
162Plugins are loaded in order of their (increasing) priority, starting
163with 0. Therefore, the plugin with the highest priority value will be
164loaded last.
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000165
166
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000167Customizing LLVMC: the compilation graph
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000168========================================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000169
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000170Each TableGen configuration file should include the common
171definitions::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000172
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000173 include "llvm/CompilerDriver/Common.td"
174 // And optionally:
175 // include "llvm/CompilerDriver/Tools.td"
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000176 // which contains some useful tool definitions.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000177
178Internally, LLVMC stores information about possible source
179transformations in form of a graph. Nodes in this graph represent
180tools, and edges between two nodes represent a transformation path. A
181special "root" node is used to mark entry points for the
182transformations. LLVMC also assigns a weight to each edge (more on
183this later) to choose between several alternative edges.
184
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000185The definition of the compilation graph (see file
186``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000187
188 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000189 Edge<"root", "llvm_gcc_c">,
190 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000191 ...
192
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000193 Edge<"llvm_gcc_c", "llc">,
194 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000195 ...
196
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000197 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
198 (inc_weight))>,
199 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
200 (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000201 ...
202
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000203 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000204 (case (input_languages_contain "c++"), (inc_weight),
205 (or (parameter_equals "linker", "g++"),
206 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000207 ...
208
209 ]>;
210
211As you can see, the edges can be either default or optional, where
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000212optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000213used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000214to tools via their names (as strings). This makes it possible to add
215edges to an existing compilation graph in plugins without having to
216know about all tool definitions used in the graph.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000217
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000218The default edges are assigned a weight of 1, and optional edges get a
219weight of 0 + 2*N where N is the number of tests that evaluated to
220true in the ``case`` expression. It is also possible to provide an
221integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
222the weight is increased (or decreased) by the provided value instead
223of the default 2.
224
225When passing an input file through the graph, LLVMC picks the edge
226with the maximum weight. To avoid ambiguity, there should be only one
227default edge between two nodes (with the exception of the root node,
228which gets a special treatment - there you are allowed to specify one
229default edge *per language*).
230
231To get a visual representation of the compilation graph (useful for
232debugging), run ``llvmc2 --view-graph``. You will need ``dot`` and
233``gsview`` installed for this to work properly.
234
235
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000236Writing a tool description
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000237==========================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000238
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000239As was said earlier, nodes in the compilation graph represent tools,
240which are described separately. A tool definition looks like this
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000241(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000242
243 def llvm_gcc_cpp : Tool<[
244 (in_language "c++"),
245 (out_language "llvm-assembler"),
246 (output_suffix "bc"),
247 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
248 (sink)
249 ]>;
250
251This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
252``llvm-g++``. As you can see, a tool definition is just a list of
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000253properties; most of them should be self-explanatory. The ``sink``
254property means that this tool should be passed all command-line
255options that lack explicit descriptions.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256
257The complete list of the currently implemented tool properties follows:
258
259* Possible tool properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000260
Mikhail Glushenkov5ccf28f2008-09-22 20:45:17 +0000261 - ``in_language`` - input language name. Can be either a string or a
262 list, in case the tool supports multiple input languages.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000263
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000264 - ``out_language`` - output language name.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000265
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000266 - ``output_suffix`` - output file suffix.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000267
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000268 - ``cmd_line`` - the actual command used to run the tool. You can
269 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
270 with ``>``, hook invocations (``$CALL``), environment variables
271 (via ``$ENV``) and the ``case`` construct (more on this below).
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000272
273 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000274 list of input files and joins them together. Used for linkers.
275
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000276 - ``sink`` - all command-line options that are not handled by other
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000277 tools are passed to this tool.
278
279The next tool definition is slightly more complex::
280
281 def llvm_gcc_linker : Tool<[
282 (in_language "object-code"),
283 (out_language "executable"),
284 (output_suffix "out"),
285 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
286 (join),
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000287 (prefix_list_option "L", (forward),
288 (help "add a directory to link path")),
289 (prefix_list_option "l", (forward),
290 (help "search a library when linking")),
291 (prefix_list_option "Wl", (unpack_values),
292 (help "pass options to linker"))
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000293 ]>;
294
295This tool has a "join" property, which means that it behaves like a
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000296linker. This tool also defines several command-line options: ``-l``,
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000297``-L`` and ``-Wl`` which have their usual meaning. An option has two
298attributes: a name and a (possibly empty) list of properties. All
299currently implemented option types and properties are described below:
300
301* Possible option types:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000302
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000303 - ``switch_option`` - a simple boolean switch, for example ``-time``.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000304
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000305 - ``parameter_option`` - option that takes an argument, for example
306 ``-std=c99``;
307
308 - ``parameter_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000309 occurence of the option is allowed.
310
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000311 - ``prefix_option`` - same as the parameter_option, but the option name
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000312 and parameter value are not separated.
313
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000314 - ``prefix_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000315 occurence of the option is allowed; example: ``-lm -lpthread``.
316
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000317 - ``alias_option`` - a special option type for creating
318 aliases. Unlike other option types, aliases are not allowed to
319 have any properties besides the aliased option name. Usage
320 example: ``(alias_option "preprocess", "E")``
321
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000322
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000323* Possible option properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000324
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000325 - ``append_cmd`` - append a string to the tool invocation command.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000326
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000327 - ``forward`` - forward this option unchanged.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000328
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000329 - ``forward_as`` - Change the name of this option, but forward the
330 argument unchanged. Example: ``(forward_as "--disable-optimize")``.
331
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000332 - ``output_suffix`` - modify the output suffix of this
Mikhail Glushenkovfdee9542008-09-22 20:46:19 +0000333 tool. Example: ``(switch "E", (output_suffix "i")``.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000334
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000335 - ``stop_compilation`` - stop compilation after this phase.
336
337 - ``unpack_values`` - used for for splitting and forwarding
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000338 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
339 converted to ``-foo=bar -baz`` and appended to the tool invocation
340 command.
341
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000342 - ``help`` - help string associated with this option. Used for
343 ``--help`` output.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000344
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000345 - ``required`` - this option is obligatory.
346
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000347
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000348Option list - specifying all options in a single place
349======================================================
350
351It can be handy to have all information about options gathered in a
352single place to provide an overview. This can be achieved by using a
353so-called ``OptionList``::
354
355 def Options : OptionList<[
356 (switch_option "E", (help "Help string")),
357 (alias_option "quiet", "q")
358 ...
359 ]>;
360
361``OptionList`` is also a good place to specify option aliases.
362
363Tool-specific option properties like ``append_cmd`` have (obviously)
364no meaning in the context of ``OptionList``, so the only properties
365allowed there are ``help`` and ``required``.
366
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +0000367Option lists are used at file scope. See the file
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000368``plugins/Clang/Clang.td`` for an example of ``OptionList`` usage.
369
370.. _hooks:
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000371
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000372Using hooks and environment variables in the ``cmd_line`` property
373==================================================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000374
375Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
376this is not sufficient: for example, we may want to specify tool names
377in the configuration file. This can be achieved via the mechanism of
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000378hooks - to write your own hooks, just add their definitions to the
379``PluginMain.cpp`` or drop a ``.cpp`` file into the
380``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000381namespace and have the signature ``std::string hooks::MyHookName
382(void)``. They can be used from the ``cmd_line`` tool property::
383
384 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
385
386It is also possible to use environment variables in the same manner::
387
388 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
389
390To change the command line string based on user-provided options use
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000391the ``case`` expression (documented below)::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000392
393 (cmd_line
394 (case
395 (switch_on "E"),
396 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
397 (default),
398 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
399
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000400Conditional evaluation: the ``case`` expression
401===============================================
402
403The 'case' construct can be used to calculate weights of the optional
404edges and to choose between several alternative command line strings
405in the ``cmd_line`` tool property. It is designed after the
406similarly-named construct in functional languages and takes the form
407``(case (test_1), statement_1, (test_2), statement_2, ... (test_N),
408statement_N)``. The statements are evaluated only if the corresponding
409tests evaluate to true.
410
411Examples::
412
413 // Increases edge weight by 5 if "-A" is provided on the
414 // command-line, and by 5 more if "-B" is also provided.
415 (case
416 (switch_on "A"), (inc_weight 5),
417 (switch_on "B"), (inc_weight 5))
418
419 // Evaluates to "cmdline1" if option "-A" is provided on the
420 // command line, otherwise to "cmdline2"
421 (case
422 (switch_on "A"), "cmdline1",
423 (switch_on "B"), "cmdline2",
424 (default), "cmdline3")
425
426Note the slight difference in 'case' expression handling in contexts
427of edge weights and command line specification - in the second example
428the value of the ``"B"`` switch is never checked when switch ``"A"`` is
429enabled, and the whole expression always evaluates to ``"cmdline1"`` in
430that case.
431
432Case expressions can also be nested, i.e. the following is legal::
433
434 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
435 (default), ...)
436
437You should, however, try to avoid doing that because it hurts
438readability. It is usually better to split tool descriptions and/or
439use TableGen inheritance instead.
440
441* Possible tests are:
442
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000443 - ``switch_on`` - Returns true if a given command-line switch is
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000444 provided by the user. Example: ``(switch_on "opt")``. Note that
445 you have to define all possible command-line options separately in
446 the tool descriptions. See the next section for the discussion of
447 different kinds of command-line options.
448
449 - ``parameter_equals`` - Returns true if a command-line parameter equals
450 a given value. Example: ``(parameter_equals "W", "all")``.
451
452 - ``element_in_list`` - Returns true if a command-line parameter list
453 includes a given value. Example: ``(parameter_in_list "l", "pthread")``.
454
455 - ``input_languages_contain`` - Returns true if a given language
456 belongs to the current input language set. Example:
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000457 ``(input_languages_contain "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000458
459 - ``in_language`` - Evaluates to true if the language of the input
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000460 file equals to the argument. At the moment works only with
461 ``cmd_line`` property on non-join nodes. Example: ``(in_language
462 "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000463
464 - ``not_empty`` - Returns true if a given option (which should be
465 either a parameter or a parameter list) is set by the
Mikhail Glushenkov07376512008-09-22 20:48:22 +0000466 user. Example: ``(not_empty "o")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000467
468 - ``default`` - Always evaluates to true. Should always be the last
469 test in the ``case`` expression.
470
471 - ``and`` - A standard logical combinator that returns true iff all
472 of its arguments return true. Used like this: ``(and (test1),
473 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
474 but not encouraged.
475
476 - ``or`` - Another logical combinator that returns true only if any
477 one of its arguments returns true. Example: ``(or (test1),
478 (test2), ... (testN))``.
479
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000480
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000481Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000482============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000483
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000484One last thing that you will need to modify when adding support for a
485new language to LLVMC is the language map, which defines mappings from
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000486file extensions to language names. It is used to choose the proper
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000487toolchain(s) for a given input file set. Language map definition looks
488like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000489
490 def LanguageMap : LanguageMap<
491 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
492 LangToSuffixes<"c", ["c"]>,
493 ...
494 ]>;
495
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000496Debugging
497=========
498
499When writing LLVMC plugins, it can be useful to get a visual view of
500the resulting compilation graph. This can be achieved via the command
501line option ``--view-graph``. This command assumes that Graphviz [2]_ and
502Ghostview [3]_ are installed. There is also a ``--dump-graph`` option that
503creates a Graphviz source file(``compilation-graph.dot``) in the
504current directory.
505
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000506
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000507References
508==========
509
510.. [1] TableGen Fundamentals
511 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000512
513.. [2] Graphviz
514 http://www.graphviz.org/
515
516.. [3] Ghostview
517 http://pages.cs.wisc.edu/~ghost/