Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 1 | ====================== |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 2 | Tutorial - Using LLVMC |
| 3 | ====================== |
Mikhail Glushenkov | 23f522a | 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 | d565203 | 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 | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 19 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 20 | LLVMC is a generic compiler driver, which plays the same role for LLVM as the |
| 21 | ``gcc`` program does for GCC - the difference being that LLVMC is designed to be |
| 22 | more adaptable and easier to customize. Most of LLVMC functionality is |
| 23 | implemented via high-level TableGen code, from which a corresponding C++ source |
| 24 | file is automatically generated. This tutorial describes the basic usage and |
| 25 | configuration of LLVMC. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 26 | |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 27 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 28 | Using the ``llvmc`` program |
| 29 | =========================== |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 30 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 31 | In general, ``llvmc`` tries to be command-line compatible with ``gcc`` as much |
| 32 | as possible, so most of the familiar options work:: |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 33 | |
Mikhail Glushenkov | 113ec35 | 2008-11-25 21:38:12 +0000 | [diff] [blame] | 34 | $ llvmc -O3 -Wall hello.cpp |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 35 | $ ./a.out |
| 36 | hello |
| 37 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 38 | This will invoke ``llvm-g++`` under the hood (you can see which commands are |
| 39 | executed by using the ``-v`` option). For further help on command-line LLVMC |
| 40 | usage, refer to the ``llvmc --help`` output. |
Mikhail Glushenkov | ebdeca7 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 41 | |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 42 | |
| 43 | Using LLVMC to generate toolchain drivers |
Mikhail Glushenkov | 270cae3 | 2008-05-30 06:25:24 +0000 | [diff] [blame] | 44 | ========================================= |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 45 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 46 | LLVMC-based drivers are written mostly using TableGen_, so you need to be |
| 47 | familiar with it to get anything done. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 48 | |
Mikhail Glushenkov | 4aecec1 | 2009-06-17 02:56:08 +0000 | [diff] [blame] | 49 | .. _TableGen: http://llvm.org/docs/TableGenFundamentals.html |
Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 50 | |
Mikhail Glushenkov | 530f399 | 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 | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 53 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 54 | $ cd $LLVM_OBJ_DIR/tools/examples/Simple |
| 55 | $ make |
Mikhail Glushenkov | 8323748 | 2008-10-15 09:29:13 +0000 | [diff] [blame] | 56 | $ cat > hello.c |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 57 | #include <stdio.h> |
| 58 | int main() { printf("Hello\n"); } |
| 59 | $ $LLVM_BIN_DIR/Simple -v hello.c |
| 60 | gcc hello.c -o hello.out |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 61 | $ ./hello.out |
| 62 | Hello |
| 63 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 64 | We have thus produced a simple driver called, appropriately, ``Simple``, from |
| 65 | the input TableGen file ``Simple.td``. The ``llvmc`` program itself is generated |
| 66 | using a similar process (see ``llvmc/src``). Contents of the file ``Simple.td`` |
| 67 | look like this:: |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 68 | |
| 69 | // Include common definitions |
Mikhail Glushenkov | ebdeca7 | 2008-11-25 21:34:29 +0000 | [diff] [blame] | 70 | include "llvm/CompilerDriver/Common.td" |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 71 | |
| 72 | // Tool descriptions |
| 73 | def gcc : Tool< |
| 74 | [(in_language "c"), |
| 75 | (out_language "executable"), |
| 76 | (output_suffix "out"), |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 77 | (command "gcc"), |
| 78 | (sink), |
| 79 | |
| 80 | // -o is what is used by default, out_file_option here is included for |
| 81 | // instructive purposes. |
| 82 | (out_file_option "-o") |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 83 | ]>; |
| 84 | |
| 85 | // Language map |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 86 | def LanguageMap : LanguageMap<[(lang_to_suffixes "c", "c")]>; |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 87 | |
| 88 | // Compilation graph |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 89 | def CompilationGraph : CompilationGraph<[(edge "root", "gcc")]>; |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 90 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 91 | As you can see, this file consists of three parts: tool descriptions, language |
| 92 | map, and the compilation graph definition. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 93 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 94 | At the heart of LLVMC is the idea of a compilation graph: vertices in this graph |
| 95 | are tools, and edges represent a transformation path between two tools (for |
| 96 | example, assembly source produced by the compiler can be transformed into |
| 97 | executable code by an assembler). The compilation graph is basically a list of |
| 98 | edges; a special node named ``root`` is used to mark graph entry points. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 99 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 100 | Tool descriptions are represented as property lists: most properties in the |
| 101 | example above should be self-explanatory; the ``sink`` property means that all |
| 102 | options lacking an explicit description should be forwarded to this tool. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 103 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 104 | The ``LanguageMap`` associates a language name with a list of suffixes and is |
| 105 | used for deciding which toolchain corresponds to a given input file. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 106 | |
Mikhail Glushenkov | 834b93b | 2011-04-24 14:17:32 +0000 | [diff] [blame^] | 107 | To learn more about writing your own drivers with LLVMC, refer to the reference |
| 108 | manual and examples in the ``examples`` directory. Of a particular interest is |
| 109 | the ``Skeleton`` example, which can serve as a template for your LLVMC-based |
| 110 | drivers. |
Mikhail Glushenkov | cd0858e | 2008-05-30 06:14:42 +0000 | [diff] [blame] | 111 | |
Mikhail Glushenkov | 68319f8 | 2008-12-11 23:24:40 +0000 | [diff] [blame] | 112 | .. raw:: html |
Mikhail Glushenkov | d565203 | 2008-12-13 02:28:58 +0000 | [diff] [blame] | 113 | |
| 114 | <hr /> |
| 115 | <address> |
| 116 | <a href="http://jigsaw.w3.org/css-validator/check/referer"> |
| 117 | <img src="http://jigsaw.w3.org/css-validator/images/vcss-blue" |
| 118 | alt="Valid CSS" /></a> |
| 119 | <a href="http://validator.w3.org/check?uri=referer"> |
| 120 | <img src="http://www.w3.org/Icons/valid-xhtml10-blue" |
| 121 | alt="Valid XHTML 1.0 Transitional"/></a> |
| 122 | |
| 123 | <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br /> |
| 124 | <a href="http://llvm.org">LLVM Compiler Infrastructure</a><br /> |
| 125 | |
| 126 | Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $ |
| 127 | </address> |