7cd54d9beafd505f2bd39666c4aaa0b613249dd6
[oota-llvm.git] / docs / tutorial / OCamlLangImpl4.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   <meta name="author" content="Erick Tryzelaar">
10   <link rel="stylesheet" href="../llvm.css" type="text/css">
11 </head>
12
13 <body>
14
15 <div class="doc_title">Kaleidoscope: Adding JIT and Optimizer Support</div>
16
17 <ul>
18 <li><a href="index.html">Up to Tutorial Index</a></li>
19 <li>Chapter 4
20   <ol>
21     <li><a href="#intro">Chapter 4 Introduction</a></li>
22     <li><a href="#trivialconstfold">Trivial Constant Folding</a></li>
23     <li><a href="#optimizerpasses">LLVM Optimization Passes</a></li>
24     <li><a href="#jit">Adding a JIT Compiler</a></li>
25     <li><a href="#code">Full Code Listing</a></li>
26   </ol>
27 </li>
28 <li><a href="OCamlLangImpl5.html">Chapter 5</a>: Extending the Language: Control
29 Flow</li>
30 </ul>
31
32 <div class="doc_author">
33         <p>
34                 Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
35                 and <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a>
36         </p>
37 </div>
38
39 <!-- *********************************************************************** -->
40 <div class="doc_section"><a name="intro">Chapter 4 Introduction</a></div>
41 <!-- *********************************************************************** -->
42
43 <div class="doc_text">
44
45 <p>Welcome to Chapter 4 of the "<a href="index.html">Implementing a language
46 with LLVM</a>" tutorial.  Chapters 1-3 described the implementation of a simple
47 language and added support for generating LLVM IR.  This chapter describes
48 two new techniques: adding optimizer support to your language, and adding JIT
49 compiler support.  These additions will demonstrate how to get nice, efficient code
50 for the Kaleidoscope language.</p>
51
52 </div>
53
54 <!-- *********************************************************************** -->
55 <div class="doc_section"><a name="trivialconstfold">Trivial Constant
56 Folding</a></div>
57 <!-- *********************************************************************** -->
58
59 <div class="doc_text">
60
61 <p><b>Note:</b> the default <tt>IRBuilder</tt> now always includes the constant 
62 folding optimisations below.<p>
63
64 <p>
65 Our demonstration for Chapter 3 is elegant and easy to extend.  Unfortunately,
66 it does not produce wonderful code.  For example, when compiling simple code,
67 we don't get obvious optimizations:</p>
68
69 <div class="doc_code">
70 <pre>
71 ready&gt; <b>def test(x) 1+2+x;</b>
72 Read function definition:
73 define double @test(double %x) {
74 entry:
75         %addtmp = add double 1.000000e+00, 2.000000e+00
76         %addtmp1 = add double %addtmp, %x
77         ret double %addtmp1
78 }
79 </pre>
80 </div>
81
82 <p>This code is a very, very literal transcription of the AST built by parsing
83 the input. As such, this transcription lacks optimizations like constant folding
84 (we'd like to get "<tt>add x, 3.0</tt>" in the example above) as well as other
85 more important optimizations.  Constant folding, in particular, is a very common
86 and very important optimization: so much so that many language implementors
87 implement constant folding support in their AST representation.</p>
88
89 <p>With LLVM, you don't need this support in the AST.  Since all calls to build
90 LLVM IR go through the LLVM builder, it would be nice if the builder itself
91 checked to see if there was a constant folding opportunity when you call it.
92 If so, it could just do the constant fold and return the constant instead of
93 creating an instruction.  This is exactly what the <tt>LLVMFoldingBuilder</tt>
94 class does.
95
96 <p>All we did was switch from <tt>LLVMBuilder</tt> to
97 <tt>LLVMFoldingBuilder</tt>.  Though we change no other code, we now have all of our
98 instructions implicitly constant folded without us having to do anything
99 about it.  For example, the input above now compiles to:</p>
100
101 <div class="doc_code">
102 <pre>
103 ready&gt; <b>def test(x) 1+2+x;</b>
104 Read function definition:
105 define double @test(double %x) {
106 entry:
107         %addtmp = add double 3.000000e+00, %x
108         ret double %addtmp
109 }
110 </pre>
111 </div>
112
113 <p>Well, that was easy :).  In practice, we recommend always using
114 <tt>LLVMFoldingBuilder</tt> when generating code like this.  It has no
115 "syntactic overhead" for its use (you don't have to uglify your compiler with
116 constant checks everywhere) and it can dramatically reduce the amount of
117 LLVM IR that is generated in some cases (particular for languages with a macro
118 preprocessor or that use a lot of constants).</p>
119
120 <p>On the other hand, the <tt>LLVMFoldingBuilder</tt> is limited by the fact
121 that it does all of its analysis inline with the code as it is built.  If you
122 take a slightly more complex example:</p>
123
124 <div class="doc_code">
125 <pre>
126 ready&gt; <b>def test(x) (1+2+x)*(x+(1+2));</b>
127 ready&gt; Read function definition:
128 define double @test(double %x) {
129 entry:
130         %addtmp = add double 3.000000e+00, %x
131         %addtmp1 = add double %x, 3.000000e+00
132         %multmp = mul double %addtmp, %addtmp1
133         ret double %multmp
134 }
135 </pre>
136 </div>
137
138 <p>In this case, the LHS and RHS of the multiplication are the same value.  We'd
139 really like to see this generate "<tt>tmp = x+3; result = tmp*tmp;</tt>" instead
140 of computing "<tt>x*3</tt>" twice.</p>
141
142 <p>Unfortunately, no amount of local analysis will be able to detect and correct
143 this.  This requires two transformations: reassociation of expressions (to
144 make the add's lexically identical) and Common Subexpression Elimination (CSE)
145 to  delete the redundant add instruction.  Fortunately, LLVM provides a broad
146 range of optimizations that you can use, in the form of "passes".</p>
147
148 </div>
149
150 <!-- *********************************************************************** -->
151 <div class="doc_section"><a name="optimizerpasses">LLVM Optimization
152  Passes</a></div>
153 <!-- *********************************************************************** -->
154
155 <div class="doc_text">
156
157 <p>LLVM provides many optimization passes, which do many different sorts of
158 things and have different tradeoffs.  Unlike other systems, LLVM doesn't hold
159 to the mistaken notion that one set of optimizations is right for all languages
160 and for all situations.  LLVM allows a compiler implementor to make complete
161 decisions about what optimizations to use, in which order, and in what
162 situation.</p>
163
164 <p>As a concrete example, LLVM supports both "whole module" passes, which look
165 across as large of body of code as they can (often a whole file, but if run
166 at link time, this can be a substantial portion of the whole program).  It also
167 supports and includes "per-function" passes which just operate on a single
168 function at a time, without looking at other functions.  For more information
169 on passes and how they are run, see the <a href="../WritingAnLLVMPass.html">How
170 to Write a Pass</a> document and the <a href="../Passes.html">List of LLVM
171 Passes</a>.</p>
172
173 <p>For Kaleidoscope, we are currently generating functions on the fly, one at
174 a time, as the user types them in.  We aren't shooting for the ultimate
175 optimization experience in this setting, but we also want to catch the easy and
176 quick stuff where possible.  As such, we will choose to run a few per-function
177 optimizations as the user types the function in.  If we wanted to make a "static
178 Kaleidoscope compiler", we would use exactly the code we have now, except that
179 we would defer running the optimizer until the entire file has been parsed.</p>
180
181 <p>In order to get per-function optimizations going, we need to set up a
182 <a href="../WritingAnLLVMPass.html#passmanager">Llvm.PassManager</a> to hold and
183 organize the LLVM optimizations that we want to run.  Once we have that, we can
184 add a set of optimizations to run.  The code looks like this:</p>
185
186 <div class="doc_code">
187 <pre>
188   (* Create the JIT. *)
189   let the_module_provider = ModuleProvider.create Codegen.the_module in
190   let the_execution_engine = ExecutionEngine.create the_module_provider in
191   let the_fpm = PassManager.create_function the_module_provider in
192
193   (* Set up the optimizer pipeline.  Start with registering info about how the
194    * target lays out data structures. *)
195   TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
196
197   (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
198   add_instruction_combining the_fpm;
199
200   (* reassociate expressions. *)
201   add_reassociation the_fpm;
202
203   (* Eliminate Common SubExpressions. *)
204   add_gvn the_fpm;
205
206   (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
207   add_cfg_simplification the_fpm;
208
209   ignore (PassManager.initialize the_fpm);
210
211   (* Run the main "interpreter loop" now. *)
212   Toplevel.main_loop the_fpm the_execution_engine stream;
213 </pre>
214 </div>
215
216 <p>This code defines two values, an <tt>Llvm.llmoduleprovider</tt> and a
217 <tt>Llvm.PassManager.t</tt>.  The former is basically a wrapper around our
218 <tt>Llvm.llmodule</tt> that the <tt>Llvm.PassManager.t</tt> requires.  It
219 provides certain flexibility that we're not going to take advantage of here,
220 so I won't dive into any details about it.</p>
221
222 <p>The meat of the matter here, is the definition of "<tt>the_fpm</tt>".  It
223 requires a pointer to the <tt>the_module</tt> (through the
224 <tt>the_module_provider</tt>) to construct itself.  Once it is set up, we use a
225 series of "add" calls to add a bunch of LLVM passes.  The first pass is
226 basically boilerplate, it adds a pass so that later optimizations know how the
227 data structures in the program are layed out.  The
228 "<tt>the_execution_engine</tt>" variable is related to the JIT, which we will
229 get to in the next section.</p>
230
231 <p>In this case, we choose to add 4 optimization passes.  The passes we chose
232 here are a pretty standard set of "cleanup" optimizations that are useful for
233 a wide variety of code.  I won't delve into what they do but, believe me,
234 they are a good starting place :).</p>
235
236 <p>Once the <tt>Llvm.PassManager.</tt> is set up, we need to make use of it.
237 We do this by running it after our newly created function is constructed (in
238 <tt>Codegen.codegen_func</tt>), but before it is returned to the client:</p>
239
240 <div class="doc_code">
241 <pre>
242 let codegen_func the_fpm = function
243       ...
244       try
245         let ret_val = codegen_expr body in
246
247         (* Finish off the function. *)
248         let _ = build_ret ret_val builder in
249
250         (* Validate the generated code, checking for consistency. *)
251         Llvm_analysis.assert_valid_function the_function;
252
253         (* Optimize the function. *)
254         let _ = PassManager.run_function the_function the_fpm in
255
256         the_function
257 </pre>
258 </div>
259
260 <p>As you can see, this is pretty straightforward.  The <tt>the_fpm</tt>
261 optimizes and updates the LLVM Function* in place, improving (hopefully) its
262 body.  With this in place, we can try our test above again:</p>
263
264 <div class="doc_code">
265 <pre>
266 ready&gt; <b>def test(x) (1+2+x)*(x+(1+2));</b>
267 ready&gt; Read function definition:
268 define double @test(double %x) {
269 entry:
270         %addtmp = add double %x, 3.000000e+00
271         %multmp = mul double %addtmp, %addtmp
272         ret double %multmp
273 }
274 </pre>
275 </div>
276
277 <p>As expected, we now get our nicely optimized code, saving a floating point
278 add instruction from every execution of this function.</p>
279
280 <p>LLVM provides a wide variety of optimizations that can be used in certain
281 circumstances.  Some <a href="../Passes.html">documentation about the various
282 passes</a> is available, but it isn't very complete.  Another good source of
283 ideas can come from looking at the passes that <tt>llvm-gcc</tt> or
284 <tt>llvm-ld</tt> run to get started.  The "<tt>opt</tt>" tool allows you to
285 experiment with passes from the command line, so you can see if they do
286 anything.</p>
287
288 <p>Now that we have reasonable code coming out of our front-end, lets talk about
289 executing it!</p>
290
291 </div>
292
293 <!-- *********************************************************************** -->
294 <div class="doc_section"><a name="jit">Adding a JIT Compiler</a></div>
295 <!-- *********************************************************************** -->
296
297 <div class="doc_text">
298
299 <p>Code that is available in LLVM IR can have a wide variety of tools
300 applied to it.  For example, you can run optimizations on it (as we did above),
301 you can dump it out in textual or binary forms, you can compile the code to an
302 assembly file (.s) for some target, or you can JIT compile it.  The nice thing
303 about the LLVM IR representation is that it is the "common currency" between
304 many different parts of the compiler.
305 </p>
306
307 <p>In this section, we'll add JIT compiler support to our interpreter.  The
308 basic idea that we want for Kaleidoscope is to have the user enter function
309 bodies as they do now, but immediately evaluate the top-level expressions they
310 type in.  For example, if they type in "1 + 2;", we should evaluate and print
311 out 3.  If they define a function, they should be able to call it from the
312 command line.</p>
313
314 <p>In order to do this, we first declare and initialize the JIT.  This is done
315 by adding a global variable and a call in <tt>main</tt>:</p>
316
317 <div class="doc_code">
318 <pre>
319 ...
320 let main () =
321   ...
322   <b>(* Create the JIT. *)
323   let the_module_provider = ModuleProvider.create Codegen.the_module in
324   let the_execution_engine = ExecutionEngine.create the_module_provider in</b>
325   ...
326 </pre>
327 </div>
328
329 <p>This creates an abstract "Execution Engine" which can be either a JIT
330 compiler or the LLVM interpreter.  LLVM will automatically pick a JIT compiler
331 for you if one is available for your platform, otherwise it will fall back to
332 the interpreter.</p>
333
334 <p>Once the <tt>Llvm_executionengine.ExecutionEngine.t</tt> is created, the JIT
335 is ready to be used.  There are a variety of APIs that are useful, but the
336 simplest one is the "<tt>Llvm_executionengine.ExecutionEngine.run_function</tt>"
337 function.  This method JIT compiles the specified LLVM Function and returns a
338 function pointer to the generated machine code.  In our case, this means that we
339 can change the code that parses a top-level expression to look like this:</p>
340
341 <div class="doc_code">
342 <pre>
343             (* Evaluate a top-level expression into an anonymous function. *)
344             let e = Parser.parse_toplevel stream in
345             print_endline "parsed a top-level expr";
346             let the_function = Codegen.codegen_func the_fpm e in
347             dump_value the_function;
348
349             (* JIT the function, returning a function pointer. *)
350             let result = ExecutionEngine.run_function the_function [||]
351               the_execution_engine in
352
353             print_string "Evaluated to ";
354             print_float (GenericValue.as_float double_type result);
355             print_newline ();
356 </pre>
357 </div>
358
359 <p>Recall that we compile top-level expressions into a self-contained LLVM
360 function that takes no arguments and returns the computed double.  Because the
361 LLVM JIT compiler matches the native platform ABI, this means that you can just
362 cast the result pointer to a function pointer of that type and call it directly.
363 This means, there is no difference between JIT compiled code and native machine
364 code that is statically linked into your application.</p>
365
366 <p>With just these two changes, lets see how Kaleidoscope works now!</p>
367
368 <div class="doc_code">
369 <pre>
370 ready&gt; <b>4+5;</b>
371 define double @""() {
372 entry:
373         ret double 9.000000e+00
374 }
375
376 <em>Evaluated to 9.000000</em>
377 </pre>
378 </div>
379
380 <p>Well this looks like it is basically working.  The dump of the function
381 shows the "no argument function that always returns double" that we synthesize
382 for each top level expression that is typed in.  This demonstrates very basic
383 functionality, but can we do more?</p>
384
385 <div class="doc_code">
386 <pre>
387 ready&gt; <b>def testfunc(x y) x + y*2; </b>
388 Read function definition:
389 define double @testfunc(double %x, double %y) {
390 entry:
391         %multmp = mul double %y, 2.000000e+00
392         %addtmp = add double %multmp, %x
393         ret double %addtmp
394 }
395
396 ready&gt; <b>testfunc(4, 10);</b>
397 define double @""() {
398 entry:
399         %calltmp = call double @testfunc( double 4.000000e+00, double 1.000000e+01 )
400         ret double %calltmp
401 }
402
403 <em>Evaluated to 24.000000</em>
404 </pre>
405 </div>
406
407 <p>This illustrates that we can now call user code, but there is something a bit
408 subtle going on here.  Note that we only invoke the JIT on the anonymous
409 functions that <em>call testfunc</em>, but we never invoked it on <em>testfunc
410 </em>itself.</p>
411
412 <p>What actually happened here is that the anonymous function was JIT'd when
413 requested.  When the Kaleidoscope app calls through the function pointer that is
414 returned, the anonymous function starts executing.  It ends up making the call
415 to the "testfunc" function, and ends up in a stub that invokes the JIT, lazily,
416 on testfunc.  Once the JIT finishes lazily compiling testfunc,
417 it returns and the code re-executes the call.</p>
418
419 <p>In summary, the JIT will lazily JIT code, on the fly, as it is needed.  The
420 JIT provides a number of other more advanced interfaces for things like freeing
421 allocated machine code, rejit'ing functions to update them, etc.  However, even
422 with this simple code, we get some surprisingly powerful capabilities - check
423 this out (I removed the dump of the anonymous functions, you should get the idea
424 by now :) :</p>
425
426 <div class="doc_code">
427 <pre>
428 ready&gt; <b>extern sin(x);</b>
429 Read extern:
430 declare double @sin(double)
431
432 ready&gt; <b>extern cos(x);</b>
433 Read extern:
434 declare double @cos(double)
435
436 ready&gt; <b>sin(1.0);</b>
437 <em>Evaluated to 0.841471</em>
438
439 ready&gt; <b>def foo(x) sin(x)*sin(x) + cos(x)*cos(x);</b>
440 Read function definition:
441 define double @foo(double %x) {
442 entry:
443         %calltmp = call double @sin( double %x )
444         %multmp = mul double %calltmp, %calltmp
445         %calltmp2 = call double @cos( double %x )
446         %multmp4 = mul double %calltmp2, %calltmp2
447         %addtmp = add double %multmp, %multmp4
448         ret double %addtmp
449 }
450
451 ready&gt; <b>foo(4.0);</b>
452 <em>Evaluated to 1.000000</em>
453 </pre>
454 </div>
455
456 <p>Whoa, how does the JIT know about sin and cos?  The answer is surprisingly
457 simple: in this example, the JIT started execution of a function and got to a
458 function call.  It realized that the function was not yet JIT compiled and
459 invoked the standard set of routines to resolve the function.  In this case,
460 there is no body defined for the function, so the JIT ended up calling
461 "<tt>dlsym("sin")</tt>" on the Kaleidoscope process itself.  Since
462 "<tt>sin</tt>" is defined within the JIT's address space, it simply patches up
463 calls in the module to call the libm version of <tt>sin</tt> directly.</p>
464
465 <p>The LLVM JIT provides a number of interfaces (look in the
466 <tt>llvm_executionengine.mli</tt> file) for controlling how unknown functions
467 get resolved.  It allows you to establish explicit mappings between IR objects
468 and addresses (useful for LLVM global variables that you want to map to static
469 tables, for example), allows you to dynamically decide on the fly based on the
470 function name, and even allows you to have the JIT abort itself if any lazy
471 compilation is attempted.</p>
472
473 <p>One interesting application of this is that we can now extend the language
474 by writing arbitrary C code to implement operations.  For example, if we add:
475 </p>
476
477 <div class="doc_code">
478 <pre>
479 /* putchard - putchar that takes a double and returns 0. */
480 extern "C"
481 double putchard(double X) {
482   putchar((char)X);
483   return 0;
484 }
485 </pre>
486 </div>
487
488 <p>Now we can produce simple output to the console by using things like:
489 "<tt>extern putchard(x); putchard(120);</tt>", which prints a lowercase 'x' on
490 the console (120 is the ASCII code for 'x').  Similar code could be used to
491 implement file I/O, console input, and many other capabilities in
492 Kaleidoscope.</p>
493
494 <p>This completes the JIT and optimizer chapter of the Kaleidoscope tutorial. At
495 this point, we can compile a non-Turing-complete programming language, optimize
496 and JIT compile it in a user-driven way.  Next up we'll look into <a
497 href="OCamlLangImpl5.html">extending the language with control flow
498 constructs</a>, tackling some interesting LLVM IR issues along the way.</p>
499
500 </div>
501
502 <!-- *********************************************************************** -->
503 <div class="doc_section"><a name="code">Full Code Listing</a></div>
504 <!-- *********************************************************************** -->
505
506 <div class="doc_text">
507
508 <p>
509 Here is the complete code listing for our running example, enhanced with the
510 LLVM JIT and optimizer.  To build this example, use:
511 </p>
512
513 <div class="doc_code">
514 <pre>
515 # Compile
516 ocamlbuild toy.byte
517 # Run
518 ./toy.byte
519 </pre>
520 </div>
521
522 <p>Here is the code:</p>
523
524 <dl>
525 <dt>_tags:</dt>
526 <dd class="doc_code">
527 <pre>
528 &lt;{lexer,parser}.ml&gt;: use_camlp4, pp(camlp4of)
529 &lt;*.{byte,native}&gt;: g++, use_llvm, use_llvm_analysis
530 &lt;*.{byte,native}&gt;: use_llvm_executionengine, use_llvm_target
531 &lt;*.{byte,native}&gt;: use_llvm_scalar_opts, use_bindings
532 </pre>
533 </dd>
534
535 <dt>myocamlbuild.ml:</dt>
536 <dd class="doc_code">
537 <pre>
538 open Ocamlbuild_plugin;;
539
540 ocaml_lib ~extern:true "llvm";;
541 ocaml_lib ~extern:true "llvm_analysis";;
542 ocaml_lib ~extern:true "llvm_executionengine";;
543 ocaml_lib ~extern:true "llvm_target";;
544 ocaml_lib ~extern:true "llvm_scalar_opts";;
545
546 flag ["link"; "ocaml"; "g++"] (S[A"-cc"; A"g++"]);;
547 dep ["link"; "ocaml"; "use_bindings"] ["bindings.o"];;
548 </pre>
549 </dd>
550
551 <dt>token.ml:</dt>
552 <dd class="doc_code">
553 <pre>
554 (*===----------------------------------------------------------------------===
555  * Lexer Tokens
556  *===----------------------------------------------------------------------===*)
557
558 (* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of
559  * these others for known things. *)
560 type token =
561   (* commands *)
562   | Def | Extern
563
564   (* primary *)
565   | Ident of string | Number of float
566
567   (* unknown *)
568   | Kwd of char
569 </pre>
570 </dd>
571
572 <dt>lexer.ml:</dt>
573 <dd class="doc_code">
574 <pre>
575 (*===----------------------------------------------------------------------===
576  * Lexer
577  *===----------------------------------------------------------------------===*)
578
579 let rec lex = parser
580   (* Skip any whitespace. *)
581   | [&lt; ' (' ' | '\n' | '\r' | '\t'); stream &gt;] -&gt; lex stream
582
583   (* identifier: [a-zA-Z][a-zA-Z0-9] *)
584   | [&lt; ' ('A' .. 'Z' | 'a' .. 'z' as c); stream &gt;] -&gt;
585       let buffer = Buffer.create 1 in
586       Buffer.add_char buffer c;
587       lex_ident buffer stream
588
589   (* number: [0-9.]+ *)
590   | [&lt; ' ('0' .. '9' as c); stream &gt;] -&gt;
591       let buffer = Buffer.create 1 in
592       Buffer.add_char buffer c;
593       lex_number buffer stream
594
595   (* Comment until end of line. *)
596   | [&lt; ' ('#'); stream &gt;] -&gt;
597       lex_comment stream
598
599   (* Otherwise, just return the character as its ascii value. *)
600   | [&lt; 'c; stream &gt;] -&gt;
601       [&lt; 'Token.Kwd c; lex stream &gt;]
602
603   (* end of stream. *)
604   | [&lt; &gt;] -&gt; [&lt; &gt;]
605
606 and lex_number buffer = parser
607   | [&lt; ' ('0' .. '9' | '.' as c); stream &gt;] -&gt;
608       Buffer.add_char buffer c;
609       lex_number buffer stream
610   | [&lt; stream=lex &gt;] -&gt;
611       [&lt; 'Token.Number (float_of_string (Buffer.contents buffer)); stream &gt;]
612
613 and lex_ident buffer = parser
614   | [&lt; ' ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' as c); stream &gt;] -&gt;
615       Buffer.add_char buffer c;
616       lex_ident buffer stream
617   | [&lt; stream=lex &gt;] -&gt;
618       match Buffer.contents buffer with
619       | "def" -&gt; [&lt; 'Token.Def; stream &gt;]
620       | "extern" -&gt; [&lt; 'Token.Extern; stream &gt;]
621       | id -&gt; [&lt; 'Token.Ident id; stream &gt;]
622
623 and lex_comment = parser
624   | [&lt; ' ('\n'); stream=lex &gt;] -&gt; stream
625   | [&lt; 'c; e=lex_comment &gt;] -&gt; e
626   | [&lt; &gt;] -&gt; [&lt; &gt;]
627 </pre>
628 </dd>
629
630 <dt>ast.ml:</dt>
631 <dd class="doc_code">
632 <pre>
633 (*===----------------------------------------------------------------------===
634  * Abstract Syntax Tree (aka Parse Tree)
635  *===----------------------------------------------------------------------===*)
636
637 (* expr - Base type for all expression nodes. *)
638 type expr =
639   (* variant for numeric literals like "1.0". *)
640   | Number of float
641
642   (* variant for referencing a variable, like "a". *)
643   | Variable of string
644
645   (* variant for a binary operator. *)
646   | Binary of char * expr * expr
647
648   (* variant for function calls. *)
649   | Call of string * expr array
650
651 (* proto - This type represents the "prototype" for a function, which captures
652  * its name, and its argument names (thus implicitly the number of arguments the
653  * function takes). *)
654 type proto = Prototype of string * string array
655
656 (* func - This type represents a function definition itself. *)
657 type func = Function of proto * expr
658 </pre>
659 </dd>
660
661 <dt>parser.ml:</dt>
662 <dd class="doc_code">
663 <pre>
664 (*===---------------------------------------------------------------------===
665  * Parser
666  *===---------------------------------------------------------------------===*)
667
668 (* binop_precedence - This holds the precedence for each binary operator that is
669  * defined *)
670 let binop_precedence:(char, int) Hashtbl.t = Hashtbl.create 10
671
672 (* precedence - Get the precedence of the pending binary operator token. *)
673 let precedence c = try Hashtbl.find binop_precedence c with Not_found -&gt; -1
674
675 (* primary
676  *   ::= identifier
677  *   ::= numberexpr
678  *   ::= parenexpr *)
679 let rec parse_primary = parser
680   (* numberexpr ::= number *)
681   | [&lt; 'Token.Number n &gt;] -&gt; Ast.Number n
682
683   (* parenexpr ::= '(' expression ')' *)
684   | [&lt; 'Token.Kwd '('; e=parse_expr; 'Token.Kwd ')' ?? "expected ')'" &gt;] -&gt; e
685
686   (* identifierexpr
687    *   ::= identifier
688    *   ::= identifier '(' argumentexpr ')' *)
689   | [&lt; 'Token.Ident id; stream &gt;] -&gt;
690       let rec parse_args accumulator = parser
691         | [&lt; e=parse_expr; stream &gt;] -&gt;
692             begin parser
693               | [&lt; 'Token.Kwd ','; e=parse_args (e :: accumulator) &gt;] -&gt; e
694               | [&lt; &gt;] -&gt; e :: accumulator
695             end stream
696         | [&lt; &gt;] -&gt; accumulator
697       in
698       let rec parse_ident id = parser
699         (* Call. *)
700         | [&lt; 'Token.Kwd '(';
701              args=parse_args [];
702              'Token.Kwd ')' ?? "expected ')'"&gt;] -&gt;
703             Ast.Call (id, Array.of_list (List.rev args))
704
705         (* Simple variable ref. *)
706         | [&lt; &gt;] -&gt; Ast.Variable id
707       in
708       parse_ident id stream
709
710   | [&lt; &gt;] -&gt; raise (Stream.Error "unknown token when expecting an expression.")
711
712 (* binoprhs
713  *   ::= ('+' primary)* *)
714 and parse_bin_rhs expr_prec lhs stream =
715   match Stream.peek stream with
716   (* If this is a binop, find its precedence. *)
717   | Some (Token.Kwd c) when Hashtbl.mem binop_precedence c -&gt;
718       let token_prec = precedence c in
719
720       (* If this is a binop that binds at least as tightly as the current binop,
721        * consume it, otherwise we are done. *)
722       if token_prec &lt; expr_prec then lhs else begin
723         (* Eat the binop. *)
724         Stream.junk stream;
725
726         (* Parse the primary expression after the binary operator. *)
727         let rhs = parse_primary stream in
728
729         (* Okay, we know this is a binop. *)
730         let rhs =
731           match Stream.peek stream with
732           | Some (Token.Kwd c2) -&gt;
733               (* If BinOp binds less tightly with rhs than the operator after
734                * rhs, let the pending operator take rhs as its lhs. *)
735               let next_prec = precedence c2 in
736               if token_prec &lt; next_prec
737               then parse_bin_rhs (token_prec + 1) rhs stream
738               else rhs
739           | _ -&gt; rhs
740         in
741
742         (* Merge lhs/rhs. *)
743         let lhs = Ast.Binary (c, lhs, rhs) in
744         parse_bin_rhs expr_prec lhs stream
745       end
746   | _ -&gt; lhs
747
748 (* expression
749  *   ::= primary binoprhs *)
750 and parse_expr = parser
751   | [&lt; lhs=parse_primary; stream &gt;] -&gt; parse_bin_rhs 0 lhs stream
752
753 (* prototype
754  *   ::= id '(' id* ')' *)
755 let parse_prototype =
756   let rec parse_args accumulator = parser
757     | [&lt; 'Token.Ident id; e=parse_args (id::accumulator) &gt;] -&gt; e
758     | [&lt; &gt;] -&gt; accumulator
759   in
760
761   parser
762   | [&lt; 'Token.Ident id;
763        'Token.Kwd '(' ?? "expected '(' in prototype";
764        args=parse_args [];
765        'Token.Kwd ')' ?? "expected ')' in prototype" &gt;] -&gt;
766       (* success. *)
767       Ast.Prototype (id, Array.of_list (List.rev args))
768
769   | [&lt; &gt;] -&gt;
770       raise (Stream.Error "expected function name in prototype")
771
772 (* definition ::= 'def' prototype expression *)
773 let parse_definition = parser
774   | [&lt; 'Token.Def; p=parse_prototype; e=parse_expr &gt;] -&gt;
775       Ast.Function (p, e)
776
777 (* toplevelexpr ::= expression *)
778 let parse_toplevel = parser
779   | [&lt; e=parse_expr &gt;] -&gt;
780       (* Make an anonymous proto. *)
781       Ast.Function (Ast.Prototype ("", [||]), e)
782
783 (*  external ::= 'extern' prototype *)
784 let parse_extern = parser
785   | [&lt; 'Token.Extern; e=parse_prototype &gt;] -&gt; e
786 </pre>
787 </dd>
788
789 <dt>codegen.ml:</dt>
790 <dd class="doc_code">
791 <pre>
792 (*===----------------------------------------------------------------------===
793  * Code Generation
794  *===----------------------------------------------------------------------===*)
795
796 open Llvm
797
798 exception Error of string
799
800 let context = global_context ()
801 let the_module = create_module context "my cool jit"
802 let builder = builder context
803 let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
804
805 let rec codegen_expr = function
806   | Ast.Number n -&gt; const_float double_type n
807   | Ast.Variable name -&gt;
808       (try Hashtbl.find named_values name with
809         | Not_found -&gt; raise (Error "unknown variable name"))
810   | Ast.Binary (op, lhs, rhs) -&gt;
811       let lhs_val = codegen_expr lhs in
812       let rhs_val = codegen_expr rhs in
813       begin
814         match op with
815         | '+' -&gt; build_add lhs_val rhs_val "addtmp" builder
816         | '-' -&gt; build_sub lhs_val rhs_val "subtmp" builder
817         | '*' -&gt; build_mul lhs_val rhs_val "multmp" builder
818         | '&lt;' -&gt;
819             (* Convert bool 0/1 to double 0.0 or 1.0 *)
820             let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in
821             build_uitofp i double_type "booltmp" builder
822         | _ -&gt; raise (Error "invalid binary operator")
823       end
824   | Ast.Call (callee, args) -&gt;
825       (* Look up the name in the module table. *)
826       let callee =
827         match lookup_function callee the_module with
828         | Some callee -&gt; callee
829         | None -&gt; raise (Error "unknown function referenced")
830       in
831       let params = params callee in
832
833       (* If argument mismatch error. *)
834       if Array.length params == Array.length args then () else
835         raise (Error "incorrect # arguments passed");
836       let args = Array.map codegen_expr args in
837       build_call callee args "calltmp" builder
838
839 let codegen_proto = function
840   | Ast.Prototype (name, args) -&gt;
841       (* Make the function type: double(double,double) etc. *)
842       let doubles = Array.make (Array.length args) double_type in
843       let ft = function_type double_type doubles in
844       let f =
845         match lookup_function name the_module with
846         | None -&gt; declare_function name ft the_module
847
848         (* If 'f' conflicted, there was already something named 'name'. If it
849          * has a body, don't allow redefinition or reextern. *)
850         | Some f -&gt;
851             (* If 'f' already has a body, reject this. *)
852             if block_begin f &lt;&gt; At_end f then
853               raise (Error "redefinition of function");
854
855             (* If 'f' took a different number of arguments, reject. *)
856             if element_type (type_of f) &lt;&gt; ft then
857               raise (Error "redefinition of function with different # args");
858             f
859       in
860
861       (* Set names for all arguments. *)
862       Array.iteri (fun i a -&gt;
863         let n = args.(i) in
864         set_value_name n a;
865         Hashtbl.add named_values n a;
866       ) (params f);
867       f
868
869 let codegen_func the_fpm = function
870   | Ast.Function (proto, body) -&gt;
871       Hashtbl.clear named_values;
872       let the_function = codegen_proto proto in
873
874       (* Create a new basic block to start insertion into. *)
875       let bb = append_block "entry" the_function in
876       position_at_end bb builder;
877
878       try
879         let ret_val = codegen_expr body in
880
881         (* Finish off the function. *)
882         let _ = build_ret ret_val builder in
883
884         (* Validate the generated code, checking for consistency. *)
885         Llvm_analysis.assert_valid_function the_function;
886
887         (* Optimize the function. *)
888         let _ = PassManager.run_function the_function the_fpm in
889
890         the_function
891       with e -&gt;
892         delete_function the_function;
893         raise e
894 </pre>
895 </dd>
896
897 <dt>toplevel.ml:</dt>
898 <dd class="doc_code">
899 <pre>
900 (*===----------------------------------------------------------------------===
901  * Top-Level parsing and JIT Driver
902  *===----------------------------------------------------------------------===*)
903
904 open Llvm
905 open Llvm_executionengine
906
907 (* top ::= definition | external | expression | ';' *)
908 let rec main_loop the_fpm the_execution_engine stream =
909   match Stream.peek stream with
910   | None -&gt; ()
911
912   (* ignore top-level semicolons. *)
913   | Some (Token.Kwd ';') -&gt;
914       Stream.junk stream;
915       main_loop the_fpm the_execution_engine stream
916
917   | Some token -&gt;
918       begin
919         try match token with
920         | Token.Def -&gt;
921             let e = Parser.parse_definition stream in
922             print_endline "parsed a function definition.";
923             dump_value (Codegen.codegen_func the_fpm e);
924         | Token.Extern -&gt;
925             let e = Parser.parse_extern stream in
926             print_endline "parsed an extern.";
927             dump_value (Codegen.codegen_proto e);
928         | _ -&gt;
929             (* Evaluate a top-level expression into an anonymous function. *)
930             let e = Parser.parse_toplevel stream in
931             print_endline "parsed a top-level expr";
932             let the_function = Codegen.codegen_func the_fpm e in
933             dump_value the_function;
934
935             (* JIT the function, returning a function pointer. *)
936             let result = ExecutionEngine.run_function the_function [||]
937               the_execution_engine in
938
939             print_string "Evaluated to ";
940             print_float (GenericValue.as_float double_type result);
941             print_newline ();
942         with Stream.Error s | Codegen.Error s -&gt;
943           (* Skip token for error recovery. *)
944           Stream.junk stream;
945           print_endline s;
946       end;
947       print_string "ready&gt; "; flush stdout;
948       main_loop the_fpm the_execution_engine stream
949 </pre>
950 </dd>
951
952 <dt>toy.ml:</dt>
953 <dd class="doc_code">
954 <pre>
955 (*===----------------------------------------------------------------------===
956  * Main driver code.
957  *===----------------------------------------------------------------------===*)
958
959 open Llvm
960 open Llvm_executionengine
961 open Llvm_target
962 open Llvm_scalar_opts
963
964 let main () =
965   (* Install standard binary operators.
966    * 1 is the lowest precedence. *)
967   Hashtbl.add Parser.binop_precedence '&lt;' 10;
968   Hashtbl.add Parser.binop_precedence '+' 20;
969   Hashtbl.add Parser.binop_precedence '-' 20;
970   Hashtbl.add Parser.binop_precedence '*' 40;    (* highest. *)
971
972   (* Prime the first token. *)
973   print_string "ready&gt; "; flush stdout;
974   let stream = Lexer.lex (Stream.of_channel stdin) in
975
976   (* Create the JIT. *)
977   let the_module_provider = ModuleProvider.create Codegen.the_module in
978   let the_execution_engine = ExecutionEngine.create the_module_provider in
979   let the_fpm = PassManager.create_function the_module_provider in
980
981   (* Set up the optimizer pipeline.  Start with registering info about how the
982    * target lays out data structures. *)
983   TargetData.add (ExecutionEngine.target_data the_execution_engine) the_fpm;
984
985   (* Do simple "peephole" optimizations and bit-twiddling optzn. *)
986   add_instruction_combining the_fpm;
987
988   (* reassociate expressions. *)
989   add_reassociation the_fpm;
990
991   (* Eliminate Common SubExpressions. *)
992   add_gvn the_fpm;
993
994   (* Simplify the control flow graph (deleting unreachable blocks, etc). *)
995   add_cfg_simplification the_fpm;
996
997   ignore (PassManager.initialize the_fpm);
998
999   (* Run the main "interpreter loop" now. *)
1000   Toplevel.main_loop the_fpm the_execution_engine stream;
1001
1002   (* Print out all the generated code. *)
1003   dump_module Codegen.the_module
1004 ;;
1005
1006 main ()
1007 </pre>
1008 </dd>
1009
1010 <dt>bindings.c</dt>
1011 <dd class="doc_code">
1012 <pre>
1013 #include &lt;stdio.h&gt;
1014
1015 /* putchard - putchar that takes a double and returns 0. */
1016 extern double putchard(double X) {
1017   putchar((char)X);
1018   return 0;
1019 }
1020 </pre>
1021 </dd>
1022 </dl>
1023
1024 <a href="OCamlLangImpl5.html">Next: Extending the language: control flow</a>
1025 </div>
1026
1027 <!-- *********************************************************************** -->
1028 <hr>
1029 <address>
1030   <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1031   src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1032   <a href="http://validator.w3.org/check/referer"><img
1033   src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1034
1035   <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1036   <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a><br>
1037   <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
1038   Last modified: $Date: 2007-10-17 11:05:13 -0700 (Wed, 17 Oct 2007) $
1039 </address>
1040 </body>
1041 </html>