blob: 40292ee6271acecc26a49c41e48eaa3c4dfb3a78 [file] [log] [blame]
Mikhail Glushenkov270cae32008-05-30 06:25:24 +00001======================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00002Tutorial - Using LLVMC
3======================
Mikhail Glushenkov536637f2008-11-25 21:34:53 +00004:Author: Mikhail Glushenkov <foldr@codedegers.com>
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +00005
6LLVMC is a generic compiler driver, which plays the same role for LLVM
7as the ``gcc`` program does for GCC - the difference being that LLVMC
Mikhail Glushenkov83237482008-10-15 09:29:13 +00008is designed to be more adaptable and easier to customize. Most of
9LLVMC functionality is implemented via plugins, which can be loaded
10dynamically or compiled in. This tutorial describes the basic usage
11and configuration of LLVMC.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000012
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000013
14.. contents::
15
16
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000017Compiling with LLVMC
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000018====================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000019
20In general, LLVMC tries to be command-line compatible with ``gcc`` as
21much as possible, so most of the familiar options work::
22
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000023 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000024 $ ./a.out
25 hello
26
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000027This will invoke ``llvm-g++`` under the hood (you can see which
28commands are executed by using the ``-v`` option). For further help on
29command-line LLVMC usage, refer to the ``llvmc --help`` output.
30
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000031
32Using LLVMC to generate toolchain drivers
Mikhail Glushenkov270cae32008-05-30 06:25:24 +000033=========================================
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000034
Mikhail Glushenkov83237482008-10-15 09:29:13 +000035LLVMC plugins are written mostly using TableGen [1]_, so you need to
36be familiar with it to get anything done.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000037
Mikhail Glushenkov83237482008-10-15 09:29:13 +000038Start by compiling ``plugins/Simple/Simple.td``, which is a primitive
39wrapper for ``gcc``::
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000040
Mikhail Glushenkov113ec352008-11-25 21:38:12 +000041 $ cd $LLVM_DIR/tools/llvmc
Mikhail Glushenkov83237482008-10-15 09:29:13 +000042 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
43 $ cat > hello.c
44 [...]
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000045 $ mygcc hello.c
46 $ ./hello.out
47 Hello
48
Mikhail Glushenkov83237482008-10-15 09:29:13 +000049Here we link our plugin with the LLVMC core statically to form an
50executable file called ``mygcc``. It is also possible to build our
51plugin as a standalone dynamic library; this is described in the
52reference manual.
53
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000054Contents of the file ``Simple.td`` look like this::
55
56 // Include common definitions
Mikhail Glushenkovebdeca72008-11-25 21:34:29 +000057 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000058
59 // Tool descriptions
60 def gcc : Tool<
61 [(in_language "c"),
62 (out_language "executable"),
63 (output_suffix "out"),
64 (cmd_line "gcc $INFILE -o $OUTFILE"),
65 (sink)
66 ]>;
67
68 // Language map
69 def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>;
70
71 // Compilation graph
Mikhail Glushenkov01088772008-11-17 17:29:18 +000072 def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>;
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000073
74As you can see, this file consists of three parts: tool descriptions,
75language map, and the compilation graph definition.
76
Mikhail Glushenkov83237482008-10-15 09:29:13 +000077At the heart of LLVMC is the idea of a compilation graph: vertices in
78this graph are tools, and edges represent a transformation path
Mikhail Glushenkov5ccf28f2008-09-22 20:45:17 +000079between two tools (for example, assembly source produced by the
Mikhail Glushenkov83237482008-10-15 09:29:13 +000080compiler can be transformed into executable code by an assembler). The
81compilation graph is basically a list of edges; a special node named
82``root`` is used to mark graph entry points.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000083
Mikhail Glushenkov83237482008-10-15 09:29:13 +000084Tool descriptions are represented as property lists: most properties
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000085in the example above should be self-explanatory; the ``sink`` property
86means that all options lacking an explicit description should be
87forwarded to this tool.
88
Mikhail Glushenkov83237482008-10-15 09:29:13 +000089The ``LanguageMap`` associates a language name with a list of suffixes
90and is used for deciding which toolchain corresponds to a given input
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000091file.
92
93To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkov83237482008-10-15 09:29:13 +000094manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkovcd0858e2008-05-30 06:14:42 +000095
96References
97==========
98
99.. [1] TableGen Fundamentals
100 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html
Mikhail Glushenkov68319f82008-12-11 23:24:40 +0000101
102.. raw:: html
103 :file: footer.html