blob: 66253ee0dca6bac37d930891387d2886350d0ffd [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001===================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Customizing LLVMC: Reference Manual
3===================================
Mikhail Glushenkov23f522a2008-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 Glushenkovd5652032008-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 Korobeynikovac67b7e2008-03-23 08:57:20 +000019
Mikhail Glushenkov77ddce92008-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 Glushenkov83237482008-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 Korobeynikovac67b7e2008-03-23 08:57:20 +000032
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000033Because LLVMC employs TableGen_ as its configuration language, you
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000034need to be familiar with it to customize LLVMC.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000035
Mikhail Glushenkovd5652032008-12-13 02:28:58 +000036.. _TableGen: http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000037
38
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000039Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000040====================
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000041
Mikhail Glushenkovcd0858e2008-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 Korobeynikovac67b7e2008-03-23 08:57:20 +000045
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000046 $ # This works as expected:
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000047 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000048 $ ./a.out
49 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000050
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000051One nice feature of LLVMC is that one doesn't have to distinguish
Mikhail Glushenkov77ddce92008-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 Glushenkovcd0858e2008-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 Korobeynikovac67b7e2008-03-23 08:57:20 +000057
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000058 $ # hello.c is really a C++ file
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000059 $ llvmc -x c++ hello.c
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000060 $ ./a.out
61 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000062
Mikhail Glushenkov77ddce92008-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 Korobeynikovac67b7e2008-03-23 08:57:20 +000066
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000067 $ llvmc -c hello.cpp
68 $ llvmc hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000069 [A lot of link-time errors skipped]
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000070 $ llvmc --linker=c++ hello.o
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +000071 $ ./a.out
72 hello
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +000073
Mikhail Glushenkov4410e322008-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 Glushenkov83237482008-10-15 09:29:13 +000078
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000079Predefined options
80==================
81
82LLVMC has some built-in options that can't be overridden in the
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +000083configuration libraries:
Mikhail Glushenkov270cae32008-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 Glushenkov83237482008-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 Glushenkov270cae32008-05-30 06:25:24 +000093* ``-v`` - Enable verbose mode, i.e. print out all executed commands.
94
95* ``--view-graph`` - Show a graphical representation of the compilation
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +000096 graph. Requires that you have ``dot`` and ``gv`` programs
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000097 installed. Hidden option, useful for debugging.
98
99* ``--write-graph`` - Write a ``compilation-graph.dot`` file in the
100 current directory with the compilation graph description in the
101 Graphviz format. Hidden option, useful for debugging.
102
Mikhail Glushenkov73296102008-05-30 06:29:17 +0000103* ``--save-temps`` - Write temporary files to the current directory
104 and do not delete them on exit. Hidden option, useful for debugging.
105
106* ``--help``, ``--help-hidden``, ``--version`` - These options have
107 their standard meaning.
108
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000109
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000110Compiling LLVMC plugins
111=======================
112
113It's easiest to start working on your own LLVMC plugin by copying the
114skeleton project which lives under ``$LLVMC_DIR/plugins/Simple``::
115
116 $ cd $LLVMC_DIR/plugins
117 $ cp -r Simple MyPlugin
118 $ cd MyPlugin
119 $ ls
120 Makefile PluginMain.cpp Simple.td
121
122As you can see, our basic plugin consists of only two files (not
123counting the build script). ``Simple.td`` contains TableGen
124description of the compilation graph; its format is documented in the
125following sections. ``PluginMain.cpp`` is just a helper file used to
126compile the auto-generated C++ code produced from TableGen source. It
127can also contain hook definitions (see `below`__).
128
129__ hooks_
130
131The first thing that you should do is to change the ``LLVMC_PLUGIN``
132variable in the ``Makefile`` to avoid conflicts (since this variable
133is used to name the resulting library)::
134
135 LLVMC_PLUGIN=MyPlugin
136
137It is also a good idea to rename ``Simple.td`` to something less
138generic::
139
140 $ mv Simple.td MyPlugin.td
141
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000142Note that the plugin source directory must be placed under
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000143``$LLVMC_DIR/plugins`` to make use of the existing build
144infrastructure. To build a version of the LLVMC executable called
145``mydriver`` with your plugin compiled in, use the following command::
146
147 $ cd $LLVMC_DIR
148 $ make BUILTIN_PLUGINS=MyPlugin DRIVER_NAME=mydriver
149
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000150To build your plugin as a dynamic library, just ``cd`` to its source
151directory and run ``make``. The resulting file will be called
152``LLVMC$(LLVMC_PLUGIN).$(DLL_EXTENSION)`` (in our case,
153``LLVMCMyPlugin.so``). This library can be then loaded in with the
154``-load`` option. Example::
155
156 $ cd $LLVMC_DIR/plugins/Simple
157 $ make
Mikhail Glushenkov113ec352008-11-25 21:38:12 +0000158 $ llvmc -load $LLVM_DIR/Release/lib/LLVMCSimple.so
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000159
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000160Sometimes, you will want a 'bare-bones' version of LLVMC that has no
161built-in plugins. It can be compiled with the following command::
162
163 $ cd $LLVMC_DIR
164 $ make BUILTIN_PLUGINS=""
165
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000166
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000167Customizing LLVMC: the compilation graph
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000168========================================
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000169
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000170Each TableGen configuration file should include the common
171definitions::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000172
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000173 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000174
175Internally, LLVMC stores information about possible source
176transformations in form of a graph. Nodes in this graph represent
177tools, and edges between two nodes represent a transformation path. A
178special "root" node is used to mark entry points for the
179transformations. LLVMC also assigns a weight to each edge (more on
180this later) to choose between several alternative edges.
181
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000182The definition of the compilation graph (see file
183``plugins/Base/Base.td`` for an example) is just a list of edges::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000184
185 def CompilationGraph : CompilationGraph<[
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000186 Edge<"root", "llvm_gcc_c">,
187 Edge<"root", "llvm_gcc_assembler">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000188 ...
189
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000190 Edge<"llvm_gcc_c", "llc">,
191 Edge<"llvm_gcc_cpp", "llc">,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000192 ...
193
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000194 OptionalEdge<"llvm_gcc_c", "opt", (case (switch_on "opt"),
195 (inc_weight))>,
196 OptionalEdge<"llvm_gcc_cpp", "opt", (case (switch_on "opt"),
197 (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000198 ...
199
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000200 OptionalEdge<"llvm_gcc_assembler", "llvm_gcc_cpp_linker",
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000201 (case (input_languages_contain "c++"), (inc_weight),
202 (or (parameter_equals "linker", "g++"),
203 (parameter_equals "linker", "c++")), (inc_weight))>,
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000204 ...
205
206 ]>;
207
208As you can see, the edges can be either default or optional, where
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000209optional edges are differentiated by an additional ``case`` expression
Mikhail Glushenkov01088772008-11-17 17:29:18 +0000210used to calculate the weight of this edge. Notice also that we refer
Mikhail Glushenkovf80f0aa2008-11-25 21:34:01 +0000211to tools via their names (as strings). This makes it possible to add
212edges to an existing compilation graph in plugins without having to
213know about all tool definitions used in the graph.
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000214
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000215The default edges are assigned a weight of 1, and optional edges get a
216weight of 0 + 2*N where N is the number of tests that evaluated to
217true in the ``case`` expression. It is also possible to provide an
218integer parameter to ``inc_weight`` and ``dec_weight`` - in this case,
219the weight is increased (or decreased) by the provided value instead
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000220of the default 2. It is also possible to change the default weight of
221an optional edge by using the ``default`` clause of the ``case``
222construct.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000223
224When passing an input file through the graph, LLVMC picks the edge
225with the maximum weight. To avoid ambiguity, there should be only one
226default edge between two nodes (with the exception of the root node,
227which gets a special treatment - there you are allowed to specify one
228default edge *per language*).
229
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000230When multiple plugins are loaded, their compilation graphs are merged
Mikhail Glushenkov3321b0f2008-11-28 00:12:09 +0000231together. Since multiple edges that have the same end nodes are not
232allowed (i.e. the graph is not a multigraph), an edge defined in
Mikhail Glushenkov7e6d70a2008-11-26 22:59:45 +0000233several plugins will be replaced by the definition from the plugin
234that was loaded last. Plugin load order can be controlled by using the
235plugin priority feature described above.
236
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000237To get a visual representation of the compilation graph (useful for
Mikhail Glushenkov113ec352008-11-25 21:38:12 +0000238debugging), run ``llvmc --view-graph``. You will need ``dot`` and
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000239``gsview`` installed for this to work properly.
240
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000241Describing options
242==================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000243
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000244Command-line options that the plugin supports are defined by using an
245``OptionList``::
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000246
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000247 def Options : OptionList<[
248 (switch_option "E", (help "Help string")),
249 (alias_option "quiet", "q")
250 ...
251 ]>;
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000252
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000253As you can see, the option list is just a list of DAGs, where each DAG
254is an option description consisting of the option name and some
255properties. A plugin can define more than one option list (they are
256all merged together in the end), which can be handy if one wants to
257separate option groups syntactically.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000258
259* Possible option types:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000260
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000261 - ``switch_option`` - a simple boolean switch, for example ``-time``.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000262
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000263 - ``parameter_option`` - option that takes an argument, for example
264 ``-std=c99``;
265
266 - ``parameter_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000267 occurence of the option is allowed.
268
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000269 - ``prefix_option`` - same as the parameter_option, but the option name
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000270 and parameter value are not separated.
271
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000272 - ``prefix_list_option`` - same as the above, but more than one
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000273 occurence of the option is allowed; example: ``-lm -lpthread``.
274
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000275 - ``alias_option`` - a special option type for creating
276 aliases. Unlike other option types, aliases are not allowed to
277 have any properties besides the aliased option name. Usage
278 example: ``(alias_option "preprocess", "E")``
279
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000280
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000281* Possible option properties:
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000282
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000283 - ``help`` - help string associated with this option. Used for
284 ``--help`` output.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000285
Mikhail Glushenkov77ddce92008-05-06 18:17:19 +0000286 - ``required`` - this option is obligatory.
287
Mikhail Glushenkov739c7202008-11-28 00:13:25 +0000288 - ``hidden`` - this option should not appear in the ``--help``
289 output (but should appear in the ``--help-hidden`` output).
290
291 - ``really_hidden`` - the option should not appear in any help
292 output.
293
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000294 - ``extern`` - this option is defined in some other plugin, see below.
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000295
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000296External options
297----------------
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000298
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000299Sometimes, when linking several plugins together, one plugin needs to
300access options defined in some other plugin. Because of the way
301options are implemented, such options should be marked as
302``extern``. This is what the ``extern`` option property is
303for. Example::
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000304
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000305 ...
306 (switch_option "E", (extern))
307 ...
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000308
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000309See also the section on plugin `priorities`__.
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000310
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000311__ priorities_
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000312
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000313.. _case:
Mikhail Glushenkov83237482008-10-15 09:29:13 +0000314
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000315Conditional evaluation
316======================
Mikhail Glushenkov0ab8ac32008-05-30 06:28:00 +0000317
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000318The 'case' construct is the main means by which programmability is
319achieved in LLVMC. It can be used to calculate edge weights, program
320actions and modify the shell commands to be executed. The 'case'
321expression is designed after the similarly-named construct in
322functional languages and takes the form ``(case (test_1), statement_1,
323(test_2), statement_2, ... (test_N), statement_N)``. The statements
324are evaluated only if the corresponding tests evaluate to true.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000325
326Examples::
327
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000328 // Edge weight calculation
329
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000330 // Increases edge weight by 5 if "-A" is provided on the
331 // command-line, and by 5 more if "-B" is also provided.
332 (case
333 (switch_on "A"), (inc_weight 5),
334 (switch_on "B"), (inc_weight 5))
335
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000336
337 // Tool command line specification
338
339 // Evaluates to "cmdline1" if the option "-A" is provided on the
340 // command line; to "cmdline2" if "-B" is provided;
341 // otherwise to "cmdline3".
342
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000343 (case
344 (switch_on "A"), "cmdline1",
345 (switch_on "B"), "cmdline2",
346 (default), "cmdline3")
347
348Note the slight difference in 'case' expression handling in contexts
349of edge weights and command line specification - in the second example
350the value of the ``"B"`` switch is never checked when switch ``"A"`` is
351enabled, and the whole expression always evaluates to ``"cmdline1"`` in
352that case.
353
354Case expressions can also be nested, i.e. the following is legal::
355
356 (case (switch_on "E"), (case (switch_on "o"), ..., (default), ...)
357 (default), ...)
358
359You should, however, try to avoid doing that because it hurts
360readability. It is usually better to split tool descriptions and/or
361use TableGen inheritance instead.
362
363* Possible tests are:
364
Mikhail Glushenkov536637f2008-11-25 21:34:53 +0000365 - ``switch_on`` - Returns true if a given command-line switch is
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000366 provided by the user. Example: ``(switch_on "opt")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000367
368 - ``parameter_equals`` - Returns true if a command-line parameter equals
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000369 a given value.
370 Example: ``(parameter_equals "W", "all")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000371
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000372 - ``element_in_list`` - Returns true if a command-line parameter
373 list contains a given value.
374 Example: ``(parameter_in_list "l", "pthread")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000375
376 - ``input_languages_contain`` - Returns true if a given language
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000377 belongs to the current input language set.
378 Example: ``(input_languages_contain "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000379
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000380 - ``in_language`` - Evaluates to true if the input file language
381 equals to the argument. At the moment works only with ``cmd_line``
382 and ``actions`` (on non-join nodes).
383 Example: ``(in_language "c++")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000384
385 - ``not_empty`` - Returns true if a given option (which should be
386 either a parameter or a parameter list) is set by the
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000387 user.
388 Example: ``(not_empty "o")``.
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000389
390 - ``default`` - Always evaluates to true. Should always be the last
391 test in the ``case`` expression.
392
393 - ``and`` - A standard logical combinator that returns true iff all
394 of its arguments return true. Used like this: ``(and (test1),
395 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
396 but not encouraged.
397
398 - ``or`` - Another logical combinator that returns true only if any
399 one of its arguments returns true. Example: ``(or (test1),
400 (test2), ... (testN))``.
401
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000402
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000403Writing a tool description
404==========================
405
406As was said earlier, nodes in the compilation graph represent tools,
407which are described separately. A tool definition looks like this
408(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
409
410 def llvm_gcc_cpp : Tool<[
411 (in_language "c++"),
412 (out_language "llvm-assembler"),
413 (output_suffix "bc"),
414 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
415 (sink)
416 ]>;
417
418This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
419``llvm-g++``. As you can see, a tool definition is just a list of
420properties; most of them should be self-explanatory. The ``sink``
421property means that this tool should be passed all command-line
422options that aren't mentioned in the option list.
423
424The complete list of all currently implemented tool properties follows.
425
426* Possible tool properties:
427
428 - ``in_language`` - input language name. Can be either a string or a
429 list, in case the tool supports multiple input languages.
430
431 - ``out_language`` - output language name. Tools are not allowed to
432 have multiple output languages.
433
434 - ``output_suffix`` - output file suffix. Can also be changed
435 dynamically, see documentation on actions.
436
437 - ``cmd_line`` - the actual command used to run the tool. You can
438 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
439 with ``>``, hook invocations (``$CALL``), environment variables
440 (via ``$ENV``) and the ``case`` construct.
441
442 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
443 list of input files and joins them together. Used for linkers.
444
445 - ``sink`` - all command-line options that are not handled by other
446 tools are passed to this tool.
447
448 - ``actions`` - A single big ``case`` expression that specifies how
449 this tool reacts on command-line options (described in more detail
450 below).
451
452Actions
453-------
454
455A tool often needs to react to command-line options, and this is
456precisely what the ``actions`` property is for. The next example
457illustrates this feature::
458
459 def llvm_gcc_linker : Tool<[
460 (in_language "object-code"),
461 (out_language "executable"),
462 (output_suffix "out"),
463 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
464 (join),
465 (actions (case (not_empty "L"), (forward "L"),
466 (not_empty "l"), (forward "l"),
467 (not_empty "dummy"),
468 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
469 ]>;
470
471The ``actions`` tool property is implemented on top of the omnipresent
472``case`` expression. It associates one or more different *actions*
473with given conditions - in the example, the actions are ``forward``,
474which forwards a given option unchanged, and ``append_cmd``, which
475appends a given string to the tool execution command. Multiple actions
476can be associated with a single condition by using a list of actions
477(used in the example to append some dummy options). The same ``case``
478construct can also be used in the ``cmd_line`` property to modify the
479tool command line.
480
481The "join" property used in the example means that this tool behaves
482like a linker.
483
484The list of all possible actions follows.
485
486* Possible actions:
487
488 - ``append_cmd`` - append a string to the tool invocation
489 command.
490 Example: ``(case (switch_on "pthread"), (append_cmd "-lpthread"))``
491
492 - ``forward`` - forward an option unchanged.
493 Example: ``(forward "Wall")``.
494
495 - ``forward_as`` - Change the name of an option, but forward the
496 argument unchanged.
497 Example: ``(forward_as "O0" "--disable-optimization")``.
498
499 - ``output_suffix`` - modify the output suffix of this
500 tool.
501 Example: ``(output_suffix "i")``.
502
503 - ``stop_compilation`` - stop compilation after this tool processes
504 its input. Used without arguments.
505
506 - ``unpack_values`` - used for for splitting and forwarding
507 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
508 converted to ``-foo=bar -baz`` and appended to the tool invocation
509 command.
510 Example: ``(unpack_values "Wa,")``.
511
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000512Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000513============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000514
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000515If you are adding support for a new language to LLVMC, you'll need to
516modify the language map, which defines mappings from file extensions
517to language names. It is used to choose the proper toolchain(s) for a
518given input file set. Language map definition looks like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000519
520 def LanguageMap : LanguageMap<
521 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
522 LangToSuffixes<"c", ["c"]>,
523 ...
524 ]>;
525
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000526For example, without those definitions the following command wouldn't work::
527
528 $ llvmc hello.cpp
529 llvmc: Unknown suffix: cpp
530
531The language map entries should be added only for tools that are
532linked with the root node. Since tools are not allowed to have
533multiple output languages, for nodes "inside" the graph the input and
534output languages should match. This is enforced at compile-time.
535
536
537More advanced topics
538====================
539
540.. _hooks:
541
542Hooks and environment variables
543-------------------------------
544
545Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
546this is not sufficient: for example, we may want to specify tool names
547in the configuration file. This can be achieved via the mechanism of
548hooks - to write your own hooks, just add their definitions to the
549``PluginMain.cpp`` or drop a ``.cpp`` file into the
550``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
551namespace and have the signature ``std::string hooks::MyHookName
552(void)``. They can be used from the ``cmd_line`` tool property::
553
554 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
555
556It is also possible to use environment variables in the same manner::
557
558 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
559
560To change the command line string based on user-provided options use
561the ``case`` expression (documented `above`__)::
562
563 (cmd_line
564 (case
565 (switch_on "E"),
566 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
567 (default),
568 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
569
570__ case_
571
572.. _priorities:
573
574How plugins are loaded
575----------------------
576
577It is possible for LLVMC plugins to depend on each other. For example,
578one can create edges between nodes defined in some other plugin. To
579make this work, however, that plugin should be loaded first. To
580achieve this, the concept of plugin priority was introduced. By
581default, every plugin has priority zero; to specify the priority
582explicitly, put the following line in your plugin's TableGen file::
583
584 def Priority : PluginPriority<$PRIORITY_VALUE>;
585 # Where PRIORITY_VALUE is some integer > 0
586
587Plugins are loaded in order of their (increasing) priority, starting
588with 0. Therefore, the plugin with the highest priority value will be
589loaded last.
590
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000591Debugging
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000592---------
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000593
594When writing LLVMC plugins, it can be useful to get a visual view of
595the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000596line option ``--view-graph``. This command assumes that Graphviz_ and
597Ghostview_ are installed. There is also a ``--dump-graph`` option that
598creates a Graphviz source file (``compilation-graph.dot``) in the
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000599current directory.
600
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000601.. _Graphviz: http://www.graphviz.org/
602.. _Ghostview: http://pages.cs.wisc.edu/~ghost/
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000603
604.. raw:: html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000605
606 <hr />
607 <address>
608 <a href="http://jigsaw.w3.org/css-validator/check/referer">
609 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
610 alt="Valid CSS" /></a>
611 <a href="http://validator.w3.org/check?uri=referer">
612 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
613 alt="Valid XHTML 1.0 Transitional"/></a>
614
615 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
616 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
617
618 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
619 </address>