Some (basic) stuff about compilers

"Compilers: Backend to Frontend and Back to Front Again" tutorial

Building a compiler, extending a grammar step by step

  • Input: toy scheme
  • Output: x86 executable
  • The tutorial uses scheme as the implementation language, I used Haskell

Introduces tagged pointer representation

Puts the integer representation in %eax register

Introduce stack to save intermediate values

Introduces an environment to map variables to stack positions

Introduces a calling convention putting parameters in the stack

Another approach: multipass compilers

"Essentials of compilation" book: https://github.com/IUCompilerCourse/Essentials-of-Compilation/releases/tag/racket-MIT-press

 

https://brinckerhoff.org/clements/2194-csc431/essentials-of-compilation.pdf

General idea

  • Input is scheme like. Parsing is not a priority.
  • Multiple intermediate languages
  • One pass mapping one to the other
  • Final pass outputs x86_64 assembler

A way of validating:

  • Uniquify: make var names unique
  • remove-complex: makes arguments either vars or consts, but not complex ops
  • explicate-control: makes order of execution explicit
  • uncover-local: ❌ not in the edition I'm reading 😅. Just collecting variables names and putting them in the info
  • select-instr: transforms C into a pseudo x86 with vars

  • assign-homes: places vars into the stack
  • patch-instr: make sure that each instruction adheres to
    the restrictions regarding which arguments can be memory references
  • before print-x86: put a prelude and main labels

Example: explicate-control

Other passes we can do: partial-evaluation

Next topics in the book which I haven't got to

  • Register allocation: not everything needs to be in the stack
  • Apparently there's some type checking and garbage collection in later chapters!

compilers-basics

By Miguel Vilá

compilers-basics

  • 108