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