96b776211d8da799d7d415041467e1ba9c0f79d8
[oota-llvm.git] / docs / tutorial / LangImpl8.html
1 <!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
27 language with LLVM</a>" tutorial.  In the course of this tutorial, we have grown
28 our little Kaleidoscope language from being a useless toy, to being a
29 semi-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
32 taken.  We built the entire lexer, parser, AST, code generator, and an 
33 interactive 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
37 user defined binary and unary operators, it uses JIT compilation for immediate
38 evaluation, 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
42 to define, build, and play with languages.  Building a compiler need not be a
43 scary or mystical process!  Now that you've seen some of the basics, I strongly
44 encourage 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
48 modern software engineering, they are often useful when putting together quick
49 little hacks like the Kaleidoscope compiler itself.  Fortunately, our current
50 setup makes it very easy to add global variables: just have value lookup check
51 to see if an unresolved variable is in the global variable symbol table before
52 rejecting 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
56 type double.  This gives the language a very nice elegance, because only
57 supporting one type means that you never have to specify types.  Different
58 languages have different ways of handling this.  The easiest way is to require
59 the user to specify types for every variable definition, and record the type
60 of 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
63 extending the type system in all sorts of interesting ways.  Simple arrays are
64 very easy and are quite useful for many different applications.  Adding them is
65 mostly an exercise in learning how the LLVM <a 
66 href="../LangRef.html#i_getelementptr">getelementptr</a> instruction works.
67 The getelementptr instruction is so nifty/unconventional, it <a 
68 href="../GetElementPtr.html">has its own FAQ</a>!).</li>
69
70 <li><b>standard runtime</b> - Our current language allows the user to access
71 arbitrary external functions, and we use it for things like "printd" and
72 "putchard".  As you extend the language to add higher-level constructs, often
73 these constructs make the most amount of sense to be lowered into calls into a
74 language-supplied runtime.  For example, if you add hash tables to the language,
75 it would probably make sense to add the routines to a runtime, instead of 
76 inlining them all the way.</li>
77
78 <li><b>memory management</b> - Currently we can only access the stack in
79 Kaleidoscope.  It would also be useful to be able to allocate heap memory,
80 either with calls to the standard libc malloc/free interface or with a garbage
81 collector.  If you choose to use garbage collection, note that LLVM fully
82 supports <a href="../GarbageCollection.html">Accurate Garbage Collection</a>
83 including algorithms that move objects and need to scan/update the stack.</li>
84
85 <li><b>debugger support</b> - LLVM supports generation of <a 
86 href="../SourceLevelDebugging.html">DWARF Debug info</a> which is understood by
87 common debuggers like GDB.  Adding support for debug info is fairly 
88 straight-forward.  The best way to understand it is to compile some C/C++ code
89 with "<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 
92 href="../ExceptionHandling.html">zero cost exceptions</a> which interoperate
93 with code compiled in other languages.  You could also generate code by
94 implicitly making every function return an error value and checking it.  You 
95 could also make explicit use of setjmp/longjmp.  There are many different ways
96 to go here.</li>
97
98 <li><b>object orientation, generics, database access, complex numbers,
99 geometric programming, ...</b> - Really, there is
100 no end of crazy features that you can add to the language.</li>
101
102 </ul>
103
104 <p>
105 Have fun - try doing something crazy and unusual.  Building a language like
106 everyone else always has is much less fun than trying something a little crazy
107 and off the wall and seeing how it turns out.  If you get stuck or want to talk
108 about it, feel free to email the <a 
109 href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev">llvmdev mailing 
110 list</a>: it has lots of people who are interested in languages and are often
111 willing to help out.
112 </p>
113
114 <p>Before we end, I want to talk about some "tips and tricks" for generating
115 LLVM IR.  These are some of the more subtle things that may not be obvious, but
116 are 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>