<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Kaleidoscope: Implementing code generation to LLVM IR</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Chris Lattner">
<link rel="stylesheet" href="../_static/llvm.css" type="text/css">
</head>
<body>
<h1>Kaleidoscope: Code generation to LLVM IR</h1>
<ul>
<li><a href="index.html">Up to Tutorial Index</a></li>
<li>Chapter 3
<ol>
<li><a href="#intro">Chapter 3 Introduction</a></li>
<li><a href="#basics">Code Generation Setup</a></li>
<li><a href="#exprs">Expression Code Generation</a></li>
<li><a href="#funcs">Function Code Generation</a></li>
<li><a href="#driver">Driver Changes and Closing Thoughts</a></li>
<li><a href="#code">Full Code Listing</a></li>
</ol>
</li>
<li><a href="LangImpl4.html">Chapter 4</a>: Adding JIT and Optimizer
Support</li>
</ul>
<div class="doc_author">
<p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a></p>
</div>
<!-- *********************************************************************** -->
<h2><a name="intro">Chapter 3 Introduction</a></h2>
<!-- *********************************************************************** -->
<div>
<p>Welcome to Chapter 3 of the "<a href="index.html">Implementing a language
with LLVM</a>" tutorial. This chapter shows you how to transform the <a
href="LangImpl2.html">Abstract Syntax Tree</a>, built in Chapter 2, into LLVM IR.
This will teach you a little bit about how LLVM does things, as well as
demonstrate how easy it is to use. It's much more work to build a lexer and
parser than it is to generate LLVM IR code. :)
</p>
<p><b>Please note</b>: the code in this chapter and later require LLVM 2.2 or
later. LLVM 2.1 and before will not work with it. Also note that you need
to use a version of this tutorial that matches your LLVM release: If you are
using an official LLVM release, use the version of the documentation included
with your release or on the <a href="http://llvm.org/releases/">llvm.org
releases page</a>.</p>
</div>
<!-- *********************************************************************** -->
<h2><a name="basics">Code Generation Setup</a></h2>
<!-- *********************************************************************** -->
<div>
<p>
In order to generate LLVM IR, we want some simple setup to get started. First
we define virtual code generation (codegen) methods in each AST class:</p>
<div class="doc_code">
<pre>
/// ExprAST - Base class for all expression nodes.
class ExprAST {
public:
virtual ~ExprAST() {}
<b>virtual Value *Codegen() = 0;</b>
};
/// NumberExprAST - Expression class for numeric literals like "1.0".
class NumberExprAST : public ExprAST {
double Val;
public:
NumberExprAST(double val) : Val(val) {}
<b>virtual Value *Codegen();</b>
};
...
</pre>
</div>
<p>The Codegen() method says to emit IR for that AST node along with all the things it
depends on, and they all return an LLVM Value object.
"Value" is the class used to represent a "<a
href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
Assignment (SSA)</a> register" or "SSA value" in LLVM. The most distinct aspect
of SSA values is that their value is computed as the related instruction
executes, and it does not get a new value until (and if) the instruction
re-executes. In other words, there is no way to "change" an SSA value. For
more information, please read up on <a
href="http://en.wikipedia.org/wiki/Static_single_assignment_form">Static Single
Assignment</a> - the concepts are really quite natural once you grok them.</p>
<p>Note that instead of adding virtual methods to the ExprAST class hierarchy,
it could also make sense to use a <a
href="http://en.wikipedia.org/wiki/Visitor_pattern">visitor pattern</a> or some
other way to model this. Again, this tutorial won't dwell on good software
engineering practices: for our purposes, add