blob: 3033cfcafb8cd06f2cf583ab50121f092570f26b [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001===================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkovd5652032008-12-13 02:28:58 +00004
5.. contents::
6
7.. raw:: html
8
9 <div class="doc_author">
10 <p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
11 </div>
12
13Introduction
14============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000015
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000016LLVMC is a generic compiler driver, designed to be customizable and
17extensible. It plays the same role for LLVM as the ``gcc`` program
18does for GCC - LLVMC's job is essentially to transform a set of input
19files into a set of targets depending on configuration rules and user
20options. What makes LLVMC different is that these transformation rules
21are completely customizable - in fact, LLVMC knows nothing about the
22specifics of transformation (even the command-line options are mostly
23not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov83237482008-10-15 09:29:13 +000024abstract graph. The structure of this graph is completely determined
25by plugins, which can be either statically or dynamically linked. This
26makes it possible to easily adapt LLVMC for other purposes - for
27example, as a build tool for game resources.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000029Because LLVMC employs TableGen_ as its configuration language, you
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000030need to be familiar with it to customize LLVMC.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000031
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000032.. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000033
34
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000035Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000036====================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000037
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000038LLVMC tries hard to be as compatible with ``gcc`` as possible,
39although there are some small differences. Most of the time, however,
40you shouldn't be able to notice them::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000041
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000042 $ # This works as expected:
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000043 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000044 $ ./a.out
45 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000046
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000047One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000048between different compilers for different languages (think ``g++`` and
49``gcc``) - the right toolchain is chosen automatically based on input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000050language names (which are, in turn, determined from file
51extensions). If you want to force files ending with ".c" to compile as
52C++, use the ``-x`` option, just like you would do it with ``gcc``::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000054 $ # hello.c is really a C++ file
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000055 $ llvmc -x c++ hello.c
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000056 $ ./a.out
57 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000058
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000059On the other hand, when using LLVMC as a linker to combine several C++
60object files you should provide the ``--linker`` option since it's
61impossible for LLVMC to choose the right linker in that case::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000062
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000063 $ llvmc -c hello.cpp
64 $ llvmc hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000065 [A lot of link-time errors skipped]
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000066 $ llvmc --linker=c++ hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000067 $ ./a.out
68 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000069
Mikhail Glushenkov4410e322008-12-07 16:47:42 +000070By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is
71also possible to choose the work-in-progress ``clang`` compiler with
72the ``-clang`` option.
73
Mikhail Glushenkov83237482008-10-15 09:29:13 +000074
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000075Predefined options
76==================
77
78LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000079configuration libraries:
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000080
81* ``-o FILE`` - Output file name.
82
83* ``-x LANGUAGE`` - Specify the language of the following input files
84 until the next -x option.
85
Mikhail Glushenkov83237482008-10-15 09:29:13 +000086* ``-load PLUGIN_NAME`` - Load the specified plugin DLL. Example:
87 ``-load $LLVM_DIR/Release/lib/LLVMCSimple.so``.
88
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000089* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
90
91* ``--view-graph`` - Show a graphical representation of the compilation
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +000092 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000093 installed. Hidden option, useful for debugging.
94
95* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
96 current directory with the compilation graph description in the
97 Graphviz format. Hidden option, useful for debugging.
98
Mikhail Glushenkov73296102008-05-30 06:29:17 +000099* ``--save-temps`` - Write temporary files to the current directory
100 and do not delete them on exit. Hidden option, useful for debugging.
101
102* ``--help``, ``--help-hidden``, ``--version`` - These options have
103 their standard meaning.
104
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000105
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000106Compiling LLVMC plugins
107=======================
108
109It's easiest to start working on your own LLVMC plugin by copying the
110skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
111
112 $ cd $LLVMC_DIR/plugins
113 $ cp -r Simple MyPlugin
114 $ cd MyPlugin
115 $ ls
116 Makefile PluginMain.cpp Simple.td
117
118As you can see, our basic plugin consists of only two files (not
119counting the build script). ``Simple.td`` contains TableGen
120description of the compilation graph; its format is documented in the
121following sections. ``PluginMain.cpp`` is just a helper file used to
122compile the auto-generated C++ code produced from TableGen source. It
123can also contain hook definitions (see `below`__).
124
125__ hooks_
126
127The first thing that you should do is to change the ``LLVMC_PLUGIN``
128variable in the ``Makefile`` to avoid conflicts (since this variable
129is used to name the resulting library)::
130
131 LLVMC_PLUGIN=MyPlugin
132
133It is also a good idea to rename ``Simple.td`` to something less
134generic::
135
136 $ mv Simple.td MyPlugin.td
137
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000138Note that the plugin source directory must be placed under
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000139``$LLVMC_DIR/plugins`` to make use of the existing build
140infrastructure. To build a version of the LLVMC executable called
141``mydriver`` with your plugin compiled in, use the following command::
142
143 $ cd $LLVMC_DIR
144 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
145
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000146To build your plugin as a dynamic library, just ``cd`` to its source
147directory and run ``make``. The resulting file will be called
148``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
149``LLVMCMyPlugin.so``). This library can be then loaded in with the
150``-load`` option. Example::
151
152 $ cd $LLVMC_DIR/plugins/Simple
153 $ make
Mikhail Glushenkov113ec352008-11-25 21:38:12 +0000154 $ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000155
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000156Sometimes, you will want a 'bare-bones' version of LLVMC that has no
157built-in plugins. It can be compiled with the following command::
158
159 $ cd $LLVMC_DIR
160 $ make BUILTIN_PLUGINS=""
161
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000162
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000163Customizing LLVMC: the compilation graph
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000164========================================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000165
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000166Each TableGen configuration file should include the common
167definitions::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000168
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000169 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000170
171Internally, LLVMC stores information about possible source
172transformations in form of a graph. Nodes in this graph represent
173tools, and edges between two nodes represent a transformation path. A
174special "root" node is used to mark entry points for the
175transformations. LLVMC also assigns a weight to each edge (more on
176this later) to choose between several alternative edges.
177
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000178The definition of the compilation graph (see file
179``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000180
181 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000182 Edge<"root", "llvm_gcc_c">,
183 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000184 ...
185
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000186 Edge<"llvm_gcc_c", "llc">,
187 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000188 ...
189
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000190 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
191 (inc_weight))>,
192 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
193 (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000194 ...
195
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000196 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000197 (case (input_languages_contain "c++"), (inc_weight),
198 (or (parameter_equals "linker", "g++"),
199 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000200 ...
201
202 ]>;
203
204As you can see, the edges can be either default or optional, where
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000205optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000206used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000207to tools via their names (as strings). This makes it possible to add
208edges to an existing compilation graph in plugins without having to
209know about all tool definitions used in the graph.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000210
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000211The default edges are assigned a weight of 1, and optional edges get a
212weight of 0 + 2*N where N is the number of tests that evaluated to
213true in the ``case`` expression. It is also possible to provide an
214integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
215the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000216of the default 2. It is also possible to change the default weight of
217an optional edge by using the ``default`` clause of the ``case``
218construct.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000219
220When passing an input file through the graph, LLVMC picks the edge
221with the maximum weight. To avoid ambiguity, there should be only one
222default edge between two nodes (with the exception of the root node,
223which gets a special treatment - there you are allowed to specify one
224default edge *per language*).
225
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000226When multiple plugins are loaded, their compilation graphs are merged
Mikhail Glushenkov3321b0f2008-11-28 00:12:09 +0000227together. Since multiple edges that have the same end nodes are not
228allowed (i.e. the graph is not a multigraph), an edge defined in
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000229several plugins will be replaced by the definition from the plugin
230that was loaded last. Plugin load order can be controlled by using the
231plugin priority feature described above.
232
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000233To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov113ec352008-11-25 21:38:12 +0000234debugging), run ``llvmc --view-graph``. You will need ``dot`` and
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000235``gsview`` installed for this to work properly.
236
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000237Describing options
238==================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000239
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000240Command-line options that the plugin supports are defined by using an
241``OptionList``::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000242
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000243 def Options : OptionList<[
244 (switch_option "E", (help "Help string")),
245 (alias_option "quiet", "q")
246 ...
247 ]>;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000248
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000249As you can see, the option list is just a list of DAGs, where each DAG
250is an option description consisting of the option name and some
251properties. A plugin can define more than one option list (they are
252all merged together in the end), which can be handy if one wants to
253separate option groups syntactically.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000254
255* Possible option types:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000256
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000257 - ``switch_option`` - a simple boolean switch, for example ``-time``.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000258
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000259 - ``parameter_option`` - option that takes an argument, for example
260 ``-std=c99``;
261
262 - ``parameter_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000263 occurence of the option is allowed.
264
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000265 - ``prefix_option`` - same as the parameter_option, but the option name
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000266 and parameter value are not separated.
267
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000268 - ``prefix_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000269 occurence of the option is allowed; example: ``-lm -lpthread``.
270
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000271 - ``alias_option`` - a special option type for creating
272 aliases. Unlike other option types, aliases are not allowed to
273 have any properties besides the aliased option name. Usage
274 example: ``(alias_option "preprocess", "E")``
275
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000276
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000277* Possible option properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000278
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000279 - ``help`` - help string associated with this option. Used for
280 ``--help`` output.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000281
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000282 - ``required`` - this option is obligatory.
283
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000284 - ``hidden`` - this option should not appear in the ``--help``
285 output (but should appear in the ``--help-hidden`` output).
286
287 - ``really_hidden`` - the option should not appear in any help
288 output.
289
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000290 - ``extern`` - this option is defined in some other plugin, see below.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000291
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000292External options
293----------------
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000294
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000295Sometimes, when linking several plugins together, one plugin needs to
296access options defined in some other plugin. Because of the way
297options are implemented, such options should be marked as
298``extern``. This is what the ``extern`` option property is
299for. Example::
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000300
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000301 ...
302 (switch_option "E", (extern))
303 ...
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000304
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000305See also the section on plugin `priorities`__.
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000306
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000307__ priorities_
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000308
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000309.. _case:
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000310
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000311Conditional evaluation
312======================
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000313
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000314The 'case' construct is the main means by which programmability is
315achieved in LLVMC. It can be used to calculate edge weights, program
316actions and modify the shell commands to be executed. The 'case'
317expression is designed after the similarly-named construct in
318functional languages and takes the form ``(case (test_1), statement_1,
319(test_2), statement_2, ... (test_N), statement_N)``. The statements
320are evaluated only if the corresponding tests evaluate to true.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000321
322Examples::
323
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000324 // Edge weight calculation
325
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000326 // Increases edge weight by 5 if "-A" is provided on the
327 // command-line, and by 5 more if "-B" is also provided.
328 (case
329 (switch_on "A"), (inc_weight 5),
330 (switch_on "B"), (inc_weight 5))
331
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000332
333 // Tool command line specification
334
335 // Evaluates to "cmdline1" if the option "-A" is provided on the
336 // command line; to "cmdline2" if "-B" is provided;
337 // otherwise to "cmdline3".
338
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000339 (case
340 (switch_on "A"), "cmdline1",
341 (switch_on "B"), "cmdline2",
342 (default), "cmdline3")
343
344Note the slight difference in 'case' expression handling in contexts
345of edge weights and command line specification - in the second example
346the value of the ``"B"`` switch is never checked when switch ``"A"`` is
347enabled, and the whole expression always evaluates to ``"cmdline1"`` in
348that case.
349
350Case 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
355You should, however, try to avoid doing that because it hurts
356readability. It is usually better to split tool descriptions and/or
357use TableGen inheritance instead.
358
359* Possible tests are:
360
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000361 - ``switch_on`` - Returns true if a given command-line switch is
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000362 provided by the user. Example: ``(switch_on "opt")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000363
364 - ``parameter_equals`` - Returns true if a command-line parameter equals
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000365 a given value.
366 Example: ``(parameter_equals "W", "all")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000367
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000368 - ``element_in_list`` - Returns true if a command-line parameter
369 list contains a given value.
370 Example: ``(parameter_in_list "l", "pthread")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000371
372 - ``input_languages_contain`` - Returns true if a given language
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000373 belongs to the current input language set.
374 Example: ``(input_languages_contain "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000375
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000376 - ``in_language`` - Evaluates to true if the input file language
377 equals to the argument. At the moment works only with ``cmd_line``
378 and ``actions`` (on non-join nodes).
379 Example: ``(in_language "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000380
381 - ``not_empty`` - Returns true if a given option (which should be
382 either a parameter or a parameter list) is set by the
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000383 user.
384 Example: ``(not_empty "o")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000385
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 Glushenkovcd0858e2008-05-30 06:14:42 +0000398
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000399Writing a tool description
400==========================
401
402As was said earlier, nodes in the compilation graph represent tools,
403which are described separately. A tool definition looks like this
404(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
405
406 def llvm_gcc_cpp : Tool<[
407 (in_language "c++"),
408 (out_language "llvm-assembler"),
409 (output_suffix "bc"),
410 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
411 (sink)
412 ]>;
413
414This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
415``llvm-g++``. As you can see, a tool definition is just a list of
416properties; most of them should be self-explanatory. The ``sink``
417property means that this tool should be passed all command-line
418options that aren't mentioned in the option list.
419
420The complete list of all currently implemented tool properties follows.
421
422* Possible tool properties:
423
424 - ``in_language`` - input language name. Can be either a string or a
425 list, in case the tool supports multiple input languages.
426
427 - ``out_language`` - output language name. Tools are not allowed to
428 have multiple output languages.
429
430 - ``output_suffix`` - output file suffix. Can also be changed
431 dynamically, see documentation on actions.
432
433 - ``cmd_line`` - the actual command used to run the tool. You can
434 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
435 with ``>``, hook invocations (``$CALL``), environment variables
436 (via ``$ENV``) and the ``case`` construct.
437
438 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
439 list of input files and joins them together. Used for linkers.
440
441 - ``sink`` - all command-line options that are not handled by other
442 tools are passed to this tool.
443
444 - ``actions`` - A single big ``case`` expression that specifies how
445 this tool reacts on command-line options (described in more detail
446 below).
447
448Actions
449-------
450
451A tool often needs to react to command-line options, and this is
452precisely what the ``actions`` property is for. The next example
453illustrates this feature::
454
455 def llvm_gcc_linker : Tool<[
456 (in_language "object-code"),
457 (out_language "executable"),
458 (output_suffix "out"),
459 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
460 (join),
461 (actions (case (not_empty "L"), (forward "L"),
462 (not_empty "l"), (forward "l"),
463 (not_empty "dummy"),
464 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
465 ]>;
466
467The ``actions`` tool property is implemented on top of the omnipresent
468``case`` expression. It associates one or more different *actions*
469with given conditions - in the example, the actions are ``forward``,
470which forwards a given option unchanged, and ``append_cmd``, which
471appends a given string to the tool execution command. Multiple actions
472can be associated with a single condition by using a list of actions
473(used in the example to append some dummy options). The same ``case``
474construct can also be used in the ``cmd_line`` property to modify the
475tool command line.
476
477The "join" property used in the example means that this tool behaves
478like a linker.
479
480The list of all possible actions follows.
481
482* Possible actions:
483
484 - ``append_cmd`` - append a string to the tool invocation
485 command.
486 Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``
487
488 - ``forward`` - forward an option unchanged.
489 Example: ``(forward "Wall")``.
490
491 - ``forward_as`` - Change the name of an option, but forward the
492 argument unchanged.
493 Example: ``(forward_as "O0" "--disable-optimization")``.
494
495 - ``output_suffix`` - modify the output suffix of this
496 tool.
497 Example: ``(output_suffix "i")``.
498
499 - ``stop_compilation`` - stop compilation after this tool processes
500 its input. Used without arguments.
501
502 - ``unpack_values`` - used for for splitting and forwarding
503 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
504 converted to ``-foo=bar -baz`` and appended to the tool invocation
505 command.
506 Example: ``(unpack_values "Wa,")``.
507
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000508Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000509============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000510
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000511If you are adding support for a new language to LLVMC, you'll need to
512modify the language map, which defines mappings from file extensions
513to language names. It is used to choose the proper toolchain(s) for a
514given input file set. Language map definition looks like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000515
516 def LanguageMap : LanguageMap<
517 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
518 LangToSuffixes<"c", ["c"]>,
519 ...
520 ]>;
521
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000522For example, without those definitions the following command wouldn't work::
523
524 $ llvmc hello.cpp
525 llvmc: Unknown suffix: cpp
526
527The language map entries should be added only for tools that are
528linked with the root node. Since tools are not allowed to have
529multiple output languages, for nodes "inside" the graph the input and
530output languages should match. This is enforced at compile-time.
531
532
533More advanced topics
534====================
535
536.. _hooks:
537
538Hooks and environment variables
539-------------------------------
540
541Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
542this is not sufficient: for example, we may want to specify tool names
543in the configuration file. This can be achieved via the mechanism of
544hooks - to write your own hooks, just add their definitions to the
545``PluginMain.cpp`` or drop a ``.cpp`` file into the
546``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
547namespace and have the signature ``std::string hooks::MyHookName
548(void)``. They can be used from the ``cmd_line`` tool property::
549
550 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
551
552It is also possible to use environment variables in the same manner::
553
554 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
555
556To change the command line string based on user-provided options use
557the ``case`` expression (documented `above`__)::
558
559 (cmd_line
560 (case
561 (switch_on "E"),
562 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
563 (default),
564 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
565
566__ case_
567
568.. _priorities:
569
570How plugins are loaded
571----------------------
572
573It is possible for LLVMC plugins to depend on each other. For example,
574one can create edges between nodes defined in some other plugin. To
575make this work, however, that plugin should be loaded first. To
576achieve this, the concept of plugin priority was introduced. By
577default, every plugin has priority zero; to specify the priority
578explicitly, put the following line in your plugin's TableGen file::
579
580 def Priority : PluginPriority<$PRIORITY_VALUE>;
581 # Where PRIORITY_VALUE is some integer > 0
582
583Plugins are loaded in order of their (increasing) priority, starting
584with 0. Therefore, the plugin with the highest priority value will be
585loaded last.
586
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000587Debugging
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000588---------
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000589
590When writing LLVMC plugins, it can be useful to get a visual view of
591the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000592line option ``--view-graph``. This command assumes that Graphviz_ and
593Ghostview_ are installed. There is also a ``--dump-graph`` option that
594creates a Graphviz source file (``compilation-graph.dot``) in the
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000595current directory.
596
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000597.. _Graphviz: http://www.graphviz.org/
598.. _Ghostview: http://pages.cs.wisc.edu/~ghost/
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000599
600.. raw:: html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000601
602 <hr />
603 <address>
604 <a href="http://jigsaw.w3.org/css-validator/check/referer">
605 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
606 alt="Valid CSS" /></a>
607 <a href="http://validator.w3.org/check?uri=referer">
608 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
609 alt="Valid XHTML 1.0 Transitional"/></a>
610
611 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
612 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
613
614 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
615 </address>