| 1 |
\section{MLRisc Generation} |
| 2 |
Every compiler will eventually compile down to an abstract machine |
| 3 |
that it believes will execute source programs efficiently. The |
| 4 |
abstract machine will typically consists of abstract machine |
| 5 |
registers and instructions, one or more stacks, and parameter |
| 6 |
passing conventions. The hope is that all this will map down |
| 7 |
efficiently onto the target machine. Indeed, the abstract machine |
| 8 |
should be reasonably close to architectures that are envisioned as |
| 9 |
possible targets. Several step need to be followed in the generation |
| 10 |
of MLRisc. |
| 11 |
|
| 12 |
\begin{enumerate} |
| 13 |
\item The first step in generating target machine code is to define |
| 14 |
the MLRisc intermediate representation after it has been |
| 15 |
appropriately specialized. The interfaces that describe the |
| 16 |
dimensions of specialization are quite simple. Depending on the |
| 17 |
compiler, these may be target dependent; for example, in the SML/NJ |
| 18 |
compiler, the encoding of registers used to indicate the roots of |
| 19 |
garbage collection depend on how the runtime system decodes the |
| 20 |
information. |
| 21 |
|
| 22 |
\item The only real connection between the MLRisc intermediate |
| 23 |
representation and the target machine is that the first |
| 24 |
$0..K-1$ MLRisc registers map onto the first $K$ |
| 25 |
physical registers on the target machine. Thus some mapping of |
| 26 |
dedicated abstract machine registers to physical target registers is |
| 27 |
required. It is not always necessary to map abstract machine |
| 28 |
registers to physical machine registers. For example, on |
| 29 |
architectures like the x86 with few registers, some abstract machine |
| 30 |
registers may be mapped to fixed memory locations. Thus an abstract |
| 31 |
machine register like the \sml{maskReg} may have something like: |
| 32 |
\begin{SML} |
| 33 |
LOAD(32, LABEL maskRegLab) |
| 34 |
\end{SML} |
| 35 |
spliced instead. |
| 36 |
|
| 37 |
\item The unit of compilation is called a |
| 38 |
\href{cluster.html}{cluster} which |
| 39 |
is the smallest unit for inter-procedural optimizations. A cluster |
| 40 |
will typically consist of several entry points that may call each |
| 41 |
other, as well as call local functions in the module. For maximum |
| 42 |
flexibility, the parameter passing convention for local functions |
| 43 |
should be specialized by the \href{mlrisc-ra.html}{register allocator}. |
| 44 |
|
| 45 |
Once the MLRisc trees for a cluster have been built, they must |
| 46 |
be converted into target assembly or machine code. This is done by |
| 47 |
building up a function (\newdef{codegen}) that |
| 48 |
glues together optimizations modules that have been specialized. For |
| 49 |
example, the target instruction set must be specialized to hold the |
| 50 |
MLRisc constants; the flowgraph must be specialized to carry these |
| 51 |
instructions as well as the MLRisc pseudo-ops; the optimization |
| 52 |
modules must know about several front end constraints such as how to |
| 53 |
spill registers. |
| 54 |
\end{enumerate} |
| 55 |
|
| 56 |
If the module that translates the abstract machine instructions |
| 57 |
into MLRisc instructions has been appropriately parameterized, then |
| 58 |
it can be reused for multiple target architectures. For high level |
| 59 |
languages it is better to generate MLRisc instructions from the high |
| 60 |
level intermediate form used by the front end of the compiler. |