blob: 45ee6e9ffc31c85c2cb838982fde4a1ab12b2a03 [file] [log] [blame]
Erick Tryzelaar37c076b2008-03-30 09:57:12 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3
4<html>
5<head>
6 <title>Kaleidoscope: Implementing code generation to LLVM IR</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
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000015<h1>Kaleidoscope: Code generation to LLVM IR</h1>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +000016
17<ul>
18<li><a href="index.html">Up to Tutorial Index</a></li>
19<li>Chapter 3
20 <ol>
21 <li><a href="#intro">Chapter 3 Introduction</a></li>
22 <li><a href="#basics">Code Generation Setup</a></li>
23 <li><a href="#exprs">Expression Code Generation</a></li>
24 <li><a href="#funcs">Function Code Generation</a></li>
25 <li><a href="#driver">Driver Changes and Closing Thoughts</a></li>
26 <li><a href="#code">Full Code Listing</a></li>
27 </ol>
28</li>
Chris Lattnerff17b032008-12-12 04:20:01 +000029<li><a href="OCamlLangImpl4.html">Chapter 4</a>: Adding JIT and Optimizer
Erick Tryzelaar37c076b2008-03-30 09:57:12 +000030Support</li>
31</ul>
32
33<div class="doc_author">
34 <p>
35 Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
36 and <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a>
37 </p>
38</div>
39
40<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000041<h2><a name="intro">Chapter 3 Introduction</a></h2>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +000042<!-- *********************************************************************** -->
43
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000044<div>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +000045
46<p>Welcome to Chapter 3 of the "<a href="index.html">Implementing a language
47with LLVM</a>" tutorial. This chapter shows you how to transform the <a
48href="OCamlLangImpl2.html">Abstract Syntax Tree</a>, built in Chapter 2, into
49LLVM IR. This will teach you a little bit about how LLVM does things, as well
50as demonstrate how easy it is to use. It's much more work to build a lexer and
51parser than it is to generate LLVM IR code. :)
52</p>
53
54<p><b>Please note</b>: the code in this chapter and later require LLVM 2.3 or
55LLVM SVN to work. LLVM 2.2 and before will not work with it.</p>
56
57</div>
58
59<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +000060<h2><a name="basics">Code Generation Setup</a></h2>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +000061<!-- *********************************************************************** -->
62
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +000063<div>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +000064
65<p>
66In order to generate LLVM IR, we want some simple setup to get started. First
67we define virtual code generation (codegen) methods in each AST class:</p>
68
69<div class="doc_code">
70<pre>
71let rec codegen_expr = function
72 | Ast.Number n -&gt; ...
73 | Ast.Variable name -&gt; ...
74</pre>
75</div>
76
77<p>The <tt>Codegen.codegen_expr</tt> function says to emit IR for that AST node
78along with all the things it depends on, and they all return an LLVM Value
79object. "Value" is the class used to represent a "<a
80href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
81Assignment (SSA)</a> register" or "SSA value" in LLVM. The most distinct aspect
82of SSA values is that their value is computed as the related instruction
83executes, and it does not get a new value until (and if) the instruction
84re-executes. In other words, there is no way to "change" an SSA value. For
85more information, please read up on <a
86href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
87Assignment</a> - the concepts are really quite natural once you grok them.</p>
88
89<p>The
90second thing we want is an "Error" exception like we used for the parser, which
91will be used to report errors found during code generation (for example, use of
92an undeclared parameter):</p>
93
94<div class="doc_code">
95<pre>
96exception Error of string
97
Erick Tryzelaar1f3d2762009-08-19 17:32:38 +000098let the_module = create_module (global_context ()) "my cool jit"
99let builder = builder (global_context ())
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000100let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
Erick Tryzelaar9ef76b92010-03-08 19:32:18 +0000101let double_type = double_type context
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000102</pre>
103</div>
104
105<p>The static variables will be used during code generation.
106<tt>Codgen.the_module</tt> is the LLVM construct that contains all of the
107functions and global variables in a chunk of code. In many ways, it is the
108top-level structure that the LLVM IR uses to contain code.</p>
109
110<p>The <tt>Codegen.builder</tt> object is a helper object that makes it easy to
111generate LLVM instructions. Instances of the <a
Duncan Sands89f6d882008-04-13 06:22:09 +0000112href="http://llvm.org/doxygen/IRBuilder_8h-source.html"><tt>IRBuilder</tt></a>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000113class keep track of the current place to insert instructions and has methods to
114create new instructions.</p>
115
116<p>The <tt>Codegen.named_values</tt> map keeps track of which values are defined
117in the current scope and what their LLVM representation is. (In other words, it
118is a symbol table for the code). In this form of Kaleidoscope, the only things
119that can be referenced are function parameters. As such, function parameters
120will be in this map when generating code for their function body.</p>
121
122<p>
123With these basics in place, we can start talking about how to generate code for
124each expression. Note that this assumes that the <tt>Codgen.builder</tt> has
125been set up to generate code <em>into</em> something. For now, we'll assume
126that this has already been done, and we'll just use it to emit code.</p>
127
128</div>
129
130<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000131<h2><a name="exprs">Expression Code Generation</a></h2>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000132<!-- *********************************************************************** -->
133
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000134<div>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000135
136<p>Generating LLVM code for expression nodes is very straightforward: less
137than 30 lines of commented code for all four of our expression nodes. First
138we'll do numeric literals:</p>
139
140<div class="doc_code">
141<pre>
142 | Ast.Number n -&gt; const_float double_type n
143</pre>
144</div>
145
146<p>In the LLVM IR, numeric constants are represented with the
147<tt>ConstantFP</tt> class, which holds the numeric value in an <tt>APFloat</tt>
148internally (<tt>APFloat</tt> has the capability of holding floating point
149constants of <em>A</em>rbitrary <em>P</em>recision). This code basically just
150creates and returns a <tt>ConstantFP</tt>. Note that in the LLVM IR
151that constants are all uniqued together and shared. For this reason, the API
Gabor Greif97e378e2008-05-21 18:30:15 +0000152uses "the foo::get(..)" idiom instead of "new foo(..)" or "foo::Create(..)".</p>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000153
154<div class="doc_code">
155<pre>
156 | Ast.Variable name -&gt;
157 (try Hashtbl.find named_values name with
158 | Not_found -&gt; raise (Error "unknown variable name"))
159</pre>
160</div>
161
162<p>References to variables are also quite simple using LLVM. In the simple
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000163version of Kaleidoscope, we assume that the variable has already been emitted
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000164somewhere and its value is available. In practice, the only values that can be
165in the <tt>Codegen.named_values</tt> map are function arguments. This code
166simply checks to see that the specified name is in the map (if not, an unknown
167variable is being referenced) and returns the value for it. In future chapters,
168we'll add support for <a href="LangImpl5.html#for">loop induction variables</a>
169in the symbol table, and for <a href="LangImpl7.html#localvars">local
170variables</a>.</p>
171
172<div class="doc_code">
173<pre>
174 | Ast.Binary (op, lhs, rhs) -&gt;
175 let lhs_val = codegen_expr lhs in
176 let rhs_val = codegen_expr rhs in
177 begin
178 match op with
179 | '+' -&gt; build_add lhs_val rhs_val "addtmp" builder
180 | '-' -&gt; build_sub lhs_val rhs_val "subtmp" builder
181 | '*' -&gt; build_mul lhs_val rhs_val "multmp" builder
182 | '&lt;' -&gt;
183 (* Convert bool 0/1 to double 0.0 or 1.0 *)
184 let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in
185 build_uitofp i double_type "booltmp" builder
186 | _ -&gt; raise (Error "invalid binary operator")
Erick Tryzelaar35295ff2008-03-31 08:44:50 +0000187 end
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000188</pre>
189</div>
190
191<p>Binary operators start to get more interesting. The basic idea here is that
192we recursively emit code for the left-hand side of the expression, then the
193right-hand side, then we compute the result of the binary expression. In this
194code, we do a simple switch on the opcode to create the right LLVM instruction.
195</p>
196
197<p>In the example above, the LLVM builder class is starting to show its value.
Duncan Sands89f6d882008-04-13 06:22:09 +0000198IRBuilder knows where to insert the newly created instruction, all you have to
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000199do is specify what instruction to create (e.g. with <tt>Llvm.create_add</tt>),
200which operands to use (<tt>lhs</tt> and <tt>rhs</tt> here) and optionally
201provide a name for the generated instruction.</p>
202
203<p>One nice thing about LLVM is that the name is just a hint. For instance, if
204the code above emits multiple "addtmp" variables, LLVM will automatically
205provide each one with an increasing, unique numeric suffix. Local value names
206for instructions are purely optional, but it makes it much easier to read the
207IR dumps.</p>
208
209<p><a href="../LangRef.html#instref">LLVM instructions</a> are constrained by
210strict rules: for example, the Left and Right operators of
211an <a href="../LangRef.html#i_add">add instruction</a> must have the same
212type, and the result type of the add must match the operand types. Because
213all values in Kaleidoscope are doubles, this makes for very simple code for add,
214sub and mul.</p>
215
216<p>On the other hand, LLVM specifies that the <a
217href="../LangRef.html#i_fcmp">fcmp instruction</a> always returns an 'i1' value
218(a one bit integer). The problem with this is that Kaleidoscope wants the value to be a 0.0 or 1.0 value. In order to get these semantics, we combine the fcmp instruction with
219a <a href="../LangRef.html#i_uitofp">uitofp instruction</a>. This instruction
220converts its input integer into a floating point value by treating the input
221as an unsigned value. In contrast, if we used the <a
222href="../LangRef.html#i_sitofp">sitofp instruction</a>, the Kaleidoscope '&lt;'
223operator would return 0.0 and -1.0, depending on the input value.</p>
224
225<div class="doc_code">
226<pre>
227 | Ast.Call (callee, args) -&gt;
228 (* Look up the name in the module table. *)
229 let callee =
230 match lookup_function callee the_module with
231 | Some callee -&gt; callee
232 | None -&gt; raise (Error "unknown function referenced")
233 in
234 let params = params callee in
235
236 (* If argument mismatch error. *)
237 if Array.length params == Array.length args then () else
238 raise (Error "incorrect # arguments passed");
239 let args = Array.map codegen_expr args in
240 build_call callee args "calltmp" builder
241</pre>
242</div>
243
244<p>Code generation for function calls is quite straightforward with LLVM. The
245code above initially does a function name lookup in the LLVM Module's symbol
246table. Recall that the LLVM Module is the container that holds all of the
247functions we are JIT'ing. By giving each function the same name as what the
248user specifies, we can use the LLVM symbol table to resolve function names for
249us.</p>
250
251<p>Once we have the function to call, we recursively codegen each argument that
252is to be passed in, and create an LLVM <a href="../LangRef.html#i_call">call
253instruction</a>. Note that LLVM uses the native C calling conventions by
254default, allowing these calls to also call into standard library functions like
255"sin" and "cos", with no additional effort.</p>
256
257<p>This wraps up our handling of the four basic expressions that we have so far
258in Kaleidoscope. Feel free to go in and add some more. For example, by
259browsing the <a href="../LangRef.html">LLVM language reference</a> you'll find
260several other interesting instructions that are really easy to plug into our
261basic framework.</p>
262
263</div>
264
265<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000266<h2><a name="funcs">Function Code Generation</a></h2>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000267<!-- *********************************************************************** -->
268
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000269<div>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000270
271<p>Code generation for prototypes and functions must handle a number of
272details, which make their code less beautiful than expression code
273generation, but allows us to illustrate some important points. First, lets
274talk about code generation for prototypes: they are used both for function
275bodies and external function declarations. The code starts with:</p>
276
277<div class="doc_code">
278<pre>
279let codegen_proto = function
280 | Ast.Prototype (name, args) -&gt;
281 (* Make the function type: double(double,double) etc. *)
282 let doubles = Array.make (Array.length args) double_type in
283 let ft = function_type double_type doubles in
Erick Tryzelaar35295ff2008-03-31 08:44:50 +0000284 let f =
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000285 match lookup_function name the_module with
286</pre>
287</div>
288
289<p>This code packs a lot of power into a few lines. Note first that this
290function returns a "Function*" instead of a "Value*" (although at the moment
291they both are modeled by <tt>llvalue</tt> in ocaml). Because a "prototype"
292really talks about the external interface for a function (not the value computed
293by an expression), it makes sense for it to return the LLVM Function it
294corresponds to when codegen'd.</p>
295
296<p>The call to <tt>Llvm.function_type</tt> creates the <tt>Llvm.llvalue</tt>
297that should be used for a given Prototype. Since all function arguments in
298Kaleidoscope are of type double, the first line creates a vector of "N" LLVM
299double types. It then uses the <tt>Llvm.function_type</tt> method to create a
300function type that takes "N" doubles as arguments, returns one double as a
301result, and that is not vararg (that uses the function
302<tt>Llvm.var_arg_function_type</tt>). Note that Types in LLVM are uniqued just
303like <tt>Constant</tt>s are, so you don't "new" a type, you "get" it.</p>
304
305<p>The final line above checks if the function has already been defined in
306<tt>Codegen.the_module</tt>. If not, we will create it.</p>
307
308<div class="doc_code">
309<pre>
310 | None -&gt; declare_function name ft the_module
311</pre>
312</div>
313
314<p>This indicates the type and name to use, as well as which module to insert
315into. By default we assume a function has
316<tt>Llvm.Linkage.ExternalLinkage</tt>. "<a href="LangRef.html#linkage">external
317linkage</a>" means that the function may be defined outside the current module
318and/or that it is callable by functions outside the module. The "<tt>name</tt>"
319passed in is the name the user specified: this name is registered in
320"<tt>Codegen.the_module</tt>"s symbol table, which is used by the function call
321code above.</p>
322
323<p>In Kaleidoscope, I choose to allow redefinitions of functions in two cases:
324first, we want to allow 'extern'ing a function more than once, as long as the
325prototypes for the externs match (since all arguments have the same type, we
326just have to check that the number of arguments match). Second, we want to
Benjamin Kramer8040cd32009-10-12 14:46:08 +0000327allow 'extern'ing a function and then defining a body for it. This is useful
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000328when defining mutually recursive functions.</p>
329
330<div class="doc_code">
331<pre>
332 (* If 'f' conflicted, there was already something named 'name'. If it
333 * has a body, don't allow redefinition or reextern. *)
334 | Some f -&gt;
335 (* If 'f' already has a body, reject this. *)
336 if Array.length (basic_blocks f) == 0 then () else
337 raise (Error "redefinition of function");
338
339 (* If 'f' took a different number of arguments, reject. *)
340 if Array.length (params f) == Array.length args then () else
341 raise (Error "redefinition of function with different # args");
342 f
343 in
344</pre>
345</div>
346
347<p>In order to verify the logic above, we first check to see if the pre-existing
348function is "empty". In this case, empty means that it has no basic blocks in
349it, which means it has no body. If it has no body, it is a forward
350declaration. Since we don't allow anything after a full definition of the
351function, the code rejects this case. If the previous reference to a function
352was an 'extern', we simply verify that the number of arguments for that
353definition and this one match up. If not, we emit an error.</p>
354
355<div class="doc_code">
356<pre>
357 (* Set names for all arguments. *)
358 Array.iteri (fun i a -&gt;
359 let n = args.(i) in
360 set_value_name n a;
361 Hashtbl.add named_values n a;
362 ) (params f);
363 f
364</pre>
365</div>
366
367<p>The last bit of code for prototypes loops over all of the arguments in the
368function, setting the name of the LLVM Argument objects to match, and registering
369the arguments in the <tt>Codegen.named_values</tt> map for future use by the
370<tt>Ast.Variable</tt> variant. Once this is set up, it returns the Function
371object to the caller. Note that we don't check for conflicting
372argument names here (e.g. "extern foo(a b a)"). Doing so would be very
373straight-forward with the mechanics we have already used above.</p>
374
375<div class="doc_code">
376<pre>
377let codegen_func = function
378 | Ast.Function (proto, body) -&gt;
379 Hashtbl.clear named_values;
380 let the_function = codegen_proto proto in
381</pre>
382</div>
383
384<p>Code generation for function definitions starts out simply enough: we just
385codegen the prototype (Proto) and verify that it is ok. We then clear out the
386<tt>Codegen.named_values</tt> map to make sure that there isn't anything in it
387from the last function we compiled. Code generation of the prototype ensures
388that there is an LLVM Function object that is ready to go for us.</p>
389
390<div class="doc_code">
391<pre>
392 (* Create a new basic block to start insertion into. *)
Erick Tryzelaar9ef76b92010-03-08 19:32:18 +0000393 let bb = append_block context "entry" the_function in
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000394 position_at_end bb builder;
395
396 try
397 let ret_val = codegen_expr body in
398</pre>
399</div>
400
401<p>Now we get to the point where the <tt>Codegen.builder</tt> is set up. The
402first line creates a new
403<a href="http://en.wikipedia.org/wiki/Basic_block">basic block</a> (named
404"entry"), which is inserted into <tt>the_function</tt>. The second line then
405tells the builder that new instructions should be inserted into the end of the
406new basic block. Basic blocks in LLVM are an important part of functions that
407define the <a
408href="http://en.wikipedia.org/wiki/Control_flow_graph">Control Flow Graph</a>.
409Since we don't have any control flow, our functions will only contain one
410block at this point. We'll fix this in <a href="OCamlLangImpl5.html">Chapter
4115</a> :).</p>
412
413<div class="doc_code">
414<pre>
415 let ret_val = codegen_expr body in
416
417 (* Finish off the function. *)
418 let _ = build_ret ret_val builder in
419
420 (* Validate the generated code, checking for consistency. *)
421 Llvm_analysis.assert_valid_function the_function;
422
423 the_function
424</pre>
425</div>
426
427<p>Once the insertion point is set up, we call the <tt>Codegen.codegen_func</tt>
428method for the root expression of the function. If no error happens, this emits
429code to compute the expression into the entry block and returns the value that
430was computed. Assuming no error, we then create an LLVM <a
431href="../LangRef.html#i_ret">ret instruction</a>, which completes the function.
432Once the function is built, we call
433<tt>Llvm_analysis.assert_valid_function</tt>, which is provided by LLVM. This
434function does a variety of consistency checks on the generated code, to
435determine if our compiler is doing everything right. Using this is important:
436it can catch a lot of bugs. Once the function is finished and validated, we
437return it.</p>
438
439<div class="doc_code">
440<pre>
441 with e -&gt;
442 delete_function the_function;
443 raise e
444</pre>
445</div>
446
447<p>The only piece left here is handling of the error case. For simplicity, we
448handle this by merely deleting the function we produced with the
449<tt>Llvm.delete_function</tt> method. This allows the user to redefine a
450function that they incorrectly typed in before: if we didn't delete it, it
451would live in the symbol table, with a body, preventing future redefinition.</p>
452
453<p>This code does have a bug, though. Since the <tt>Codegen.codegen_proto</tt>
454can return a previously defined forward declaration, our code can actually delete
455a forward declaration. There are a number of ways to fix this bug, see what you
456can come up with! Here is a testcase:</p>
457
458<div class="doc_code">
459<pre>
460extern foo(a b); # ok, defines foo.
461def foo(a b) c; # error, 'c' is invalid.
462def bar() foo(1, 2); # error, unknown function "foo"
463</pre>
464</div>
465
466</div>
467
468<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000469<h2><a name="driver">Driver Changes and Closing Thoughts</a></h2>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000470<!-- *********************************************************************** -->
471
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000472<div>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000473
474<p>
475For now, code generation to LLVM doesn't really get us much, except that we can
476look at the pretty IR calls. The sample code inserts calls to Codegen into the
477"<tt>Toplevel.main_loop</tt>", and then dumps out the LLVM IR. This gives a
478nice way to look at the LLVM IR for simple functions. For example:
479</p>
480
481<div class="doc_code">
482<pre>
483ready&gt; <b>4+5</b>;
484Read top-level expression:
485define double @""() {
486entry:
Dan Gohmana9445e12010-03-02 01:11:08 +0000487 %addtmp = fadd double 4.000000e+00, 5.000000e+00
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000488 ret double %addtmp
489}
490</pre>
491</div>
492
493<p>Note how the parser turns the top-level expression into anonymous functions
Chris Lattnerff17b032008-12-12 04:20:01 +0000494for us. This will be handy when we add <a href="OCamlLangImpl4.html#jit">JIT
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000495support</a> in the next chapter. Also note that the code is very literally
496transcribed, no optimizations are being performed. We will
497<a href="OCamlLangImpl4.html#trivialconstfold">add optimizations</a> explicitly
498in the next chapter.</p>
499
500<div class="doc_code">
501<pre>
502ready&gt; <b>def foo(a b) a*a + 2*a*b + b*b;</b>
503Read function definition:
504define double @foo(double %a, double %b) {
505entry:
Dan Gohmana9445e12010-03-02 01:11:08 +0000506 %multmp = fmul double %a, %a
507 %multmp1 = fmul double 2.000000e+00, %a
508 %multmp2 = fmul double %multmp1, %b
509 %addtmp = fadd double %multmp, %multmp2
510 %multmp3 = fmul double %b, %b
511 %addtmp4 = fadd double %addtmp, %multmp3
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000512 ret double %addtmp4
513}
514</pre>
515</div>
516
517<p>This shows some simple arithmetic. Notice the striking similarity to the
518LLVM builder calls that we use to create the instructions.</p>
519
520<div class="doc_code">
521<pre>
522ready&gt; <b>def bar(a) foo(a, 4.0) + bar(31337);</b>
523Read function definition:
524define double @bar(double %a) {
525entry:
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000526 %calltmp = call double @foo(double %a, double 4.000000e+00)
527 %calltmp1 = call double @bar(double 3.133700e+04)
Dan Gohmana9445e12010-03-02 01:11:08 +0000528 %addtmp = fadd double %calltmp, %calltmp1
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000529 ret double %addtmp
530}
531</pre>
532</div>
533
534<p>This shows some function calls. Note that this function will take a long
535time to execute if you call it. In the future we'll add conditional control
536flow to actually make recursion useful :).</p>
537
538<div class="doc_code">
539<pre>
540ready&gt; <b>extern cos(x);</b>
541Read extern:
542declare double @cos(double)
543
544ready&gt; <b>cos(1.234);</b>
545Read top-level expression:
546define double @""() {
547entry:
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000548 %calltmp = call double @cos(double 1.234000e+00)
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000549 ret double %calltmp
550}
551</pre>
552</div>
553
554<p>This shows an extern for the libm "cos" function, and a call to it.</p>
555
556
557<div class="doc_code">
558<pre>
559ready&gt; <b>^D</b>
560; ModuleID = 'my cool jit'
561
562define double @""() {
563entry:
Dan Gohmana9445e12010-03-02 01:11:08 +0000564 %addtmp = fadd double 4.000000e+00, 5.000000e+00
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000565 ret double %addtmp
566}
567
568define double @foo(double %a, double %b) {
569entry:
Dan Gohmana9445e12010-03-02 01:11:08 +0000570 %multmp = fmul double %a, %a
571 %multmp1 = fmul double 2.000000e+00, %a
572 %multmp2 = fmul double %multmp1, %b
573 %addtmp = fadd double %multmp, %multmp2
574 %multmp3 = fmul double %b, %b
575 %addtmp4 = fadd double %addtmp, %multmp3
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000576 ret double %addtmp4
577}
578
579define double @bar(double %a) {
580entry:
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000581 %calltmp = call double @foo(double %a, double 4.000000e+00)
582 %calltmp1 = call double @bar(double 3.133700e+04)
Dan Gohmana9445e12010-03-02 01:11:08 +0000583 %addtmp = fadd double %calltmp, %calltmp1
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000584 ret double %addtmp
585}
586
587declare double @cos(double)
588
589define double @""() {
590entry:
Dan Gohman3dfb3cf2010-05-28 17:07:41 +0000591 %calltmp = call double @cos(double 1.234000e+00)
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000592 ret double %calltmp
593}
594</pre>
595</div>
596
597<p>When you quit the current demo, it dumps out the IR for the entire module
598generated. Here you can see the big picture with all the functions referencing
599each other.</p>
600
601<p>This wraps up the third chapter of the Kaleidoscope tutorial. Up next, we'll
Chris Lattnerff17b032008-12-12 04:20:01 +0000602describe how to <a href="OCamlLangImpl4.html">add JIT codegen and optimizer
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000603support</a> to this so we can actually start running code!</p>
604
605</div>
606
607
608<!-- *********************************************************************** -->
NAKAMURA Takumi05d02652011-04-18 23:59:50 +0000609<h2><a name="code">Full Code Listing</a></h2>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000610<!-- *********************************************************************** -->
611
NAKAMURA Takumif5af6ad2011-04-23 00:30:22 +0000612<div>
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000613
614<p>
615Here is the complete code listing for our running example, enhanced with the
616LLVM code generator. Because this uses the LLVM libraries, we need to link
617them in. To do this, we use the <a
618href="http://llvm.org/cmds/llvm-config.html">llvm-config</a> tool to inform
619our makefile/command line about which options to use:</p>
620
621<div class="doc_code">
622<pre>
623# Compile
624ocamlbuild toy.byte
625# Run
626./toy.byte
627</pre>
628</div>
629
630<p>Here is the code:</p>
631
632<dl>
633<dt>_tags:</dt>
634<dd class="doc_code">
635<pre>
636&lt;{lexer,parser}.ml&gt;: use_camlp4, pp(camlp4of)
637&lt;*.{byte,native}&gt;: g++, use_llvm, use_llvm_analysis
638</pre>
639</dd>
640
641<dt>myocamlbuild.ml:</dt>
642<dd class="doc_code">
643<pre>
644open Ocamlbuild_plugin;;
645
646ocaml_lib ~extern:true "llvm";;
647ocaml_lib ~extern:true "llvm_analysis";;
648
649flag ["link"; "ocaml"; "g++"] (S[A"-cc"; A"g++"]);;
650</pre>
651</dd>
652
653<dt>token.ml:</dt>
654<dd class="doc_code">
655<pre>
656(*===----------------------------------------------------------------------===
657 * Lexer Tokens
658 *===----------------------------------------------------------------------===*)
659
660(* The lexer returns these 'Kwd' if it is an unknown character, otherwise one of
661 * these others for known things. *)
662type token =
663 (* commands *)
664 | Def | Extern
665
666 (* primary *)
667 | Ident of string | Number of float
668
669 (* unknown *)
670 | Kwd of char
671</pre>
672</dd>
673
674<dt>lexer.ml:</dt>
675<dd class="doc_code">
676<pre>
677(*===----------------------------------------------------------------------===
678 * Lexer
679 *===----------------------------------------------------------------------===*)
680
681let rec lex = parser
682 (* Skip any whitespace. *)
683 | [&lt; ' (' ' | '\n' | '\r' | '\t'); stream &gt;] -&gt; lex stream
684
685 (* identifier: [a-zA-Z][a-zA-Z0-9] *)
686 | [&lt; ' ('A' .. 'Z' | 'a' .. 'z' as c); stream &gt;] -&gt;
687 let buffer = Buffer.create 1 in
688 Buffer.add_char buffer c;
689 lex_ident buffer stream
690
691 (* number: [0-9.]+ *)
692 | [&lt; ' ('0' .. '9' as c); stream &gt;] -&gt;
693 let buffer = Buffer.create 1 in
694 Buffer.add_char buffer c;
695 lex_number buffer stream
696
697 (* Comment until end of line. *)
698 | [&lt; ' ('#'); stream &gt;] -&gt;
699 lex_comment stream
700
701 (* Otherwise, just return the character as its ascii value. *)
702 | [&lt; 'c; stream &gt;] -&gt;
703 [&lt; 'Token.Kwd c; lex stream &gt;]
704
705 (* end of stream. *)
706 | [&lt; &gt;] -&gt; [&lt; &gt;]
707
708and lex_number buffer = parser
709 | [&lt; ' ('0' .. '9' | '.' as c); stream &gt;] -&gt;
710 Buffer.add_char buffer c;
711 lex_number buffer stream
712 | [&lt; stream=lex &gt;] -&gt;
713 [&lt; 'Token.Number (float_of_string (Buffer.contents buffer)); stream &gt;]
714
715and lex_ident buffer = parser
716 | [&lt; ' ('A' .. 'Z' | 'a' .. 'z' | '0' .. '9' as c); stream &gt;] -&gt;
717 Buffer.add_char buffer c;
718 lex_ident buffer stream
719 | [&lt; stream=lex &gt;] -&gt;
720 match Buffer.contents buffer with
721 | "def" -&gt; [&lt; 'Token.Def; stream &gt;]
722 | "extern" -&gt; [&lt; 'Token.Extern; stream &gt;]
723 | id -&gt; [&lt; 'Token.Ident id; stream &gt;]
724
725and lex_comment = parser
726 | [&lt; ' ('\n'); stream=lex &gt;] -&gt; stream
727 | [&lt; 'c; e=lex_comment &gt;] -&gt; e
728 | [&lt; &gt;] -&gt; [&lt; &gt;]
729</pre>
730</dd>
731
732<dt>ast.ml:</dt>
733<dd class="doc_code">
734<pre>
735(*===----------------------------------------------------------------------===
736 * Abstract Syntax Tree (aka Parse Tree)
737 *===----------------------------------------------------------------------===*)
738
739(* expr - Base type for all expression nodes. *)
740type expr =
741 (* variant for numeric literals like "1.0". *)
742 | Number of float
743
744 (* variant for referencing a variable, like "a". *)
745 | Variable of string
746
747 (* variant for a binary operator. *)
748 | Binary of char * expr * expr
749
750 (* variant for function calls. *)
751 | Call of string * expr array
752
753(* proto - This type represents the "prototype" for a function, which captures
754 * its name, and its argument names (thus implicitly the number of arguments the
755 * function takes). *)
756type proto = Prototype of string * string array
757
758(* func - This type represents a function definition itself. *)
759type func = Function of proto * expr
760</pre>
761</dd>
762
763<dt>parser.ml:</dt>
764<dd class="doc_code">
765<pre>
766(*===---------------------------------------------------------------------===
767 * Parser
768 *===---------------------------------------------------------------------===*)
769
770(* binop_precedence - This holds the precedence for each binary operator that is
771 * defined *)
772let binop_precedence:(char, int) Hashtbl.t = Hashtbl.create 10
773
774(* precedence - Get the precedence of the pending binary operator token. *)
775let precedence c = try Hashtbl.find binop_precedence c with Not_found -&gt; -1
776
777(* primary
778 * ::= identifier
779 * ::= numberexpr
780 * ::= parenexpr *)
781let rec parse_primary = parser
782 (* numberexpr ::= number *)
783 | [&lt; 'Token.Number n &gt;] -&gt; Ast.Number n
784
785 (* parenexpr ::= '(' expression ')' *)
786 | [&lt; 'Token.Kwd '('; e=parse_expr; 'Token.Kwd ')' ?? "expected ')'" &gt;] -&gt; e
787
788 (* identifierexpr
789 * ::= identifier
790 * ::= identifier '(' argumentexpr ')' *)
791 | [&lt; 'Token.Ident id; stream &gt;] -&gt;
792 let rec parse_args accumulator = parser
793 | [&lt; e=parse_expr; stream &gt;] -&gt;
794 begin parser
795 | [&lt; 'Token.Kwd ','; e=parse_args (e :: accumulator) &gt;] -&gt; e
796 | [&lt; &gt;] -&gt; e :: accumulator
797 end stream
798 | [&lt; &gt;] -&gt; accumulator
799 in
800 let rec parse_ident id = parser
801 (* Call. *)
802 | [&lt; 'Token.Kwd '(';
803 args=parse_args [];
804 'Token.Kwd ')' ?? "expected ')'"&gt;] -&gt;
805 Ast.Call (id, Array.of_list (List.rev args))
806
807 (* Simple variable ref. *)
808 | [&lt; &gt;] -&gt; Ast.Variable id
809 in
810 parse_ident id stream
811
812 | [&lt; &gt;] -&gt; raise (Stream.Error "unknown token when expecting an expression.")
813
814(* binoprhs
815 * ::= ('+' primary)* *)
816and parse_bin_rhs expr_prec lhs stream =
817 match Stream.peek stream with
818 (* If this is a binop, find its precedence. *)
819 | Some (Token.Kwd c) when Hashtbl.mem binop_precedence c -&gt;
820 let token_prec = precedence c in
821
822 (* If this is a binop that binds at least as tightly as the current binop,
823 * consume it, otherwise we are done. *)
824 if token_prec &lt; expr_prec then lhs else begin
825 (* Eat the binop. *)
826 Stream.junk stream;
827
828 (* Parse the primary expression after the binary operator. *)
829 let rhs = parse_primary stream in
830
831 (* Okay, we know this is a binop. *)
832 let rhs =
833 match Stream.peek stream with
834 | Some (Token.Kwd c2) -&gt;
835 (* If BinOp binds less tightly with rhs than the operator after
836 * rhs, let the pending operator take rhs as its lhs. *)
837 let next_prec = precedence c2 in
838 if token_prec &lt; next_prec
839 then parse_bin_rhs (token_prec + 1) rhs stream
840 else rhs
841 | _ -&gt; rhs
842 in
843
844 (* Merge lhs/rhs. *)
845 let lhs = Ast.Binary (c, lhs, rhs) in
846 parse_bin_rhs expr_prec lhs stream
847 end
848 | _ -&gt; lhs
849
850(* expression
851 * ::= primary binoprhs *)
852and parse_expr = parser
853 | [&lt; lhs=parse_primary; stream &gt;] -&gt; parse_bin_rhs 0 lhs stream
854
855(* prototype
856 * ::= id '(' id* ')' *)
857let parse_prototype =
858 let rec parse_args accumulator = parser
859 | [&lt; 'Token.Ident id; e=parse_args (id::accumulator) &gt;] -&gt; e
860 | [&lt; &gt;] -&gt; accumulator
861 in
862
863 parser
864 | [&lt; 'Token.Ident id;
865 'Token.Kwd '(' ?? "expected '(' in prototype";
866 args=parse_args [];
867 'Token.Kwd ')' ?? "expected ')' in prototype" &gt;] -&gt;
868 (* success. *)
869 Ast.Prototype (id, Array.of_list (List.rev args))
870
871 | [&lt; &gt;] -&gt;
872 raise (Stream.Error "expected function name in prototype")
873
874(* definition ::= 'def' prototype expression *)
875let parse_definition = parser
876 | [&lt; 'Token.Def; p=parse_prototype; e=parse_expr &gt;] -&gt;
877 Ast.Function (p, e)
878
879(* toplevelexpr ::= expression *)
880let parse_toplevel = parser
881 | [&lt; e=parse_expr &gt;] -&gt;
882 (* Make an anonymous proto. *)
883 Ast.Function (Ast.Prototype ("", [||]), e)
884
885(* external ::= 'extern' prototype *)
886let parse_extern = parser
887 | [&lt; 'Token.Extern; e=parse_prototype &gt;] -&gt; e
888</pre>
889</dd>
890
891<dt>codegen.ml:</dt>
892<dd class="doc_code">
893<pre>
894(*===----------------------------------------------------------------------===
895 * Code Generation
896 *===----------------------------------------------------------------------===*)
897
898open Llvm
899
900exception Error of string
901
Erick Tryzelaar1f3d2762009-08-19 17:32:38 +0000902let context = global_context ()
903let the_module = create_module context "my cool jit"
904let builder = builder context
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000905let named_values:(string, llvalue) Hashtbl.t = Hashtbl.create 10
Erick Tryzelaar9ef76b92010-03-08 19:32:18 +0000906let double_type = double_type context
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000907
908let rec codegen_expr = function
909 | Ast.Number n -&gt; const_float double_type n
910 | Ast.Variable name -&gt;
911 (try Hashtbl.find named_values name with
912 | Not_found -&gt; raise (Error "unknown variable name"))
913 | Ast.Binary (op, lhs, rhs) -&gt;
914 let lhs_val = codegen_expr lhs in
915 let rhs_val = codegen_expr rhs in
916 begin
917 match op with
918 | '+' -&gt; build_add lhs_val rhs_val "addtmp" builder
919 | '-' -&gt; build_sub lhs_val rhs_val "subtmp" builder
920 | '*' -&gt; build_mul lhs_val rhs_val "multmp" builder
921 | '&lt;' -&gt;
922 (* Convert bool 0/1 to double 0.0 or 1.0 *)
923 let i = build_fcmp Fcmp.Ult lhs_val rhs_val "cmptmp" builder in
924 build_uitofp i double_type "booltmp" builder
925 | _ -&gt; raise (Error "invalid binary operator")
926 end
927 | Ast.Call (callee, args) -&gt;
928 (* Look up the name in the module table. *)
929 let callee =
930 match lookup_function callee the_module with
931 | Some callee -&gt; callee
932 | None -&gt; raise (Error "unknown function referenced")
933 in
934 let params = params callee in
935
936 (* If argument mismatch error. *)
937 if Array.length params == Array.length args then () else
938 raise (Error "incorrect # arguments passed");
939 let args = Array.map codegen_expr args in
940 build_call callee args "calltmp" builder
941
942let codegen_proto = function
943 | Ast.Prototype (name, args) -&gt;
944 (* Make the function type: double(double,double) etc. *)
945 let doubles = Array.make (Array.length args) double_type in
946 let ft = function_type double_type doubles in
947 let f =
948 match lookup_function name the_module with
949 | None -&gt; declare_function name ft the_module
950
951 (* If 'f' conflicted, there was already something named 'name'. If it
952 * has a body, don't allow redefinition or reextern. *)
953 | Some f -&gt;
954 (* If 'f' already has a body, reject this. *)
955 if block_begin f &lt;&gt; At_end f then
956 raise (Error "redefinition of function");
957
958 (* If 'f' took a different number of arguments, reject. *)
959 if element_type (type_of f) &lt;&gt; ft then
960 raise (Error "redefinition of function with different # args");
961 f
962 in
963
964 (* Set names for all arguments. *)
965 Array.iteri (fun i a -&gt;
966 let n = args.(i) in
967 set_value_name n a;
968 Hashtbl.add named_values n a;
969 ) (params f);
970 f
971
972let codegen_func = function
973 | Ast.Function (proto, body) -&gt;
974 Hashtbl.clear named_values;
975 let the_function = codegen_proto proto in
976
977 (* Create a new basic block to start insertion into. *)
Erick Tryzelaar9ef76b92010-03-08 19:32:18 +0000978 let bb = append_block context "entry" the_function in
Erick Tryzelaar37c076b2008-03-30 09:57:12 +0000979 position_at_end bb builder;
980
981 try
982 let ret_val = codegen_expr body in
983
984 (* Finish off the function. *)
985 let _ = build_ret ret_val builder in
986
987 (* Validate the generated code, checking for consistency. *)
988 Llvm_analysis.assert_valid_function the_function;
989
990 the_function
991 with e -&gt;
992 delete_function the_function;
993 raise e
994</pre>
995</dd>
996
997<dt>toplevel.ml:</dt>
998<dd class="doc_code">
999<pre>
1000(*===----------------------------------------------------------------------===
1001 * Top-Level parsing and JIT Driver
1002 *===----------------------------------------------------------------------===*)
1003
1004open Llvm
1005
1006(* top ::= definition | external | expression | ';' *)
1007let rec main_loop stream =
1008 match Stream.peek stream with
1009 | None -&gt; ()
1010
1011 (* ignore top-level semicolons. *)
1012 | Some (Token.Kwd ';') -&gt;
1013 Stream.junk stream;
1014 main_loop stream
1015
1016 | Some token -&gt;
1017 begin
1018 try match token with
1019 | Token.Def -&gt;
1020 let e = Parser.parse_definition stream in
1021 print_endline "parsed a function definition.";
1022 dump_value (Codegen.codegen_func e);
1023 | Token.Extern -&gt;
1024 let e = Parser.parse_extern stream in
1025 print_endline "parsed an extern.";
1026 dump_value (Codegen.codegen_proto e);
1027 | _ -&gt;
1028 (* Evaluate a top-level expression into an anonymous function. *)
1029 let e = Parser.parse_toplevel stream in
1030 print_endline "parsed a top-level expr";
1031 dump_value (Codegen.codegen_func e);
1032 with Stream.Error s | Codegen.Error s -&gt;
1033 (* Skip token for error recovery. *)
1034 Stream.junk stream;
1035 print_endline s;
1036 end;
1037 print_string "ready&gt; "; flush stdout;
1038 main_loop stream
1039</pre>
1040</dd>
1041
1042<dt>toy.ml:</dt>
1043<dd class="doc_code">
1044<pre>
1045(*===----------------------------------------------------------------------===
1046 * Main driver code.
1047 *===----------------------------------------------------------------------===*)
1048
1049open Llvm
1050
1051let main () =
1052 (* Install standard binary operators.
1053 * 1 is the lowest precedence. *)
1054 Hashtbl.add Parser.binop_precedence '&lt;' 10;
1055 Hashtbl.add Parser.binop_precedence '+' 20;
1056 Hashtbl.add Parser.binop_precedence '-' 20;
1057 Hashtbl.add Parser.binop_precedence '*' 40; (* highest. *)
1058
1059 (* Prime the first token. *)
1060 print_string "ready&gt; "; flush stdout;
1061 let stream = Lexer.lex (Stream.of_channel stdin) in
1062
1063 (* Run the main "interpreter loop" now. *)
1064 Toplevel.main_loop stream;
1065
1066 (* Print out all the generated code. *)
1067 dump_module Codegen.the_module
1068;;
1069
1070main ()
1071</pre>
1072</dd>
1073</dl>
1074
1075<a href="OCamlLangImpl4.html">Next: Adding JIT and Optimizer Support</a>
1076</div>
1077
1078<!-- *********************************************************************** -->
1079<hr>
1080<address>
1081 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
1082 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
1083 <a href="http://validator.w3.org/check/referer"><img
1084 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
1085
1086 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
1087 <a href="mailto:idadesub@users.sourceforge.net">Erick Tryzelaar</a><br>
NAKAMURA Takumib9a33632011-04-09 02:13:37 +00001088 <a href="http://llvm.org/">The LLVM Compiler Infrastructure</a><br>
Dan Gohman523e3922010-02-03 17:27:31 +00001089 Last modified: $Date$
Erick Tryzelaar37c076b2008-03-30 09:57:12 +00001090</address>
1091</body>
1092</html>