User Tools

Site Tools


Photon Compiler Development: Some Cleaning

This is the 13th article of a series (table of contents) about compiler development with LLVM using OCaml. We intend to develop a compiler for a subset of OCaml large enough to allow our compiler to compile itself.

This article is more a status update than anything else. It only brings some library updates and code clean-up.

Status Update

It has been a long time since my last Photon-related post. I had few time for Photon development last months, overwhelmed by professional work, NGO activities and my other hobbies.

I also felt back into my old sin of experimenting too much in too many directions, without keeping straight on path. I multiplied branches in my Photon own Git repository, and experimented again with various language design issues. I even hesitated to pretend this whole series never existed and start from scratch again, possibly towards another goal language.

To prevent this from occurring again and again, I will try to be more regular with updates. In the process, I'll switch to smaller, less formal posts. Feel free to ask questions in the comments!


LLVM 2.9

Photon now links with version 2.9 of the LLVM library. Required changes were a path change in and a small adaptation to an API change for module creation in the Interpreter.

Embedding Interpreter into Compiler

Our interpreter and compiler were sharing much state. It was not easy to enforce this internal state being used in a consistent manner in both modules. I finally decided to remove the Interpreter module, moving its functionality directly into Compiler.

Fixing a Bug with Negate

Negate operators for integer and floating-point numbers (respectively I_neg and F_neg in our AST) were compiled using the same build_neg LLVM function.

I don't know whether this bug was already present or if it is due to a change in LLVM API, but this does not work with floating-point numbers. One has to use build_fneg for these.

The code has been fixed accordingly.

Removed LLVM Bug Workaround

We encountered some bugs in LLVM OCaml bindings previously. We had to use workarounds to circumvent these bugs.

As these bugs have been fixed in version 2.9 of LLVM bindings, the corresponding workaround code has been removed.

Shortened Tests

Tests have been modified to run more quickly. When we will bring optimization on the table, we will consider longer test to obtain more meaningful comparisons.

Cosmetic Changes

  • main has been split into smaller functions.
  • Some error messages have been improved.
  • Error handling now mostly uses string messages, rather than custom error types. This is less flexible, but simpler and sufficient for current use.
  • Various other small code improvements.

In the next installment, we will improve unit type handling.

Source Code

The code accompanying this article is available in archive photon-tut-13.tar.xz or through the git repository:

git clone
cd photon
git checkout 13-cleanup.2


Enter your comment. Wiki syntax is allowed:
blog/2012/01/24-some_cleaning.txt · Last modified: 2012/01/24 22:07 by csoldani