8000 GitHub - davidwholm/miniracket at integers-and-variables · GitHub
[go: up one dir, main page]

Skip to content

About

Compiler for a small Scheme-like language down to x86_64 assembly.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

miniracket

A compiler for the language of the first chapter in Essentials of Compilation by Jeremy G. Siek.

Language Description

program         := expr
expr            := integer | identifier | let | primitive
let             := (let ((identifier expr)) expr)
primitive       := (primitive-op expr*)
primitive-op    := + | - | read

Passes

The compiler has the following passes:

  • parse, takes a s-expression and produces an AST
  • uniquify, replaces all variables with unique names to make scoping and shadowing explicit
  • remove-complex-operands, simplifies all operands of primitive operations to be atomic, potentially resulting in additional temporary variables
  • type-check, type checks the program and associates types with all variables
  • explicate-control, flattens the AST into a linear sequence of C-like statements
  • select-instructions, selects X86 instructions based on said C-like statements
  • assign-homes, assigns to each variable a stack location and replaces the corresponding variable occurrences with dereferences
  • patch-instructions, makes sure that all instructions are valid X86 (at most one memory reference per instruction, etc.)
  • prelude-and-conclusion, generates necessary prelude and conclusion to set up for the main code (stashing rbp, adjusting rsp etc.)
  • print-x86, takes the program and translates it into a string of valid X86 code, ready to be assembled

Usage

The compiler takes either a filename to read from or reads from stdin. It spits out x86 assembly code, which can be redirected to file.

$ echo "(+ 10 32)" | racket main.rkt > program.S
$ gcc program.S runtime.c -o program
$ ./program
$ echo $?
42

About

Compiler for a small Scheme-like language down to x86_64 assembly.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0