blob: 9aca609ca1de6e335f7a53ed2f90a15d8000cec3 [file] [log] [blame]
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +00001===================================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkovb7677be2008-12-13 17:51:47 +00004..
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 Glushenkov6d1e9282008-12-13 02:28:58 +00008
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
17Introduction
18============
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000019
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000020LLVMC is a generic compiler driver, designed to be customizable and
21extensible. It plays the same role for LLVM as the ``gcc`` program
22does for GCC - LLVMC's job is essentially to transform a set of input
23files into a set of targets depending on configuration rules and user
24options. What makes LLVMC different is that these transformation rules
25are completely customizable - in fact, LLVMC knows nothing about the
26specifics of transformation (even the command-line options are mostly
27not hard-coded) and regards the transformation structure as an
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000028abstract graph. The structure of this graph is completely determined
29by plugins, which can be either statically or dynamically linked. This
30makes it possible to easily adapt LLVMC for other purposes - for
31example, as a build tool for game resources.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000032
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +000033Because LLVMC employs TableGen_ as its configuration language, you
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000034need to be familiar with it to customize LLVMC.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000035
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +000036.. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000037
38
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000039Compiling with LLVMC
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000040====================
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000041
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000042LLVMC tries hard to be as compatible with ``gcc`` as possible,
43although there are some small differences. Most of the time, however,
44you shouldn't be able to notice them::
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000045
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000046 $ # This works as expected:
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000047 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000048 $ ./a.out
49 hello
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000050
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000051One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000052between different compilers for different languages (think ``g++`` and
53``gcc``) - the right toolchain is chosen automatically based on input
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000054language names (which are, in turn, determined from file
55extensions). If you want to force files ending with ".c" to compile as
56C++, use the ``-x`` option, just like you would do it with ``gcc``::
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000057
Mikhail Glushenkov99f10642008-11-25 21:34:29 +000058 $ # hello.c is really a C++ file
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000059 $ llvmc -x c++ hello.c
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000060 $ ./a.out
61 hello
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000062
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000063On the other hand, when using LLVMC as a linker to combine several C++
64object files you should provide the ``--linker`` option since it's
65impossible for LLVMC to choose the right linker in that case::
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000066
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000067 $ llvmc -c hello.cpp
68 $ llvmc hello.o
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000069 [A lot of link-time errors skipped]
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000070 $ llvmc --linker=c++ hello.o
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000071 $ ./a.out
72 hello
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000073
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +000074By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is
75also possible to choose the work-in-progress ``clang`` compiler with
76the ``-clang`` option.
77
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000078
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000079Predefined options
80==================
81
82LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +000083configuration libraries:
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000084
85* ``-o FILE`` - Output file name.
86
87* ``-x LANGUAGE`` - Specify the language of the following input files
88 until the next -x option.
89
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000090* ``-load PLUGIN_NAME`` - Load the specified plugin DLL. Example:
91 ``-load $LLVM_DIR/Release/lib/LLVMCSimple.so``.
92
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000093* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
94
Mikhail Glushenkovf300a822009-01-09 16:16:27 +000095* ``--check-graph`` - Check the compilation for common errors like
96 mismatched output/input language names, multiple default edges and
97 cycles. Hidden option, useful for debugging.
98
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000099* ``--view-graph`` - Show a graphical representation of the compilation
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000100 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000101 installed. Hidden option, useful for debugging.
102
103* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
104 current directory with the compilation graph description in the
105 Graphviz format. Hidden option, useful for debugging.
106
Mikhail Glushenkova5bdf6e2008-05-30 06:29:17 +0000107* ``--save-temps`` - Write temporary files to the current directory
108 and do not delete them on exit. Hidden option, useful for debugging.
109
110* ``--help``, ``--help-hidden``, ``--version`` - These options have
111 their standard meaning.
112
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000113
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000114Compiling LLVMC plugins
115=======================
116
117It's easiest to start working on your own LLVMC plugin by copying the
118skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
119
120 $ cd $LLVMC_DIR/plugins
121 $ cp -r Simple MyPlugin
122 $ cd MyPlugin
123 $ ls
124 Makefile PluginMain.cpp Simple.td
125
126As you can see, our basic plugin consists of only two files (not
127counting the build script). ``Simple.td`` contains TableGen
128description of the compilation graph; its format is documented in the
129following sections. ``PluginMain.cpp`` is just a helper file used to
130compile the auto-generated C++ code produced from TableGen source. It
131can also contain hook definitions (see `below`__).
132
133__ hooks_
134
135The first thing that you should do is to change the ``LLVMC_PLUGIN``
136variable in the ``Makefile`` to avoid conflicts (since this variable
137is used to name the resulting library)::
138
139 LLVMC_PLUGIN=MyPlugin
140
141It is also a good idea to rename ``Simple.td`` to something less
142generic::
143
144 $ mv Simple.td MyPlugin.td
145
Mikhail Glushenkovd333fce2008-11-25 21:34:01 +0000146Note that the plugin source directory must be placed under
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000147``$LLVMC_DIR/plugins`` to make use of the existing build
148infrastructure. To build a version of the LLVMC executable called
149``mydriver`` with your plugin compiled in, use the following command::
150
151 $ cd $LLVMC_DIR
152 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
153
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000154To build your plugin as a dynamic library, just ``cd`` to its source
155directory and run ``make``. The resulting file will be called
156``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
157``LLVMCMyPlugin.so``). This library can be then loaded in with the
158``-load`` option. Example::
159
160 $ cd $LLVMC_DIR/plugins/Simple
161 $ make
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +0000162 $ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000163
Mikhail Glushenkovd333fce2008-11-25 21:34:01 +0000164Sometimes, you will want a 'bare-bones' version of LLVMC that has no
165built-in plugins. It can be compiled with the following command::
166
167 $ cd $LLVMC_DIR
168 $ make BUILTIN_PLUGINS=""
169
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000170
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000171Customizing LLVMC: the compilation graph
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000172========================================
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000173
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000174Each TableGen configuration file should include the common
175definitions::
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000176
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000177 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000178
179Internally, LLVMC stores information about possible source
180transformations in form of a graph. Nodes in this graph represent
181tools, and edges between two nodes represent a transformation path. A
182special "root" node is used to mark entry points for the
183transformations. LLVMC also assigns a weight to each edge (more on
184this later) to choose between several alternative edges.
185
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000186The definition of the compilation graph (see file
187``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000188
189 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000190 Edge<"root", "llvm_gcc_c">,
191 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000192 ...
193
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000194 Edge<"llvm_gcc_c", "llc">,
195 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000196 ...
197
Mikhail Glushenkov817b2f42008-11-25 21:34:53 +0000198 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
199 (inc_weight))>,
200 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
201 (inc_weight))>,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000202 ...
203
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000204 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000205 (case (input_languages_contain "c++"), (inc_weight),
206 (or (parameter_equals "linker", "g++"),
207 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000208 ...
209
210 ]>;
211
212As you can see, the edges can be either default or optional, where
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000213optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000214used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovd333fce2008-11-25 21:34:01 +0000215to tools via their names (as strings). This makes it possible to add
216edges to an existing compilation graph in plugins without having to
217know about all tool definitions used in the graph.
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000218
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000219The default edges are assigned a weight of 1, and optional edges get a
220weight of 0 + 2*N where N is the number of tests that evaluated to
221true in the ``case`` expression. It is also possible to provide an
222integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
223the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +0000224of the default 2. It is also possible to change the default weight of
225an optional edge by using the ``default`` clause of the ``case``
226construct.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000227
228When passing an input file through the graph, LLVMC picks the edge
229with the maximum weight. To avoid ambiguity, there should be only one
230default edge between two nodes (with the exception of the root node,
231which gets a special treatment - there you are allowed to specify one
232default edge *per language*).
233
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +0000234When multiple plugins are loaded, their compilation graphs are merged
Mikhail Glushenkov642e9a12008-11-28 00:12:09 +0000235together. Since multiple edges that have the same end nodes are not
236allowed (i.e. the graph is not a multigraph), an edge defined in
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +0000237several plugins will be replaced by the definition from the plugin
238that was loaded last. Plugin load order can be controlled by using the
239plugin priority feature described above.
240
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000241To get a visual representation of the compilation graph (useful for
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +0000242debugging), run ``llvmc --view-graph``. You will need ``dot`` and
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000243``gsview`` installed for this to work properly.
244
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000245Describing options
246==================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000247
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000248Command-line options that the plugin supports are defined by using an
249``OptionList``::
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000250
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000251 def Options : OptionList<[
252 (switch_option "E", (help "Help string")),
253 (alias_option "quiet", "q")
254 ...
255 ]>;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000256
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000257As you can see, the option list is just a list of DAGs, where each DAG
258is an option description consisting of the option name and some
259properties. A plugin can define more than one option list (they are
260all merged together in the end), which can be handy if one wants to
261separate option groups syntactically.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000262
263* Possible option types:
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000264
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000265 - ``switch_option`` - a simple boolean switch without arguments, for example
266 ``-O2`` or ``-time``. At most one occurrence is allowed.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000267
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000268 - ``parameter_option`` - option that takes one argument, for example
269 ``-std=c99``. It is also allowed to use spaces instead of the equality
270 sign: ``-std c99``. At most one occurrence is allowed.
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000271
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000272 - ``parameter_list_option`` - same as the above, but more than one option
273 occurence is allowed.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000274
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000275 - ``prefix_option`` - same as the parameter_option, but the option name and
276 argument do not have to be separated. Example: ``-ofile``. This can be also
277 specified as ``-o file``; however, ``-o=file`` will be parsed incorrectly
278 (``=file`` will be interpreted as option value). At most one occurrence is
279 allowed.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000280
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000281 - ``prefix_list_option`` - same as the above, but more than one occurence of
282 the option is allowed; example: ``-lm -lpthread``.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000283
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000284 - ``alias_option`` - a special option type for creating aliases. Unlike other
285 option types, aliases are not allowed to have any properties besides the
286 aliased option name. Usage example: ``(alias_option "preprocess", "E")``
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000287
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000288
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000289* Possible option properties:
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000290
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000291 - ``help`` - help string associated with this option. Used for ``--help``
292 output.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000293
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000294 - ``required`` - this option must be specified exactly once (or, in case of
295 the list options without the ``multi_val`` property, at least
296 once). Incompatible with ``zero_or_one`` and ``one_or_more``.
297
298 - ``one_or_more`` - the option must be specified at least one time. Useful
299 only for list options in conjunction with ``multi_val``; for ordinary lists
300 it is synonymous with ``required``. Incompatible with ``required`` and
301 ``zero_or_one``.
302
303 - ``zero_or_one`` - the option can be specified zero or one times. Useful
304 only for list options in conjunction with ``multi_val``. Incompatible with
305 ``required`` and ``one_or_more``.
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000306
Mikhail Glushenkov336ad702009-01-15 02:42:40 +0000307 - ``hidden`` - the description of this option will not appear in
308 the ``--help`` output (but will appear in the ``--help-hidden``
309 output).
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000310
Mikhail Glushenkov336ad702009-01-15 02:42:40 +0000311 - ``really_hidden`` - the option will not be mentioned in any help
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000312 output.
313
Mikhail Glushenkov8139ba32009-01-28 03:47:20 +0000314 - ``multi_val n`` - this option takes *n* arguments (can be useful in some
315 special cases). Usage example: ``(parameter_list_option "foo", (multi_val
316 3))``. Only list options can have this attribute; you can, however, use
317 the ``one_or_more`` and ``zero_or_one`` properties.
318
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000319 - ``extern`` - this option is defined in some other plugin, see below.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000320
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000321External options
322----------------
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000323
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000324Sometimes, when linking several plugins together, one plugin needs to
325access options defined in some other plugin. Because of the way
Mikhail Glushenkov336ad702009-01-15 02:42:40 +0000326options are implemented, such options must be marked as
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000327``extern``. This is what the ``extern`` option property is
328for. Example::
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000329
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000330 ...
331 (switch_option "E", (extern))
332 ...
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000333
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000334See also the section on plugin `priorities`__.
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000335
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000336__ priorities_
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000337
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000338.. _case:
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000339
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000340Conditional evaluation
341======================
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000342
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000343The 'case' construct is the main means by which programmability is
344achieved in LLVMC. It can be used to calculate edge weights, program
345actions and modify the shell commands to be executed. The 'case'
346expression is designed after the similarly-named construct in
347functional languages and takes the form ``(case (test_1), statement_1,
348(test_2), statement_2, ... (test_N), statement_N)``. The statements
349are evaluated only if the corresponding tests evaluate to true.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000350
351Examples::
352
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000353 // Edge weight calculation
354
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000355 // Increases edge weight by 5 if "-A" is provided on the
356 // command-line, and by 5 more if "-B" is also provided.
357 (case
358 (switch_on "A"), (inc_weight 5),
359 (switch_on "B"), (inc_weight 5))
360
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000361
362 // Tool command line specification
363
364 // Evaluates to "cmdline1" if the option "-A" is provided on the
365 // command line; to "cmdline2" if "-B" is provided;
366 // otherwise to "cmdline3".
367
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000368 (case
369 (switch_on "A"), "cmdline1",
370 (switch_on "B"), "cmdline2",
371 (default), "cmdline3")
372
373Note the slight difference in 'case' expression handling in contexts
374of edge weights and command line specification - in the second example
375the value of the ``"B"`` switch is never checked when switch ``"A"`` is
376enabled, and the whole expression always evaluates to ``"cmdline1"`` in
377that case.
378
379Case expressions can also be nested, i.e. the following is legal::
380
381 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
382 (default), ...)
383
384You should, however, try to avoid doing that because it hurts
385readability. It is usually better to split tool descriptions and/or
386use TableGen inheritance instead.
387
388* Possible tests are:
389
Mikhail Glushenkov817b2f42008-11-25 21:34:53 +0000390 - ``switch_on`` - Returns true if a given command-line switch is
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000391 provided by the user. Example: ``(switch_on "opt")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000392
393 - ``parameter_equals`` - Returns true if a command-line parameter equals
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000394 a given value.
395 Example: ``(parameter_equals "W", "all")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000396
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000397 - ``element_in_list`` - Returns true if a command-line parameter
398 list contains a given value.
399 Example: ``(parameter_in_list "l", "pthread")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000400
401 - ``input_languages_contain`` - Returns true if a given language
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000402 belongs to the current input language set.
403 Example: ``(input_languages_contain "c++")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000404
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000405 - ``in_language`` - Evaluates to true if the input file language
406 equals to the argument. At the moment works only with ``cmd_line``
407 and ``actions`` (on non-join nodes).
408 Example: ``(in_language "c++")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000409
410 - ``not_empty`` - Returns true if a given option (which should be
411 either a parameter or a parameter list) is set by the
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000412 user.
413 Example: ``(not_empty "o")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000414
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000415 - ``empty`` - The opposite of ``not_empty``. Equivalent to ``(not (not_empty
416 X))``. Provided for convenience.
417
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000418 - ``default`` - Always evaluates to true. Should always be the last
419 test in the ``case`` expression.
420
421 - ``and`` - A standard logical combinator that returns true iff all
422 of its arguments return true. Used like this: ``(and (test1),
423 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
424 but not encouraged.
425
426 - ``or`` - Another logical combinator that returns true only if any
427 one of its arguments returns true. Example: ``(or (test1),
428 (test2), ... (testN))``.
429
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000430
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000431Writing a tool description
432==========================
433
434As was said earlier, nodes in the compilation graph represent tools,
435which are described separately. A tool definition looks like this
436(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
437
438 def llvm_gcc_cpp : Tool<[
439 (in_language "c++"),
440 (out_language "llvm-assembler"),
441 (output_suffix "bc"),
442 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
443 (sink)
444 ]>;
445
446This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
447``llvm-g++``. As you can see, a tool definition is just a list of
448properties; most of them should be self-explanatory. The ``sink``
449property means that this tool should be passed all command-line
450options that aren't mentioned in the option list.
451
452The complete list of all currently implemented tool properties follows.
453
454* Possible tool properties:
455
456 - ``in_language`` - input language name. Can be either a string or a
457 list, in case the tool supports multiple input languages.
458
459 - ``out_language`` - output language name. Tools are not allowed to
460 have multiple output languages.
461
462 - ``output_suffix`` - output file suffix. Can also be changed
463 dynamically, see documentation on actions.
464
465 - ``cmd_line`` - the actual command used to run the tool. You can
466 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
467 with ``>``, hook invocations (``$CALL``), environment variables
468 (via ``$ENV``) and the ``case`` construct.
469
470 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
471 list of input files and joins them together. Used for linkers.
472
473 - ``sink`` - all command-line options that are not handled by other
474 tools are passed to this tool.
475
476 - ``actions`` - A single big ``case`` expression that specifies how
477 this tool reacts on command-line options (described in more detail
478 below).
479
480Actions
481-------
482
483A tool often needs to react to command-line options, and this is
484precisely what the ``actions`` property is for. The next example
485illustrates this feature::
486
487 def llvm_gcc_linker : Tool<[
488 (in_language "object-code"),
489 (out_language "executable"),
490 (output_suffix "out"),
491 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
492 (join),
493 (actions (case (not_empty "L"), (forward "L"),
494 (not_empty "l"), (forward "l"),
495 (not_empty "dummy"),
496 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
497 ]>;
498
499The ``actions`` tool property is implemented on top of the omnipresent
500``case`` expression. It associates one or more different *actions*
501with given conditions - in the example, the actions are ``forward``,
502which forwards a given option unchanged, and ``append_cmd``, which
503appends a given string to the tool execution command. Multiple actions
504can be associated with a single condition by using a list of actions
505(used in the example to append some dummy options). The same ``case``
506construct can also be used in the ``cmd_line`` property to modify the
507tool command line.
508
509The "join" property used in the example means that this tool behaves
510like a linker.
511
512The list of all possible actions follows.
513
514* Possible actions:
515
516 - ``append_cmd`` - append a string to the tool invocation
517 command.
Mikhail Glushenkov43dc4ca2008-12-17 02:47:01 +0000518 Example: ``(case (switch_on "pthread"), (append_cmd
519 "-lpthread"))``
520
521 - ``error` - exit with error.
522 Example: ``(error "Mixing -c and -S is not allowed!")``.
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000523
524 - ``forward`` - forward an option unchanged.
525 Example: ``(forward "Wall")``.
526
527 - ``forward_as`` - Change the name of an option, but forward the
528 argument unchanged.
529 Example: ``(forward_as "O0" "--disable-optimization")``.
530
531 - ``output_suffix`` - modify the output suffix of this
532 tool.
533 Example: ``(output_suffix "i")``.
534
535 - ``stop_compilation`` - stop compilation after this tool processes
536 its input. Used without arguments.
537
538 - ``unpack_values`` - used for for splitting and forwarding
539 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
540 converted to ``-foo=bar -baz`` and appended to the tool invocation
541 command.
542 Example: ``(unpack_values "Wa,")``.
543
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000544Language map
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000545============
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000546
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000547If you are adding support for a new language to LLVMC, you'll need to
548modify the language map, which defines mappings from file extensions
549to language names. It is used to choose the proper toolchain(s) for a
550given input file set. Language map definition looks like this::
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000551
552 def LanguageMap : LanguageMap<
553 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
554 LangToSuffixes<"c", ["c"]>,
555 ...
556 ]>;
557
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000558For example, without those definitions the following command wouldn't work::
559
560 $ llvmc hello.cpp
561 llvmc: Unknown suffix: cpp
562
563The language map entries should be added only for tools that are
564linked with the root node. Since tools are not allowed to have
565multiple output languages, for nodes "inside" the graph the input and
566output languages should match. This is enforced at compile-time.
567
568
569More advanced topics
570====================
571
572.. _hooks:
573
574Hooks and environment variables
575-------------------------------
576
577Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +0000578this is not sufficient: for example, we may want to specify tool paths
579or names in the configuration file. This can be easily achieved via
580the hooks mechanism. To write your own hooks, just add their
581definitions to the ``PluginMain.cpp`` or drop a ``.cpp`` file into the
582your plugin directory. Hooks should live in the ``hooks`` namespace
Mikhail Glushenkoved765fe2009-01-21 13:04:33 +0000583and have the signature ``std::string hooks::MyHookName ([const char*
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +0000584Arg0 [ const char* Arg2 [, ...]]])``. They can be used from the
585``cmd_line`` tool property::
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000586
587 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
588
Mikhail Glushenkovab0f3cb2009-01-21 13:04:00 +0000589To pass arguments to hooks, use the following syntax::
590
591 (cmd_line "$CALL(MyHook, 'Arg1', 'Arg2', 'Arg # 3')/path/to/file -o1 -o2")
592
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000593It is also possible to use environment variables in the same manner::
594
595 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
596
597To change the command line string based on user-provided options use
598the ``case`` expression (documented `above`__)::
599
600 (cmd_line
601 (case
602 (switch_on "E"),
603 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
604 (default),
605 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
606
607__ case_
608
609.. _priorities:
610
611How plugins are loaded
612----------------------
613
614It is possible for LLVMC plugins to depend on each other. For example,
615one can create edges between nodes defined in some other plugin. To
616make this work, however, that plugin should be loaded first. To
617achieve this, the concept of plugin priority was introduced. By
618default, every plugin has priority zero; to specify the priority
619explicitly, put the following line in your plugin's TableGen file::
620
621 def Priority : PluginPriority<$PRIORITY_VALUE>;
622 # Where PRIORITY_VALUE is some integer > 0
623
624Plugins are loaded in order of their (increasing) priority, starting
625with 0. Therefore, the plugin with the highest priority value will be
626loaded last.
627
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000628Debugging
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000629---------
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000630
631When writing LLVMC plugins, it can be useful to get a visual view of
632the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +0000633line option ``--view-graph``. This command assumes that Graphviz_ and
634Ghostview_ are installed. There is also a ``--dump-graph`` option that
635creates a Graphviz source file (``compilation-graph.dot``) in the
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000636current directory.
637
Mikhail Glushenkov336ad702009-01-15 02:42:40 +0000638Another useful ``llvmc`` option is ``--check-graph``. It checks the
639compilation graph for common errors like mismatched output/input
640language names, multiple default edges and cycles. These checks can't
641be performed at compile-time because the plugins can load code
642dynamically. When invoked with ``--check-graph``, ``llvmc`` doesn't
643perform any compilation tasks and returns the number of encountered
644errors as its status code.
Mikhail Glushenkovf300a822009-01-09 16:16:27 +0000645
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +0000646.. _Graphviz: http://www.graphviz.org/
647.. _Ghostview: http://pages.cs.wisc.edu/~ghost/
Mikhail Glushenkovac251f22008-12-11 23:24:40 +0000648
649.. raw:: html
Mikhail Glushenkov6d1e9282008-12-13 02:28:58 +0000650
651 <hr />
652 <address>
653 <a href="http://jigsaw.w3.org/css-validator/check/referer">
654 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
655 alt="Valid CSS" /></a>
656 <a href="http://validator.w3.org/check?uri=referer">
657 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
658 alt="Valid XHTML 1.0 Transitional"/></a>
659
660 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
661 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
662
663 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
664 </address>