blob: 4fc53e09624bbe944385a9a3e0ef0b293a2ec6da [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001===================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkov536637f2008-11-25 21:34:53 +00004:Author: Mikhail Glushenkov <foldr@codedegers.com>
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +00005
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +00006LLVMC is a generic compiler driver, designed to be customizable and
7extensible. It plays the same role for LLVM as the ``gcc`` program
8does for GCC - LLVMC's job is essentially to transform a set of input
9files into a set of targets depending on configuration rules and user
10options. What makes LLVMC different is that these transformation rules
11are completely customizable - in fact, LLVMC knows nothing about the
12specifics of transformation (even the command-line options are mostly
13not hard-coded) and regards the transformation structure as an
Mikhail Glushenkov83237482008-10-15 09:29:13 +000014abstract graph. The structure of this graph is completely determined
15by plugins, which can be either statically or dynamically linked. This
16makes it possible to easily adapt LLVMC for other purposes - for
17example, as a build tool for game resources.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000018
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000019Because LLVMC employs TableGen [1]_ as its configuration language, you
20need to be familiar with it to customize LLVMC.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000021
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000022
23.. contents::
24
25
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000026Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000027====================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000028
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000029LLVMC tries hard to be as compatible with ``gcc`` as possible,
30although there are some small differences. Most of the time, however,
31you shouldn't be able to notice them::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000032
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000033 $ # This works as expected:
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000034 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000035 $ ./a.out
36 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000037
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000038One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000039between different compilers for different languages (think ``g++`` and
40``gcc``) - the right toolchain is chosen automatically based on input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000041language names (which are, in turn, determined from file
42extensions). If you want to force files ending with ".c" to compile as
43C++, use the ``-x`` option, just like you would do it with ``gcc``::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000044
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000045 $ # hello.c is really a C++ file
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000046 $ llvmc -x c++ hello.c
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000047 $ ./a.out
48 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000049
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000050On the other hand, when using LLVMC as a linker to combine several C++
51object files you should provide the ``--linker`` option since it's
52impossible for LLVMC to choose the right linker in that case::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000053
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000054 $ llvmc -c hello.cpp
55 $ llvmc hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000056 [A lot of link-time errors skipped]
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000057 $ llvmc --linker=c++ hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000058 $ ./a.out
59 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000060
Mikhail Glushenkov4410e322008-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 Glushenkov83237482008-10-15 09:29:13 +000065
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000066Predefined options
67==================
68
69LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000070configuration libraries:
Mikhail Glushenkov270cae32008-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 Glushenkov83237482008-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 Glushenkov270cae32008-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 Glushenkov9ecd30c2008-09-22 20:48:48 +000083 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov270cae32008-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 Glushenkov73296102008-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 Glushenkov77ddce92008-05-06 18:17:19 +000096
Mikhail Glushenkov83237482008-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 Glushenkovf80f0aa2008-11-25 21:34:01 +0000129Note that the plugin source directory must be placed under
Mikhail Glushenkov83237482008-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 Glushenkov83237482008-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 Glushenkov113ec352008-11-25 21:38:12 +0000145 $ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000146
Mikhail Glushenkovf80f0aa2008-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 Glushenkov83237482008-10-15 09:29:13 +0000153
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000154Customizing LLVMC: the compilation graph
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000155========================================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000156
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000157Each TableGen configuration file should include the common
158definitions::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000159
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000160 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000161
162Internally, LLVMC stores information about possible source
163transformations in form of a graph. Nodes in this graph represent
164tools, and edges between two nodes represent a transformation path. A
165special "root" node is used to mark entry points for the
166transformations. LLVMC also assigns a weight to each edge (more on
167this later) to choose between several alternative edges.
168
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000169The definition of the compilation graph (see file
170``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000171
172 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000173 Edge<"root", "llvm_gcc_c">,
174 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000175 ...
176
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000177 Edge<"llvm_gcc_c", "llc">,
178 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000179 ...
180
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000181 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
182 (inc_weight))>,
183 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
184 (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000185 ...
186
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000187 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000188 (case (input_languages_contain "c++"), (inc_weight),
189 (or (parameter_equals "linker", "g++"),
190 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000191 ...
192
193 ]>;
194
195As you can see, the edges can be either default or optional, where
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000196optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000197used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000198to tools via their names (as strings). This makes it possible to add
199edges to an existing compilation graph in plugins without having to
200know about all tool definitions used in the graph.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000201
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000202The default edges are assigned a weight of 1, and optional edges get a
203weight of 0 + 2*N where N is the number of tests that evaluated to
204true in the ``case`` expression. It is also possible to provide an
205integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
206the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000207of the default 2. It is also possible to change the default weight of
208an optional edge by using the ``default`` clause of the ``case``
209construct.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000210
211When passing an input file through the graph, LLVMC picks the edge
212with the maximum weight. To avoid ambiguity, there should be only one
213default edge between two nodes (with the exception of the root node,
214which gets a special treatment - there you are allowed to specify one
215default edge *per language*).
216
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000217When multiple plugins are loaded, their compilation graphs are merged
Mikhail Glushenkov3321b0f2008-11-28 00:12:09 +0000218together. Since multiple edges that have the same end nodes are not
219allowed (i.e. the graph is not a multigraph), an edge defined in
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000220several plugins will be replaced by the definition from the plugin
221that was loaded last. Plugin load order can be controlled by using the
222plugin priority feature described above.
223
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000224To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov113ec352008-11-25 21:38:12 +0000225debugging), run ``llvmc --view-graph``. You will need ``dot`` and
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000226``gsview`` installed for this to work properly.
227
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000228Describing options
229==================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000230
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000231Command-line options that the plugin supports are defined by using an
232``OptionList``::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000233
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000234 def Options : OptionList<[
235 (switch_option "E", (help "Help string")),
236 (alias_option "quiet", "q")
237 ...
238 ]>;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000239
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000240As you can see, the option list is just a list of DAGs, where each DAG
241is an option description consisting of the option name and some
242properties. A plugin can define more than one option list (they are
243all merged together in the end), which can be handy if one wants to
244separate option groups syntactically.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000245
246* Possible option types:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000247
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000248 - ``switch_option`` - a simple boolean switch, for example ``-time``.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000249
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000250 - ``parameter_option`` - option that takes an argument, for example
251 ``-std=c99``;
252
253 - ``parameter_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000254 occurence of the option is allowed.
255
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000256 - ``prefix_option`` - same as the parameter_option, but the option name
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000257 and parameter value are not separated.
258
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000259 - ``prefix_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000260 occurence of the option is allowed; example: ``-lm -lpthread``.
261
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000262 - ``alias_option`` - a special option type for creating
263 aliases. Unlike other option types, aliases are not allowed to
264 have any properties besides the aliased option name. Usage
265 example: ``(alias_option "preprocess", "E")``
266
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000267
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000268* Possible option properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000269
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000270 - ``help`` - help string associated with this option. Used for
271 ``--help`` output.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000272
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000273 - ``required`` - this option is obligatory.
274
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000275 - ``hidden`` - this option should not appear in the ``--help``
276 output (but should appear in the ``--help-hidden`` output).
277
278 - ``really_hidden`` - the option should not appear in any help
279 output.
280
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000281 - ``extern`` - this option is defined in some other plugin, see below.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000282
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000283External options
284----------------
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000285
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000286Sometimes, when linking several plugins together, one plugin needs to
287access options defined in some other plugin. Because of the way
288options are implemented, such options should be marked as
289``extern``. This is what the ``extern`` option property is
290for. Example::
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000291
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000292 ...
293 (switch_option "E", (extern))
294 ...
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000295
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000296See also the section on plugin `priorities`__.
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000297
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000298__ priorities_
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000299
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000300.. _case:
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000301
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000302Conditional evaluation
303======================
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000304
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000305The 'case' construct is the main means by which programmability is
306achieved in LLVMC. It can be used to calculate edge weights, program
307actions and modify the shell commands to be executed. The 'case'
308expression is designed after the similarly-named construct in
309functional languages and takes the form ``(case (test_1), statement_1,
310(test_2), statement_2, ... (test_N), statement_N)``. The statements
311are evaluated only if the corresponding tests evaluate to true.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000312
313Examples::
314
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000315 // Edge weight calculation
316
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000317 // Increases edge weight by 5 if "-A" is provided on the
318 // command-line, and by 5 more if "-B" is also provided.
319 (case
320 (switch_on "A"), (inc_weight 5),
321 (switch_on "B"), (inc_weight 5))
322
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000323
324 // Tool command line specification
325
326 // Evaluates to "cmdline1" if the option "-A" is provided on the
327 // command line; to "cmdline2" if "-B" is provided;
328 // otherwise to "cmdline3".
329
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000330 (case
331 (switch_on "A"), "cmdline1",
332 (switch_on "B"), "cmdline2",
333 (default), "cmdline3")
334
335Note the slight difference in 'case' expression handling in contexts
336of edge weights and command line specification - in the second example
337the value of the ``"B"`` switch is never checked when switch ``"A"`` is
338enabled, and the whole expression always evaluates to ``"cmdline1"`` in
339that case.
340
341Case expressions can also be nested, i.e. the following is legal::
342
343 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
344 (default), ...)
345
346You should, however, try to avoid doing that because it hurts
347readability. It is usually better to split tool descriptions and/or
348use TableGen inheritance instead.
349
350* Possible tests are:
351
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000352 - ``switch_on`` - Returns true if a given command-line switch is
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000353 provided by the user. Example: ``(switch_on "opt")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000354
355 - ``parameter_equals`` - Returns true if a command-line parameter equals
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000356 a given value.
357 Example: ``(parameter_equals "W", "all")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000358
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000359 - ``element_in_list`` - Returns true if a command-line parameter
360 list contains a given value.
361 Example: ``(parameter_in_list "l", "pthread")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000362
363 - ``input_languages_contain`` - Returns true if a given language
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000364 belongs to the current input language set.
365 Example: ``(input_languages_contain "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000366
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000367 - ``in_language`` - Evaluates to true if the input file language
368 equals to the argument. At the moment works only with ``cmd_line``
369 and ``actions`` (on non-join nodes).
370 Example: ``(in_language "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000371
372 - ``not_empty`` - Returns true if a given option (which should be
373 either a parameter or a parameter list) is set by the
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000374 user.
375 Example: ``(not_empty "o")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000376
377 - ``default`` - Always evaluates to true. Should always be the last
378 test in the ``case`` expression.
379
380 - ``and`` - A standard logical combinator that returns true iff all
381 of its arguments return true. Used like this: ``(and (test1),
382 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
383 but not encouraged.
384
385 - ``or`` - Another logical combinator that returns true only if any
386 one of its arguments returns true. Example: ``(or (test1),
387 (test2), ... (testN))``.
388
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000389
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000390Writing a tool description
391==========================
392
393As was said earlier, nodes in the compilation graph represent tools,
394which are described separately. A tool definition looks like this
395(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
396
397 def llvm_gcc_cpp : Tool<[
398 (in_language "c++"),
399 (out_language "llvm-assembler"),
400 (output_suffix "bc"),
401 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
402 (sink)
403 ]>;
404
405This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
406``llvm-g++``. As you can see, a tool definition is just a list of
407properties; most of them should be self-explanatory. The ``sink``
408property means that this tool should be passed all command-line
409options that aren't mentioned in the option list.
410
411The complete list of all currently implemented tool properties follows.
412
413* Possible tool properties:
414
415 - ``in_language`` - input language name. Can be either a string or a
416 list, in case the tool supports multiple input languages.
417
418 - ``out_language`` - output language name. Tools are not allowed to
419 have multiple output languages.
420
421 - ``output_suffix`` - output file suffix. Can also be changed
422 dynamically, see documentation on actions.
423
424 - ``cmd_line`` - the actual command used to run the tool. You can
425 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
426 with ``>``, hook invocations (``$CALL``), environment variables
427 (via ``$ENV``) and the ``case`` construct.
428
429 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
430 list of input files and joins them together. Used for linkers.
431
432 - ``sink`` - all command-line options that are not handled by other
433 tools are passed to this tool.
434
435 - ``actions`` - A single big ``case`` expression that specifies how
436 this tool reacts on command-line options (described in more detail
437 below).
438
439Actions
440-------
441
442A tool often needs to react to command-line options, and this is
443precisely what the ``actions`` property is for. The next example
444illustrates this feature::
445
446 def llvm_gcc_linker : Tool<[
447 (in_language "object-code"),
448 (out_language "executable"),
449 (output_suffix "out"),
450 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
451 (join),
452 (actions (case (not_empty "L"), (forward "L"),
453 (not_empty "l"), (forward "l"),
454 (not_empty "dummy"),
455 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
456 ]>;
457
458The ``actions`` tool property is implemented on top of the omnipresent
459``case`` expression. It associates one or more different *actions*
460with given conditions - in the example, the actions are ``forward``,
461which forwards a given option unchanged, and ``append_cmd``, which
462appends a given string to the tool execution command. Multiple actions
463can be associated with a single condition by using a list of actions
464(used in the example to append some dummy options). The same ``case``
465construct can also be used in the ``cmd_line`` property to modify the
466tool command line.
467
468The "join" property used in the example means that this tool behaves
469like a linker.
470
471The list of all possible actions follows.
472
473* Possible actions:
474
475 - ``append_cmd`` - append a string to the tool invocation
476 command.
477 Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``
478
479 - ``forward`` - forward an option unchanged.
480 Example: ``(forward "Wall")``.
481
482 - ``forward_as`` - Change the name of an option, but forward the
483 argument unchanged.
484 Example: ``(forward_as "O0" "--disable-optimization")``.
485
486 - ``output_suffix`` - modify the output suffix of this
487 tool.
488 Example: ``(output_suffix "i")``.
489
490 - ``stop_compilation`` - stop compilation after this tool processes
491 its input. Used without arguments.
492
493 - ``unpack_values`` - used for for splitting and forwarding
494 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
495 converted to ``-foo=bar -baz`` and appended to the tool invocation
496 command.
497 Example: ``(unpack_values "Wa,")``.
498
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000499Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000500============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000501
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000502If you are adding support for a new language to LLVMC, you'll need to
503modify the language map, which defines mappings from file extensions
504to language names. It is used to choose the proper toolchain(s) for a
505given input file set. Language map definition looks like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000506
507 def LanguageMap : LanguageMap<
508 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
509 LangToSuffixes<"c", ["c"]>,
510 ...
511 ]>;
512
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000513For example, without those definitions the following command wouldn't work::
514
515 $ llvmc hello.cpp
516 llvmc: Unknown suffix: cpp
517
518The language map entries should be added only for tools that are
519linked with the root node. Since tools are not allowed to have
520multiple output languages, for nodes "inside" the graph the input and
521output languages should match. This is enforced at compile-time.
522
523
524More advanced topics
525====================
526
527.. _hooks:
528
529Hooks and environment variables
530-------------------------------
531
532Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
533this is not sufficient: for example, we may want to specify tool names
534in the configuration file. This can be achieved via the mechanism of
535hooks - to write your own hooks, just add their definitions to the
536``PluginMain.cpp`` or drop a ``.cpp`` file into the
537``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
538namespace and have the signature ``std::string hooks::MyHookName
539(void)``. They can be used from the ``cmd_line`` tool property::
540
541 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
542
543It is also possible to use environment variables in the same manner::
544
545 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
546
547To change the command line string based on user-provided options use
548the ``case`` expression (documented `above`__)::
549
550 (cmd_line
551 (case
552 (switch_on "E"),
553 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
554 (default),
555 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
556
557__ case_
558
559.. _priorities:
560
561How plugins are loaded
562----------------------
563
564It is possible for LLVMC plugins to depend on each other. For example,
565one can create edges between nodes defined in some other plugin. To
566make this work, however, that plugin should be loaded first. To
567achieve this, the concept of plugin priority was introduced. By
568default, every plugin has priority zero; to specify the priority
569explicitly, put the following line in your plugin's TableGen file::
570
571 def Priority : PluginPriority<$PRIORITY_VALUE>;
572 # Where PRIORITY_VALUE is some integer > 0
573
574Plugins are loaded in order of their (increasing) priority, starting
575with 0. Therefore, the plugin with the highest priority value will be
576loaded last.
577
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000578Debugging
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000579---------
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000580
581When writing LLVMC plugins, it can be useful to get a visual view of
582the resulting compilation graph. This can be achieved via the command
583line option ``--view-graph``. This command assumes that Graphviz [2]_ and
584Ghostview [3]_ are installed. There is also a ``--dump-graph`` option that
585creates a Graphviz source file(``compilation-graph.dot``) in the
586current directory.
587
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000588
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000589References
590==========
591
592.. [1] TableGen Fundamentals
593 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000594
595.. [2] Graphviz
596 http://www.graphviz.org/
597
598.. [3] Ghostview
599 http://pages.cs.wisc.edu/~ghost/