aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2007-10-15-llvm-is-a-baby-killer.html
blob: 0bd16232f12a21db4a5c8b6dd4fd323a2db2e1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
---
date: "2007-10-15T05:38:29Z"
title: LLVM is a Baby Killer
---

<img src='http://pablotron.org/gallery/misc/baby_kill-small.png' 
  width='200' height='199' align='right'/>
<p>
I've spent some time sifting through the documentation for <a href="http://llvm.org/">LLVM</a>, and
I'm both impressed and horrified.  I'm not sure I've ever seen so much
documentation about something that looks really cool that I'm not
interested in.  I've been reading the <a href="http://en.wikipedia.org/wiki/Compilers:_Principles%2C_Techniques%2C_and_Tools">dragon book</a> and <a href='http://www.amazon.com/Advanced-Compiler-Design-Implementation-Muchnick/dp/1558603204'>Advanced Compiler Design and Implementation</a>, and I <em>wanted</em> to use LLVM to generate a simple
runtime <acronym title='Just in Time (compiler)'>JIT</acronym> compiler, but I can't seem to find the necessary
documentation.  </p>

<p>I see plenty of documentation on the IR, the various command-line tools,
and just about everything else LLVM-related except for a straightforward
tutorial that explains how to </p>

<ol>
<li>blast either opcodes or IR at the LLVM <acronym title='Application Programming Interface'>API</acronym>, and </li>
<li>have LLVM either emit an ELF/blob/whatever or execute the result.  </li>
</ol>

<p>I did manage to find a couple of examples that give me half of what I
want.  The source code ships with a sample <a href="http://en.wikipedia.org/wiki/Brainfuck">Brainfuck</a> compiler which
reads an input bf file and emits LLVM IR.  There are a couple more
examples that show how to construct a basic JIT, then create some
functions that do frustratingly simple things like calculate Fibonacci
numbers or add two integers together, but nothing that's too much more
detailed than that.</p>

<p>Oh, did I mention that the online API documentation is broken?</p>

<p>A bit of googling turned up libjit, which is almost exactly what I want.
It's got a simple C API (instead of the nightmarishly bloated C++
monstrosity that is the LLVM API), a simple build and link system, and
<a href="http://maybe.dotgnu.info:8000/~krokas/books/libJIT/libjit_toc.html">simple, straightforward and complete documentation</a>.  In
short, it's everything that I want.  </p>

<p>It's also GPL-licensed, which makes it almost completely unusable.  I
found <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2006-April/005581.html">this 2006 email exchange</a> comparing libgit and LLVM.  The
author, who apparently works on libgcj, mentions all of my problems with
LLVM (sans the anti-C++ bias).  </p>

<p>My other crazy idea was to generate C as an IR and blast it at
<a href="http://fabrice.bellard.free.fr/tcc/">libtcc</a>, but with my limited fiddling I couldn't get to do anything
other than segfault on me.  Incidentally, the documentation for libtcc 
is a single file and it's still better than what's available for LLVM.</p>

<p>So here's my question: Is there a decent book or tutorial that walks
through the LLVM API?  I've Googled to the end of the earth and back,
I've seen the <a href="http://llvm.org/docs/Stacker.html">Stacker</a> documentation, and I've read through
<em>everything</em> in the LLVM documentation section, and none of it was
what I'm looking for.  I'm open to suggestions...</p>