| 1 |
\section{Instruction Selection} \label{sec:instrsel} |
| 2 |
Instruction selection modules are reponsible for translating |
| 3 |
\href{mltree.html}{MLTree} statements into a flowgraph consisting |
| 4 |
of target machine instructions. MLRISC decomposes this complex task |
| 5 |
into \emph{three} components: |
| 6 |
\begin{description} |
| 7 |
\item[Instruction selection modules] which are responsible for |
| 8 |
mapping a sequence of MLTree statements into a sequence target machine code, |
| 9 |
\item[Flowgraph builders] which are responsible for constructing |
| 10 |
the graph representation of the program from a sequence of target machine |
| 11 |
instructions, and |
| 12 |
\item[Client Extender] which are responsible for compiling |
| 13 |
MLTree extensions (see also Section~\ref{sec:mltree-extension}). |
| 14 |
\end{description} |
| 15 |
By detaching these components, extra flexiblity is obtained. For example, |
| 16 |
the MLRISC system uses two different internal representations. The |
| 17 |
first, \href{cluster.html}{cluster}, is a \emph{light-weight} representation |
| 18 |
which is suitable for simple compilers without extensive |
| 19 |
optimizations; the second, \href{mlrisc-ir.html}{MLRISC IR}, is a |
| 20 |
\emph{heavy duty} representation which allows very complex transformations |
| 21 |
to be performed. Since the flowgraph builders are detached from the |
| 22 |
instruction selection modules, the same instruction selection modules |
| 23 |
can be used for both representations. |
| 24 |
|
| 25 |
For consistency, the three components communicate to each other |
| 26 |
via the same \href{stream.html}{stream} interface. |
| 27 |
|
| 28 |
\subsection{Interface Definition} |
| 29 |
All instruction selection modules satisfy the following signature: |
| 30 |
|
| 31 |
\begin{SML} |
| 32 |
signature \mlrischref{mltree/mltreecomp.sig}{MLTREECOMP} = |
| 33 |
sig |
| 34 |
structure T : \href{mltree.html}{MLTREE} |
| 35 |
structure I : \href{instructions.html}{INSTRUCTIONS} |
| 36 |
structure C : \href{cells.html}{CELLS} |
| 37 |
sharing T.LabelExp = I.\href{labelexp.html}{LabelExp} |
| 38 |
sharing I.C = C |
| 39 |
|
| 40 |
type instrStream = (I.instruction,C.regmap,C.cellset) T.stream |
| 41 |
type mltreeStream = (T.stm,C.regmap,T.mlrisc list) T.stream |
| 42 |
|
| 43 |
val selectInstructions : instrStream -> mltreeStream |
| 44 |
end |
| 45 |
\end{SML} |
| 46 |
Intuitively, this signature states that |
| 47 |
the instruction selection module |
| 48 |
returns a function that can transform a stream of MLTree statements |
| 49 |
(\newtype{mltreeStream}) into a stream of instructions of the target |
| 50 |
machine (\newtype{instrStream}). |
| 51 |
|
| 52 |
\subsubsection{Compiling Client Extensions} |
| 53 |
|
| 54 |
Compilation of client extensions to MLTREE is controlled by the |
| 55 |
following signature: |
| 56 |
\begin{SML} |
| 57 |
signature \mlrischref{mltree/mltreecomp.sig}{MLTREE_EXTENSION_COMP} = |
| 58 |
sig |
| 59 |
structure T : \href{mltree.html}{MLTREE} |
| 60 |
structure I : \href{instructions.html}{INSTRUCTIONS} |
| 61 |
structure C : \href{cells.html}{CELLS} |
| 62 |
sharing T.LabelExp = I.\href{labelexp.html}{LabelExp} |
| 63 |
sharing I.C = C |
| 64 |
|
| 65 |
type reducer = |
| 66 |
(I.instruction,C.regmap,C.cellset,I.operand,I.addressing_mode) T.reducer |
| 67 |
|
| 68 |
val compileSext : reducer -> \{stm:T.sext, an:T.an list\} -> unit |
| 69 |
val compileRext : reducer -> \{e:T.ty * T.rext, rd:C.cell, an:T.an list\} -> unit |
| 70 |
val compileFext : reducer -> \{e:T.ty * T.fext, fd:C.cell, an:T.an list\} -> unit |
| 71 |
val compileCCext : reducer -> \{e:T.ty * T.ccext, ccd:C.cell, an:T.an list\} -> unit |
| 72 |
end |
| 73 |
\end{SML} |
| 74 |
|
| 75 |
Methods \verb|compileSext|, \verb|compileRext|, etc.~are callbacks that |
| 76 |
are responsible for compiling MLTREE extensions. The arguments |
| 77 |
to these callbacks have the following meaning: |
| 78 |
\begin{description} |
| 79 |
\item[reducer] The first argument is always the \newtype{reducer}, |
| 80 |
which contains internal methods for translating MLTree constructs |
| 81 |
into machine code. These methods are supplied by the instruction |
| 82 |
selection modules. |
| 83 |
\item[an] This is a list of annotations that should be attached to the |
| 84 |
generated code. |
| 85 |
\item[ty, fty] These are the types of the extension construct. |
| 86 |
\item[stm, e] This are the extension statement and expression. |
| 87 |
\item[rd, fd, cd] These are the target registers of the |
| 88 |
expression extension, i.e.~the callback should generate the appropriate |
| 89 |
code for the expression and writes the result to this target. |
| 90 |
\end{description} |
| 91 |
|
| 92 |
For example, when an instruction selection encounters a |
| 93 |
\verb|FOR(|$i,a,b,s$\verb|)| statement extension |
| 94 |
defined in Section~\ref{sec:mltree-extension}, the callback |
| 95 |
\begin{SML} |
| 96 |
compileStm reducer \{ stm=FOR(\(i,a,b,s\)), an=an \} |
| 97 |
\end{SML} |
| 98 |
\noindent is be involved. |
| 99 |
|
| 100 |
The \newtype{reducer} type is defined |
| 101 |
in the signature \mlrischref{mltree/mltree.sig}{MLTREE} and has the |
| 102 |
following type: |
| 103 |
\begin{SML} |
| 104 |
datatype reducer = |
| 105 |
REDUCER of |
| 106 |
\{ reduceRexp : rexp -> reg, |
| 107 |
reduceFexp : fexp -> reg, |
| 108 |
reduceCCexp : ccexp -> reg, |
| 109 |
reduceStm : stm * an list -> unit, |
| 110 |
operand : rexp -> I.operand, |
| 111 |
reduceOperand : I.operand -> reg, |
| 112 |
addressOf : rexp -> I.addressing_mode, |
| 113 |
emit : I.instruction * an list -> unit, |
| 114 |
instrStream : (I.instruction,C.regmap,C.cellset) stream, |
| 115 |
mltreeStream : (stm,C.regmap,mlrisc list) stream |
| 116 |
\} |
| 117 |
\end{SML} |
| 118 |
|
| 119 |
The components of the reducer are |
| 120 |
\begin{description} |
| 121 |
\item[reduceRexp, reduceFexp, reduceCCexp] These functions |
| 122 |
take an expression of type integer, floating point and condition code, |
| 123 |
translate them into machine code and return the |
| 124 |
register that holds the result. |
| 125 |
\item[reduceStm] This function takes an MLTree statement and translates |
| 126 |
it into machine code. it also takes a second argument, which is the |
| 127 |
list of annotations that should be attached to the statement. |
| 128 |
\item[operand] This function translates an \sml{rexp} into an |
| 129 |
\sml{operand} of the machine architecture. |
| 130 |
\item[reduceOperand] This function takes an operand of the machine |
| 131 |
architecture and reduces it into an integer register. |
| 132 |
\item[addressOf] This function takes an \sml{rexp}, treats |
| 133 |
it as an address expression and translates it into the appropriate |
| 134 |
\sml{addresssing_mode} of the target architecture. |
| 135 |
\item[emit] This function emits an instruction with attached annotations |
| 136 |
to the instruction stream |
| 137 |
\item[instrStream, mltreeStream] These are the instruction stream |
| 138 |
and mltree streams that are currently bound to the extender. |
| 139 |
\end{description} |
| 140 |
|
| 141 |
\subsubsection{Extension Example} |
| 142 |
Here is an example of how the extender mechanism can be used, |
| 143 |
using the \sml{DSP_MLTREE} extensions defined in |
| 144 |
Section~\ref{sec:mltree-extension}. We need supply two |
| 145 |
new functions, \verb|compileDSPStm| for compiling the \verb|FOR| |
| 146 |
construct, and \verb|compileDSPRexp| for compiling the \verb|SUM|, |
| 147 |
and saturated arithmetic instructions. |
| 148 |
|
| 149 |
The first function, \sml{compileDSPStm}, is generic and simply |
| 150 |
translates the \verb|FOR| loop into the appropriate branches. |
| 151 |
Basically, we will translate \verb|FOR(|$i,start,stop,body$\verb|)| into |
| 152 |
the following loop in pseudo code: |
| 153 |
\begin{SML} |
| 154 |
limit = \(stop\) |
| 155 |
\(i\) = \(start\) |
| 156 |
goto test |
| 157 |
loop: \(body\) |
| 158 |
\(i\) = \(i\) + 1 |
| 159 |
test: if \(i\) <= limit goto loop |
| 160 |
\end{SML} |
| 161 |
This transformation can be implemented as follows: |
| 162 |
\begin{SML} |
| 163 |
functor DSPMLTreeExtensionComp |
| 164 |
(structure I : DSP_INSTRUCTION_SET |
| 165 |
structure T : DSP_MLTREE |
| 166 |
sharing I.LabelExp = T.LabelExp |
| 167 |
) = |
| 168 |
struct |
| 169 |
structure I = I |
| 170 |
structure T = T |
| 171 |
structure C = I.C |
| 172 |
|
| 173 |
type reducer = |
| 174 |
(I.instruction,C.regmap,C.cellset,I.operand,I.addressing_mode) T.reducer |
| 175 |
|
| 176 |
fun mark(s, []) = s |
| 177 |
| mark(s, a::an) = mark(ANNOTATION(s, a), an) |
| 178 |
fun compileSext (REDUCER\{reduceStm, ...\}) |
| 179 |
\{stm=FOR(i, start, stop, body), an\} = |
| 180 |
let val limit = C.newReg() |
| 181 |
val loop = Label.newLabel "" |
| 182 |
val test = Label.newLabel "" |
| 183 |
in reduceStm( |
| 184 |
SEQ[MV(32, i, start), |
| 185 |
MV(32, limit, stop), |
| 186 |
JMP([], [LABEL(LabelExp.LABEL test)], []), |
| 187 |
LABEL loop, |
| 188 |
body, |
| 189 |
MV(32, i, ADD(32, REG(32, i), LI 1), |
| 190 |
LABEL test, |
| 191 |
mark(BCC([], |
| 192 |
CMP(32, LE, REG(32, i), REG(32, limit)), |
| 193 |
loop), |
| 194 |
an), |
| 195 |
] |
| 196 |
) |
| 197 |
end |
| 198 |
|
| 199 |
... |
| 200 |
\end{SML} |
| 201 |
In this transformation, we have chosen to proprogate the annotation |
| 202 |
\verb|an| into the branch constructor. |
| 203 |
|
| 204 |
Assuming the target architecture that we are translated into contains |
| 205 |
saturated arithmetic instructions \verb|SADD|, \verb|SSUB|, \verb|SMUL| |
| 206 |
and \verb|SDIV|, the DSP extensions |
| 207 |
\verb|SUM| and saturated arithmetic expressions can be handled as follows. |
| 208 |
\begin{SML} |
| 209 |
fun compileRext (REDUCER\{reduceStm, reduceRexp, emit, ...\}) |
| 210 |
\{ty, e, rd, an\} = |
| 211 |
(case (ty,e) of |
| 212 |
(_,T.SUM(i, a, b, exp)) => |
| 213 |
reduceStm(SEQ[MV(ty, rd, LI 0), |
| 214 |
FOR(i, a, b, |
| 215 |
mark(MV(ty, rd, ADD(ty, REG(ty, rd), exp)), an)) |
| 216 |
] |
| 217 |
) |
| 218 |
| (32,T.SADD(x,y)) => emit(I.SADD\{r1=reduceRexp x,r2=reduceRexp y,rd=rd\},an) |
| 219 |
| (32,T.SSUB(x,y)) => emit(I.SSUB\{r1=reduceRexp x,r2=reduceRexp y,rd=rd\},an) |
| 220 |
| (32,T.SMUL(x,y)) => emit(I.SMUL\{r1=reduceRexp x,r2=reduceRexp y,rd=rd\},an) |
| 221 |
| (32,T.SDIV(x,y)) => emit(I.SDIV\{r1=reduceRexp x,r2=reduceRexp y,rd=rd\},an) |
| 222 |
| ... |
| 223 |
) |
| 224 |
|
| 225 |
fun compileFext _ _ = () |
| 226 |
fun compileCCext _ _ = () |
| 227 |
|
| 228 |
end |
| 229 |
\end{SML} |
| 230 |
|
| 231 |
Note that in this example, we have simply chosen to reduce |
| 232 |
a \verb|SUM| expression into the high level \verb|FOR| construct. |
| 233 |
Clearly, other translation schemes are possible. |
| 234 |
|
| 235 |
\subsection{Instruction Selection Modules} |
| 236 |
Here are the actual code for the various back ends: |
| 237 |
\begin{enumerate} |
| 238 |
\item \mlrischref{sparc/mltree/sparc.sml}{Sparc} |
| 239 |
\item \mlrischref{hppa/mltree/hppa.sml}{PA-RISC} |
| 240 |
\item \mlrischref{alpha/mltree/alpha.sml}{Alpha} |
| 241 |
\item \mlrischref{ppc/mltree/ppc.sml}{Power PC} |
| 242 |
\item \mlrischref{x86/mltree/x86.sml}{X86} |
| 243 |
\item C6xx |
| 244 |
\end{enumerate} |