blob: 6189ec21828d9dc9bde23bafd2b9c012a1cda718 [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
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000390 - ``empty`` - The opposite of ``not_empty``. Equivalent to ``(not (not_empty
391 X))``. Provided for convenience.
392
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000393 - ``default`` - Always evaluates to true. Should always be the last
394 test in the ``case`` expression.
395
396 - ``and`` - A standard logical combinator that returns true iff all
397 of its arguments return true. Used like this: ``(and (test1),
398 (test2), ... (testN))``. Nesting of ``and`` and ``or`` is allowed,
399 but not encouraged.
400
401 - ``or`` - Another logical combinator that returns true only if any
402 one of its arguments returns true. Example: ``(or (test1),
403 (test2), ... (testN))``.
404
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +0000405
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000406Writing a tool description
407==========================
408
409As was said earlier, nodes in the compilation graph represent tools,
410which are described separately. A tool definition looks like this
411(taken from the ``include/llvm/CompilerDriver/Tools.td`` file)::
412
413 def llvm_gcc_cpp : Tool<[
414 (in_language "c++"),
415 (out_language "llvm-assembler"),
416 (output_suffix "bc"),
417 (cmd_line "llvm-g++ -c $INFILE -o $OUTFILE -emit-llvm"),
418 (sink)
419 ]>;
420
421This defines a new tool called ``llvm_gcc_cpp``, which is an alias for
422``llvm-g++``. As you can see, a tool definition is just a list of
423properties; most of them should be self-explanatory. The ``sink``
424property means that this tool should be passed all command-line
425options that aren't mentioned in the option list.
426
427The complete list of all currently implemented tool properties follows.
428
429* Possible tool properties:
430
431 - ``in_language`` - input language name. Can be either a string or a
432 list, in case the tool supports multiple input languages.
433
434 - ``out_language`` - output language name. Tools are not allowed to
435 have multiple output languages.
436
437 - ``output_suffix`` - output file suffix. Can also be changed
438 dynamically, see documentation on actions.
439
440 - ``cmd_line`` - the actual command used to run the tool. You can
441 use ``$INFILE`` and ``$OUTFILE`` variables, output redirection
442 with ``>``, hook invocations (``$CALL``), environment variables
443 (via ``$ENV``) and the ``case`` construct.
444
445 - ``join`` - this tool is a "join node" in the graph, i.e. it gets a
446 list of input files and joins them together. Used for linkers.
447
448 - ``sink`` - all command-line options that are not handled by other
449 tools are passed to this tool.
450
451 - ``actions`` - A single big ``case`` expression that specifies how
452 this tool reacts on command-line options (described in more detail
453 below).
454
455Actions
456-------
457
458A tool often needs to react to command-line options, and this is
459precisely what the ``actions`` property is for. The next example
460illustrates this feature::
461
462 def llvm_gcc_linker : Tool<[
463 (in_language "object-code"),
464 (out_language "executable"),
465 (output_suffix "out"),
466 (cmd_line "llvm-gcc $INFILE -o $OUTFILE"),
467 (join),
468 (actions (case (not_empty "L"), (forward "L"),
469 (not_empty "l"), (forward "l"),
470 (not_empty "dummy"),
471 [(append_cmd "-dummy1"), (append_cmd "-dummy2")])
472 ]>;
473
474The ``actions`` tool property is implemented on top of the omnipresent
475``case`` expression. It associates one or more different *actions*
476with given conditions - in the example, the actions are ``forward``,
477which forwards a given option unchanged, and ``append_cmd``, which
478appends a given string to the tool execution command. Multiple actions
479can be associated with a single condition by using a list of actions
480(used in the example to append some dummy options). The same ``case``
481construct can also be used in the ``cmd_line`` property to modify the
482tool command line.
483
484The "join" property used in the example means that this tool behaves
485like a linker.
486
487The list of all possible actions follows.
488
489* Possible actions:
490
491 - ``append_cmd`` - append a string to the tool invocation
492 command.
Mikhail Glushenkov5c2b6b22008-12-17 02:47:01 +0000493 Example: ``(case (switch_on "pthread"), (append_cmd
494 "-lpthread"))``
495
496 - ``error` - exit with error.
497 Example: ``(error "Mixing -c and -S is not allowed!")``.
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000498
499 - ``forward`` - forward an option unchanged.
500 Example: ``(forward "Wall")``.
501
502 - ``forward_as`` - Change the name of an option, but forward the
503 argument unchanged.
504 Example: ``(forward_as "O0" "--disable-optimization")``.
505
506 - ``output_suffix`` - modify the output suffix of this
507 tool.
508 Example: ``(output_suffix "i")``.
509
510 - ``stop_compilation`` - stop compilation after this tool processes
511 its input. Used without arguments.
512
513 - ``unpack_values`` - used for for splitting and forwarding
514 comma-separated lists of options, e.g. ``-Wa,-foo=bar,-baz`` is
515 converted to ``-foo=bar -baz`` and appended to the tool invocation
516 command.
517 Example: ``(unpack_values "Wa,")``.
518
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000519Language map
Mikhail Glushenkov270cae32008-05-30 06:25:24 +0000520============
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000521
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000522If you are adding support for a new language to LLVMC, you'll need to
523modify the language map, which defines mappings from file extensions
524to language names. It is used to choose the proper toolchain(s) for a
525given input file set. Language map definition looks like this::
Anton Korobeynikovac67b7e2008-03-23 08:57:20 +0000526
527 def LanguageMap : LanguageMap<
528 [LangToSuffixes<"c++", ["cc", "cp", "cxx", "cpp", "CPP", "c++", "C"]>,
529 LangToSuffixes<"c", ["c"]>,
530 ...
531 ]>;
532
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000533For example, without those definitions the following command wouldn't work::
534
535 $ llvmc hello.cpp
536 llvmc: Unknown suffix: cpp
537
538The language map entries should be added only for tools that are
539linked with the root node. Since tools are not allowed to have
540multiple output languages, for nodes "inside" the graph the input and
541output languages should match. This is enforced at compile-time.
542
543
544More advanced topics
545====================
546
547.. _hooks:
548
549Hooks and environment variables
550-------------------------------
551
552Normally, LLVMC executes programs from the system ``PATH``. Sometimes,
553this is not sufficient: for example, we may want to specify tool names
554in the configuration file. This can be achieved via the mechanism of
555hooks - to write your own hooks, just add their definitions to the
556``PluginMain.cpp`` or drop a ``.cpp`` file into the
557``$LLVMC_DIR/driver`` directory. Hooks should live in the ``hooks``
558namespace and have the signature ``std::string hooks::MyHookName
559(void)``. They can be used from the ``cmd_line`` tool property::
560
561 (cmd_line "$CALL(MyHook)/path/to/file -o $CALL(AnotherHook)")
562
563It is also possible to use environment variables in the same manner::
564
565 (cmd_line "$ENV(VAR1)/path/to/file -o $ENV(VAR2)")
566
567To change the command line string based on user-provided options use
568the ``case`` expression (documented `above`__)::
569
570 (cmd_line
571 (case
572 (switch_on "E"),
573 "llvm-g++ -E -x c $INFILE -o $OUTFILE",
574 (default),
575 "llvm-g++ -c -x c $INFILE -o $OUTFILE -emit-llvm"))
576
577__ case_
578
579.. _priorities:
580
581How plugins are loaded
582----------------------
583
584It is possible for LLVMC plugins to depend on each other. For example,
585one can create edges between nodes defined in some other plugin. To
586make this work, however, that plugin should be loaded first. To
587achieve this, the concept of plugin priority was introduced. By
588default, every plugin has priority zero; to specify the priority
589explicitly, put the following line in your plugin's TableGen file::
590
591 def Priority : PluginPriority<$PRIORITY_VALUE>;
592 # Where PRIORITY_VALUE is some integer > 0
593
594Plugins are loaded in order of their (increasing) priority, starting
595with 0. Therefore, the plugin with the highest priority value will be
596loaded last.
597
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000598Debugging
Mikhail Glushenkov4410e322008-12-07 16:47:42 +0000599---------
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000600
601When writing LLVMC plugins, it can be useful to get a visual view of
602the resulting compilation graph. This can be achieved via the command
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000603line option ``--view-graph``. This command assumes that Graphviz_ and
604Ghostview_ are installed. There is also a ``--dump-graph`` option that
605creates a Graphviz source file (``compilation-graph.dot``) in the
Mikhail Glushenkov9ecd30c2008-09-22 20:48:48 +0000606current directory.
607
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000608.. _Graphviz: http://www.graphviz.org/
609.. _Ghostview: http://pages.cs.wisc.edu/~ghost/
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000610
611.. raw:: html
Mikhail Glushenkovd5652032008-12-13 02:28:58 +0000612
613 <hr />
614 <address>
615 <a href="http://jigsaw.w3.org/css-validator/check/referer">
616 <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
617 alt="Valid CSS" /></a>
618 <a href="http://validator.w3.org/check?uri=referer">
619 <img src="http://www.w3.org/Icons/valid-xhtml10-blue"
620 alt="Valid XHTML 1.0 Transitional"/></a>
621
622 <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
623 <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
624
625 Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
626 </address>