blob: 211d14a3f5577b1a7b300b2954bb26c3c8f263fb [file] [log] [blame]
Chris Lattner00c992d2007-11-03 08:55:29 +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: Extending the Language: Mutable Variables / SSA
7 construction</title>
8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
9 <meta name="author" content="Chris Lattner">
10 <link rel="stylesheet" href="../llvm.css" type="text/css">
11</head>
12
13<body>
14
15<div class="doc_title">Kaleidoscope: Extending the Language: Mutable Variables</div>
16
Chris Lattner128eb862007-11-05 19:06:59 +000017<ul>
Chris Lattner0e555b12007-11-05 20:04:56 +000018<li><a href="index.html">Up to Tutorial Index</a></li>
Chris Lattner128eb862007-11-05 19:06:59 +000019<li>Chapter 7
20 <ol>
21 <li><a href="#intro">Chapter 7 Introduction</a></li>
22 <li><a href="#why">Why is this a hard problem?</a></li>
23 <li><a href="#memory">Memory in LLVM</a></li>
24 <li><a href="#kalvars">Mutable Variables in Kaleidoscope</a></li>
25 <li><a href="#adjustments">Adjusting Existing Variables for
26 Mutation</a></li>
27 <li><a href="#assignment">New Assignment Operator</a></li>
28 <li><a href="#localvars">User-defined Local Variables</a></li>
29 <li><a href="#code">Full Code Listing</a></li>
30 </ol>
31</li>
Chris Lattner0e555b12007-11-05 20:04:56 +000032<li><a href="LangImpl8.html">Chapter 8</a>: Conclusion and other useful LLVM
33 tidbits</li>
Chris Lattner128eb862007-11-05 19:06:59 +000034</ul>
35
Chris Lattner00c992d2007-11-03 08:55:29 +000036<div class="doc_author">
37 <p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
38</div>
39
40<!-- *********************************************************************** -->
Chris Lattner128eb862007-11-05 19:06:59 +000041<div class="doc_section"><a name="intro">Chapter 7 Introduction</a></div>
Chris Lattner00c992d2007-11-03 08:55:29 +000042<!-- *********************************************************************** -->
43
44<div class="doc_text">
45
Chris Lattner128eb862007-11-05 19:06:59 +000046<p>Welcome to Chapter 7 of the "<a href="index.html">Implementing a language
47with LLVM</a>" tutorial. In chapters 1 through 6, we've built a very
48respectable, albeit simple, <a
Chris Lattner00c992d2007-11-03 08:55:29 +000049href="http://en.wikipedia.org/wiki/Functional_programming">functional
50programming language</a>. In our journey, we learned some parsing techniques,
51how to build and represent an AST, how to build LLVM IR, and how to optimize
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +000052the resultant code as well as JIT compile it.</p>
Chris Lattner00c992d2007-11-03 08:55:29 +000053
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +000054<p>While Kaleidoscope is interesting as a functional language, the fact that it
55is functional makes it "too easy" to generate LLVM IR for it. In particular, a
56functional language makes it very easy to build LLVM IR directly in <a
Chris Lattner00c992d2007-11-03 08:55:29 +000057href="http://en.wikipedia.org/wiki/Static_single_assignment_form">SSA form</a>.
58Since LLVM requires that the input code be in SSA form, this is a very nice
59property and it is often unclear to newcomers how to generate code for an
60imperative language with mutable variables.</p>
61
62<p>The short (and happy) summary of this chapter is that there is no need for
63your front-end to build SSA form: LLVM provides highly tuned and well tested
64support for this, though the way it works is a bit unexpected for some.</p>
65
66</div>
67
68<!-- *********************************************************************** -->
69<div class="doc_section"><a name="why">Why is this a hard problem?</a></div>
70<!-- *********************************************************************** -->
71
72<div class="doc_text">
73
74<p>
75To understand why mutable variables cause complexities in SSA construction,
76consider this extremely simple C example:
77</p>
78
79<div class="doc_code">
80<pre>
81int G, H;
82int test(_Bool Condition) {
83 int X;
84 if (Condition)
85 X = G;
86 else
87 X = H;
88 return X;
89}
90</pre>
91</div>
92
93<p>In this case, we have the variable "X", whose value depends on the path
94executed in the program. Because there are two different possible values for X
95before the return instruction, a PHI node is inserted to merge the two values.
96The LLVM IR that we want for this example looks like this:</p>
97
98<div class="doc_code">
99<pre>
100@G = weak global i32 0 ; type of @G is i32*
101@H = weak global i32 0 ; type of @H is i32*
102
103define i32 @test(i1 %Condition) {
104entry:
105 br i1 %Condition, label %cond_true, label %cond_false
106
107cond_true:
108 %X.0 = load i32* @G
109 br label %cond_next
110
111cond_false:
112 %X.1 = load i32* @H
113 br label %cond_next
114
115cond_next:
116 %X.2 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
117 ret i32 %X.2
118}
119</pre>
120</div>
121
122<p>In this example, the loads from the G and H global variables are explicit in
123the LLVM IR, and they live in the then/else branches of the if statement
124(cond_true/cond_false). In order to merge the incoming values, the X.2 phi node
125in the cond_next block selects the right value to use based on where control
126flow is coming from: if control flow comes from the cond_false block, X.2 gets
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000127the value of X.1. Alternatively, if control flow comes from cond_true, it gets
Chris Lattner00c992d2007-11-03 08:55:29 +0000128the value of X.0. The intent of this chapter is not to explain the details of
129SSA form. For more information, see one of the many <a
130href="http://en.wikipedia.org/wiki/Static_single_assignment_form">online
131references</a>.</p>
132
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000133<p>The question for this article is "who places the phi nodes when lowering
Chris Lattner00c992d2007-11-03 08:55:29 +0000134assignments to mutable variables?". The issue here is that LLVM
135<em>requires</em> that its IR be in SSA form: there is no "non-ssa" mode for it.
136However, SSA construction requires non-trivial algorithms and data structures,
137so it is inconvenient and wasteful for every front-end to have to reproduce this
138logic.</p>
139
140</div>
141
142<!-- *********************************************************************** -->
143<div class="doc_section"><a name="memory">Memory in LLVM</a></div>
144<!-- *********************************************************************** -->
145
146<div class="doc_text">
147
148<p>The 'trick' here is that while LLVM does require all register values to be
149in SSA form, it does not require (or permit) memory objects to be in SSA form.
150In the example above, note that the loads from G and H are direct accesses to
151G and H: they are not renamed or versioned. This differs from some other
Chris Lattner2e5d07e2007-11-04 19:42:13 +0000152compiler systems, which do try to version memory objects. In LLVM, instead of
Chris Lattner00c992d2007-11-03 08:55:29 +0000153encoding dataflow analysis of memory into the LLVM IR, it is handled with <a
154href="../WritingAnLLVMPass.html">Analysis Passes</a> which are computed on
155demand.</p>
156
157<p>
158With this in mind, the high-level idea is that we want to make a stack variable
159(which lives in memory, because it is on the stack) for each mutable object in
160a function. To take advantage of this trick, we need to talk about how LLVM
161represents stack variables.
162</p>
163
164<p>In LLVM, all memory accesses are explicit with load/store instructions, and
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000165it is carefully designed not to have (or need) an "address-of" operator. Notice
Chris Lattner00c992d2007-11-03 08:55:29 +0000166how the type of the @G/@H global variables is actually "i32*" even though the
167variable is defined as "i32". What this means is that @G defines <em>space</em>
168for an i32 in the global data area, but its <em>name</em> actually refers to the
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000169address for that space. Stack variables work the same way, except that instead of
170being declared with global variable definitions, they are declared with the
Chris Lattner00c992d2007-11-03 08:55:29 +0000171<a href="../LangRef.html#i_alloca">LLVM alloca instruction</a>:</p>
172
173<div class="doc_code">
174<pre>
Chris Lattner1e46a6c2007-11-07 06:34:39 +0000175define i32 @example() {
Chris Lattner00c992d2007-11-03 08:55:29 +0000176entry:
177 %X = alloca i32 ; type of %X is i32*.
178 ...
179 %tmp = load i32* %X ; load the stack value %X from the stack.
180 %tmp2 = add i32 %tmp, 1 ; increment it
181 store i32 %tmp2, i32* %X ; store it back
182 ...
183</pre>
184</div>
185
186<p>This code shows an example of how you can declare and manipulate a stack
187variable in the LLVM IR. Stack memory allocated with the alloca instruction is
188fully general: you can pass the address of the stack slot to functions, you can
189store it in other variables, etc. In our example above, we could rewrite the
190example to use the alloca technique to avoid using a PHI node:</p>
191
192<div class="doc_code">
193<pre>
194@G = weak global i32 0 ; type of @G is i32*
195@H = weak global i32 0 ; type of @H is i32*
196
197define i32 @test(i1 %Condition) {
198entry:
199 %X = alloca i32 ; type of %X is i32*.
200 br i1 %Condition, label %cond_true, label %cond_false
201
202cond_true:
203 %X.0 = load i32* @G
204 store i32 %X.0, i32* %X ; Update X
205 br label %cond_next
206
207cond_false:
208 %X.1 = load i32* @H
209 store i32 %X.1, i32* %X ; Update X
210 br label %cond_next
211
212cond_next:
213 %X.2 = load i32* %X ; Read X
214 ret i32 %X.2
215}
216</pre>
217</div>
218
219<p>With this, we have discovered a way to handle arbitrary mutable variables
220without the need to create Phi nodes at all:</p>
221
222<ol>
223<li>Each mutable variable becomes a stack allocation.</li>
224<li>Each read of the variable becomes a load from the stack.</li>
225<li>Each update of the variable becomes a store to the stack.</li>
226<li>Taking the address of a variable just uses the stack address directly.</li>
227</ol>
228
229<p>While this solution has solved our immediate problem, it introduced another
230one: we have now apparently introduced a lot of stack traffic for very simple
231and common operations, a major performance problem. Fortunately for us, the
232LLVM optimizer has a highly-tuned optimization pass named "mem2reg" that handles
233this case, promoting allocas like this into SSA registers, inserting Phi nodes
234as appropriate. If you run this example through the pass, for example, you'll
235get:</p>
236
237<div class="doc_code">
238<pre>
239$ <b>llvm-as &lt; example.ll | opt -mem2reg | llvm-dis</b>
240@G = weak global i32 0
241@H = weak global i32 0
242
243define i32 @test(i1 %Condition) {
244entry:
245 br i1 %Condition, label %cond_true, label %cond_false
246
247cond_true:
248 %X.0 = load i32* @G
249 br label %cond_next
250
251cond_false:
252 %X.1 = load i32* @H
253 br label %cond_next
254
255cond_next:
256 %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
257 ret i32 %X.01
258}
259</pre>
Chris Lattnere7198312007-11-03 22:22:30 +0000260</div>
Chris Lattner00c992d2007-11-03 08:55:29 +0000261
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000262<p>The mem2reg pass implements the standard "iterated dominance frontier"
Chris Lattnere7198312007-11-03 22:22:30 +0000263algorithm for constructing SSA form and has a number of optimizations that speed
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000264up (very common) degenerate cases. The mem2reg optimization pass is the answer to dealing
265with mutable variables, and we highly recommend that you depend on it. Note that
Chris Lattnere7198312007-11-03 22:22:30 +0000266mem2reg only works on variables in certain circumstances:</p>
Chris Lattner00c992d2007-11-03 08:55:29 +0000267
Chris Lattnere7198312007-11-03 22:22:30 +0000268<ol>
269<li>mem2reg is alloca-driven: it looks for allocas and if it can handle them, it
270promotes them. It does not apply to global variables or heap allocations.</li>
Chris Lattner00c992d2007-11-03 08:55:29 +0000271
Chris Lattnere7198312007-11-03 22:22:30 +0000272<li>mem2reg only looks for alloca instructions in the entry block of the
273function. Being in the entry block guarantees that the alloca is only executed
274once, which makes analysis simpler.</li>
Chris Lattner00c992d2007-11-03 08:55:29 +0000275
Chris Lattnere7198312007-11-03 22:22:30 +0000276<li>mem2reg only promotes allocas whose uses are direct loads and stores. If
277the address of the stack object is passed to a function, or if any funny pointer
278arithmetic is involved, the alloca will not be promoted.</li>
279
Chris Lattnera56b22d2007-11-05 17:45:54 +0000280<li>mem2reg only works on allocas of <a
281href="../LangRef.html#t_classifications">first class</a>
282values (such as pointers, scalars and vectors), and only if the array size
Chris Lattnere7198312007-11-03 22:22:30 +0000283of the allocation is 1 (or missing in the .ll file). mem2reg is not capable of
284promoting structs or arrays to registers. Note that the "scalarrepl" pass is
285more powerful and can promote structs, "unions", and arrays in many cases.</li>
286
287</ol>
288
289<p>
290All of these properties are easy to satisfy for most imperative languages, and
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000291we'll illustrate it below with Kaleidoscope. The final question you may be
Chris Lattnere7198312007-11-03 22:22:30 +0000292asking is: should I bother with this nonsense for my front-end? Wouldn't it be
293better if I just did SSA construction directly, avoiding use of the mem2reg
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000294optimization pass? In short, we strongly recommend that you use this technique
Chris Lattnere7198312007-11-03 22:22:30 +0000295for building SSA form, unless there is an extremely good reason not to. Using
296this technique is:</p>
297
298<ul>
299<li>Proven and well tested: llvm-gcc and clang both use this technique for local
300mutable variables. As such, the most common clients of LLVM are using this to
301handle a bulk of their variables. You can be sure that bugs are found fast and
302fixed early.</li>
303
304<li>Extremely Fast: mem2reg has a number of special cases that make it fast in
305common cases as well as fully general. For example, it has fast-paths for
306variables that are only used in a single block, variables that only have one
307assignment point, good heuristics to avoid insertion of unneeded phi nodes, etc.
308</li>
309
310<li>Needed for debug info generation: <a href="../SourceLevelDebugging.html">
311Debug information in LLVM</a> relies on having the address of the variable
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000312exposed so that debug info can be attached to it. This technique dovetails
313very naturally with this style of debug info.</li>
Chris Lattnere7198312007-11-03 22:22:30 +0000314</ul>
315
316<p>If nothing else, this makes it much easier to get your front-end up and
317running, and is very simple to implement. Lets extend Kaleidoscope with mutable
318variables now!
Chris Lattner00c992d2007-11-03 08:55:29 +0000319</p>
Chris Lattner62a709d2007-11-05 00:23:57 +0000320
Chris Lattner00c992d2007-11-03 08:55:29 +0000321</div>
322
Chris Lattner62a709d2007-11-05 00:23:57 +0000323<!-- *********************************************************************** -->
324<div class="doc_section"><a name="kalvars">Mutable Variables in
325Kaleidoscope</a></div>
326<!-- *********************************************************************** -->
327
328<div class="doc_text">
329
330<p>Now that we know the sort of problem we want to tackle, lets see what this
331looks like in the context of our little Kaleidoscope language. We're going to
332add two features:</p>
333
334<ol>
335<li>The ability to mutate variables with the '=' operator.</li>
336<li>The ability to define new variables.</li>
337</ol>
338
339<p>While the first item is really what this is about, we only have variables
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000340for incoming arguments as well as for induction variables, and redefining those only
Chris Lattner62a709d2007-11-05 00:23:57 +0000341goes so far :). Also, the ability to define new variables is a
342useful thing regardless of whether you will be mutating them. Here's a
343motivating example that shows how we could use these:</p>
344
345<div class="doc_code">
346<pre>
347# Define ':' for sequencing: as a low-precedence operator that ignores operands
348# and just returns the RHS.
349def binary : 1 (x y) y;
350
351# Recursive fib, we could do this before.
352def fib(x)
353 if (x &lt; 3) then
354 1
355 else
356 fib(x-1)+fib(x-2);
357
358# Iterative fib.
359def fibi(x)
360 <b>var a = 1, b = 1, c in</b>
Chris Lattner1e46a6c2007-11-07 06:34:39 +0000361 (for i = 3, i &lt; x in
Chris Lattner62a709d2007-11-05 00:23:57 +0000362 <b>c = a + b</b> :
363 <b>a = b</b> :
364 <b>b = c</b>) :
365 b;
366
367# Call it.
368fibi(10);
369</pre>
370</div>
371
372<p>
373In order to mutate variables, we have to change our existing variables to use
374the "alloca trick". Once we have that, we'll add our new operator, then extend
375Kaleidoscope to support new variable definitions.
376</p>
377
378</div>
379
380<!-- *********************************************************************** -->
381<div class="doc_section"><a name="adjustments">Adjusting Existing Variables for
382Mutation</a></div>
383<!-- *********************************************************************** -->
384
385<div class="doc_text">
386
387<p>
388The symbol table in Kaleidoscope is managed at code generation time by the
389'<tt>NamedValues</tt>' map. This map currently keeps track of the LLVM "Value*"
390that holds the double value for the named variable. In order to support
391mutation, we need to change this slightly, so that it <tt>NamedValues</tt> holds
392the <em>memory location</em> of the variable in question. Note that this
393change is a refactoring: it changes the structure of the code, but does not
394(by itself) change the behavior of the compiler. All of these changes are
395isolated in the Kaleidoscope code generator.</p>
396
397<p>
398At this point in Kaleidoscope's development, it only supports variables for two
399things: incoming arguments to functions and the induction variable of 'for'
400loops. For consistency, we'll allow mutation of these variables in addition to
401other user-defined variables. This means that these will both need memory
402locations.
403</p>
404
405<p>To start our transformation of Kaleidoscope, we'll change the NamedValues
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000406map so that it maps to AllocaInst* instead of Value*. Once we do this, the C++
407compiler will tell us what parts of the code we need to update:</p>
Chris Lattner62a709d2007-11-05 00:23:57 +0000408
409<div class="doc_code">
410<pre>
411static std::map&lt;std::string, AllocaInst*&gt; NamedValues;
412</pre>
413</div>
414
415<p>Also, since we will need to create these alloca's, we'll use a helper
416function that ensures that the allocas are created in the entry block of the
417function:</p>
418
419<div class="doc_code">
420<pre>
421/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
422/// the function. This is used for mutable variables etc.
423static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
424 const std::string &amp;VarName) {
Gabor Greifd6c1ed02009-03-11 19:51:07 +0000425 IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),
Duncan Sands89f6d882008-04-13 06:22:09 +0000426 TheFunction-&gt;getEntryBlock().begin());
Nick Lewycky422094c2009-09-13 21:38:54 +0000427 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
428 VarName.c_str());
Chris Lattner62a709d2007-11-05 00:23:57 +0000429}
430</pre>
431</div>
432
Duncan Sands89f6d882008-04-13 06:22:09 +0000433<p>This funny looking code creates an IRBuilder object that is pointing at
Chris Lattner62a709d2007-11-05 00:23:57 +0000434the first instruction (.begin()) of the entry block. It then creates an alloca
435with the expected name and returns it. Because all values in Kaleidoscope are
436doubles, there is no need to pass in a type to use.</p>
437
438<p>With this in place, the first functionality change we want to make is to
439variable references. In our new scheme, variables live on the stack, so code
440generating a reference to them actually needs to produce a load from the stack
441slot:</p>
442
443<div class="doc_code">
444<pre>
445Value *VariableExprAST::Codegen() {
446 // Look this variable up in the function.
447 Value *V = NamedValues[Name];
448 if (V == 0) return ErrorV("Unknown variable name");
449
Chris Lattner1e46a6c2007-11-07 06:34:39 +0000450 <b>// Load the value.
451 return Builder.CreateLoad(V, Name.c_str());</b>
Chris Lattner62a709d2007-11-05 00:23:57 +0000452}
453</pre>
454</div>
455
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000456<p>As you can see, this is pretty straightforward. Now we need to update the
Chris Lattner62a709d2007-11-05 00:23:57 +0000457things that define the variables to set up the alloca. We'll start with
458<tt>ForExprAST::Codegen</tt> (see the <a href="#code">full code listing</a> for
459the unabridged code):</p>
460
461<div class="doc_code">
462<pre>
463 Function *TheFunction = Builder.GetInsertBlock()->getParent();
464
465 <b>// Create an alloca for the variable in the entry block.
466 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);</b>
467
468 // Emit the start code first, without 'variable' in scope.
469 Value *StartVal = Start-&gt;Codegen();
470 if (StartVal == 0) return 0;
471
472 <b>// Store the value into the alloca.
473 Builder.CreateStore(StartVal, Alloca);</b>
474 ...
475
476 // Compute the end condition.
477 Value *EndCond = End-&gt;Codegen();
478 if (EndCond == 0) return EndCond;
479
480 <b>// Reload, increment, and restore the alloca. This handles the case where
481 // the body of the loop mutates the variable.
482 Value *CurVar = Builder.CreateLoad(Alloca);
483 Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar");
484 Builder.CreateStore(NextVar, Alloca);</b>
485 ...
486</pre>
487</div>
488
489<p>This code is virtually identical to the code <a
490href="LangImpl5.html#forcodegen">before we allowed mutable variables</a>. The
491big difference is that we no longer have to construct a PHI node, and we use
492load/store to access the variable as needed.</p>
493
494<p>To support mutable argument variables, we need to also make allocas for them.
495The code for this is also pretty simple:</p>
496
497<div class="doc_code">
498<pre>
499/// CreateArgumentAllocas - Create an alloca for each argument and register the
500/// argument in the symbol table so that references to it will succeed.
501void PrototypeAST::CreateArgumentAllocas(Function *F) {
502 Function::arg_iterator AI = F-&gt;arg_begin();
503 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
504 // Create an alloca for this variable.
505 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
506
507 // Store the initial value into the alloca.
508 Builder.CreateStore(AI, Alloca);
509
510 // Add arguments to variable symbol table.
511 NamedValues[Args[Idx]] = Alloca;
512 }
513}
514</pre>
515</div>
516
517<p>For each argument, we make an alloca, store the input value to the function
518into the alloca, and register the alloca as the memory location for the
519argument. This method gets invoked by <tt>FunctionAST::Codegen</tt> right after
520it sets up the entry block for the function.</p>
521
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000522<p>The final missing piece is adding the mem2reg pass, which allows us to get
Chris Lattner62a709d2007-11-05 00:23:57 +0000523good codegen once again:</p>
524
525<div class="doc_code">
526<pre>
527 // Set up the optimizer pipeline. Start with registering info about how the
528 // target lays out data structures.
529 OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
530 <b>// Promote allocas to registers.
531 OurFPM.add(createPromoteMemoryToRegisterPass());</b>
532 // Do simple "peephole" optimizations and bit-twiddling optzns.
533 OurFPM.add(createInstructionCombiningPass());
534 // Reassociate expressions.
535 OurFPM.add(createReassociatePass());
536</pre>
537</div>
538
539<p>It is interesting to see what the code looks like before and after the
540mem2reg optimization runs. For example, this is the before/after code for our
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000541recursive fib function. Before the optimization:</p>
Chris Lattner62a709d2007-11-05 00:23:57 +0000542
543<div class="doc_code">
544<pre>
545define double @fib(double %x) {
546entry:
547 <b>%x1 = alloca double
548 store double %x, double* %x1
549 %x2 = load double* %x1</b>
Chris Lattner71155212007-11-06 01:39:12 +0000550 %cmptmp = fcmp ult double %x2, 3.000000e+00
551 %booltmp = uitofp i1 %cmptmp to double
Chris Lattner62a709d2007-11-05 00:23:57 +0000552 %ifcond = fcmp one double %booltmp, 0.000000e+00
553 br i1 %ifcond, label %then, label %else
554
555then: ; preds = %entry
556 br label %ifcont
557
558else: ; preds = %entry
559 <b>%x3 = load double* %x1</b>
560 %subtmp = sub double %x3, 1.000000e+00
561 %calltmp = call double @fib( double %subtmp )
562 <b>%x4 = load double* %x1</b>
563 %subtmp5 = sub double %x4, 2.000000e+00
564 %calltmp6 = call double @fib( double %subtmp5 )
565 %addtmp = add double %calltmp, %calltmp6
566 br label %ifcont
567
568ifcont: ; preds = %else, %then
569 %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
570 ret double %iftmp
571}
572</pre>
573</div>
574
575<p>Here there is only one variable (x, the input argument) but you can still
576see the extremely simple-minded code generation strategy we are using. In the
577entry block, an alloca is created, and the initial input value is stored into
578it. Each reference to the variable does a reload from the stack. Also, note
579that we didn't modify the if/then/else expression, so it still inserts a PHI
580node. While we could make an alloca for it, it is actually easier to create a
581PHI node for it, so we still just make the PHI.</p>
582
583<p>Here is the code after the mem2reg pass runs:</p>
584
585<div class="doc_code">
586<pre>
587define double @fib(double %x) {
588entry:
Chris Lattner71155212007-11-06 01:39:12 +0000589 %cmptmp = fcmp ult double <b>%x</b>, 3.000000e+00
590 %booltmp = uitofp i1 %cmptmp to double
Chris Lattner62a709d2007-11-05 00:23:57 +0000591 %ifcond = fcmp one double %booltmp, 0.000000e+00
592 br i1 %ifcond, label %then, label %else
593
594then:
595 br label %ifcont
596
597else:
598 %subtmp = sub double <b>%x</b>, 1.000000e+00
599 %calltmp = call double @fib( double %subtmp )
600 %subtmp5 = sub double <b>%x</b>, 2.000000e+00
601 %calltmp6 = call double @fib( double %subtmp5 )
602 %addtmp = add double %calltmp, %calltmp6
603 br label %ifcont
604
605ifcont: ; preds = %else, %then
606 %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
607 ret double %iftmp
608}
609</pre>
610</div>
611
612<p>This is a trivial case for mem2reg, since there are no redefinitions of the
613variable. The point of showing this is to calm your tension about inserting
614such blatent inefficiencies :).</p>
615
616<p>After the rest of the optimizers run, we get:</p>
617
618<div class="doc_code">
619<pre>
620define double @fib(double %x) {
621entry:
Chris Lattner71155212007-11-06 01:39:12 +0000622 %cmptmp = fcmp ult double %x, 3.000000e+00
623 %booltmp = uitofp i1 %cmptmp to double
Chris Lattner62a709d2007-11-05 00:23:57 +0000624 %ifcond = fcmp ueq double %booltmp, 0.000000e+00
625 br i1 %ifcond, label %else, label %ifcont
626
627else:
628 %subtmp = sub double %x, 1.000000e+00
629 %calltmp = call double @fib( double %subtmp )
630 %subtmp5 = sub double %x, 2.000000e+00
631 %calltmp6 = call double @fib( double %subtmp5 )
632 %addtmp = add double %calltmp, %calltmp6
633 ret double %addtmp
634
635ifcont:
636 ret double 1.000000e+00
637}
638</pre>
639</div>
640
641<p>Here we see that the simplifycfg pass decided to clone the return instruction
642into the end of the 'else' block. This allowed it to eliminate some branches
643and the PHI node.</p>
644
645<p>Now that all symbol table references are updated to use stack variables,
646we'll add the assignment operator.</p>
647
648</div>
649
650<!-- *********************************************************************** -->
651<div class="doc_section"><a name="assignment">New Assignment Operator</a></div>
652<!-- *********************************************************************** -->
653
654<div class="doc_text">
655
656<p>With our current framework, adding a new assignment operator is really
657simple. We will parse it just like any other binary operator, but handle it
658internally (instead of allowing the user to define it). The first step is to
659set a precedence:</p>
660
661<div class="doc_code">
662<pre>
663 int main() {
664 // Install standard binary operators.
665 // 1 is lowest precedence.
666 <b>BinopPrecedence['='] = 2;</b>
667 BinopPrecedence['&lt;'] = 10;
668 BinopPrecedence['+'] = 20;
669 BinopPrecedence['-'] = 20;
670</pre>
671</div>
672
673<p>Now that the parser knows the precedence of the binary operator, it takes
674care of all the parsing and AST generation. We just need to implement codegen
675for the assignment operator. This looks like:</p>
676
677<div class="doc_code">
678<pre>
679Value *BinaryExprAST::Codegen() {
680 // Special case '=' because we don't want to emit the LHS as an expression.
681 if (Op == '=') {
682 // Assignment requires the LHS to be an identifier.
683 VariableExprAST *LHSE = dynamic_cast&lt;VariableExprAST*&gt;(LHS);
684 if (!LHSE)
685 return ErrorV("destination of '=' must be a variable");
686</pre>
687</div>
688
689<p>Unlike the rest of the binary operators, our assignment operator doesn't
690follow the "emit LHS, emit RHS, do computation" model. As such, it is handled
691as a special case before the other binary operators are handled. The other
Chris Lattner1e46a6c2007-11-07 06:34:39 +0000692strange thing is that it requires the LHS to be a variable. It is invalid to
693have "(x+1) = expr" - only things like "x = expr" are allowed.
Chris Lattner62a709d2007-11-05 00:23:57 +0000694</p>
695
696<div class="doc_code">
697<pre>
698 // Codegen the RHS.
699 Value *Val = RHS-&gt;Codegen();
700 if (Val == 0) return 0;
701
702 // Look up the name.
703 Value *Variable = NamedValues[LHSE-&gt;getName()];
704 if (Variable == 0) return ErrorV("Unknown variable name");
705
706 Builder.CreateStore(Val, Variable);
707 return Val;
708 }
709 ...
710</pre>
711</div>
712
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000713<p>Once we have the variable, codegen'ing the assignment is straightforward:
Chris Lattner62a709d2007-11-05 00:23:57 +0000714we emit the RHS of the assignment, create a store, and return the computed
715value. Returning a value allows for chained assignments like "X = (Y = Z)".</p>
716
717<p>Now that we have an assignment operator, we can mutate loop variables and
718arguments. For example, we can now run code like this:</p>
719
720<div class="doc_code">
721<pre>
722# Function to print a double.
723extern printd(x);
724
725# Define ':' for sequencing: as a low-precedence operator that ignores operands
726# and just returns the RHS.
727def binary : 1 (x y) y;
728
729def test(x)
730 printd(x) :
731 x = 4 :
732 printd(x);
733
734test(123);
735</pre>
736</div>
737
738<p>When run, this example prints "123" and then "4", showing that we did
739actually mutate the value! Okay, we have now officially implemented our goal:
740getting this to work requires SSA construction in the general case. However,
741to be really useful, we want the ability to define our own local variables, lets
742add this next!
743</p>
744
745</div>
746
747<!-- *********************************************************************** -->
748<div class="doc_section"><a name="localvars">User-defined Local
749Variables</a></div>
750<!-- *********************************************************************** -->
751
752<div class="doc_text">
753
754<p>Adding var/in is just like any other other extensions we made to
755Kaleidoscope: we extend the lexer, the parser, the AST and the code generator.
756The first step for adding our new 'var/in' construct is to extend the lexer.
757As before, this is pretty trivial, the code looks like this:</p>
758
759<div class="doc_code">
760<pre>
761enum Token {
762 ...
763 <b>// var definition
764 tok_var = -13</b>
765...
766}
767...
768static int gettok() {
769...
770 if (IdentifierStr == "in") return tok_in;
771 if (IdentifierStr == "binary") return tok_binary;
772 if (IdentifierStr == "unary") return tok_unary;
773 <b>if (IdentifierStr == "var") return tok_var;</b>
774 return tok_identifier;
775...
776</pre>
777</div>
778
779<p>The next step is to define the AST node that we will construct. For var/in,
Chris Lattner1e46a6c2007-11-07 06:34:39 +0000780it looks like this:</p>
Chris Lattner62a709d2007-11-05 00:23:57 +0000781
782<div class="doc_code">
783<pre>
784/// VarExprAST - Expression class for var/in
785class VarExprAST : public ExprAST {
786 std::vector&lt;std::pair&lt;std::string, ExprAST*&gt; &gt; VarNames;
787 ExprAST *Body;
788public:
789 VarExprAST(const std::vector&lt;std::pair&lt;std::string, ExprAST*&gt; &gt; &amp;varnames,
790 ExprAST *body)
791 : VarNames(varnames), Body(body) {}
792
793 virtual Value *Codegen();
794};
795</pre>
796</div>
797
798<p>var/in allows a list of names to be defined all at once, and each name can
799optionally have an initializer value. As such, we capture this information in
800the VarNames vector. Also, var/in has a body, this body is allowed to access
Chris Lattner1e46a6c2007-11-07 06:34:39 +0000801the variables defined by the var/in.</p>
Chris Lattner62a709d2007-11-05 00:23:57 +0000802
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000803<p>With this in place, we can define the parser pieces. The first thing we do is add
Chris Lattner62a709d2007-11-05 00:23:57 +0000804it as a primary expression:</p>
805
806<div class="doc_code">
807<pre>
808/// primary
809/// ::= identifierexpr
810/// ::= numberexpr
811/// ::= parenexpr
812/// ::= ifexpr
813/// ::= forexpr
814<b>/// ::= varexpr</b>
815static ExprAST *ParsePrimary() {
816 switch (CurTok) {
817 default: return Error("unknown token when expecting an expression");
818 case tok_identifier: return ParseIdentifierExpr();
819 case tok_number: return ParseNumberExpr();
820 case '(': return ParseParenExpr();
821 case tok_if: return ParseIfExpr();
822 case tok_for: return ParseForExpr();
823 <b>case tok_var: return ParseVarExpr();</b>
824 }
825}
826</pre>
827</div>
828
829<p>Next we define ParseVarExpr:</p>
830
831<div class="doc_code">
832<pre>
Chris Lattner20a0c802007-11-05 17:54:34 +0000833/// varexpr ::= 'var' identifier ('=' expression)?
834// (',' identifier ('=' expression)?)* 'in' expression
Chris Lattner62a709d2007-11-05 00:23:57 +0000835static ExprAST *ParseVarExpr() {
836 getNextToken(); // eat the var.
837
838 std::vector&lt;std::pair&lt;std::string, ExprAST*&gt; &gt; VarNames;
839
840 // At least one variable name is required.
841 if (CurTok != tok_identifier)
842 return Error("expected identifier after var");
843</pre>
844</div>
845
846<p>The first part of this code parses the list of identifier/expr pairs into the
847local <tt>VarNames</tt> vector.
848
849<div class="doc_code">
850<pre>
851 while (1) {
852 std::string Name = IdentifierStr;
Chris Lattner20a0c802007-11-05 17:54:34 +0000853 getNextToken(); // eat identifier.
Chris Lattner62a709d2007-11-05 00:23:57 +0000854
855 // Read the optional initializer.
856 ExprAST *Init = 0;
857 if (CurTok == '=') {
858 getNextToken(); // eat the '='.
859
860 Init = ParseExpression();
861 if (Init == 0) return 0;
862 }
863
864 VarNames.push_back(std::make_pair(Name, Init));
865
866 // End of var list, exit loop.
867 if (CurTok != ',') break;
868 getNextToken(); // eat the ','.
869
870 if (CurTok != tok_identifier)
871 return Error("expected identifier list after var");
872 }
873</pre>
874</div>
875
876<p>Once all the variables are parsed, we then parse the body and create the
877AST node:</p>
878
879<div class="doc_code">
880<pre>
881 // At this point, we have to have 'in'.
882 if (CurTok != tok_in)
883 return Error("expected 'in' keyword after 'var'");
884 getNextToken(); // eat 'in'.
885
886 ExprAST *Body = ParseExpression();
887 if (Body == 0) return 0;
888
889 return new VarExprAST(VarNames, Body);
890}
891</pre>
892</div>
893
894<p>Now that we can parse and represent the code, we need to support emission of
895LLVM IR for it. This code starts out with:</p>
896
897<div class="doc_code">
898<pre>
899Value *VarExprAST::Codegen() {
900 std::vector&lt;AllocaInst *&gt; OldBindings;
901
902 Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
903
904 // Register all variables and emit their initializer.
905 for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
906 const std::string &amp;VarName = VarNames[i].first;
907 ExprAST *Init = VarNames[i].second;
908</pre>
909</div>
910
911<p>Basically it loops over all the variables, installing them one at a time.
912For each variable we put into the symbol table, we remember the previous value
913that we replace in OldBindings.</p>
914
915<div class="doc_code">
916<pre>
917 // Emit the initializer before adding the variable to scope, this prevents
918 // the initializer from referencing the variable itself, and permits stuff
919 // like this:
920 // var a = 1 in
921 // var a = a in ... # refers to outer 'a'.
922 Value *InitVal;
923 if (Init) {
924 InitVal = Init-&gt;Codegen();
925 if (InitVal == 0) return 0;
926 } else { // If not specified, use 0.0.
Owen Anderson6f83c9c2009-07-27 20:59:43 +0000927 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
Chris Lattner62a709d2007-11-05 00:23:57 +0000928 }
929
930 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
931 Builder.CreateStore(InitVal, Alloca);
932
933 // Remember the old variable binding so that we can restore the binding when
934 // we unrecurse.
935 OldBindings.push_back(NamedValues[VarName]);
936
937 // Remember this binding.
938 NamedValues[VarName] = Alloca;
939 }
940</pre>
941</div>
942
943<p>There are more comments here than code. The basic idea is that we emit the
944initializer, create the alloca, then update the symbol table to point to it.
945Once all the variables are installed in the symbol table, we evaluate the body
946of the var/in expression:</p>
947
948<div class="doc_code">
949<pre>
950 // Codegen the body, now that all vars are in scope.
951 Value *BodyVal = Body-&gt;Codegen();
952 if (BodyVal == 0) return 0;
953</pre>
954</div>
955
956<p>Finally, before returning, we restore the previous variable bindings:</p>
957
958<div class="doc_code">
959<pre>
960 // Pop all our variables from scope.
961 for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
962 NamedValues[VarNames[i].first] = OldBindings[i];
963
964 // Return the body computation.
965 return BodyVal;
966}
967</pre>
968</div>
969
970<p>The end result of all of this is that we get properly scoped variable
971definitions, and we even (trivially) allow mutation of them :).</p>
972
973<p>With this, we completed what we set out to do. Our nice iterative fib
974example from the intro compiles and runs just fine. The mem2reg pass optimizes
975all of our stack variables into SSA registers, inserting PHI nodes where needed,
Chris Lattnerb7e6b1a2007-11-15 04:51:31 +0000976and our front-end remains simple: no "iterated dominance frontier" computation
Chris Lattner62a709d2007-11-05 00:23:57 +0000977anywhere in sight.</p>
978
979</div>
Chris Lattner00c992d2007-11-03 08:55:29 +0000980
981<!-- *********************************************************************** -->
982<div class="doc_section"><a name="code">Full Code Listing</a></div>
983<!-- *********************************************************************** -->
984
985<div class="doc_text">
986
987<p>
Chris Lattner62a709d2007-11-05 00:23:57 +0000988Here is the complete code listing for our running example, enhanced with mutable
989variables and var/in support. To build this example, use:
Chris Lattner00c992d2007-11-03 08:55:29 +0000990</p>
991
992<div class="doc_code">
993<pre>
994 # Compile
995 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
996 # Run
997 ./toy
998</pre>
999</div>
1000
1001<p>Here is the code:</p>
1002
1003<div class="doc_code">
1004<pre>
Chris Lattner62a709d2007-11-05 00:23:57 +00001005#include "llvm/DerivedTypes.h"
1006#include "llvm/ExecutionEngine/ExecutionEngine.h"
Nick Lewycky422094c2009-09-13 21:38:54 +00001007#include "llvm/ExecutionEngine/Interpreter.h"
1008#include "llvm/ExecutionEngine/JIT.h"
Owen Andersond1fbd142009-07-08 20:50:47 +00001009#include "llvm/LLVMContext.h"
Chris Lattner62a709d2007-11-05 00:23:57 +00001010#include "llvm/Module.h"
Chris Lattner62a709d2007-11-05 00:23:57 +00001011#include "llvm/PassManager.h"
1012#include "llvm/Analysis/Verifier.h"
1013#include "llvm/Target/TargetData.h"
Nick Lewycky422094c2009-09-13 21:38:54 +00001014#include "llvm/Target/TargetSelect.h"
Chris Lattner62a709d2007-11-05 00:23:57 +00001015#include "llvm/Transforms/Scalar.h"
Duncan Sands89f6d882008-04-13 06:22:09 +00001016#include "llvm/Support/IRBuilder.h"
Chris Lattner62a709d2007-11-05 00:23:57 +00001017#include &lt;cstdio&gt;
1018#include &lt;string&gt;
1019#include &lt;map&gt;
1020#include &lt;vector&gt;
1021using namespace llvm;
1022
1023//===----------------------------------------------------------------------===//
1024// Lexer
1025//===----------------------------------------------------------------------===//
1026
1027// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
1028// of these for known things.
1029enum Token {
1030 tok_eof = -1,
1031
1032 // commands
1033 tok_def = -2, tok_extern = -3,
1034
1035 // primary
1036 tok_identifier = -4, tok_number = -5,
1037
1038 // control
1039 tok_if = -6, tok_then = -7, tok_else = -8,
1040 tok_for = -9, tok_in = -10,
1041
1042 // operators
1043 tok_binary = -11, tok_unary = -12,
1044
1045 // var definition
1046 tok_var = -13
1047};
1048
1049static std::string IdentifierStr; // Filled in if tok_identifier
1050static double NumVal; // Filled in if tok_number
1051
1052/// gettok - Return the next token from standard input.
1053static int gettok() {
1054 static int LastChar = ' ';
1055
1056 // Skip any whitespace.
1057 while (isspace(LastChar))
1058 LastChar = getchar();
1059
1060 if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
1061 IdentifierStr = LastChar;
1062 while (isalnum((LastChar = getchar())))
1063 IdentifierStr += LastChar;
1064
1065 if (IdentifierStr == "def") return tok_def;
1066 if (IdentifierStr == "extern") return tok_extern;
1067 if (IdentifierStr == "if") return tok_if;
1068 if (IdentifierStr == "then") return tok_then;
1069 if (IdentifierStr == "else") return tok_else;
1070 if (IdentifierStr == "for") return tok_for;
1071 if (IdentifierStr == "in") return tok_in;
1072 if (IdentifierStr == "binary") return tok_binary;
1073 if (IdentifierStr == "unary") return tok_unary;
1074 if (IdentifierStr == "var") return tok_var;
1075 return tok_identifier;
1076 }
1077
1078 if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
1079 std::string NumStr;
1080 do {
1081 NumStr += LastChar;
1082 LastChar = getchar();
1083 } while (isdigit(LastChar) || LastChar == '.');
1084
1085 NumVal = strtod(NumStr.c_str(), 0);
1086 return tok_number;
1087 }
1088
1089 if (LastChar == '#') {
1090 // Comment until end of line.
1091 do LastChar = getchar();
Chris Lattnerc80c23f2007-12-02 22:46:01 +00001092 while (LastChar != EOF &amp;&amp; LastChar != '\n' &amp;&amp; LastChar != '\r');
Chris Lattner62a709d2007-11-05 00:23:57 +00001093
1094 if (LastChar != EOF)
1095 return gettok();
1096 }
1097
1098 // Check for end of file. Don't eat the EOF.
1099 if (LastChar == EOF)
1100 return tok_eof;
1101
1102 // Otherwise, just return the character as its ascii value.
1103 int ThisChar = LastChar;
1104 LastChar = getchar();
1105 return ThisChar;
1106}
1107
1108//===----------------------------------------------------------------------===//
1109// Abstract Syntax Tree (aka Parse Tree)
1110//===----------------------------------------------------------------------===//
1111
1112/// ExprAST - Base class for all expression nodes.
1113class ExprAST {
1114public:
1115 virtual ~ExprAST() {}
1116 virtual Value *Codegen() = 0;
1117};
1118
1119/// NumberExprAST - Expression class for numeric literals like "1.0".
1120class NumberExprAST : public ExprAST {
1121 double Val;
1122public:
1123 NumberExprAST(double val) : Val(val) {}
1124 virtual Value *Codegen();
1125};
1126
1127/// VariableExprAST - Expression class for referencing a variable, like "a".
1128class VariableExprAST : public ExprAST {
1129 std::string Name;
1130public:
1131 VariableExprAST(const std::string &amp;name) : Name(name) {}
1132 const std::string &amp;getName() const { return Name; }
1133 virtual Value *Codegen();
1134};
1135
1136/// UnaryExprAST - Expression class for a unary operator.
1137class UnaryExprAST : public ExprAST {
1138 char Opcode;
1139 ExprAST *Operand;
1140public:
1141 UnaryExprAST(char opcode, ExprAST *operand)
1142 : Opcode(opcode), Operand(operand) {}
1143 virtual Value *Codegen();
1144};
1145
1146/// BinaryExprAST - Expression class for a binary operator.
1147class BinaryExprAST : public ExprAST {
1148 char Op;
1149 ExprAST *LHS, *RHS;
1150public:
1151 BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
1152 : Op(op), LHS(lhs), RHS(rhs) {}
1153 virtual Value *Codegen();
1154};
1155
1156/// CallExprAST - Expression class for function calls.
1157class CallExprAST : public ExprAST {
1158 std::string Callee;
1159 std::vector&lt;ExprAST*&gt; Args;
1160public:
1161 CallExprAST(const std::string &amp;callee, std::vector&lt;ExprAST*&gt; &amp;args)
1162 : Callee(callee), Args(args) {}
1163 virtual Value *Codegen();
1164};
1165
1166/// IfExprAST - Expression class for if/then/else.
1167class IfExprAST : public ExprAST {
1168 ExprAST *Cond, *Then, *Else;
1169public:
1170 IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
1171 : Cond(cond), Then(then), Else(_else) {}
1172 virtual Value *Codegen();
1173};
1174
1175/// ForExprAST - Expression class for for/in.
1176class ForExprAST : public ExprAST {
1177 std::string VarName;
1178 ExprAST *Start, *End, *Step, *Body;
1179public:
1180 ForExprAST(const std::string &amp;varname, ExprAST *start, ExprAST *end,
1181 ExprAST *step, ExprAST *body)
1182 : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
1183 virtual Value *Codegen();
1184};
1185
1186/// VarExprAST - Expression class for var/in
1187class VarExprAST : public ExprAST {
1188 std::vector&lt;std::pair&lt;std::string, ExprAST*&gt; &gt; VarNames;
1189 ExprAST *Body;
1190public:
1191 VarExprAST(const std::vector&lt;std::pair&lt;std::string, ExprAST*&gt; &gt; &amp;varnames,
1192 ExprAST *body)
1193 : VarNames(varnames), Body(body) {}
1194
1195 virtual Value *Codegen();
1196};
1197
1198/// PrototypeAST - This class represents the "prototype" for a function,
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001199/// which captures its name, and its argument names (thus implicitly the number
1200/// of arguments the function takes), as well as if it is an operator.
Chris Lattner62a709d2007-11-05 00:23:57 +00001201class PrototypeAST {
1202 std::string Name;
1203 std::vector&lt;std::string&gt; Args;
1204 bool isOperator;
1205 unsigned Precedence; // Precedence if a binary op.
1206public:
1207 PrototypeAST(const std::string &amp;name, const std::vector&lt;std::string&gt; &amp;args,
1208 bool isoperator = false, unsigned prec = 0)
1209 : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
1210
1211 bool isUnaryOp() const { return isOperator &amp;&amp; Args.size() == 1; }
1212 bool isBinaryOp() const { return isOperator &amp;&amp; Args.size() == 2; }
1213
1214 char getOperatorName() const {
1215 assert(isUnaryOp() || isBinaryOp());
1216 return Name[Name.size()-1];
1217 }
1218
1219 unsigned getBinaryPrecedence() const { return Precedence; }
1220
1221 Function *Codegen();
1222
1223 void CreateArgumentAllocas(Function *F);
1224};
1225
1226/// FunctionAST - This class represents a function definition itself.
1227class FunctionAST {
1228 PrototypeAST *Proto;
1229 ExprAST *Body;
1230public:
1231 FunctionAST(PrototypeAST *proto, ExprAST *body)
1232 : Proto(proto), Body(body) {}
1233
1234 Function *Codegen();
1235};
1236
1237//===----------------------------------------------------------------------===//
1238// Parser
1239//===----------------------------------------------------------------------===//
1240
1241/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001242/// token the parser is looking at. getNextToken reads another token from the
Chris Lattner62a709d2007-11-05 00:23:57 +00001243/// lexer and updates CurTok with its results.
1244static int CurTok;
1245static int getNextToken() {
1246 return CurTok = gettok();
1247}
1248
1249/// BinopPrecedence - This holds the precedence for each binary operator that is
1250/// defined.
1251static std::map&lt;char, int&gt; BinopPrecedence;
1252
1253/// GetTokPrecedence - Get the precedence of the pending binary operator token.
1254static int GetTokPrecedence() {
1255 if (!isascii(CurTok))
1256 return -1;
1257
1258 // Make sure it's a declared binop.
1259 int TokPrec = BinopPrecedence[CurTok];
1260 if (TokPrec &lt;= 0) return -1;
1261 return TokPrec;
1262}
1263
1264/// Error* - These are little helper functions for error handling.
1265ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
1266PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
1267FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
1268
1269static ExprAST *ParseExpression();
1270
1271/// identifierexpr
Chris Lattner20a0c802007-11-05 17:54:34 +00001272/// ::= identifier
1273/// ::= identifier '(' expression* ')'
Chris Lattner62a709d2007-11-05 00:23:57 +00001274static ExprAST *ParseIdentifierExpr() {
1275 std::string IdName = IdentifierStr;
1276
Chris Lattner20a0c802007-11-05 17:54:34 +00001277 getNextToken(); // eat identifier.
Chris Lattner62a709d2007-11-05 00:23:57 +00001278
1279 if (CurTok != '(') // Simple variable ref.
1280 return new VariableExprAST(IdName);
1281
1282 // Call.
1283 getNextToken(); // eat (
1284 std::vector&lt;ExprAST*&gt; Args;
1285 if (CurTok != ')') {
1286 while (1) {
1287 ExprAST *Arg = ParseExpression();
1288 if (!Arg) return 0;
1289 Args.push_back(Arg);
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001290
Chris Lattner62a709d2007-11-05 00:23:57 +00001291 if (CurTok == ')') break;
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001292
Chris Lattner62a709d2007-11-05 00:23:57 +00001293 if (CurTok != ',')
Chris Lattner6c4be9c2008-04-14 16:44:41 +00001294 return Error("Expected ')' or ',' in argument list");
Chris Lattner62a709d2007-11-05 00:23:57 +00001295 getNextToken();
1296 }
1297 }
1298
1299 // Eat the ')'.
1300 getNextToken();
1301
1302 return new CallExprAST(IdName, Args);
1303}
1304
1305/// numberexpr ::= number
1306static ExprAST *ParseNumberExpr() {
1307 ExprAST *Result = new NumberExprAST(NumVal);
1308 getNextToken(); // consume the number
1309 return Result;
1310}
1311
1312/// parenexpr ::= '(' expression ')'
1313static ExprAST *ParseParenExpr() {
1314 getNextToken(); // eat (.
1315 ExprAST *V = ParseExpression();
1316 if (!V) return 0;
1317
1318 if (CurTok != ')')
1319 return Error("expected ')'");
1320 getNextToken(); // eat ).
1321 return V;
1322}
1323
1324/// ifexpr ::= 'if' expression 'then' expression 'else' expression
1325static ExprAST *ParseIfExpr() {
1326 getNextToken(); // eat the if.
1327
1328 // condition.
1329 ExprAST *Cond = ParseExpression();
1330 if (!Cond) return 0;
1331
1332 if (CurTok != tok_then)
1333 return Error("expected then");
1334 getNextToken(); // eat the then
1335
1336 ExprAST *Then = ParseExpression();
1337 if (Then == 0) return 0;
1338
1339 if (CurTok != tok_else)
1340 return Error("expected else");
1341
1342 getNextToken();
1343
1344 ExprAST *Else = ParseExpression();
1345 if (!Else) return 0;
1346
1347 return new IfExprAST(Cond, Then, Else);
1348}
1349
Chris Lattner20a0c802007-11-05 17:54:34 +00001350/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
Chris Lattner62a709d2007-11-05 00:23:57 +00001351static ExprAST *ParseForExpr() {
1352 getNextToken(); // eat the for.
1353
1354 if (CurTok != tok_identifier)
1355 return Error("expected identifier after for");
1356
1357 std::string IdName = IdentifierStr;
Chris Lattner20a0c802007-11-05 17:54:34 +00001358 getNextToken(); // eat identifier.
Chris Lattner62a709d2007-11-05 00:23:57 +00001359
1360 if (CurTok != '=')
1361 return Error("expected '=' after for");
1362 getNextToken(); // eat '='.
1363
1364
1365 ExprAST *Start = ParseExpression();
1366 if (Start == 0) return 0;
1367 if (CurTok != ',')
1368 return Error("expected ',' after for start value");
1369 getNextToken();
1370
1371 ExprAST *End = ParseExpression();
1372 if (End == 0) return 0;
1373
1374 // The step value is optional.
1375 ExprAST *Step = 0;
1376 if (CurTok == ',') {
1377 getNextToken();
1378 Step = ParseExpression();
1379 if (Step == 0) return 0;
1380 }
1381
1382 if (CurTok != tok_in)
1383 return Error("expected 'in' after for");
1384 getNextToken(); // eat 'in'.
1385
1386 ExprAST *Body = ParseExpression();
1387 if (Body == 0) return 0;
1388
1389 return new ForExprAST(IdName, Start, End, Step, Body);
1390}
1391
Chris Lattner20a0c802007-11-05 17:54:34 +00001392/// varexpr ::= 'var' identifier ('=' expression)?
1393// (',' identifier ('=' expression)?)* 'in' expression
Chris Lattner62a709d2007-11-05 00:23:57 +00001394static ExprAST *ParseVarExpr() {
1395 getNextToken(); // eat the var.
1396
1397 std::vector&lt;std::pair&lt;std::string, ExprAST*&gt; &gt; VarNames;
1398
1399 // At least one variable name is required.
1400 if (CurTok != tok_identifier)
1401 return Error("expected identifier after var");
1402
1403 while (1) {
1404 std::string Name = IdentifierStr;
Chris Lattner20a0c802007-11-05 17:54:34 +00001405 getNextToken(); // eat identifier.
Chris Lattner62a709d2007-11-05 00:23:57 +00001406
1407 // Read the optional initializer.
1408 ExprAST *Init = 0;
1409 if (CurTok == '=') {
1410 getNextToken(); // eat the '='.
1411
1412 Init = ParseExpression();
1413 if (Init == 0) return 0;
1414 }
1415
1416 VarNames.push_back(std::make_pair(Name, Init));
1417
1418 // End of var list, exit loop.
1419 if (CurTok != ',') break;
1420 getNextToken(); // eat the ','.
1421
1422 if (CurTok != tok_identifier)
1423 return Error("expected identifier list after var");
1424 }
1425
1426 // At this point, we have to have 'in'.
1427 if (CurTok != tok_in)
1428 return Error("expected 'in' keyword after 'var'");
1429 getNextToken(); // eat 'in'.
1430
1431 ExprAST *Body = ParseExpression();
1432 if (Body == 0) return 0;
1433
1434 return new VarExprAST(VarNames, Body);
1435}
1436
Chris Lattner62a709d2007-11-05 00:23:57 +00001437/// primary
1438/// ::= identifierexpr
1439/// ::= numberexpr
1440/// ::= parenexpr
1441/// ::= ifexpr
1442/// ::= forexpr
1443/// ::= varexpr
1444static ExprAST *ParsePrimary() {
1445 switch (CurTok) {
1446 default: return Error("unknown token when expecting an expression");
1447 case tok_identifier: return ParseIdentifierExpr();
1448 case tok_number: return ParseNumberExpr();
1449 case '(': return ParseParenExpr();
1450 case tok_if: return ParseIfExpr();
1451 case tok_for: return ParseForExpr();
1452 case tok_var: return ParseVarExpr();
1453 }
1454}
1455
1456/// unary
1457/// ::= primary
1458/// ::= '!' unary
1459static ExprAST *ParseUnary() {
1460 // If the current token is not an operator, it must be a primary expr.
1461 if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
1462 return ParsePrimary();
1463
1464 // If this is a unary operator, read it.
1465 int Opc = CurTok;
1466 getNextToken();
1467 if (ExprAST *Operand = ParseUnary())
1468 return new UnaryExprAST(Opc, Operand);
1469 return 0;
1470}
1471
1472/// binoprhs
1473/// ::= ('+' unary)*
1474static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
1475 // If this is a binop, find its precedence.
1476 while (1) {
1477 int TokPrec = GetTokPrecedence();
1478
1479 // If this is a binop that binds at least as tightly as the current binop,
1480 // consume it, otherwise we are done.
1481 if (TokPrec &lt; ExprPrec)
1482 return LHS;
1483
1484 // Okay, we know this is a binop.
1485 int BinOp = CurTok;
1486 getNextToken(); // eat binop
1487
1488 // Parse the unary expression after the binary operator.
1489 ExprAST *RHS = ParseUnary();
1490 if (!RHS) return 0;
1491
1492 // If BinOp binds less tightly with RHS than the operator after RHS, let
1493 // the pending operator take RHS as its LHS.
1494 int NextPrec = GetTokPrecedence();
1495 if (TokPrec &lt; NextPrec) {
1496 RHS = ParseBinOpRHS(TokPrec+1, RHS);
1497 if (RHS == 0) return 0;
1498 }
1499
1500 // Merge LHS/RHS.
1501 LHS = new BinaryExprAST(BinOp, LHS, RHS);
1502 }
1503}
1504
1505/// expression
1506/// ::= unary binoprhs
1507///
1508static ExprAST *ParseExpression() {
1509 ExprAST *LHS = ParseUnary();
1510 if (!LHS) return 0;
1511
1512 return ParseBinOpRHS(0, LHS);
1513}
1514
1515/// prototype
1516/// ::= id '(' id* ')'
1517/// ::= binary LETTER number? (id, id)
1518/// ::= unary LETTER (id)
1519static PrototypeAST *ParsePrototype() {
1520 std::string FnName;
1521
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001522 unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
Chris Lattner62a709d2007-11-05 00:23:57 +00001523 unsigned BinaryPrecedence = 30;
1524
1525 switch (CurTok) {
1526 default:
1527 return ErrorP("Expected function name in prototype");
1528 case tok_identifier:
1529 FnName = IdentifierStr;
1530 Kind = 0;
1531 getNextToken();
1532 break;
1533 case tok_unary:
1534 getNextToken();
1535 if (!isascii(CurTok))
1536 return ErrorP("Expected unary operator");
1537 FnName = "unary";
1538 FnName += (char)CurTok;
1539 Kind = 1;
1540 getNextToken();
1541 break;
1542 case tok_binary:
1543 getNextToken();
1544 if (!isascii(CurTok))
1545 return ErrorP("Expected binary operator");
1546 FnName = "binary";
1547 FnName += (char)CurTok;
1548 Kind = 2;
1549 getNextToken();
1550
1551 // Read the precedence if present.
1552 if (CurTok == tok_number) {
1553 if (NumVal &lt; 1 || NumVal &gt; 100)
1554 return ErrorP("Invalid precedecnce: must be 1..100");
1555 BinaryPrecedence = (unsigned)NumVal;
1556 getNextToken();
1557 }
1558 break;
1559 }
1560
1561 if (CurTok != '(')
1562 return ErrorP("Expected '(' in prototype");
1563
1564 std::vector&lt;std::string&gt; ArgNames;
1565 while (getNextToken() == tok_identifier)
1566 ArgNames.push_back(IdentifierStr);
1567 if (CurTok != ')')
1568 return ErrorP("Expected ')' in prototype");
1569
1570 // success.
1571 getNextToken(); // eat ')'.
1572
1573 // Verify right number of names for operator.
1574 if (Kind &amp;&amp; ArgNames.size() != Kind)
1575 return ErrorP("Invalid number of operands for operator");
1576
1577 return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
1578}
1579
1580/// definition ::= 'def' prototype expression
1581static FunctionAST *ParseDefinition() {
1582 getNextToken(); // eat def.
1583 PrototypeAST *Proto = ParsePrototype();
1584 if (Proto == 0) return 0;
1585
1586 if (ExprAST *E = ParseExpression())
1587 return new FunctionAST(Proto, E);
1588 return 0;
1589}
1590
1591/// toplevelexpr ::= expression
1592static FunctionAST *ParseTopLevelExpr() {
1593 if (ExprAST *E = ParseExpression()) {
1594 // Make an anonymous proto.
1595 PrototypeAST *Proto = new PrototypeAST("", std::vector&lt;std::string&gt;());
1596 return new FunctionAST(Proto, E);
1597 }
1598 return 0;
1599}
1600
1601/// external ::= 'extern' prototype
1602static PrototypeAST *ParseExtern() {
1603 getNextToken(); // eat extern.
1604 return ParsePrototype();
1605}
1606
1607//===----------------------------------------------------------------------===//
1608// Code Generation
1609//===----------------------------------------------------------------------===//
1610
1611static Module *TheModule;
Owen Andersond1fbd142009-07-08 20:50:47 +00001612static IRBuilder&lt;&gt; Builder(getGlobalContext());
Chris Lattner62a709d2007-11-05 00:23:57 +00001613static std::map&lt;std::string, AllocaInst*&gt; NamedValues;
1614static FunctionPassManager *TheFPM;
1615
1616Value *ErrorV(const char *Str) { Error(Str); return 0; }
1617
1618/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
1619/// the function. This is used for mutable variables etc.
1620static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
1621 const std::string &amp;VarName) {
Gabor Greifd6c1ed02009-03-11 19:51:07 +00001622 IRBuilder&lt;&gt; TmpB(&amp;TheFunction-&gt;getEntryBlock(),
Duncan Sands89f6d882008-04-13 06:22:09 +00001623 TheFunction-&gt;getEntryBlock().begin());
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001624 return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
1625 VarName.c_str());
Chris Lattner62a709d2007-11-05 00:23:57 +00001626}
1627
Chris Lattner62a709d2007-11-05 00:23:57 +00001628Value *NumberExprAST::Codegen() {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001629 return ConstantFP::get(getGlobalContext(), APFloat(Val));
Chris Lattner62a709d2007-11-05 00:23:57 +00001630}
1631
1632Value *VariableExprAST::Codegen() {
1633 // Look this variable up in the function.
1634 Value *V = NamedValues[Name];
1635 if (V == 0) return ErrorV("Unknown variable name");
1636
1637 // Load the value.
1638 return Builder.CreateLoad(V, Name.c_str());
1639}
1640
1641Value *UnaryExprAST::Codegen() {
1642 Value *OperandV = Operand-&gt;Codegen();
1643 if (OperandV == 0) return 0;
1644
1645 Function *F = TheModule-&gt;getFunction(std::string("unary")+Opcode);
1646 if (F == 0)
1647 return ErrorV("Unknown unary operator");
1648
1649 return Builder.CreateCall(F, OperandV, "unop");
1650}
1651
Chris Lattner62a709d2007-11-05 00:23:57 +00001652Value *BinaryExprAST::Codegen() {
1653 // Special case '=' because we don't want to emit the LHS as an expression.
1654 if (Op == '=') {
1655 // Assignment requires the LHS to be an identifier.
1656 VariableExprAST *LHSE = dynamic_cast&lt;VariableExprAST*&gt;(LHS);
1657 if (!LHSE)
1658 return ErrorV("destination of '=' must be a variable");
1659 // Codegen the RHS.
1660 Value *Val = RHS-&gt;Codegen();
1661 if (Val == 0) return 0;
1662
1663 // Look up the name.
1664 Value *Variable = NamedValues[LHSE-&gt;getName()];
1665 if (Variable == 0) return ErrorV("Unknown variable name");
1666
1667 Builder.CreateStore(Val, Variable);
1668 return Val;
1669 }
1670
Chris Lattner62a709d2007-11-05 00:23:57 +00001671 Value *L = LHS-&gt;Codegen();
1672 Value *R = RHS-&gt;Codegen();
1673 if (L == 0 || R == 0) return 0;
1674
1675 switch (Op) {
1676 case '+': return Builder.CreateAdd(L, R, "addtmp");
1677 case '-': return Builder.CreateSub(L, R, "subtmp");
1678 case '*': return Builder.CreateMul(L, R, "multmp");
1679 case '&lt;':
Chris Lattner71155212007-11-06 01:39:12 +00001680 L = Builder.CreateFCmpULT(L, R, "cmptmp");
Chris Lattner62a709d2007-11-05 00:23:57 +00001681 // Convert bool 0/1 to double 0.0 or 1.0
Nick Lewycky422094c2009-09-13 21:38:54 +00001682 return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
1683 "booltmp");
Chris Lattner62a709d2007-11-05 00:23:57 +00001684 default: break;
1685 }
1686
1687 // If it wasn't a builtin binary operator, it must be a user defined one. Emit
1688 // a call to it.
1689 Function *F = TheModule-&gt;getFunction(std::string("binary")+Op);
1690 assert(F &amp;&amp; "binary operator not found!");
1691
1692 Value *Ops[] = { L, R };
1693 return Builder.CreateCall(F, Ops, Ops+2, "binop");
1694}
1695
1696Value *CallExprAST::Codegen() {
1697 // Look up the name in the global module table.
1698 Function *CalleeF = TheModule-&gt;getFunction(Callee);
1699 if (CalleeF == 0)
1700 return ErrorV("Unknown function referenced");
1701
1702 // If argument mismatch error.
1703 if (CalleeF-&gt;arg_size() != Args.size())
1704 return ErrorV("Incorrect # arguments passed");
1705
1706 std::vector&lt;Value*&gt; ArgsV;
1707 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1708 ArgsV.push_back(Args[i]-&gt;Codegen());
1709 if (ArgsV.back() == 0) return 0;
1710 }
1711
1712 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1713}
1714
1715Value *IfExprAST::Codegen() {
1716 Value *CondV = Cond-&gt;Codegen();
1717 if (CondV == 0) return 0;
1718
1719 // Convert condition to a bool by comparing equal to 0.0.
1720 CondV = Builder.CreateFCmpONE(CondV,
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001721 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
Chris Lattner62a709d2007-11-05 00:23:57 +00001722 "ifcond");
1723
1724 Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
1725
1726 // Create blocks for the then and else cases. Insert the 'then' block at the
1727 // end of the function.
Owen Anderson1d0be152009-08-13 21:58:54 +00001728 BasicBlock *ThenBB = BasicBlock::Create(getGlobalContext(), "then", TheFunction);
1729 BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
1730 BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
Chris Lattner62a709d2007-11-05 00:23:57 +00001731
1732 Builder.CreateCondBr(CondV, ThenBB, ElseBB);
1733
1734 // Emit then value.
1735 Builder.SetInsertPoint(ThenBB);
1736
1737 Value *ThenV = Then-&gt;Codegen();
1738 if (ThenV == 0) return 0;
1739
1740 Builder.CreateBr(MergeBB);
1741 // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
1742 ThenBB = Builder.GetInsertBlock();
1743
1744 // Emit else block.
1745 TheFunction-&gt;getBasicBlockList().push_back(ElseBB);
1746 Builder.SetInsertPoint(ElseBB);
1747
1748 Value *ElseV = Else-&gt;Codegen();
1749 if (ElseV == 0) return 0;
1750
1751 Builder.CreateBr(MergeBB);
1752 // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
1753 ElseBB = Builder.GetInsertBlock();
1754
1755 // Emit merge block.
1756 TheFunction-&gt;getBasicBlockList().push_back(MergeBB);
1757 Builder.SetInsertPoint(MergeBB);
Nick Lewycky422094c2009-09-13 21:38:54 +00001758 PHINode *PN = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
1759 "iftmp");
Chris Lattner62a709d2007-11-05 00:23:57 +00001760
1761 PN-&gt;addIncoming(ThenV, ThenBB);
1762 PN-&gt;addIncoming(ElseV, ElseBB);
1763 return PN;
1764}
1765
1766Value *ForExprAST::Codegen() {
1767 // Output this as:
1768 // var = alloca double
1769 // ...
1770 // start = startexpr
1771 // store start -&gt; var
1772 // goto loop
1773 // loop:
1774 // ...
1775 // bodyexpr
1776 // ...
1777 // loopend:
1778 // step = stepexpr
1779 // endcond = endexpr
1780 //
1781 // curvar = load var
1782 // nextvar = curvar + step
1783 // store nextvar -&gt; var
1784 // br endcond, loop, endloop
1785 // outloop:
1786
1787 Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
1788
1789 // Create an alloca for the variable in the entry block.
1790 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1791
1792 // Emit the start code first, without 'variable' in scope.
1793 Value *StartVal = Start-&gt;Codegen();
1794 if (StartVal == 0) return 0;
1795
1796 // Store the value into the alloca.
1797 Builder.CreateStore(StartVal, Alloca);
1798
1799 // Make the new basic block for the loop header, inserting after current
1800 // block.
Owen Anderson1d0be152009-08-13 21:58:54 +00001801 BasicBlock *LoopBB = BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
Chris Lattner62a709d2007-11-05 00:23:57 +00001802
1803 // Insert an explicit fall through from the current block to the LoopBB.
1804 Builder.CreateBr(LoopBB);
1805
1806 // Start insertion in LoopBB.
1807 Builder.SetInsertPoint(LoopBB);
1808
1809 // Within the loop, the variable is defined equal to the PHI node. If it
1810 // shadows an existing variable, we have to restore it, so save it now.
1811 AllocaInst *OldVal = NamedValues[VarName];
1812 NamedValues[VarName] = Alloca;
1813
1814 // Emit the body of the loop. This, like any other expr, can change the
1815 // current BB. Note that we ignore the value computed by the body, but don't
1816 // allow an error.
1817 if (Body-&gt;Codegen() == 0)
1818 return 0;
1819
1820 // Emit the step value.
1821 Value *StepVal;
1822 if (Step) {
1823 StepVal = Step-&gt;Codegen();
1824 if (StepVal == 0) return 0;
1825 } else {
1826 // If not specified, use 1.0.
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001827 StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
Chris Lattner62a709d2007-11-05 00:23:57 +00001828 }
1829
1830 // Compute the end condition.
1831 Value *EndCond = End-&gt;Codegen();
1832 if (EndCond == 0) return EndCond;
1833
1834 // Reload, increment, and restore the alloca. This handles the case where
1835 // the body of the loop mutates the variable.
1836 Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
1837 Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar");
1838 Builder.CreateStore(NextVar, Alloca);
1839
1840 // Convert condition to a bool by comparing equal to 0.0.
1841 EndCond = Builder.CreateFCmpONE(EndCond,
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001842 ConstantFP::get(getGlobalContext(), APFloat(0.0)),
Chris Lattner62a709d2007-11-05 00:23:57 +00001843 "loopcond");
1844
1845 // Create the "after loop" block and insert it.
Owen Anderson1d0be152009-08-13 21:58:54 +00001846 BasicBlock *AfterBB = BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
Chris Lattner62a709d2007-11-05 00:23:57 +00001847
1848 // Insert the conditional branch into the end of LoopEndBB.
1849 Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
1850
1851 // Any new code will be inserted in AfterBB.
1852 Builder.SetInsertPoint(AfterBB);
1853
1854 // Restore the unshadowed variable.
1855 if (OldVal)
1856 NamedValues[VarName] = OldVal;
1857 else
1858 NamedValues.erase(VarName);
1859
1860
1861 // for expr always returns 0.0.
Owen Anderson1d0be152009-08-13 21:58:54 +00001862 return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
Chris Lattner62a709d2007-11-05 00:23:57 +00001863}
1864
1865Value *VarExprAST::Codegen() {
1866 std::vector&lt;AllocaInst *&gt; OldBindings;
1867
1868 Function *TheFunction = Builder.GetInsertBlock()-&gt;getParent();
1869
1870 // Register all variables and emit their initializer.
1871 for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
1872 const std::string &amp;VarName = VarNames[i].first;
1873 ExprAST *Init = VarNames[i].second;
1874
1875 // Emit the initializer before adding the variable to scope, this prevents
1876 // the initializer from referencing the variable itself, and permits stuff
1877 // like this:
1878 // var a = 1 in
1879 // var a = a in ... # refers to outer 'a'.
1880 Value *InitVal;
1881 if (Init) {
1882 InitVal = Init-&gt;Codegen();
1883 if (InitVal == 0) return 0;
1884 } else { // If not specified, use 0.0.
Owen Anderson6f83c9c2009-07-27 20:59:43 +00001885 InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
Chris Lattner62a709d2007-11-05 00:23:57 +00001886 }
1887
1888 AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
1889 Builder.CreateStore(InitVal, Alloca);
1890
1891 // Remember the old variable binding so that we can restore the binding when
1892 // we unrecurse.
1893 OldBindings.push_back(NamedValues[VarName]);
1894
1895 // Remember this binding.
1896 NamedValues[VarName] = Alloca;
1897 }
1898
1899 // Codegen the body, now that all vars are in scope.
1900 Value *BodyVal = Body-&gt;Codegen();
1901 if (BodyVal == 0) return 0;
1902
1903 // Pop all our variables from scope.
1904 for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
1905 NamedValues[VarNames[i].first] = OldBindings[i];
1906
1907 // Return the body computation.
1908 return BodyVal;
1909}
1910
Chris Lattner62a709d2007-11-05 00:23:57 +00001911Function *PrototypeAST::Codegen() {
1912 // Make the function type: double(double,double) etc.
Nick Lewycky422094c2009-09-13 21:38:54 +00001913 std::vector&lt;const Type*&gt; Doubles(Args.size(),
1914 Type::getDoubleTy(getGlobalContext()));
1915 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
1916 Doubles, false);
Chris Lattner62a709d2007-11-05 00:23:57 +00001917
Gabor Greifdf7d2b42008-04-19 22:25:09 +00001918 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
Chris Lattner62a709d2007-11-05 00:23:57 +00001919
1920 // If F conflicted, there was already something named 'Name'. If it has a
1921 // body, don't allow redefinition or reextern.
1922 if (F-&gt;getName() != Name) {
1923 // Delete the one we just made and get the existing one.
1924 F-&gt;eraseFromParent();
1925 F = TheModule-&gt;getFunction(Name);
1926
1927 // If F already has a body, reject this.
1928 if (!F-&gt;empty()) {
1929 ErrorF("redefinition of function");
1930 return 0;
1931 }
1932
1933 // If F took a different number of args, reject.
1934 if (F-&gt;arg_size() != Args.size()) {
1935 ErrorF("redefinition of function with different # args");
1936 return 0;
1937 }
1938 }
1939
1940 // Set names for all arguments.
1941 unsigned Idx = 0;
1942 for (Function::arg_iterator AI = F-&gt;arg_begin(); Idx != Args.size();
1943 ++AI, ++Idx)
1944 AI-&gt;setName(Args[Idx]);
1945
1946 return F;
1947}
1948
1949/// CreateArgumentAllocas - Create an alloca for each argument and register the
1950/// argument in the symbol table so that references to it will succeed.
1951void PrototypeAST::CreateArgumentAllocas(Function *F) {
1952 Function::arg_iterator AI = F-&gt;arg_begin();
1953 for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
1954 // Create an alloca for this variable.
1955 AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
1956
1957 // Store the initial value into the alloca.
1958 Builder.CreateStore(AI, Alloca);
1959
1960 // Add arguments to variable symbol table.
1961 NamedValues[Args[Idx]] = Alloca;
1962 }
1963}
1964
Chris Lattner62a709d2007-11-05 00:23:57 +00001965Function *FunctionAST::Codegen() {
1966 NamedValues.clear();
1967
1968 Function *TheFunction = Proto-&gt;Codegen();
1969 if (TheFunction == 0)
1970 return 0;
1971
1972 // If this is an operator, install it.
1973 if (Proto-&gt;isBinaryOp())
1974 BinopPrecedence[Proto-&gt;getOperatorName()] = Proto-&gt;getBinaryPrecedence();
1975
1976 // Create a new basic block to start insertion into.
Owen Anderson1d0be152009-08-13 21:58:54 +00001977 BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
Chris Lattner62a709d2007-11-05 00:23:57 +00001978 Builder.SetInsertPoint(BB);
1979
1980 // Add all arguments to the symbol table and create their allocas.
1981 Proto-&gt;CreateArgumentAllocas(TheFunction);
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00001982
Chris Lattner62a709d2007-11-05 00:23:57 +00001983 if (Value *RetVal = Body-&gt;Codegen()) {
1984 // Finish off the function.
1985 Builder.CreateRet(RetVal);
1986
1987 // Validate the generated code, checking for consistency.
1988 verifyFunction(*TheFunction);
1989
1990 // Optimize the function.
1991 TheFPM-&gt;run(*TheFunction);
1992
1993 return TheFunction;
1994 }
1995
1996 // Error reading body, remove function.
1997 TheFunction-&gt;eraseFromParent();
1998
1999 if (Proto-&gt;isBinaryOp())
2000 BinopPrecedence.erase(Proto-&gt;getOperatorName());
2001 return 0;
2002}
2003
2004//===----------------------------------------------------------------------===//
2005// Top-Level parsing and JIT Driver
2006//===----------------------------------------------------------------------===//
2007
2008static ExecutionEngine *TheExecutionEngine;
2009
2010static void HandleDefinition() {
2011 if (FunctionAST *F = ParseDefinition()) {
2012 if (Function *LF = F-&gt;Codegen()) {
2013 fprintf(stderr, "Read function definition:");
2014 LF-&gt;dump();
2015 }
2016 } else {
2017 // Skip token for error recovery.
2018 getNextToken();
2019 }
2020}
2021
2022static void HandleExtern() {
2023 if (PrototypeAST *P = ParseExtern()) {
2024 if (Function *F = P-&gt;Codegen()) {
2025 fprintf(stderr, "Read extern: ");
2026 F-&gt;dump();
2027 }
2028 } else {
2029 // Skip token for error recovery.
2030 getNextToken();
2031 }
2032}
2033
2034static void HandleTopLevelExpression() {
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00002035 // Evaluate a top-level expression into an anonymous function.
Chris Lattner62a709d2007-11-05 00:23:57 +00002036 if (FunctionAST *F = ParseTopLevelExpr()) {
2037 if (Function *LF = F-&gt;Codegen()) {
2038 // JIT the function, returning a function pointer.
2039 void *FPtr = TheExecutionEngine-&gt;getPointerToFunction(LF);
2040
2041 // Cast it to the right type (takes no arguments, returns a double) so we
2042 // can call it as a native function.
Nick Lewycky422094c2009-09-13 21:38:54 +00002043 double (*FP)() = (double (*)())(intptr_t)FPtr;
Chris Lattner62a709d2007-11-05 00:23:57 +00002044 fprintf(stderr, "Evaluated to %f\n", FP());
2045 }
2046 } else {
2047 // Skip token for error recovery.
2048 getNextToken();
2049 }
2050}
2051
2052/// top ::= definition | external | expression | ';'
2053static void MainLoop() {
2054 while (1) {
2055 fprintf(stderr, "ready&gt; ");
2056 switch (CurTok) {
2057 case tok_eof: return;
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00002058 case ';': getNextToken(); break; // ignore top-level semicolons.
Chris Lattner62a709d2007-11-05 00:23:57 +00002059 case tok_def: HandleDefinition(); break;
2060 case tok_extern: HandleExtern(); break;
2061 default: HandleTopLevelExpression(); break;
2062 }
2063 }
2064}
2065
Chris Lattner62a709d2007-11-05 00:23:57 +00002066//===----------------------------------------------------------------------===//
2067// "Library" functions that can be "extern'd" from user code.
2068//===----------------------------------------------------------------------===//
2069
2070/// putchard - putchar that takes a double and returns 0.
2071extern "C"
2072double putchard(double X) {
2073 putchar((char)X);
2074 return 0;
2075}
2076
2077/// printd - printf that takes a double prints it as "%f\n", returning 0.
2078extern "C"
2079double printd(double X) {
2080 printf("%f\n", X);
2081 return 0;
2082}
2083
2084//===----------------------------------------------------------------------===//
2085// Main driver code.
2086//===----------------------------------------------------------------------===//
2087
2088int main() {
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00002089 InitializeNativeTarget();
2090 LLVMContext &amp;Context = getGlobalContext();
2091
Chris Lattner62a709d2007-11-05 00:23:57 +00002092 // Install standard binary operators.
2093 // 1 is lowest precedence.
2094 BinopPrecedence['='] = 2;
2095 BinopPrecedence['&lt;'] = 10;
2096 BinopPrecedence['+'] = 20;
2097 BinopPrecedence['-'] = 20;
2098 BinopPrecedence['*'] = 40; // highest.
2099
2100 // Prime the first token.
2101 fprintf(stderr, "ready&gt; ");
2102 getNextToken();
2103
2104 // Make the module, which holds all the code.
Erick Tryzelaarfd1ec5e2009-09-22 21:14:49 +00002105 TheModule = new Module("my cool jit", Context);
Chris Lattner62a709d2007-11-05 00:23:57 +00002106
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002107 // Create the JIT. This takes ownership of the module.
2108 TheExecutionEngine = EngineBuilder(TheModule).create();
Chris Lattner62a709d2007-11-05 00:23:57 +00002109
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002110 FunctionPassManager OurFPM(TheModule);
Reid Kleckner60130f02009-08-26 20:58:25 +00002111
2112 // Set up the optimizer pipeline. Start with registering info about how the
2113 // target lays out data structures.
2114 OurFPM.add(new TargetData(*TheExecutionEngine-&gt;getTargetData()));
Nick Lewycky422094c2009-09-13 21:38:54 +00002115 // Promote allocas to registers.
2116 OurFPM.add(createPromoteMemoryToRegisterPass());
Reid Kleckner60130f02009-08-26 20:58:25 +00002117 // Do simple "peephole" optimizations and bit-twiddling optzns.
2118 OurFPM.add(createInstructionCombiningPass());
2119 // Reassociate expressions.
2120 OurFPM.add(createReassociatePass());
2121 // Eliminate Common SubExpressions.
2122 OurFPM.add(createGVNPass());
2123 // Simplify the control flow graph (deleting unreachable blocks, etc).
2124 OurFPM.add(createCFGSimplificationPass());
2125
Nick Lewycky422094c2009-09-13 21:38:54 +00002126 OurFPM.doInitialization();
2127
Reid Kleckner60130f02009-08-26 20:58:25 +00002128 // Set the global so the code gen can use this.
2129 TheFPM = &amp;OurFPM;
2130
2131 // Run the main "interpreter loop" now.
2132 MainLoop();
2133
2134 TheFPM = 0;
2135
2136 // Print out all of the generated code.
2137 TheModule-&gt;dump();
2138
Chris Lattner62a709d2007-11-05 00:23:57 +00002139 return 0;
2140}
Chris Lattner00c992d2007-11-03 08:55:29 +00002141</pre>
2142</div>
2143
Chris Lattner729eb142008-02-10 19:11:04 +00002144<a href="LangImpl8.html">Next: Conclusion and other useful LLVM tidbits</a>
Chris Lattner00c992d2007-11-03 08:55:29 +00002145</div>
2146
2147<!-- *********************************************************************** -->
2148<hr>
2149<address>
2150 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
2151 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
2152 <a href="http://validator.w3.org/check/referer"><img
2153 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
2154
2155 <a href="mailto:sabre@nondot.org">Chris Lattner</a><br>
2156 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Dan Gohman523e3922010-02-03 17:27:31 +00002157 Last modified: $Date$
Chris Lattner00c992d2007-11-03 08:55:29 +00002158</address>
2159</body>
2160</html>