2008-05-30 06:25:24 +00:00
|
|
|
======================
|
2008-05-30 06:14:42 +00:00
|
|
|
Tutorial - Using LLVMC
|
|
|
|
======================
|
2008-12-13 17:51:47 +00:00
|
|
|
..
|
|
|
|
This file was automatically generated by rst2html.
|
|
|
|
Please do not edit directly!
|
|
|
|
The ReST source lives in the directory 'tools/llvmc/doc'.
|
2008-12-13 02:28:58 +00:00
|
|
|
|
|
|
|
.. contents::
|
|
|
|
|
|
|
|
.. raw:: html
|
|
|
|
|
|
|
|
<div class="doc_author">
|
|
|
|
<p>Written by <a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a></p>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
Introduction
|
|
|
|
============
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
LLVMC is a generic compiler driver, which plays the same role for LLVM as the
|
|
|
|
``gcc`` program does for GCC - the difference being that LLVMC is designed to be
|
|
|
|
more adaptable and easier to customize. Most of LLVMC functionality is
|
|
|
|
implemented via high-level TableGen code, from which a corresponding C++ source
|
|
|
|
file is automatically generated. This tutorial describes the basic usage and
|
|
|
|
configuration of LLVMC.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2008-05-30 06:25:24 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
Using the ``llvmc`` program
|
|
|
|
===========================
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
In general, ``llvmc`` tries to be command-line compatible with ``gcc`` as much
|
|
|
|
as possible, so most of the familiar options work::
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2008-11-25 21:38:12 +00:00
|
|
|
$ llvmc -O3 -Wall hello.cpp
|
2008-05-30 06:14:42 +00:00
|
|
|
$ ./a.out
|
|
|
|
hello
|
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
This will invoke ``llvm-g++`` under the hood (you can see which commands are
|
|
|
|
executed by using the ``-v`` option). For further help on command-line LLVMC
|
|
|
|
usage, refer to the ``llvmc --help`` output.
|
2008-11-25 21:34:29 +00:00
|
|
|
|
2008-05-30 06:14:42 +00:00
|
|
|
|
|
|
|
Using LLVMC to generate toolchain drivers
|
2008-05-30 06:25:24 +00:00
|
|
|
=========================================
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
LLVMC-based drivers are written mostly using TableGen_, so you need to be
|
|
|
|
familiar with it to get anything done.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2009-06-17 02:56:08 +00:00
|
|
|
.. _TableGen: http://llvm.org/docs/TableGenFundamentals.html
|
2008-12-13 02:28:58 +00:00
|
|
|
|
2009-06-16 00:13:52 +00:00
|
|
|
Start by compiling ``example/Simple``, which is a primitive wrapper for
|
|
|
|
``gcc``::
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
$ cd $LLVM_OBJ_DIR/tools/examples/Simple
|
|
|
|
$ make
|
2008-10-15 09:29:13 +00:00
|
|
|
$ cat > hello.c
|
2011-04-24 14:17:32 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
int main() { printf("Hello\n"); }
|
|
|
|
$ $LLVM_BIN_DIR/Simple -v hello.c
|
|
|
|
gcc hello.c -o hello.out
|
2008-05-30 06:14:42 +00:00
|
|
|
$ ./hello.out
|
|
|
|
Hello
|
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
We have thus produced a simple driver called, appropriately, ``Simple``, from
|
|
|
|
the input TableGen file ``Simple.td``. The ``llvmc`` program itself is generated
|
|
|
|
using a similar process (see ``llvmc/src``). Contents of the file ``Simple.td``
|
|
|
|
look like this::
|
2008-05-30 06:14:42 +00:00
|
|
|
|
|
|
|
// Include common definitions
|
2008-11-25 21:34:29 +00:00
|
|
|
include "llvm/CompilerDriver/Common.td"
|
2008-05-30 06:14:42 +00:00
|
|
|
|
|
|
|
// Tool descriptions
|
|
|
|
def gcc : Tool<
|
|
|
|
[(in_language "c"),
|
|
|
|
(out_language "executable"),
|
|
|
|
(output_suffix "out"),
|
2011-04-24 14:17:32 +00:00
|
|
|
(command "gcc"),
|
|
|
|
(sink),
|
|
|
|
|
|
|
|
// -o is what is used by default, out_file_option here is included for
|
|
|
|
// instructive purposes.
|
|
|
|
(out_file_option "-o")
|
2008-05-30 06:14:42 +00:00
|
|
|
]>;
|
|
|
|
|
|
|
|
// Language map
|
2011-04-24 14:17:32 +00:00
|
|
|
def LanguageMap : LanguageMap<[(lang_to_suffixes "c", "c")]>;
|
2008-05-30 06:14:42 +00:00
|
|
|
|
|
|
|
// Compilation graph
|
2011-04-24 14:17:32 +00:00
|
|
|
def CompilationGraph : CompilationGraph<[(edge "root", "gcc")]>;
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
As you can see, this file consists of three parts: tool descriptions, language
|
|
|
|
map, and the compilation graph definition.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
At the heart of LLVMC is the idea of a compilation graph: vertices in this graph
|
|
|
|
are tools, and edges represent a transformation path between two tools (for
|
|
|
|
example, assembly source produced by the compiler can be transformed into
|
|
|
|
executable code by an assembler). The compilation graph is basically a list of
|
|
|
|
edges; a special node named ``root`` is used to mark graph entry points.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
Tool descriptions are represented as property lists: most properties in the
|
|
|
|
example above should be self-explanatory; the ``sink`` property means that all
|
|
|
|
options lacking an explicit description should be forwarded to this tool.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
The ``LanguageMap`` associates a language name with a list of suffixes and is
|
|
|
|
used for deciding which toolchain corresponds to a given input file.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2011-04-24 14:17:32 +00:00
|
|
|
To learn more about writing your own drivers with LLVMC, refer to the reference
|
|
|
|
manual and examples in the ``examples`` directory. Of a particular interest is
|
|
|
|
the ``Skeleton`` example, which can serve as a template for your LLVMC-based
|
|
|
|
drivers.
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2008-12-13 02:28:58 +00:00
|
|
|
.. raw:: html
|
2008-05-30 06:14:42 +00:00
|
|
|
|
2008-12-13 02:28:58 +00:00
|
|
|
<hr />
|
|
|
|
<address>
|
|
|
|
<a href="http://jigsaw.w3.org/css-validator/check/referer">
|
|
|
|
<img src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
|
|
|
|
alt="Valid CSS" /></a>
|
|
|
|
<a href="http://validator.w3.org/check?uri=referer">
|
|
|
|
<img src="http://www.w3.org/Icons/valid-xhtml10-blue"
|
|
|
|
alt="Valid XHTML 1.0 Transitional"/></a>
|
2008-12-11 23:24:40 +00:00
|
|
|
|
2008-12-13 02:28:58 +00:00
|
|
|
<a href="mailto:foldr@codedgers.com">Mikhail Glushenkov</a><br />
|
|
|
|
<a href="http://llvm.org">LLVM Compiler Infrastructure</a><br />
|
|
|
|
|
|
|
|
Last modified: $Date: 2008-12-11 11:34:48 -0600 (Thu, 11 Dec 2008) $
|
|
|
|
</address>
|