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 | b7677be | 2008-12-13 17:51:47 +0000 | [diff] [blame] | 4 | .. |
| 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 Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 8 | |
| 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 | |
| 17 | Introduction |
| 18 | ============ |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 19 | |
| 20 | LLVMC is a generic compiler driver, which plays the same role for LLVM |
| 21 | as the ``gcc`` program does for GCC - the difference being that LLVMC |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 22 | is designed to be more adaptable and easier to customize. Most of |
| 23 | LLVMC functionality is implemented via plugins, which can be loaded |
| 24 | dynamically or compiled in. This tutorial describes the basic usage |
| 25 | and configuration of LLVMC. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 26 | |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 27 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 28 | Compiling with LLVMC |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 29 | ==================== |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 30 | |
| 31 | In general, LLVMC tries to be command-line compatible with ``gcc`` as |
| 32 | much as possible, so most of the familiar options work:: |
| 33 | |
Mikhail Glushenkov | c7e56fe | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 34 | $ llvmc -O3 -Wall hello.cpp |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 35 | $ ./a.out |
| 36 | hello |
| 37 | |
Mikhail Glushenkov | 99f1064 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 38 | This will invoke ``llvm-g++`` under the hood (you can see which |
| 39 | commands are executed by using the ``-v`` option). For further help on |
| 40 | command-line LLVMC usage, refer to the ``llvmc --help`` output. |
| 41 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 42 | |
| 43 | Using LLVMC to generate toolchain drivers |
Mikhail Glushenkov | 772d9c9 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 44 | ========================================= |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 45 | |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 46 | LLVMC plugins are written mostly using TableGen_, so you need to |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 47 | be familiar with it to get anything done. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 48 | |
Mikhail Glushenkov | b3e4196 | 2009-06-17 02:56:08 +0000 | [diff] [blame^] | 49 | .. _TableGen: http://llvm.org/docs/TableGenFundamentals.html |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 50 | |
Mikhail Glushenkov | bc2a3d3 | 2009-06-16 00:13:52 +0000 | [diff] [blame] | 51 | Start by compiling ``example/Simple``, which is a primitive wrapper for |
| 52 | ``gcc``:: |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 53 | |
Mikhail Glushenkov | c7e56fe | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 54 | $ cd $LLVM_DIR/tools/llvmc |
Mikhail Glushenkov | b3e4196 | 2009-06-17 02:56:08 +0000 | [diff] [blame^] | 55 | $ cp -r example/Simple plugins/Simple |
Mikhail Glushenkov | bc2a3d3 | 2009-06-16 00:13:52 +0000 | [diff] [blame] | 56 | |
| 57 | # NB: A less verbose way to compile standalone LLVMC-based drivers is |
| 58 | # described in the reference manual. |
| 59 | |
| 60 | $ make LLVMC_BASED_DRIVER_NAME=mygcc LLVMC_BUILTIN_PLUGINS=Simple |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 61 | $ cat > hello.c |
| 62 | [...] |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 63 | $ mygcc hello.c |
| 64 | $ ./hello.out |
| 65 | Hello |
| 66 | |
Mikhail Glushenkov | bc2a3d3 | 2009-06-16 00:13:52 +0000 | [diff] [blame] | 67 | Here we link our plugin with the LLVMC core statically to form an executable |
| 68 | file called ``mygcc``. It is also possible to build our plugin as a dynamic |
| 69 | library to be loaded by the ``llvmc`` executable (or any other LLVMC-based |
| 70 | standalone driver); this is described in the reference manual. |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 71 | |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 72 | Contents of the file ``Simple.td`` look like this:: |
| 73 | |
| 74 | // Include common definitions |
Mikhail Glushenkov | 99f1064 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 75 | include "llvm/CompilerDriver/Common.td" |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 76 | |
| 77 | // Tool descriptions |
| 78 | def gcc : Tool< |
| 79 | [(in_language "c"), |
| 80 | (out_language "executable"), |
| 81 | (output_suffix "out"), |
| 82 | (cmd_line "gcc $INFILE -o $OUTFILE"), |
| 83 | (sink) |
| 84 | ]>; |
| 85 | |
| 86 | // Language map |
| 87 | def LanguageMap : LanguageMap<[LangToSuffixes<"c", ["c"]>]>; |
| 88 | |
| 89 | // Compilation graph |
Mikhail Glushenkov | fa99068 | 2008-11-17 17:29:18 +0000 | [diff] [blame] | 90 | def CompilationGraph : CompilationGraph<[Edge<"root", "gcc">]>; |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 91 | |
| 92 | As you can see, this file consists of three parts: tool descriptions, |
| 93 | language map, and the compilation graph definition. |
| 94 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 95 | At the heart of LLVMC is the idea of a compilation graph: vertices in |
| 96 | this graph are tools, and edges represent a transformation path |
Mikhail Glushenkov | c178ead | 2008-09-22 20:45:17 +0000 | [diff] [blame] | 97 | between two tools (for example, assembly source produced by the |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 98 | compiler can be transformed into executable code by an assembler). The |
| 99 | compilation graph is basically a list of edges; a special node named |
| 100 | ``root`` is used to mark graph entry points. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 101 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 102 | Tool descriptions are represented as property lists: most properties |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 103 | in the example above should be self-explanatory; the ``sink`` property |
| 104 | means that all options lacking an explicit description should be |
| 105 | forwarded to this tool. |
| 106 | |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 107 | The ``LanguageMap`` associates a language name with a list of suffixes |
| 108 | and is used for deciding which toolchain corresponds to a given input |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 109 | file. |
| 110 | |
| 111 | To learn more about LLVMC customization, refer to the reference |
Mikhail Glushenkov | bd51c23 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 112 | manual and plugin source code in the ``plugins`` directory. |
Mikhail Glushenkov | 1ce8722 | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 113 | |
Mikhail Glushenkov | ac251f2 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 114 | .. raw:: html |
Mikhail Glushenkov | 6d1e928 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 115 | |
| 116 | <hr /> |
| 117 | <address> |
| 118 | <a href="http://jigsaw.w3.org/css-validator/check/referer"> |
| 119 | <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" |
| 120 | alt="Valid CSS" /></a> |
| 121 | <a href="http://validator.w3.org/check?uri=referer"> |
| 122 | <img src="http://www.w3.org/Icons/valid-xhtml10-blue" |
| 123 | alt="Valid XHTML 1.0 Transitional"/></a> |
| 124 | |
| 125 | <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br /> |
| 126 | <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br /> |
| 127 | |
| 128 | Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $ |
| 129 | </address> |