In general, compilation means translating programs from a high-level language to a lower-level language. For example, the following MinCaml function, which computes the greatest common divisor of two non-negative integers,
let rec gcd m n =
if m = 0 then n else
if m <= n then gcd m (n - m) else
gcd n (m - n)
is compiled into the following SPARC assembly.
gcd.7:
cmp %i2, 0
bne be_else.18
nop
mov %i3, %i2
retl
nop
be_else.18:
cmp %i2, %i3
bg ble_else.19
nop
sub %i3, %i2, %i3
b gcd.7
nop
ble_else.19:
sub %i2, %i3, %o5
mov %i3, %i2
mov %o5, %i3
b gcd.7
nop
At a first glance, there is a huge gap between the two languages. The MinCaml compiler bridges this gap by defining appropriate intermediate languages and applying simple translations one by one. The major five gaps between MinCaml and SPARC assembly are:
To bridge these gaps, MinCaml applies translations such as
in this order. We will explain these translations and optimizations in what follows.