Building a C++ Interpreter (Cling) for x86 and ARM

Gallagher Pryor C/C++ Leave a Comment

A C++ interpreter compiled for ARM running on an x86 EC2 instance after following the given instructions. As C++ becomes more mature and high-level, an interpreted workflow might lead to mainstream C++ productivity in addition to development!

Development through a C++ interpreter (Cling) as opposed to a standard compiler is an amazing leap in productivity and a window into the newest features of C++. This post tells you how to get your own bleeding-edge C++ interpreter built right on top of the development version of LLVM. We give you a repeatable procedure via Amazon EC2.

With our prescribed steps in place, you can always have an up-to-date development version of Cling. This allows quick testing and investigation of LLVM's newest features. You will also possess a development environment as a path to contributing to Cling or learning how Cling's innards push C++ code along.

Background

Astoundingly, there is such a thing as a C++ interpreter! Thus, if you're interested in sticking to C++ for speed and efficiency, you're not confined to the code-compile-run-debug workflow. Living outside such a workflow lends jumps in productivity and a unique angle through which to see C++.

Development of a serious C++ interpreter was undertaken by the wizards at CERN starting at around 1995. (You might recognize this group as searchers for the Higgs Boson). The C++ interpreter was dubbed CINT and was designed to support a major data analysis and visualization package known as ROOT (screenshot below). The system is certainly serious as it is giving researchers a way to investigate the very fabric of the universe!

A ROOT screenshot being driven by a C++ interpreter (CINT). CINT is not a toy implementation - rather it is used to investigate the fabric of the universe.

Over the last 20 years, CINT fell behind the evolving C++ language. In response, individuals at CERN began work on a replacement for CINT dubbed Cling with the strategy of leveraging LLVM to stay up to date with the growing C++ specification. Up until recently, the project has been rather nascent, but now it is at the point where its functionality and build system make Cling worthwhile for use and contribution.

Build instructions for x86

We present a build procedure that leverages Amazon EC2. By using the cloud, you are guaranteed to use the exact Linux version and hardware capabilities that we did in testing. We assume that you have an Amazon EC2 account. The expected compute resource cost will be around two dollars.

  1. Create an m3.xlarge instance with the Debian Jessie AMI (ami-aae18fc2). Be sure to grab at least 32Gb of storage.

  2. Boot the instance and login as admin.

  3. Install required build packages on the instance.

  4. Use the distributed build script from the Cling project to clone required source code and build an x86 version of Cling with all available cores.

    ...this might take some time...

    Building cling on an x86 EC2 instance
  5. Take Cling for a spin!

Build instructions for ARM

Whereas an x86 build is readily supported by the Cling developers, building for ARM is not a standard build path. Thus, ARM build instructions are a bit more involved.

  1. Log in as admin to the (same x86) instance described.

  2. First, you'll need LLVM and Clang as they provide utilities for compilation: llvm-tblgen and clang-tblgen.

  3. Install ARM hard-float compilation tools.

  4. Install QEMU-based ARM emulation. This is a fantastic feature of newer linux kernels: ARM executables are registered with the system so that under a normal invocation, QEMU is invoked under the hood so that it seems as though ARM binaries are native.

    Having ARM emulation on our fast build machine makes debugging amazingly faster.

  5. Install build tools: cmake and ninja.

  6. Clone LLVM, Clang, and Cling to the llvm directory. This directory differs from the instructions, above, so no worries if you're following these steps in the order of the blog post. Note the specific SHA's we jump to in the repositories. As of this writing, ARM support is in and out, but will hopefully materialize for good!

  7. Configure a build in a new build directory using cmake. Note the intense and carefully crafted build flags required to do a successful cross compilation. This took quite a bit of debugging to lock down.

    Note that the following assumes that the prior step was run in /mnt resulting in an /mnt/llvm directory. Indeed, the following block assumes that it is run in the /mnt directory, as well.

  8. Do a build with ninja and install to the install subdirectory. This will take a few minutes.

  9. Fire up Cling! You can do this directly on your Amazon EC2 instance through QEMU, but watch out - it is a tad unstable in the case of recovering from syntax errors, etc. However, you can copy what you built to an ARM machine such as a Raspberry Pi or Cubox to take it for a whirl there, too!

Facebooktwitterredditlinkedinmail