blob: 96b776211d8da799d7d415041467e1ba9c0f79d8 [file] [log] [blame]
Chris Lattnerb8fc6502007-11-05 01:58:13 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3
4<html>
5<head>
6 <title>Kaleidoscope: Conclusion, ideas for extensions, and other useful tidbits</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <meta name="author" content="Chris Lattner">
9 <link rel="stylesheet" href="../llvm.css" type="text/css">
10</head>
11
12<body>
13
14<div class="doc_title">Kaleidoscope: Conclusion</div>
15
16<div class="doc_author">
17 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
18</div>
19
20<!-- *********************************************************************** -->
21<div class="doc_section"><a name="intro">Tutorial Conclusion</a></div>
22<!-- *********************************************************************** -->
23
24<div class="doc_text">
25
26<p>Welcome to the the final chapter of the "<a href="index.html">Implementing a
27language with LLVM</a>" tutorial. In the course of this tutorial, we have grown
28our little Kaleidoscope language from being a useless toy, to being a
29semi-interesting (but probably still useless) toy. :)</p>
30
31<p>It is interesting to see how far we've come, and how little code it has
32taken. We built the entire lexer, parser, AST, code generator, and an
33interactive run-loop (with a JIT!) by-hand in under 700 lines of
34(non-comment/non-blank) code.</p>
35
36<p>Our little language supports a couple of interesting features: it supports
37user defined binary and unary operators, it uses JIT compilation for immediate
38evaluation, and it supports a few control flow constructs with SSA construction.
39</p>
40
41<p>Part of the idea of this tutorial was to show you how easy and fun it can be
42to define, build, and play with languages. Building a compiler need not be a
43scary or mystical process! Now that you've seen some of the basics, I strongly
44encourage you to take the code and hack on it. For example, try adding:</p>
45
46<ul>
47<li><b>global variables</b> - While global variables have questional value in
48modern software engineering, they are often useful when putting together quick
49little hacks like the Kaleidoscope compiler itself. Fortunately, our current
50setup makes it very easy to add global variables: just have value lookup check
51to see if an unresolved variable is in the global variable symbol table before
52rejecting it. To create a new global variable, make an instance of the LLVM
53<tt>GlobalVariable</tt> class.</li>
54
55<li><b>typed variables</b> - Kaleidoscope currently only supports variables of
56type double. This gives the language a very nice elegance, because only
57supporting one type means that you never have to specify types. Different
58languages have different ways of handling this. The easiest way is to require
59the user to specify types for every variable definition, and record the type
60of the variable in the symbol table along with its Value*.</li>
61
62<li><b>arrays, structs, vectors, etc</b> - Once you add types, you can start
63extending the type system in all sorts of interesting ways. Simple arrays are
64very easy and are quite useful for many different applications. Adding them is
65mostly an exercise in learning how the LLVM <a
66href="../LangRef.html#i_getelementptr">getelementptr</a> instruction works.
67The getelementptr instruction is so nifty/unconventional, it <a
68href="../GetElementPtr.html">has its own FAQ</a>!).</li>
69
70<li><b>standard runtime</b> - Our current language allows the user to access
71arbitrary external functions, and we use it for things like "printd" and
72"putchard". As you extend the language to add higher-level constructs, often
73these constructs make the most amount of sense to be lowered into calls into a
74language-supplied runtime. For example, if you add hash tables to the language,
75it would probably make sense to add the routines to a runtime, instead of
76inlining them all the way.</li>
77
78<li><b>memory management</b> - Currently we can only access the stack in
79Kaleidoscope. It would also be useful to be able to allocate heap memory,
80either with calls to the standard libc malloc/free interface or with a garbage
81collector. If you choose to use garbage collection, note that LLVM fully
82supports <a href="../GarbageCollection.html">Accurate Garbage Collection</a>
83including algorithms that move objects and need to scan/update the stack.</li>
84
85<li><b>debugger support</b> - LLVM supports generation of <a
86href="../SourceLevelDebugging.html">DWARF Debug info</a> which is understood by
87common debuggers like GDB. Adding support for debug info is fairly
88straight-forward. The best way to understand it is to compile some C/C++ code
89with "<tt>llvm-gcc -g -O0</tt>" and taking a look at what it produces.</li>
90
91<li><b>exception handlingsupport</b> - LLVM supports generation of <a
92href="../ExceptionHandling.html">zero cost exceptions</a> which interoperate
93with code compiled in other languages. You could also generate code by
94implicitly making every function return an error value and checking it. You
95could also make explicit use of setjmp/longjmp. There are many different ways
96to go here.</li>
97
98<li><b>object orientation, generics, database access, complex numbers,
99geometric programming, ...</b> - Really, there is
100no end of crazy features that you can add to the language.</li>
101
102</ul>
103
104<p>
105Have fun - try doing something crazy and unusual. Building a language like
106everyone else always has is much less fun than trying something a little crazy
107and off the wall and seeing how it turns out. If you get stuck or want to talk
108about it, feel free to email the <a
109href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">llvmdev mailing
110list</a>: it has lots of people who are interested in languages and are often
111willing to help out.
112</p>
113
114<p>Before we end, I want to talk about some "tips and tricks" for generating
115LLVM IR. These are some of the more subtle things that may not be obvious, but
116are very useful if you want to take advantage of LLVM's capabilities.</p>
117
118</div>
119
120<!-- *********************************************************************** -->
121<div class="doc_section"><a name="tipsandtricks">Tips and Tricks</a></div>
122<!-- *********************************************************************** -->
123
124<div class="doc_text">
125
126<p></p>
127
128</div>
129
130<!-- *********************************************************************** -->
131<hr>
132<address>
133 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
134 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
135 <a href="http://validator.w3.org/check/referer"><img
136 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
137
138 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
139 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
140 Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $
141</address>
142</body>
143</html>