Compilers

spring 2024

THIS IS AN OLD VERSION - Find the 2025 course here

0. Intro & setup

Motivation

Why study compilers? This question is worth asking, since compilers are non-trivial contraptions and most of us probably won’t work directly on one of them during our careers.

While curiosity is always a good reason, there are practical reasons too:

About this material

This course aims to be straightforward, practical and hands-on. It typically presents one good way of accomplishing a task, and then asks you to try it in practice.

This means that, due to limited time, some theoretical breadth and depth has been sacrificed. If you’re looking for more, there are many great books on compilers.

The text has some green links. These reveal optional, non-essential content that expands upon or contextualizes things.

Project setup

While you can complete the course project in any language you like, and almost all popular languages should be suitable, our examples and instructions are for Python.

If you’re using Python, you can download a project template and follow the instructions in README.md to set it up.

Additional instructions for MacOS

You can get pretty far with just MacOS, but ultimately the project requires you to generate and test Assembly code for x86-64 Linux. Below are some ways to get an x86-64 Linux command line that can see your project files.

Method 1: Remote development

If you have access to an x86-64 Linux machine, you can develop on that via SSH. VSCode has a ”Remote - SSH” extension that can make this fairly seamless (Instructions).

Method 2: Docker for automated Linux VM setup

  1. Install Docker Desktop
    • On Apple Silicon Macs, install Rosetta: softwareupdate --install-rosetta
  2. Open Docker Desktop and complete the configuration wizard.
    • You don’t need to sign in.
    • You can skip the survey.
  3. In your project, create a file called Dockerfile with the following contents:
     FROM --platform=linux/amd64 debian:12
    
     RUN apt-get update \
     && apt-get install -y build-essential python3 curl git zlib1g-dev libssl-dev libbz2-dev libffi-dev libreadline-dev liblzma-dev libsqlite3-dev \
     && apt-get clean
    
     RUN curl https://pyenv.run | bash \
     && echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_python_setup \
     && echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_python_setup \
     && echo 'eval "$(pyenv init -)"' >> ~/.bash_python_setup \
     && echo 'source ~/.bash_python_setup' >> ~/.bashrc \
     && echo 'source ~/.bash_python_setup' >> ~/.profile
    
     COPY .python-version /project/
     RUN cd /project && bash -lc 'pyenv install'
    
     RUN curl -sSL https://install.python-poetry.org | python3 - \
     && echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bash_python_setup
    
     COPY pyproject.toml poetry.lock /project
     RUN cd /project && bash -lc 'poetry install' && rm -Rf /project && mkdir -p /project
    
     WORKDIR /project
    
  4. Run the following command to open a Linux shell in your project (the first run may take a while):
     docker build -t compilers-dev:latest . && \
       docker run -it --rm -v .:/project compilers-dev:latest
    
  5. Now you have an x86-64 Linux command line where you can see your code in directory /project and run the project’s usual commands like ./check.sh and ./compiler.sh ....
    • Any changes you make in the Linux environment outside of /project will be lost. To make permanent changes, edit the Dockerfile and restart the Linux environment.

Method 3: Full Linux VM

You can run a full Linux desktop in a virtual machine using e.g. UTM. In this option you could do all your development in the virtual machine window, or you could combine this with method 1 and develop in the VM over SSH.