Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 1 | ====================== |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 2 | Tutorial - Using LLVMC |
| 3 | ====================== |
Mikhail Glushenkov | 817b2f4 | 2008-11-25 21:34:53 +0000 | [diff] [blame] | 4 | :Author: Mikhail Glushenkov <foldr@codedegers.com> |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 5 | |
| 6 | LLVMC is a generic compiler driver, which plays the same role for LLVM |
| 7 | as the ``gcc`` program does for GCC - the difference being that LLVMC |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 8 | is designed to be more adaptable and easier to customize. Most of |
| 9 | LLVMC functionality is implemented via plugins, which can be loaded |
| 10 | dynamically or compiled in. This tutorial describes the basic usage |
| 11 | and configuration of LLVMC. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 12 | |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 13 | |
| 14 | .. contents:: |
| 15 | |
| 16 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 17 | Compiling with LLVMC |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 18 | ==================== |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 19 | |
| 20 | In general, LLVMC tries to be command-line compatible with ``gcc`` as |
| 21 | much as possible, so most of the familiar options work:: |
| 22 | |
Mikhail Glushenkov | c7e56fe | 2008-11-25 21:38:12 +0000 | [diff] [blame^] | 23 | $ llvmc -O3 -Wall hello.cpp |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 24 | $ ./a.out |
| 25 | hello |
| 26 | |
Mikhail Glushenkov | 99f1064 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 27 | This will invoke ``llvm-g++`` under the hood (you can see which |
| 28 | commands are executed by using the ``-v`` option). For further help on |
| 29 | command-line LLVMC usage, refer to the ``llvmc --help`` output. |
| 30 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 31 | |
| 32 | Using LLVMC to generate toolchain drivers |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 33 | ========================================= |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 34 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 35 | LLVMC plugins are written mostly using TableGen [1]_, so you need to |
| 36 | be familiar with it to get anything done. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 37 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 38 | Start by compiling ``plugins/Simple/Simple.td``, which is a primitive |
| 39 | wrapper for ``gcc``:: |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 40 | |
Mikhail Glushenkov | c7e56fe | 2008-11-25 21:38:12 +0000 | [diff] [blame^] | 41 | $ cd $LLVM_DIR/tools/llvmc |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 42 | $ make DRIVER_NAME=mygcc BUILTIN_PLUGINS=Simple |
| 43 | $ cat > hello.c |
| 44 | [...] |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 45 | $ mygcc hello.c |
| 46 | $ ./hello.out |
| 47 | Hello |
| 48 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 49 | Here we link our plugin with the LLVMC core statically to form an |
| 50 | executable file called ``mygcc``. It is also possible to build our |
| 51 | plugin as a standalone dynamic library; this is described in the |
| 52 | reference manual. |
| 53 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 54 | Contents of the file ``Simple.td`` look like this:: |
| 55 | |
| 56 | // Include common definitions |
Mikhail Glushenkov | 99f1064 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 57 | include "llvm/CompilerDriver/Common.td" |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 58 | |
| 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 Glushenkov | fa99068 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 72 | def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>; |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 73 | |
| 74 | As you can see, this file consists of three parts: tool descriptions, |
| 75 | language map, and the compilation graph definition. |
| 76 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 77 | At the heart of LLVMC is the idea of a compilation graph: vertices in |
| 78 | this graph are tools, and edges represent a transformation path |
Mikhail Glushenkov | c178ead | 2008-09-22 20:45:17 +0000 | [diff] [blame] | 79 | between two tools (for example, assembly source produced by the |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 80 | compiler can be transformed into executable code by an assembler). The |
| 81 | compilation graph is basically a list of edges; a special node named |
| 82 | ``root`` is used to mark graph entry points. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 83 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 84 | Tool descriptions are represented as property lists: most properties |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 85 | in the example above should be self-explanatory; the ``sink`` property |
| 86 | means that all options lacking an explicit description should be |
| 87 | forwarded to this tool. |
| 88 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 89 | The ``LanguageMap`` associates a language name with a list of suffixes |
| 90 | and is used for deciding which toolchain corresponds to a given input |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 91 | file. |
| 92 | |
| 93 | To learn more about LLVMC customization, refer to the reference |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 94 | manual and plugin source code in the ``plugins`` directory. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 95 | |
| 96 | References |
| 97 | ========== |
| 98 | |
| 99 | .. [1] TableGen Fundamentals |
| 100 | http://llvm.cs.uiuc.edu/docs/TableGenFundamentals.html |