State Language Compiler
Description: This is the beginning of my fiddling with the LLVM compiler.

I've create a very simple language (if I can call it *that*) which consists of global variables and static functions called states. Each state executes and should change state, otherwise the program finishes (to avoid execution leaking).

This compiler is my experimentation, then, with the LLVM infrastructure using that simple language. The simpler the language, the more time I have to fiddle with LLVM, so it probably won't grow bigger than this.

Feel free to mess up with the code, but remember it's LGPL (changed due requests).

This example is based on the code of another LLVM example, Kaleidoscope. But since that language is a bit more complicated than mine, and I'd like to get my hands dirty, I decided to create my own.

The compiler is using some Function Passes to optimise the code, one of the biggest benefits of the LLVM infrastructure. You can print the code generated with and without optimizations to compare the benefits and also run using the Just-In-Time virtual machine.

I've written a presentation to help explain why I've chosen LLVM to write a compiler. It should help you understand why LLVM is so easy and powerful, plus show a bit of the internal structure of SLC. The presentation can be found here LLVM-pet-project.pdf.


Language: Check the readme file for more details on syntax and plans.

A simple program for your delight:

# Fibonacci sequence

var prev = 1;
var this = 1;
var max = 100;
var tmp = this;

state incr
	tmp = prev + this;
	prev = this;
	this = tmp;
	goto check;
end

state check
	if this >= 100;
		exit;
	else
        print tmp;
		goto incr;
	endif
end
Limitations:
  • expression evaluated linearly (no operator precedence, use brackets)
  • printing only variables (no strings)


Usage / tests: Compile: make/gmake should suffice. You'll need LLVM from SVN and boost::shared_ptr. On Mac, remember to remove the "strip" line from the Makefile, otherwise print functions won't work.

Tests: Make test runs some tests on the sample provided. All should print OK.

Syntax: slc <source-file.slc> [(e)|o|c|p|t]

  • e: compile and execute (default)
  • t: tokenise only and show tokens
  • p: parse and show code structure
  • c: generate code and show LLVM IR
  • o: show optimized version of IR

Some examples example are included.


Source code / binaries: Linux code/binaries: slc_0.2.1.tar.gz [LGPL license] (last updated 01 Oct 2009)

New on this release: Minor bug-fixes, more tests, performance tests against C/gcc.

Mac binary: slc_0.2.1.mac.bz2, kindly compiled by Andre Uratsuka Manoel.

The code was compiled with the latest SVN repository of LLVM but should be fine with LLVM 2.6 (yet to be released). You can either download from svn or use the Linux binary (compiled on an Ubuntu 9.04) provided.


Rengolin This page was created using Vim