| 1 |
\section{Instruction Streams} |
| 2 |
|
| 3 |
\subsubsection{Overview} |
| 4 |
An \newdef{instruction stream} |
| 5 |
is an abstraction used by MLRISC to describe linearized instructions. |
| 6 |
This abstraction turns out to fit the function of |
| 7 |
many MLRISC modules. For example, |
| 8 |
a phase such as \href{instrsel.html}{Instruction Selection} |
| 9 |
can be viewed as taking an stream of |
| 10 |
\href{mltree.html}{MLTREE} statements and return a |
| 11 |
stream of \href{instructions.html}{instructions}. Similarly, |
| 12 |
phases such as \href{asm.html}{assembly output} and |
| 13 |
\href{mc.html}{machine code generation} can be seen |
| 14 |
as taking a stream of instructions and |
| 15 |
returning a stream of characters and a stream of bytes. |
| 16 |
|
| 17 |
\subsubsection{The Details} |
| 18 |
An instruction stream satisfy the following abstract signature: |
| 19 |
\begin{SML} |
| 20 |
signature \mlrischref{instructions/stream.sig}{INSTRUCTION_STREAM} = |
| 21 |
sig |
| 22 |
structure P : \href{pseudo-ops.html}{PSEUDO_OPS} |
| 23 |
|
| 24 |
datatype ('a,'b,'c,'d,'e,'f) stream = |
| 25 |
STREAM of |
| 26 |
\{ beginCluster: int -> 'b, |
| 27 |
endCluster : 'c -> unit, |
| 28 |
emit : 'a, |
| 29 |
pseudoOp : P.pseudo_op -> unit, |
| 30 |
defineLabel : Label.label -> unit, |
| 31 |
entryLabel : Label.label -> unit, |
| 32 |
comment : string -> unit, |
| 33 |
annotation : Annotations.annotation -> unit, |
| 34 |
exitBlock : 'd -> unit, |
| 35 |
alias : 'e -> unit, |
| 36 |
phi : 'f -> unit |
| 37 |
\} |
| 38 |
end |
| 39 |
\end{SML} |
| 40 |
This type is specialized in other modules as such the |
| 41 |
\href{asm.html}{assembler}, the \href{mc.html}{machine code emitter}, |
| 42 |
and the \href{instrsel.html}{instruction selection modules}. |
| 43 |
\subsubsection{The protocol} |
| 44 |
All instruction streams, irrespective of their actual types, |
| 45 |
follow the following protocol: |
| 46 |
\begin{itemize} |
| 47 |
\item The method \sml{beginCluster} should be called at the beginning of |
| 48 |
the stream to mark the start of a new compilation unit. |
| 49 |
The integer passed to this method is the number |
| 50 |
of bytes in the stream. This integer is only used for |
| 51 |
machine code emitter, which uses it to allocate space for the |
| 52 |
code string. |
| 53 |
\item The method \sml{endCluster} should be called when the entire |
| 54 |
compilation unit has been sent. |
| 55 |
\item In between these calls, the following methods can be called in any |
| 56 |
order: |
| 57 |
\begin{itemize} |
| 58 |
\item \sml{emit} -- this method emits an instruction. It takes |
| 59 |
a \href{regmap.html}{regmap} as argument. |
| 60 |
\item \sml{pseudoOp} -- this method emits a pseudo op. |
| 61 |
\item \sml{defineLabel} -- this method defines a \emph{local} label, i.e. |
| 62 |
a label that is only referenced within the same compilation unit. |
| 63 |
\item \sml{entryLabel} -- this method defines an \emph{enternal} label that |
| 64 |
marks an procedure entry, and may be referenced from other |
| 65 |
compilation units. |
| 66 |
\item \sml{comment} -- this emits a comment string |
| 67 |
\item \sml{annotation} -- this function attaches an annotation to |
| 68 |
the current basic block. |
| 69 |
\item \sml{exitBlock} -- |
| 70 |
this marks the current block as an procedure exit. |
| 71 |
\end{itemize} |
| 72 |
\end{itemize} |