Building a Human Resource Machine Compiler - Preface
Building a Human Resource Machine Compiler - Preface
This article is part of the Human Resource Machine Compiler series.
Six years ago, when I first played this level of Human Resource Machine, it struck me that the solution closely mirrors what a compiler typically does. Since then, the idea of building a compiler for Human Resource Machine has been stuck in my mind.
Writing assembly code is always a frustrating experience (for most of time). Some levels in this game demand a significant amount of effort just to get the instructions right. Every time I play, I find myself wishing for a compiler that could make the process easier.
After all these years, I’ve finally decided to turn this idea into reality. This series will cover the process of building a not-so-modern compiler, from the frontend to the backend, encompassing language design, lexical analysis, parsing, intermediate representation (IR), code generation, and optimization. We won’t be covering an assembler since it’s unnecessary for this game 😊.
Initially, my plan was to implement a backend for LLVM that could compile IR into HRM assembly, allowing one to write a C program to solve the levels. However, after spending an entire weekend on it, I realized that this approach is nearly impossible. The Human Resource Machine architecture is far too simple, and LLVM has many assumptions based on modern architectures. For example, HRM has absolutely no capability for a function call
—it has no stack nor link register. A function won't be able to know the caller address it'll need to return to, unless you manage memory addresses manually 😣. So, we’ll have to build the compiler from the scratch.
I plan to cover the following components in this series. I hope to explain these concepts in a simple and understandable way, though my knowledge is somewhat limited. If this series ever helps you solve the game’s puzzles or learn about compiler design, that would be my greatest honor.
The code is still WIP. Updates on this blog may be lagged, but I'll eventually cover all topics. You may find the code in GitHub. Please refer to the this page for the index.
- Language design
- Lexer and Parser
- Sematic Analyzer
- Intermediate Representation (IR) Generator
- IR Optimizer
- Code Generator
- Code Optimizer