Add chapter 4, feedback appreciated.
[oota-llvm.git] / docs / tutorial / LangImpl4.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: Adding JIT and Optimizer Support</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: Adding JIT and Optimizer Support</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">Part 4 Introduction</a></div>
22 <!-- *********************************************************************** -->
23
24 <div class="doc_text">
25
26 <p>Welcome to part 4 of the "<a href="index.html">Implementing a language with
27 LLVM</a>" tutorial.  Parts 1-3 described the implementation of a simple language
28 and included support for generating LLVM IR.  This chapter describes two new
29 techniques: adding optimizer support to your language, and adding JIT compiler
30 support.  This shows how to get nice efficient code for your language.</p>
31
32 </div>
33
34 <!-- *********************************************************************** -->
35 <div class="doc_section"><a name="trivialconstfold">Trivial Constant
36 Folding</a></div>
37 <!-- *********************************************************************** -->
38
39 <div class="doc_text">
40
41 <p>
42 Our demonstration for Chapter 3 is elegant and easy to extend.  Unfortunately,
43 it does not produce wonderful code.  For example, when compiling simple code,
44 we don't get obvious optimizations:</p>
45
46 <div class="doc_code">
47 <pre>
48 ready&gt; <b>def test(x) 1+2+x;</b>
49 Read function definition:
50 define double @test(double %x) {
51 entry:
52         %addtmp = add double 1.000000e+00, 2.000000e+00
53         %addtmp1 = add double %addtmp, %x
54         ret double %addtmp1
55 }
56 </pre>
57 </div>
58
59 <p>This code is a very very literal transcription of the AST built by parsing
60 our code, and as such, lacks optimizations like constant folding (we'd like to 
61 get "<tt>add x, 3.0</tt>" in the example above) as well as other more important
62 optimizations.  Constant folding in particular is a very common and very
63 important optimization: so much so that many language implementors implement
64 constant folding support in their AST representation.</p>
65
66 <p>With LLVM, you don't need to.  Since all calls to build LLVM IR go through
67 the LLVM builder, it would be nice if the builder itself checked to see if there
68 was a constant folding opportunity when you call it.  If so, it could just do
69 the constant fold and return the constant instead of creating an instruction.
70 This is exactly what the <tt>LLVMFoldingBuilder</tt> class does.  Lets make one
71 change:
72
73 <div class="doc_code">
74 <pre>
75 static LLVMFoldingBuilder Builder;
76 </pre>
77 </div>
78
79 <p>All we did was switch from <tt>LLVMBuilder</tt> to 
80 <tt>LLVMFoldingBuilder</tt>.  Though we change no other code, now all of our
81 instructions are implicitly constant folded without us having to do anything
82 about it.  For example, our example above now compiles to:</p>
83
84 <div class="doc_code">
85 <pre>
86 ready&gt; <b>def test(x) 1+2+x;</b>
87 Read function definition:
88 define double @test(double %x) {
89 entry:
90         %addtmp = add double 3.000000e+00, %x
91         ret double %addtmp
92 }
93 </pre>
94 </div>
95
96 <p>Well, that was easy.  :)  In practice, we recommend always using
97 <tt>LLVMConstantBuilder</tt> when generating code like this.  It has no
98 "syntactic overhead" for its use (you don't have to uglify your compiler with
99 constant checks everywhere) and it can dramatically reduce the amount of
100 LLVM IR that is generated in some cases (particular for languages with a macro
101 preprocessor or that use a lot of constants).</p>
102
103 <p>On the other hand, the <tt>LLVMFoldingBuilder</tt> is limited by the fact
104 that it does all of its analysis inline with the code as it is built.  If you
105 take a slightly more complex example:</p>
106
107 <div class="doc_code">
108 <pre>
109 ready&gt; <b>def test(x) (1+2+x)*(x+(1+2));</b>
110 ready> Read function definition:
111 define double @test(double %x) {
112 entry:
113         %addtmp = add double 3.000000e+00, %x
114         %addtmp1 = add double %x, 3.000000e+00
115         %multmp = mul double %addtmp, %addtmp1
116         ret double %multmp
117 }
118 </pre>
119 </div>
120
121 <p>In this case, the LHS and RHS of the multiplication are the same value.  We'd
122 really like to see this generate "<tt>tmp = x+3; result = tmp*tmp;</tt>" instead
123 of computing "<tt>x*3</tt>" twice.</p>
124
125 <p>Unfortunately, no amount of local analysis will be able to detect and correct
126 this.  This requires two transformations: reassociation of expressions (to 
127 make the add's lexically identical) and Common Subexpression Elimination (CSE)
128 to  delete the redundant add instruction.  Fortunately, LLVM provides a broad
129 range of optimizations that you can use, in the form of "passes".</p>
130
131 </div>
132
133 <!-- *********************************************************************** -->
134 <div class="doc_section"><a name="optimizerpasses">LLVM Optimization
135  Passes</a></div>
136 <!-- *********************************************************************** -->
137
138 <div class="doc_text">
139
140 <p>LLVM provides many optimization passes which do many different sorts of
141 things and have different tradeoffs.  Unlike other systems, LLVM doesn't hold
142 to the mistaken notion that one set of optimizations is right for all languages
143 and for all situations.  LLVM allows a compiler implementor to make complete
144 decisions about what optimizations to use, in which order, and in what
145 situation.</p>
146
147 <p>As a concrete example, LLVM supports both "whole module" passes, which look
148 across as large of body of code as they can (often a whole file, but if run 
149 at link time, this can be a substantial portion of the whole program).  It also
150 supports and includes "per-function" passes which just operate on a single
151 function at a time, without looking at other functions.  For more information
152 on passes and how the get run, see the <a href="../WritingAnLLVMPass.html">How
153 to Write a Pass</a> document.</p>
154
155 <p>For Kaleidoscope, we are currently generating functions on the fly, one at
156 a time, as the user types them in.  We aren't shooting for the ultimate
157 optimization experience in this setting, but we also want to catch the easy and
158 quick stuff where possible.  As such, we will choose to run a few per-function
159 optimizations as the user types the function in.  If we wanted to make a "static
160 Kaleidoscope compiler", we would use exactly the code we have now, except that
161 we would defer running the optimizer until the entire file has been parsed.</p>
162
163 <p>In order to get per-function optimizations going, we need to set up a
164 <a href="../WritingAnLLVMPass.html#passmanager">FunctionPassManager</a> to hold and
165 organize the LLVM optimizations that we want to run.  Once we have that, we can
166 add a set of optimizations to run.  The code looks like this:</p>
167
168 <div class="doc_code">
169 <pre>
170     ExistingModuleProvider OurModuleProvider(TheModule);
171     FunctionPassManager OurFPM(&amp;OurModuleProvider);
172       
173     // Set up the optimizer pipeline.  Start with registering info about how the
174     // target lays out data structures.
175     OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
176     // Do simple "peephole" optimizations and bit-twiddling optzns.
177     OurFPM.add(createInstructionCombiningPass());
178     // Reassociate expressions.
179     OurFPM.add(createReassociatePass());
180     // Eliminate Common SubExpressions.
181     OurFPM.add(createGVNPass());
182     // Simplify the control flow graph (deleting unreachable blocks, etc).
183     OurFPM.add(createCFGSimplificationPass());
184
185     // Set the global so the code gen can use this.
186     TheFPM = &amp;OurFPM;
187
188     // Run the main "interpreter loop" now.
189     MainLoop();
190 </pre>
191 </div>
192
193 <p>This code defines two objects, a <tt>ExistingModuleProvider</tt> and a
194 <tt>FunctionPassManager</tt>.  The former is basically a wrapper around our
195 <tt>Module</tt> that the PassManager requires.  It provides certain flexibility
196 that we're not going to take advantage of here, so I won't dive into what it is
197 all about.</p>
198
199 <p>The meat of the matter is the definition of the "<tt>OurFPM</tt>".  It
200 requires a pointer to the <tt>Module</tt> (through the <tt>ModuleProvider</tt>)
201 to construct itself.  Once it is set up, we use a series of "add" calls to add
202 a bunch of LLVM passes.  The first pass is basically boilerplate, it adds a pass
203 so that later optimizations know how the data structures in the program are
204 layed out.  The "<tt>TheExecutionEngine</tt>" variable is related to the JIT,
205 which we will get to in the next section.</p>
206
207 <p>In this case, we choose to add 4 optimization passes.  The passes we chose
208 here are a pretty standard set of "cleanup" optimizations that are useful for
209 a wide variety of code.  I won't delve into what they do, but believe that they
210 are a good starting place.</p>
211
212 <p>Once the passmanager, is set up, we need to make use of it.  We do this by
213 running it after our newly created function is constructed (in 
214 <tt>FunctionAST::Codegen</tt>), but before it is returned to the client:</p>
215
216 <div class="doc_code">
217 <pre>
218   if (Value *RetVal = Body->Codegen()) {
219     // Finish off the function.
220     Builder.CreateRet(RetVal);
221
222     // Validate the generated code, checking for consistency.
223     verifyFunction(*TheFunction);
224
225     // Optimize the function.
226     TheFPM->run(*TheFunction);
227     
228     return TheFunction;
229   }
230 </pre>
231 </div>
232
233 <p>As you can see, this is pretty straight-forward.  The 
234 <tt>FunctionPassManager</tt> optimizes and updates the LLVM Function* in place,
235 improving (hopefully) its body.  With this in place, we can try our test above
236 again:</p>
237
238 <div class="doc_code">
239 <pre>
240 ready&gt; <b>def test(x) (1+2+x)*(x+(1+2));</b>
241 ready> Read function definition:
242 define double @test(double %x) {
243 entry:
244         %addtmp = add double %x, 3.000000e+00
245         %multmp = mul double %addtmp, %addtmp
246         ret double %multmp
247 }
248 </pre>
249 </div>
250
251 <p>As expected, we now get our nicely optimized code, saving a floating point
252 add from the program.</p>
253
254 <p>LLVM provides a wide variety of optimizations that can be used in certain
255 circumstances.  Unfortunately we don't have a good centralized description of
256 what every pass does, but you can check out the ones that <tt>llvm-gcc</tt> or
257 <tt>llvm-ld</tt> run to get started.  The "<tt>opt</tt>" tool allows you to 
258 experiment with passes from the command line, so you can see if they do
259 anything.</p>
260
261 <p>Now that we have reasonable code coming out of our front-end, lets talk about
262 executing it!</p>
263
264 </div>
265
266 <!-- *********************************************************************** -->
267 <div class="doc_section"><a name="jit">Adding a JIT Compiler</a></div>
268 <!-- *********************************************************************** -->
269
270 <div class="doc_text">
271
272 <p>Once the code is available in LLVM IR form a wide variety of tools can be
273 applied to it.  For example, you can run optimizations on it (as we did above),
274 you can dump it out in textual or binary forms, you can compile the code to an
275 assembly file (.s) for some target, or you can JIT compile it.  The nice thing
276 about the LLVM IR representation is that it is the common currency between many
277 different parts of the compiler.
278 </p>
279
280 <p>In this chapter, we'll add JIT compiler support to our interpreter.  The
281 basic idea that we want for Kaleidoscope is to have the user enter function
282 bodies as they do now, but immediately evaluate the top-level expressions they
283 type in.  For example, if they type in "1 + 2;", we should evaluate and print
284 out 3.  If they define a function, they should be able to call it from the 
285 command line.</p>
286
287 <p>In order to do this, we first declare and initialize the JIT.  This is done
288 by adding a global variable and a call in <tt>main</tt>:</p>
289
290 <div class="doc_code">
291 <pre>
292 static ExecutionEngine *TheExecutionEngine;
293 ...
294 int main() {
295   ..
296   // Create the JIT.
297   TheExecutionEngine = ExecutionEngine::create(TheModule);
298   ..
299 }
300 </pre>
301 </div>
302
303 <p>This creates an abstract "Execution Engine" which can be either a JIT
304 compiler or the LLVM interpreter.  LLVM will automatically pick a JIT compiler
305 for you if one is available for your platform, otherwise it will fall back to
306 the interpreter.</p>
307
308 <p>Once the <tt>ExecutionEngine</tt> is created, the JIT is ready to be used.
309 There are a variety of APIs that are useful, but the most simple one is the
310 "<tt>getPointerToFunction(F)</tt>" method.  This method JIT compiles the
311 specified LLVM Function and returns a function pointer to the generated machine
312 code.  In our case, this means that we can change the code that parses a
313 top-level expression to look like this:</p>
314
315 <div class="doc_code">
316 <pre>
317 static void HandleTopLevelExpression() {
318   // Evaluate a top level expression into an anonymous function.
319   if (FunctionAST *F = ParseTopLevelExpr()) {
320     if (Function *LF = F-&gt;Codegen()) {
321       LF->dump();  // Dump the function for exposition purposes.
322     
323       // JIT the function, returning a function pointer.
324       void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);
325       
326       // Cast it to the right type (takes no arguments, returns a double) so we
327       // can call it as a native function.
328       double (*FP)() = (double (*)())FPtr;
329       fprintf(stderr, "Evaluated to %f\n", FP());
330     }
331 </pre>
332 </div>
333
334 <p>Recall that we compile top-level expressions into a self-contained LLVM
335 function that takes no arguments and returns the computed double.  Because the 
336 LLVM JIT compiler matches the native platform ABI, this means that you can just
337 cast the result pointer to a function pointer of that type and call it directly.
338 As such, there is no difference between JIT compiled code and native machine
339 code that is statically linked into your application.</p>
340
341 <p>With just these two changes, lets see how Kaleidoscope works now!</p>
342
343 <div class="doc_code">
344 <pre>
345 ready&gt; <b>4+5;</b>
346 define double @""() {
347 entry:
348         ret double 9.000000e+00
349 }
350
351 <em>Evaluated to 9.000000</em>
352 </pre>
353 </div>
354
355 <p>Well this looks like it is basically working.  The dump of the function
356 shows the "no argument function that always returns double" that we synthesize
357 for each top level expression that is typed it.  This demonstrates very basic
358 functionality, but can we do more?</p>
359
360 <div class="doc_code">
361 <pre>
362 ready&gt; def testfunc(x y) x + y*2; </b> 
363 Read function definition:
364 define double @testfunc(double %x, double %y) {
365 entry:
366         %multmp = mul double %y, 2.000000e+00
367         %addtmp = add double %multmp, %x
368         ret double %addtmp
369 }
370
371 ready&gt; <b>testfunc(4, 10);</b>
372 define double @""() {
373 entry:
374         %calltmp = call double @testfunc( double 4.000000e+00, double 1.000000e+01 )
375         ret double %calltmp
376 }
377
378 <em>Evaluated to 24.000000</em>
379 </pre>
380 </div>
381
382 <p>This illustrates that we can now call user code, but it is a bit subtle what
383 is going on here.  Note that we only invoke the JIT on the anonymous functions
384 that <em>calls testfunc</em>, but we never invoked it on <em>testfunc
385 itself</em>.</p>
386
387 <p>What actually happened here is that the anonymous function is
388 JIT'd when requested.  When the Kaleidoscope app calls through the function
389 pointer that is returned, the anonymous function starts executing.  It ends up
390 making the call for the "testfunc" function, and ends up in a stub that invokes
391 the JIT, lazily, on testfunc.  Once the JIT finishes lazily compiling testfunc,
392 it returns and the code reexecutes the call.</p>
393
394 <p>In summary, the JIT will lazily JIT code on the fly as it is needed.  The
395 JIT provides a number of other more advanced interfaces for things like freeing
396 allocated machine code, rejit'ing functions to update them, etc.  However, even
397 with this simple code, we get some surprisingly powerful capabilities - check
398 this out (I removed the dump of the anonymous functions, you should get the idea
399 by now :) :</p>
400
401 <div class="doc_code">
402 <pre>
403 ready&gt; <b>extern sin(x);</b>
404 Read extern: 
405 declare double @sin(double)
406
407 ready&gt; <b>extern cos(x);</b>
408 Read extern: 
409 declare double @cos(double)
410
411 ready&gt; <b>sin(1.0);</b>
412 <em>Evaluated to 0.841471</em>
413 ready&gt; <b>def foo(x) sin(x)*sin(x) + cos(x)*cos(x);</b>
414 Read function definition:
415 define double @foo(double %x) {
416 entry:
417         %calltmp = call double @sin( double %x )
418         %multmp = mul double %calltmp, %calltmp
419         %calltmp2 = call double @cos( double %x )
420         %multmp4 = mul double %calltmp2, %calltmp2
421         %addtmp = add double %multmp, %multmp4
422         ret double %addtmp
423 }
424
425 ready&gt; <b>foo(4.0);</b>
426 <em>Evaluated to 1.000000</em>
427 </pre>
428 </div>
429
430 <p>Whoa, how does the JIT know about sin and cos?  The answer is simple: in this
431 example, the JIT started execution of a function and got to a function call.  It
432 realized that the function was not yet JIT compiled and invoked the standard set
433 of routines to resolve the function.  In this case, there is no body defined
434 for the function, so the JIT ended up calling "<tt>dlsym("sin")</tt>" on itself.
435 Since "<tt>sin</tt>" is defined within the JIT's address space, it simply
436 patches up calls in the module to call the libm version of <tt>sin</tt>
437 directly.</p>
438
439 <p>The LLVM JIT provides a number of interfaces (look in the 
440 <tt>ExecutionEngine.h</tt> file) for controlling how unknown functions get
441 resolved.  It allows you to establish explicit mappings between IR objects and
442 addresses (useful for LLVM global variables that you want to map to static
443 tables, for example), allows you to dynamically decide on the fly based on the
444 function name, and even allows you to have the JIT abort itself if any lazy
445 compilation is attempted.</p>
446
447 <p>This completes the JIT and optimizer chapter of the Kaleidoscope tutorial. At
448 this point, we can compile a non-Turing-complete programming language, optimize
449 and JIT compile it in a user-driven way.  Next up we'll look into <a 
450 href="LangImpl5.html">extending the language with control flow constructs</a>,
451 tackling some interesting LLVM IR issues along the way.</p>
452
453 </div>
454
455 <!-- *********************************************************************** -->
456 <div class="doc_section"><a name="code">Full Code Listing</a></div>
457 <!-- *********************************************************************** -->
458
459 <div class="doc_text">
460
461 <p>
462 Here is the complete code listing for our running example, enhanced with the
463 LLVM JIT and optimizer.  To build this example, use:
464 </p>
465
466 <div class="doc_code">
467 <pre>
468    # Compile
469    g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
470    # Run
471    ./toy
472 </pre>
473 </div>
474
475 <p>Here is the code:</p>
476
477 <div class="doc_code">
478 <pre>
479 #include "llvm/DerivedTypes.h"
480 #include "llvm/ExecutionEngine/ExecutionEngine.h"
481 #include "llvm/Module.h"
482 #include "llvm/ModuleProvider.h"
483 #include "llvm/PassManager.h"
484 #include "llvm/Analysis/Verifier.h"
485 #include "llvm/Target/TargetData.h"
486 #include "llvm/Transforms/Scalar.h"
487 #include "llvm/Support/LLVMBuilder.h"
488 #include &lt;cstdio&gt;
489 #include &lt;string&gt;
490 #include &lt;map&gt;
491 #include &lt;vector&gt;
492 using namespace llvm;
493
494 //===----------------------------------------------------------------------===//
495 // Lexer
496 //===----------------------------------------------------------------------===//
497
498 // The lexer returns tokens [0-255] if it is an unknown character, otherwise one
499 // of these for known things.
500 enum Token {
501   tok_eof = -1,
502
503   // commands
504   tok_def = -2, tok_extern = -3,
505
506   // primary
507   tok_identifier = -4, tok_number = -5,
508 };
509
510 static std::string IdentifierStr;  // Filled in if tok_identifier
511 static double NumVal;              // Filled in if tok_number
512
513 /// gettok - Return the next token from standard input.
514 static int gettok() {
515   static int LastChar = ' ';
516
517   // Skip any whitespace.
518   while (isspace(LastChar))
519     LastChar = getchar();
520
521   if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
522     IdentifierStr = LastChar;
523     while (isalnum((LastChar = getchar())))
524       IdentifierStr += LastChar;
525
526     if (IdentifierStr == "def") return tok_def;
527     if (IdentifierStr == "extern") return tok_extern;
528     return tok_identifier;
529   }
530
531   if (isdigit(LastChar) || LastChar == '.') {   // Number: [0-9.]+
532     std::string NumStr;
533     do {
534       NumStr += LastChar;
535       LastChar = getchar();
536     } while (isdigit(LastChar) || LastChar == '.');
537
538     NumVal = strtod(NumStr.c_str(), 0);
539     return tok_number;
540   }
541
542   if (LastChar == '#') {
543     // Comment until end of line.
544     do LastChar = getchar();
545     while (LastChar != EOF &amp;&amp; LastChar != '\n' &amp; LastChar != '\r');
546     
547     if (LastChar != EOF)
548       return gettok();
549   }
550   
551   // Check for end of file.  Don't eat the EOF.
552   if (LastChar == EOF)
553     return tok_eof;
554
555   // Otherwise, just return the character as its ascii value.
556   int ThisChar = LastChar;
557   LastChar = getchar();
558   return ThisChar;
559 }
560
561 //===----------------------------------------------------------------------===//
562 // Abstract Syntax Tree (aka Parse Tree)
563 //===----------------------------------------------------------------------===//
564
565 /// ExprAST - Base class for all expression nodes.
566 class ExprAST {
567 public:
568   virtual ~ExprAST() {}
569   virtual Value *Codegen() = 0;
570 };
571
572 /// NumberExprAST - Expression class for numeric literals like "1.0".
573 class NumberExprAST : public ExprAST {
574   double Val;
575 public:
576   NumberExprAST(double val) : Val(val) {}
577   virtual Value *Codegen();
578 };
579
580 /// VariableExprAST - Expression class for referencing a variable, like "a".
581 class VariableExprAST : public ExprAST {
582   std::string Name;
583 public:
584   VariableExprAST(const std::string &amp;name) : Name(name) {}
585   virtual Value *Codegen();
586 };
587
588 /// BinaryExprAST - Expression class for a binary operator.
589 class BinaryExprAST : public ExprAST {
590   char Op;
591   ExprAST *LHS, *RHS;
592 public:
593   BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) 
594     : Op(op), LHS(lhs), RHS(rhs) {}
595   virtual Value *Codegen();
596 };
597
598 /// CallExprAST - Expression class for function calls.
599 class CallExprAST : public ExprAST {
600   std::string Callee;
601   std::vector&lt;ExprAST*&gt; Args;
602 public:
603   CallExprAST(const std::string &amp;callee, std::vector&lt;ExprAST*&gt; &amp;args)
604     : Callee(callee), Args(args) {}
605   virtual Value *Codegen();
606 };
607
608 /// PrototypeAST - This class represents the "prototype" for a function,
609 /// which captures its argument names as well as if it is an operator.
610 class PrototypeAST {
611   std::string Name;
612   std::vector&lt;std::string&gt; Args;
613 public:
614   PrototypeAST(const std::string &amp;name, const std::vector&lt;std::string&gt; &amp;args)
615     : Name(name), Args(args) {}
616   
617   Function *Codegen();
618 };
619
620 /// FunctionAST - This class represents a function definition itself.
621 class FunctionAST {
622   PrototypeAST *Proto;
623   ExprAST *Body;
624 public:
625   FunctionAST(PrototypeAST *proto, ExprAST *body)
626     : Proto(proto), Body(body) {}
627   
628   Function *Codegen();
629 };
630
631 //===----------------------------------------------------------------------===//
632 // Parser
633 //===----------------------------------------------------------------------===//
634
635 /// CurTok/getNextToken - Provide a simple token buffer.  CurTok is the current
636 /// token the parser it looking at.  getNextToken reads another token from the
637 /// lexer and updates CurTok with its results.
638 static int CurTok;
639 static int getNextToken() {
640   return CurTok = gettok();
641 }
642
643 /// BinopPrecedence - This holds the precedence for each binary operator that is
644 /// defined.
645 static std::map&lt;char, int&gt; BinopPrecedence;
646
647 /// GetTokPrecedence - Get the precedence of the pending binary operator token.
648 static int GetTokPrecedence() {
649   if (!isascii(CurTok))
650     return -1;
651   
652   // Make sure it's a declared binop.
653   int TokPrec = BinopPrecedence[CurTok];
654   if (TokPrec &lt;= 0) return -1;
655   return TokPrec;
656 }
657
658 /// Error* - These are little helper functions for error handling.
659 ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
660 PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
661 FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
662
663 static ExprAST *ParseExpression();
664
665 /// identifierexpr
666 ///   ::= identifer
667 ///   ::= identifer '(' expression* ')'
668 static ExprAST *ParseIdentifierExpr() {
669   std::string IdName = IdentifierStr;
670   
671   getNextToken();  // eat identifer.
672   
673   if (CurTok != '(') // Simple variable ref.
674     return new VariableExprAST(IdName);
675   
676   // Call.
677   getNextToken();  // eat (
678   std::vector&lt;ExprAST*&gt; Args;
679   while (1) {
680     ExprAST *Arg = ParseExpression();
681     if (!Arg) return 0;
682     Args.push_back(Arg);
683     
684     if (CurTok == ')') break;
685     
686     if (CurTok != ',')
687       return Error("Expected ')'");
688     getNextToken();
689   }
690
691   // Eat the ')'.
692   getNextToken();
693   
694   return new CallExprAST(IdName, Args);
695 }
696
697 /// numberexpr ::= number
698 static ExprAST *ParseNumberExpr() {
699   ExprAST *Result = new NumberExprAST(NumVal);
700   getNextToken(); // consume the number
701   return Result;
702 }
703
704 /// parenexpr ::= '(' expression ')'
705 static ExprAST *ParseParenExpr() {
706   getNextToken();  // eat (.
707   ExprAST *V = ParseExpression();
708   if (!V) return 0;
709   
710   if (CurTok != ')')
711     return Error("expected ')'");
712   getNextToken();  // eat ).
713   return V;
714 }
715
716 /// primary
717 ///   ::= identifierexpr
718 ///   ::= numberexpr
719 ///   ::= parenexpr
720 static ExprAST *ParsePrimary() {
721   switch (CurTok) {
722   default: return Error("unknown token when expecting an expression");
723   case tok_identifier: return ParseIdentifierExpr();
724   case tok_number:     return ParseNumberExpr();
725   case '(':            return ParseParenExpr();
726   }
727 }
728
729 /// binoprhs
730 ///   ::= ('+' primary)*
731 static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
732   // If this is a binop, find its precedence.
733   while (1) {
734     int TokPrec = GetTokPrecedence();
735     
736     // If this is a binop that binds at least as tightly as the current binop,
737     // consume it, otherwise we are done.
738     if (TokPrec &lt; ExprPrec)
739       return LHS;
740     
741     // Okay, we know this is a binop.
742     int BinOp = CurTok;
743     getNextToken();  // eat binop
744     
745     // Parse the primary expression after the binary operator.
746     ExprAST *RHS = ParsePrimary();
747     if (!RHS) return 0;
748     
749     // If BinOp binds less tightly with RHS than the operator after RHS, let
750     // the pending operator take RHS as its LHS.
751     int NextPrec = GetTokPrecedence();
752     if (TokPrec &lt; NextPrec) {
753       RHS = ParseBinOpRHS(TokPrec+1, RHS);
754       if (RHS == 0) return 0;
755     }
756     
757     // Merge LHS/RHS.
758     LHS = new BinaryExprAST(BinOp, LHS, RHS);
759   }
760 }
761
762 /// expression
763 ///   ::= primary binoprhs
764 ///
765 static ExprAST *ParseExpression() {
766   ExprAST *LHS = ParsePrimary();
767   if (!LHS) return 0;
768   
769   return ParseBinOpRHS(0, LHS);
770 }
771
772 /// prototype
773 ///   ::= id '(' id* ')'
774 static PrototypeAST *ParsePrototype() {
775   if (CurTok != tok_identifier)
776     return ErrorP("Expected function name in prototype");
777
778   std::string FnName = IdentifierStr;
779   getNextToken();
780   
781   if (CurTok != '(')
782     return ErrorP("Expected '(' in prototype");
783   
784   std::vector&lt;std::string&gt; ArgNames;
785   while (getNextToken() == tok_identifier)
786     ArgNames.push_back(IdentifierStr);
787   if (CurTok != ')')
788     return ErrorP("Expected ')' in prototype");
789   
790   // success.
791   getNextToken();  // eat ')'.
792   
793   return new PrototypeAST(FnName, ArgNames);
794 }
795
796 /// definition ::= 'def' prototype expression
797 static FunctionAST *ParseDefinition() {
798   getNextToken();  // eat def.
799   PrototypeAST *Proto = ParsePrototype();
800   if (Proto == 0) return 0;
801
802   if (ExprAST *E = ParseExpression())
803     return new FunctionAST(Proto, E);
804   return 0;
805 }
806
807 /// toplevelexpr ::= expression
808 static FunctionAST *ParseTopLevelExpr() {
809   if (ExprAST *E = ParseExpression()) {
810     // Make an anonymous proto.
811     PrototypeAST *Proto = new PrototypeAST("", std::vector&lt;std::string&gt;());
812     return new FunctionAST(Proto, E);
813   }
814   return 0;
815 }
816
817 /// external ::= 'extern' prototype
818 static PrototypeAST *ParseExtern() {
819   getNextToken();  // eat extern.
820   return ParsePrototype();
821 }
822
823 //===----------------------------------------------------------------------===//
824 // Code Generation
825 //===----------------------------------------------------------------------===//
826
827 static Module *TheModule;
828 static LLVMFoldingBuilder Builder;
829 static std::map&lt;std::string, Value*&gt; NamedValues;
830 static FunctionPassManager *TheFPM;
831
832 Value *ErrorV(const char *Str) { Error(Str); return 0; }
833
834 Value *NumberExprAST::Codegen() {
835   return ConstantFP::get(Type::DoubleTy, APFloat(Val));
836 }
837
838 Value *VariableExprAST::Codegen() {
839   // Look this variable up in the function.
840   Value *V = NamedValues[Name];
841   return V ? V : ErrorV("Unknown variable name");
842 }
843
844 Value *BinaryExprAST::Codegen() {
845   Value *L = LHS-&gt;Codegen();
846   Value *R = RHS-&gt;Codegen();
847   if (L == 0 || R == 0) return 0;
848   
849   switch (Op) {
850   case '+': return Builder.CreateAdd(L, R, "addtmp");
851   case '-': return Builder.CreateSub(L, R, "subtmp");
852   case '*': return Builder.CreateMul(L, R, "multmp");
853   case '&lt;':
854     L = Builder.CreateFCmpULT(L, R, "multmp");
855     // Convert bool 0/1 to double 0.0 or 1.0
856     return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
857   default: return ErrorV("invalid binary operator");
858   }
859 }
860
861 Value *CallExprAST::Codegen() {
862   // Look up the name in the global module table.
863   Function *CalleeF = TheModule-&gt;getFunction(Callee);
864   if (CalleeF == 0)
865     return ErrorV("Unknown function referenced");
866   
867   // If argument mismatch error.
868   if (CalleeF-&gt;arg_size() != Args.size())
869     return ErrorV("Incorrect # arguments passed");
870
871   std::vector&lt;Value*&gt; ArgsV;
872   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
873     ArgsV.push_back(Args[i]-&gt;Codegen());
874     if (ArgsV.back() == 0) return 0;
875   }
876   
877   return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
878 }
879
880 Function *PrototypeAST::Codegen() {
881   // Make the function type:  double(double,double) etc.
882   std::vector&lt;const Type*&gt; Doubles(Args.size(), Type::DoubleTy);
883   FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
884   
885   Function *F = new Function(FT, Function::ExternalLinkage, Name, TheModule);
886   
887   // If F conflicted, there was already something named 'Name'.  If it has a
888   // body, don't allow redefinition or reextern.
889   if (F-&gt;getName() != Name) {
890     // Delete the one we just made and get the existing one.
891     F-&gt;eraseFromParent();
892     F = TheModule-&gt;getFunction(Name);
893     
894     // If F already has a body, reject this.
895     if (!F-&gt;empty()) {
896       ErrorF("redefinition of function");
897       return 0;
898     }
899     
900     // If F took a different number of args, reject.
901     if (F-&gt;arg_size() != Args.size()) {
902       ErrorF("redefinition of function with different # args");
903       return 0;
904     }
905   }
906   
907   // Set names for all arguments.
908   unsigned Idx = 0;
909   for (Function::arg_iterator AI = F-&gt;arg_begin(); Idx != Args.size();
910        ++AI, ++Idx) {
911     AI-&gt;setName(Args[Idx]);
912     
913     // Add arguments to variable symbol table.
914     NamedValues[Args[Idx]] = AI;
915   }
916   
917   return F;
918 }
919
920 Function *FunctionAST::Codegen() {
921   NamedValues.clear();
922   
923   Function *TheFunction = Proto-&gt;Codegen();
924   if (TheFunction == 0)
925     return 0;
926   
927   // Create a new basic block to start insertion into.
928   BasicBlock *BB = new BasicBlock("entry", TheFunction);
929   Builder.SetInsertPoint(BB);
930   
931   if (Value *RetVal = Body-&gt;Codegen()) {
932     // Finish off the function.
933     Builder.CreateRet(RetVal);
934
935     // Validate the generated code, checking for consistency.
936     verifyFunction(*TheFunction);
937
938     // Optimize the function.
939     TheFPM-&gt;run(*TheFunction);
940     
941     return TheFunction;
942   }
943   
944   // Error reading body, remove function.
945   TheFunction-&gt;eraseFromParent();
946   return 0;
947 }
948
949 //===----------------------------------------------------------------------===//
950 // Top-Level parsing and JIT Driver
951 //===----------------------------------------------------------------------===//
952
953 static ExecutionEngine *TheExecutionEngine;
954
955 static void HandleDefinition() {
956   if (FunctionAST *F = ParseDefinition()) {
957     if (Function *LF = F-&gt;Codegen()) {
958       fprintf(stderr, "Read function definition:");
959       LF-&gt;dump();
960     }
961   } else {
962     // Skip token for error recovery.
963     getNextToken();
964   }
965 }
966
967 static void HandleExtern() {
968   if (PrototypeAST *P = ParseExtern()) {
969     if (Function *F = P-&gt;Codegen()) {
970       fprintf(stderr, "Read extern: ");
971       F-&gt;dump();
972     }
973   } else {
974     // Skip token for error recovery.
975     getNextToken();
976   }
977 }
978
979 static void HandleTopLevelExpression() {
980   // Evaluate a top level expression into an anonymous function.
981   if (FunctionAST *F = ParseTopLevelExpr()) {
982     if (Function *LF = F-&gt;Codegen()) {
983       // JIT the function, returning a function pointer.
984       void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);
985       
986       // Cast it to the right type (takes no arguments, returns a double) so we
987       // can call it as a native function.
988       double (*FP)() = (double (*)())FPtr;
989       fprintf(stderr, "Evaluated to %f\n", FP());
990     }
991   } else {
992     // Skip token for error recovery.
993     getNextToken();
994   }
995 }
996
997 /// top ::= definition | external | expression | ';'
998 static void MainLoop() {
999   while (1) {
1000     fprintf(stderr, "ready&gt; ");
1001     switch (CurTok) {
1002     case tok_eof:    return;
1003     case ';':        getNextToken(); break;  // ignore top level semicolons.
1004     case tok_def:    HandleDefinition(); break;
1005     case tok_extern: HandleExtern(); break;
1006     default:         HandleTopLevelExpression(); break;
1007     }
1008   }
1009 }
1010
1011
1012
1013 //===----------------------------------------------------------------------===//
1014 // "Library" functions that can be "extern'd" from user code.
1015 //===----------------------------------------------------------------------===//
1016
1017 /// putchard - putchar that takes a double and returns 0.
1018 extern "C" 
1019 double putchard(double X) {
1020   putchar((char)X);
1021   return 0;
1022 }
1023
1024 //===----------------------------------------------------------------------===//
1025 // Main driver code.
1026 //===----------------------------------------------------------------------===//
1027
1028 int main() {
1029   // Install standard binary operators.
1030   // 1 is lowest precedence.
1031   BinopPrecedence['&lt;'] = 10;
1032   BinopPrecedence['+'] = 20;
1033   BinopPrecedence['-'] = 20;
1034   BinopPrecedence['*'] = 40;  // highest.
1035
1036   // Prime the first token.
1037   fprintf(stderr, "ready&gt; ");
1038   getNextToken();
1039
1040   // Make the module, which holds all the code.
1041   TheModule = new Module("my cool jit");
1042   
1043   // Create the JIT.
1044   TheExecutionEngine = ExecutionEngine::create(TheModule);
1045
1046   {
1047     ExistingModuleProvider OurModuleProvider(TheModule);
1048     FunctionPassManager OurFPM(&amp;OurModuleProvider);
1049       
1050     // Set up the optimizer pipeline.  Start with registering info about how the
1051     // target lays out data structures.
1052     OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
1053     // Do simple "peephole" optimizations and bit-twiddling optzns.
1054     OurFPM.add(createInstructionCombiningPass());
1055     // Reassociate expressions.
1056     OurFPM.add(createReassociatePass());
1057     // Eliminate Common SubExpressions.
1058     OurFPM.add(createGVNPass());
1059     // Simplify the control flow graph (deleting unreachable blocks, etc).
1060     OurFPM.add(createCFGSimplificationPass());
1061
1062     // Set the global so the code gen can use this.
1063     TheFPM = &amp;OurFPM;
1064
1065     // Run the main "interpreter loop" now.
1066     MainLoop();
1067     
1068     TheFPM = 0;
1069   }  // Free module provider and pass manager.
1070                                    
1071                                    
1072   // Print out all of the generated code.
1073   TheModule-&gt;dump();
1074   return 0;
1075 }
1076 </pre>
1077 </div>
1078
1079 </div>
1080
1081 <!-- *********************************************************************** -->
1082 <hr>
1083 <address>
1084   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1085   src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1086   <a href="http://validator.w3.org/check/referer"><img
1087   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1088
1089   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1090   <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1091   Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $
1092 </address>
1093 </body>
1094 </html>