blob: d41f90d63490950d49f43a216e9540ddd5941ddd [file] [log] [blame]
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +00001======================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +00002Tutorial - Using LLVMC
3======================
Mikhail Glushenkov817b2f42008-11-25 21:34:53 +00004:Author: Mikhail Glushenkov <foldr@codedegers.com>
Mikhail Glushenkov1ce87222008-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 Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000012
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000013
14.. contents::
15
16
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000017Compiling with LLVMC
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000018====================
Mikhail Glushenkov1ce87222008-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 Glushenkovc7e56fe2008-11-25 21:38:12 +000023 $ llvmc -O3 -Wall hello.cpp
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000024 $ ./a.out
25 hello
26
Mikhail Glushenkov99f10642008-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 Glushenkov1ce87222008-05-30 06:14:42 +000031
32Using LLVMC to generate toolchain drivers
Mikhail Glushenkov772d9c92008-05-30 06:25:24 +000033=========================================
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000034
Mikhail Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000037
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000038Start by compiling ``plugins/Simple/Simple.td``, which is a primitive
39wrapper for ``gcc``::
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000040
Mikhail Glushenkovc7e56fe2008-11-25 21:38:12 +000041 $ cd $LLVM_DIR/tools/llvmc
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000042 $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple
43 $ cat > hello.c
44 [...]
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000045 $ mygcc hello.c
46 $ ./hello.out
47 Hello
48
Mikhail Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000054Contents of the file ``Simple.td`` look like this::
55
56 // Include common definitions
Mikhail Glushenkov99f10642008-11-25 21:34:29 +000057 include "llvm/CompilerDriver/Common.td"
Mikhail Glushenkov1ce87222008-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 Glushenkovfa990682008-11-17 17:29:18 +000072 def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>;
Mikhail Glushenkov1ce87222008-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 Glushenkovbd51c232008-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 Glushenkovc178ead2008-09-22 20:45:17 +000079between two tools (for example, assembly source produced by the
Mikhail Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000083
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000084Tool descriptions are represented as property lists: most properties
Mikhail Glushenkov1ce87222008-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 Glushenkovbd51c232008-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 Glushenkov1ce87222008-05-30 06:14:42 +000091file.
92
93To learn more about LLVMC customization, refer to the reference
Mikhail Glushenkovbd51c232008-10-15 09:29:13 +000094manual and plugin source code in the ``plugins`` directory.
Mikhail Glushenkov1ce87222008-05-30 06:14:42 +000095
96References
97==========
98
99.. [1] TableGen Fundamentals
100 http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html