Kaleidoscope: Implementing a Language with LLVM in CSharp

1. Kaleidoscope: Tutorial Introduction and the Lexer

1.1. Tutorial Introduction

Welcome to the “Implementing a language with LLVM” tutorial. This tutorial runs through the implementation of a simple language, showing how fun and easy it can be. This tutorial will get you up and started as well as help to build a framework you can extend to other languages. The code in this tutorial can also be used as a playground to hack on other LLVM specific things.

The goal of this tutorial is to progressively unveil our language, describing how it is built up over time. This will let us cover a fairly broad range of language design and LLVM-specific usage issues, showing and explaining the code for it all along the way, without overwhelming you with tons of details up front.

It is useful to point out ahead of time that this tutorial is really about teaching compiler techniques and LLVM specifically, not about teaching modern and sane software engineering principles. In practice, this means that we’ll take a number of shortcuts to simplify the exposition. For example, the code uses global variables all over the place (but unlike the official C++ version, this C# version uses nice design patterns like visitors), etc… but it is very simple. If you dig in and use the code as a basis for future projects, fixing these deficiencies shouldn’t be hard.

I’ve tried to put this tutorial together in a way that makes chapters easy to skip over if you are already familiar with or are uninterested in the various pieces. The structure of the tutorial is:

Chapter 5 to 10 are not available due to the difference between the language C# and C++ themselves and the API.

By the end of the tutorial, we’ll have written a bit less than 1000 lines of non-comment, non-blank, lines of code. With this small amount of code, we’ll have built up a very reasonable compiler for a non-trivial language including a hand-written lexer, parser, AST, as well as code generation support with a JIT compiler. While other systems may have interesting “hello world” tutorials, I think the breadth of this tutorial is a great testament to the strengths of LLVM and why you should consider it if you’re interested in language or compiler design.

A note about this tutorial: we expect you to extend the language and play with it on your own. Take the code and go crazy hacking away at it, compilers don’t need to be scary creatures - it can be a lot of fun to play with languages!

1.2. The Basic Language

This tutorial will be illustrated with a toy language that we’ll call “Kaleidoscope” (derived from “meaning beautiful, form, and view”). Kaleidoscope is a procedural language that allows you to define functions, use conditionals, math, etc. Over the course of the tutorial, we’ll extend Kaleidoscope to support the if/then/else construct, a for loop, user defined operators, JIT compilation with a simple command line interface, etc.

Because we want to keep things simple, the only datatype in Kaleidoscope is a 64-bit floating point type (aka ‘double’ in C parlance). As such, all values are implicitly double precision and the language doesn’t require type declarations. This gives the language a very nice and simple syntax. For example, the following simple example computes Fibonacci numbers:

# Compute the x'th fibonacci number.
def fib(x)
  if x < 3 then
    1
  else
    fib(x - 1) + fib(x - 2)

# This expression will compute the 40th number.
fib(40)

We also allow Kaleidoscope to call into standard library functions (the LLVM JIT makes this completely trivial). This means that you can use the extern keyword to define a function before you use it (this is also useful for mutually recursive functions). For example:

extern sin(arg);
extern cos(arg);
extern atan2(arg1 arg2);

atan2(sin(.4), cos(42))

A more interesting example is included in Chapter 6 where we write a little Kaleidoscope application that displays a Mandelbrot Set at various levels of magnification.

Lets dive into the implementation of this language!

1.3. The Lexer

When it comes to implementing a language, the first thing needed is the ability to process a text file and recognize what it says. The traditional way to do this is to use a “lexer” (aka ‘scanner’) to break the input up into “tokens”. Each token returned by the lexer includes a token code and potentially some metadata (e.g. the numeric value of a number). First, we define the possibilities:

namespace Kaleidoscope
{
  public enum Token
  {
    EOF = -1,
    DEF = -2,
    EXTERN = -3,
    IDENTIFIER = -4,
    NUMBER = -5,
  }

  public class Lexer {
    private string identifier;
    private double numVal;

Each token returned by our lexer will either be one of the Token enum values or it will be an ‘unknown’ character like +, which is returned as its ASCII value. If the current token is an identifier, the IdentifierStr global variable holds the name of the identifier. If the current token is a numeric literal (like 1.0), numVal holds its value. Note that the C++ version is using global variables for simplicity, which is not the best choice for a real language implementation, but this C# version doesn’t :).

The actual implementation of the lexer is a single function named GetNextTokenImpl. The GetNextTokenImpl function is called to return the next token from standard input. Its definition starts as:

private const int EOF = -1;

private readonly TextReader reader;

private readonly StringBuilder identifierBuilder = new StringBuilder();

private readonly StringBuilder numberBuilder = new StringBuilder();

private readonly Dictionary<char, int> binopPrecedence;

public string GetLastIdentifier()
{
  return this.identifier;
}

public double GetLastNumber()
{
  return this.numVal;
}

public int GetNextTokenImpl()
{
  int c = ' ';
  // Skip any whitespace.
  while (char.IsWhiteSpace((char) c))
  {
    c = this.reader.Read();
  }

GetNextToken works by calling the C# reader.Read() function to read characters one at a time from standard input. It eats them as it recognizes them and stores the last character read, but not processed, in LastChar. The first thing that it has to do is ignore whitespace between tokens. This is accomplished with the loop above.

The next thing GetNextToken needs to do is recognize identifiers and specific keywords like def. Kaleidoscope does this with this simple loop:

if (char.IsLetter((char) c)) // identifier: [a-zA-Z][a-zA-Z0-9]*
{
  this.identifierBuilder.Append((char) c);
  while (char.IsLetterOrDigit((char) (c = this.reader.Read())))
  {
    this.identifierBuilder.Append((char) c);
  }

  this.identifier = this.identifierBuilder.ToString();
  this.identifierBuilder.Clear();

  if (string.Equals(identifier, "def", StringComparison.Ordinal))
  {
    return (int) Token.DEF;
  }
  else if (string.Equals(identifier, "extern", StringComparison.Ordinal))
  {
    return (int) Token.EXTERN;
  }
  else
  {
    return (int) Token.IDENTIFIER;
  }
}

Note that this code sets the identifierBuilder field whenever it lexes an identifier. Also, since language keywords are matched by the same loop, we handle them here inline. Numeric values are similar:

// Number: [0-9.]+
if (char.IsDigit((char) c) || c == '.')
{
  do
  {
    this.numberBuilder.Append((char) c);
    c = this.reader.Read();
  } while (char.IsDigit((char) c) || c == '.');
  this.numVal = double.Parse(this.numberBuilder.ToString());
  this.numberBuilder.Clear();
  return (int) Token.NUMBER;
}

This is all pretty straight-forward code for processing input. When reading a numeric value from input, we use the C# double.Parse function to convert it to a numeric value that we store in numVal. Note that this isn’t doing sufficient error checking: it will incorrectly read 1.23.45.67 and handle it as if you typed in 1.23. Feel free to extend it :). Next we handle comments:

if (c == '#')
{
  // Comment until end of line.
  do
  {
    c = this.reader.Read();
  } while (c != EOF && c != '\n' && c != '\r');

  if (c != EOF)
  {
    return this.GetNextTokenImpl();
  }
}

We handle comments by skipping to the end of the line and then return the next token. Finally, if the input doesn’t match one of the above cases, it is either an operator character like + or the end of the file. These are handled with this code:

      // Check for end of file.  Don't eat the EOF.
      if (c == EOF)
      {
        return (int) Token.EOF;
      }
    
      return this.reader.Read();
    }
  }
}

With this, we have the complete lexer for the basic Kaleidoscope language (the full code listing for the Lexer is available in the next chapter of the tutorial). Next we’ll build a simple parser that uses this to build an Abstract Syntax Tree. When we have that, we’ll include a driver so that you can use the lexer and parser together.

Next: Implementing a Parser and AST