blob: 822c72bff4e84bfc5f342ce021e0a7e3fe676793 [file] [log] [blame]
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +00001===================================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkov817b2f42008-11-25 21:34:53 +00004:Author: Mikhail Glushenkov <foldr@codedegers.com>
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +00005
Mikhail Glushenkov2e6a8442008-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 Glushenkovbd51c232008-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 Korobeynikove9ffb5b2008-03-23 08:57:20 +000018
Mikhail Glushenkov2e6a8442008-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 Korobeynikove9ffb5b2008-03-23 08:57:20 +000021
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000022
23.. contents::
24
25
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000026Compiling with LLVMC
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000027====================
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000028
Mikhail Glushenkov1ce87222008-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 Korobeynikove9ffb5b2008-03-23 08:57:20 +000032
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000033 $ # This works as expected:
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000034 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000035 $ ./a.out
36 hello
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000037
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000038One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov2e6a8442008-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 Glushenkov1ce87222008-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 Korobeynikove9ffb5b2008-03-23 08:57:20 +000044
Mikhail Glushenkov99f10642008-11-25 21:34:29 +000045 $ # hello.c is really a C++ file
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000046 $ llvmc -x c++ hello.c
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000047 $ ./a.out
48 hello
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000049
Mikhail Glushenkov2e6a8442008-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 Korobeynikove9ffb5b2008-03-23 08:57:20 +000053
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000054 $ llvmc -c hello.cpp
55 $ llvmc hello.o
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000056 [A lot of link-time errors skipped]
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000057 $ llvmc --linker=c++ hello.o
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000058 $ ./a.out
59 hello
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +000060
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +000061By default, LLVMC uses ``llvm-gcc`` to compile the source code. It is
62also possible to choose the work-in-progress ``clang`` compiler with
63the ``-clang`` option.
64
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000065
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000066Predefined options
67==================
68
69LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +000070configuration libraries:
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000071
72* ``-o FILE`` - Output file name.
73
74* ``-x LANGUAGE`` - Specify the language of the following input files
75 until the next -x option.
76
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000077* ``-load PLUGIN_NAME`` - Load the specified plugin DLL. Example:
78 ``-load $LLVM_DIR/Release/lib/LLVMCSimple.so``.
79
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000080* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
81
82* ``--view-graph`` - Show a graphical representation of the compilation
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +000083 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000084 installed. Hidden option, useful for debugging.
85
86* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
87 current directory with the compilation graph description in the
88 Graphviz format. Hidden option, useful for debugging.
89
Mikhail Glushenkova5bdf6e2008-05-30 06:29:17 +000090* ``--save-temps`` - Write temporary files to the current directory
91 and do not delete them on exit. Hidden option, useful for debugging.
92
93* ``--help``, ``--help-hidden``, ``--version`` - These options have
94 their standard meaning.
95
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +000096
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000097Compiling LLVMC plugins
98=======================
99
100It's easiest to start working on your own LLVMC plugin by copying the
101skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
102
103 $ cd $LLVMC_DIR/plugins
104 $ cp -r Simple MyPlugin
105 $ cd MyPlugin
106 $ ls
107 Makefile PluginMain.cpp Simple.td
108
109As you can see, our basic plugin consists of only two files (not
110counting the build script). ``Simple.td`` contains TableGen
111description of the compilation graph; its format is documented in the
112following sections. ``PluginMain.cpp`` is just a helper file used to
113compile the auto-generated C++ code produced from TableGen source. It
114can also contain hook definitions (see `below`__).
115
116__ hooks_
117
118The first thing that you should do is to change the ``LLVMC_PLUGIN``
119variable in the ``Makefile`` to avoid conflicts (since this variable
120is used to name the resulting library)::
121
122 LLVMC_PLUGIN=MyPlugin
123
124It is also a good idea to rename ``Simple.td`` to something less
125generic::
126
127 $ mv Simple.td MyPlugin.td
128
Mikhail Glushenkovd333fce2008-11-25 21:34:01 +0000129Note that the plugin source directory must be placed under
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000130``$LLVMC_DIR/plugins`` to make use of the existing build
131infrastructure. To build a version of the LLVMC executable called
132``mydriver`` with your plugin compiled in, use the following command::
133
134 $ cd $LLVMC_DIR
135 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
136
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000137To build your plugin as a dynamic library, just ``cd`` to its source
138directory and run ``make``. The resulting file will be called
139``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
140``LLVMCMyPlugin.so``). This library can be then loaded in with the
141``-load`` option. Example::
142
143 $ cd $LLVMC_DIR/plugins/Simple
144 $ make
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +0000145 $ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000146
Mikhail Glushenkovd333fce2008-11-25 21:34:01 +0000147Sometimes, you will want a 'bare-bones' version of LLVMC that has no
148built-in plugins. It can be compiled with the following command::
149
150 $ cd $LLVMC_DIR
151 $ make BUILTIN_PLUGINS=""
152
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000153
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000154Customizing LLVMC: the compilation graph
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000155========================================
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000156
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000157Each TableGen configuration file should include the common
158definitions::
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000159
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000160 include "llvm/CompilerDriver/Common.td"
161 // And optionally:
162 // include "llvm/CompilerDriver/Tools.td"
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000163 // which contains some useful tool definitions.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000164
165Internally, LLVMC stores information about possible source
166transformations in form of a graph. Nodes in this graph represent
167tools, and edges between two nodes represent a transformation path. A
168special "root" node is used to mark entry points for the
169transformations. LLVMC also assigns a weight to each edge (more on
170this later) to choose between several alternative edges.
171
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000172The definition of the compilation graph (see file
173``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000174
175 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000176 Edge<"root", "llvm_gcc_c">,
177 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000178 ...
179
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000180 Edge<"llvm_gcc_c", "llc">,
181 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000182 ...
183
Mikhail Glushenkov817b2f42008-11-25 21:34:53 +0000184 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
185 (inc_weight))>,
186 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
187 (inc_weight))>,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000188 ...
189
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000190 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000191 (case (input_languages_contain "c++"), (inc_weight),
192 (or (parameter_equals "linker", "g++"),
193 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000194 ...
195
196 ]>;
197
198As you can see, the edges can be either default or optional, where
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000199optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkovfa990682008-11-17 17:29:18 +0000200used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovd333fce2008-11-25 21:34:01 +0000201to tools via their names (as strings). This makes it possible to add
202edges to an existing compilation graph in plugins without having to
203know about all tool definitions used in the graph.
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000204
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000205The default edges are assigned a weight of 1, and optional edges get a
206weight of 0 + 2*N where N is the number of tests that evaluated to
207true in the ``case`` expression. It is also possible to provide an
208integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
209the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +0000210of the default 2. It is also possible to change the default weight of
211an optional edge by using the ``default`` clause of the ``case``
212construct.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000213
214When passing an input file through the graph, LLVMC picks the edge
215with the maximum weight. To avoid ambiguity, there should be only one
216default edge between two nodes (with the exception of the root node,
217which gets a special treatment - there you are allowed to specify one
218default edge *per language*).
219
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +0000220When multiple plugins are loaded, their compilation graphs are merged
Mikhail Glushenkov642e9a12008-11-28 00:12:09 +0000221together. Since multiple edges that have the same end nodes are not
222allowed (i.e. the graph is not a multigraph), an edge defined in
Mikhail Glushenkov4f82fda2008-11-26 22:59:45 +0000223several plugins will be replaced by the definition from the plugin
224that was loaded last. Plugin load order can be controlled by using the
225plugin priority feature described above.
226
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000227To get a visual representation of the compilation graph (useful for
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +0000228debugging), run ``llvmc --view-graph``. You will need ``dot`` and
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000229``gsview`` installed for this to work properly.
230
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000231Describing options
232==================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000233
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000234Command-line options that the plugin supports are defined by using an
235``OptionList``::
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000236
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000237 def Options : OptionList<[
238 (switch_option "E", (help "Help string")),
239 (alias_option "quiet", "q")
240 ...
241 ]>;
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000242
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000243As you can see, the option list is just a list of DAGs, where each DAG
244is an option description consisting of the option name and some
245properties. A plugin can define more than one option list (they are
246all merged together in the end), which can be handy if one wants to
247separate option groups syntactically.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000248
249* Possible option types:
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000250
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000251 - ``switch_option`` - a simple boolean switch, for example ``-time``.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000252
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000253 - ``parameter_option`` - option that takes an argument, for example
254 ``-std=c99``;
255
256 - ``parameter_list_option`` - same as the above, but more than one
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000257 occurence of the option is allowed.
258
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000259 - ``prefix_option`` - same as the parameter_option, but the option name
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000260 and parameter value are not separated.
261
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000262 - ``prefix_list_option`` - same as the above, but more than one
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000263 occurence of the option is allowed; example: ``-lm -lpthread``.
264
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000265 - ``alias_option`` - a special option type for creating
266 aliases. Unlike other option types, aliases are not allowed to
267 have any properties besides the aliased option name. Usage
268 example: ``(alias_option "preprocess", "E")``
269
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000270
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000271* Possible option properties:
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000272
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000273 - ``help`` - help string associated with this option. Used for
274 ``--help`` output.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000275
Mikhail Glushenkov2e6a8442008-05-06 18:17:19 +0000276 - ``required`` - this option is obligatory.
277
Mikhail Glushenkovc9b650d2008-11-28 00:13:25 +0000278 - ``hidden`` - this option should not appear in the ``--help``
279 output (but should appear in the ``--help-hidden`` output).
280
281 - ``really_hidden`` - the option should not appear in any help
282 output.
283
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000284 - ``extern`` - this option is defined in some other plugin, see below.
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000285
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000286External options
287----------------
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000288
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000289Sometimes, when linking several plugins together, one plugin needs to
290access options defined in some other plugin. Because of the way
291options are implemented, such options should be marked as
292``extern``. This is what the ``extern`` option property is
293for. Example::
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000294
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000295 ...
296 (switch_option "E", (extern))
297 ...
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000298
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000299See also the section on plugin `priorities`__.
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000300
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000301__ priorities_
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000302
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000303.. _case:
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +0000304
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000305Conditional evaluation
306======================
Mikhail Glushenkov75ade502008-05-30 06:28:00 +0000307
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000308The 'case' construct is the main means by which programmability is
309achieved in LLVMC. It can be used to calculate edge weights, program
310actions and modify the shell commands to be executed. The 'case'
311expression is designed after the similarly-named construct in
312functional languages and takes the form ``(case (test_1), statement_1,
313(test_2), statement_2, ... (test_N), statement_N)``. The statements
314are evaluated only if the corresponding tests evaluate to true.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000315
316Examples::
317
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000318 // Edge weight calculation
319
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000320 // Increases edge weight by 5 if "-A" is provided on the
321 // command-line, and by 5 more if "-B" is also provided.
322 (case
323 (switch_on "A"), (inc_weight 5),
324 (switch_on "B"), (inc_weight 5))
325
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000326
327 // Tool command line specification
328
329 // Evaluates to "cmdline1" if the option "-A" is provided on the
330 // command line; to "cmdline2" if "-B" is provided;
331 // otherwise to "cmdline3".
332
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000333 (case
334 (switch_on "A"), "cmdline1",
335 (switch_on "B"), "cmdline2",
336 (default), "cmdline3")
337
338Note the slight difference in 'case' expression handling in contexts
339of edge weights and command line specification - in the second example
340the value of the ``"B"`` switch is never checked when switch ``"A"`` is
341enabled, and the whole expression always evaluates to ``"cmdline1"`` in
342that case.
343
344Case expressions can also be nested, i.e. the following is legal::
345
346 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
347 (default), ...)
348
349You should, however, try to avoid doing that because it hurts
350readability. It is usually better to split tool descriptions and/or
351use TableGen inheritance instead.
352
353* Possible tests are:
354
Mikhail Glushenkov817b2f42008-11-25 21:34:53 +0000355 - ``switch_on`` - Returns true if a given command-line switch is
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000356 provided by the user. Example: ``(switch_on "opt")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000357
358 - ``parameter_equals`` - Returns true if a command-line parameter equals
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000359 a given value.
360 Example: ``(parameter_equals "W", "all")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000361
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000362 - ``element_in_list`` - Returns true if a command-line parameter
363 list contains a given value.
364 Example: ``(parameter_in_list "l", "pthread")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000365
366 - ``input_languages_contain`` - Returns true if a given language
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000367 belongs to the current input language set.
368 Example: ``(input_languages_contain "c++")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000369
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000370 - ``in_language`` - Evaluates to true if the input file language
371 equals to the argument. At the moment works only with ``cmd_line``
372 and ``actions`` (on non-join nodes).
373 Example: ``(in_language "c++")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000374
375 - ``not_empty`` - Returns true if a given option (which should be
376 either a parameter or a parameter list) is set by the
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000377 user.
378 Example: ``(not_empty "o")``.
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000379
380 - ``default`` - Always evaluates to true. Should always be the last
381 test in the ``case`` expression.
382
383 - ``and`` - A standard logical combinator that returns true iff all
384 of its arguments return true. Used like this: ``(and (test1),
385 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
386 but not encouraged.
387
388 - ``or`` - Another logical combinator that returns true only if any
389 one of its arguments returns true. Example: ``(or (test1),
390 (test2), ... (testN))``.
391
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +0000392
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000393Writing a tool description
394==========================
395
396As was said earlier, nodes in the compilation graph represent tools,
397which are described separately. A tool definition looks like this
398(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
399
400 def llvm_gcc_cpp : Tool<[
401 (in_language "c++"),
402 (out_language "llvm-assembler"),
403 (output_suffix "bc"),
404 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
405 (sink)
406 ]>;
407
408This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
409``llvm-g++``. As you can see, a tool definition is just a list of
410properties; most of them should be self-explanatory. The ``sink``
411property means that this tool should be passed all command-line
412options that aren't mentioned in the option list.
413
414The complete list of all currently implemented tool properties follows.
415
416* Possible tool properties:
417
418 - ``in_language`` - input language name. Can be either a string or a
419 list, in case the tool supports multiple input languages.
420
421 - ``out_language`` - output language name. Tools are not allowed to
422 have multiple output languages.
423
424 - ``output_suffix`` - output file suffix. Can also be changed
425 dynamically, see documentation on actions.
426
427 - ``cmd_line`` - the actual command used to run the tool. You can
428 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
429 with ``>``, hook invocations (``$CALL``), environment variables
430 (via ``$ENV``) and the ``case`` construct.
431
432 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
433 list of input files and joins them together. Used for linkers.
434
435 - ``sink`` - all command-line options that are not handled by other
436 tools are passed to this tool.
437
438 - ``actions`` - A single big ``case`` expression that specifies how
439 this tool reacts on command-line options (described in more detail
440 below).
441
442Actions
443-------
444
445A tool often needs to react to command-line options, and this is
446precisely what the ``actions`` property is for. The next example
447illustrates this feature::
448
449 def llvm_gcc_linker : Tool<[
450 (in_language "object-code"),
451 (out_language "executable"),
452 (output_suffix "out"),
453 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
454 (join),
455 (actions (case (not_empty "L"), (forward "L"),
456 (not_empty "l"), (forward "l"),
457 (not_empty "dummy"),
458 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
459 ]>;
460
461The ``actions`` tool property is implemented on top of the omnipresent
462``case`` expression. It associates one or more different *actions*
463with given conditions - in the example, the actions are ``forward``,
464which forwards a given option unchanged, and ``append_cmd``, which
465appends a given string to the tool execution command. Multiple actions
466can be associated with a single condition by using a list of actions
467(used in the example to append some dummy options). The same ``case``
468construct can also be used in the ``cmd_line`` property to modify the
469tool command line.
470
471The "join" property used in the example means that this tool behaves
472like a linker.
473
474The list of all possible actions follows.
475
476* Possible actions:
477
478 - ``append_cmd`` - append a string to the tool invocation
479 command.
480 Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``
481
482 - ``forward`` - forward an option unchanged.
483 Example: ``(forward "Wall")``.
484
485 - ``forward_as`` - Change the name of an option, but forward the
486 argument unchanged.
487 Example: ``(forward_as "O0" "--disable-optimization")``.
488
489 - ``output_suffix`` - modify the output suffix of this
490 tool.
491 Example: ``(output_suffix "i")``.
492
493 - ``stop_compilation`` - stop compilation after this tool processes
494 its input. Used without arguments.
495
496 - ``unpack_values`` - used for for splitting and forwarding
497 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
498 converted to ``-foo=bar -baz`` and appended to the tool invocation
499 command.
500 Example: ``(unpack_values "Wa,")``.
501
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000502Language map
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +0000503============
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000504
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000505If you are adding support for a new language to LLVMC, you'll need to
506modify the language map, which defines mappings from file extensions
507to language names. It is used to choose the proper toolchain(s) for a
508given input file set. Language map definition looks like this::
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000509
510 def LanguageMap : LanguageMap<
511 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
512 LangToSuffixes<"c", ["c"]>,
513 ...
514 ]>;
515
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000516For example, without those definitions the following command wouldn't work::
517
518 $ llvmc hello.cpp
519 llvmc: Unknown suffix: cpp
520
521The language map entries should be added only for tools that are
522linked with the root node. Since tools are not allowed to have
523multiple output languages, for nodes "inside" the graph the input and
524output languages should match. This is enforced at compile-time.
525
526
527More advanced topics
528====================
529
530.. _hooks:
531
532Hooks and environment variables
533-------------------------------
534
535Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
536this is not sufficient: for example, we may want to specify tool names
537in the configuration file. This can be achieved via the mechanism of
538hooks - to write your own hooks, just add their definitions to the
539``PluginMain.cpp`` or drop a ``.cpp`` file into the
540``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
541namespace and have the signature ``std::string hooks::MyHookName
542(void)``. They can be used from the ``cmd_line`` tool property::
543
544 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
545
546It is also possible to use environment variables in the same manner::
547
548 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
549
550To change the command line string based on user-provided options use
551the ``case`` expression (documented `above`__)::
552
553 (cmd_line
554 (case
555 (switch_on "E"),
556 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
557 (default),
558 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
559
560__ case_
561
562.. _priorities:
563
564How plugins are loaded
565----------------------
566
567It is possible for LLVMC plugins to depend on each other. For example,
568one can create edges between nodes defined in some other plugin. To
569make this work, however, that plugin should be loaded first. To
570achieve this, the concept of plugin priority was introduced. By
571default, every plugin has priority zero; to specify the priority
572explicitly, put the following line in your plugin's TableGen file::
573
574 def Priority : PluginPriority<$PRIORITY_VALUE>;
575 # Where PRIORITY_VALUE is some integer > 0
576
577Plugins are loaded in order of their (increasing) priority, starting
578with 0. Therefore, the plugin with the highest priority value will be
579loaded last.
580
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000581Debugging
Mikhail Glushenkov8fdb3172008-12-07 16:47:42 +0000582---------
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000583
584When writing LLVMC plugins, it can be useful to get a visual view of
585the resulting compilation graph. This can be achieved via the command
586line option ``--view-graph``. This command assumes that Graphviz [2]_ and
587Ghostview [3]_ are installed. There is also a ``--dump-graph`` option that
588creates a Graphviz source file(``compilation-graph.dot``) in the
589current directory.
590
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000591
Anton Korobeynikove9ffb5b2008-03-23 08:57:20 +0000592References
593==========
594
595.. [1] TableGen Fundamentals
596 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkovf74495a2008-09-22 20:48:48 +0000597
598.. [2] Graphviz
599 http://www.graphviz.org/
600
601.. [3] Ghostview
602 http://pages.cs.wisc.edu/~ghost/