| 1 |
\section{Machine Description} |
| 2 |
\subsection{Overview} |
| 3 |
|
| 4 |
\newdef{MDGen} is a simple tool for generating |
| 5 |
various modules in the MLRISC customizable code generator |
| 6 |
directly from machine descriptions. These descriptions |
| 7 |
contain architectural information such as: |
| 8 |
\begin{enumerate} |
| 9 |
\item How the the register file(s) are organized. |
| 10 |
\item How instructions are encoded in machine code: MLRISC uses |
| 11 |
this information to generate machine instructions directly into a byte stream. |
| 12 |
Directly machine code generation is used in the SML/NJ compiler. |
| 13 |
\item How instructions are pretty printed in assembly: this is used |
| 14 |
for debugging and also for assembly output for other non-SML/NJ backends. |
| 15 |
\item How instructions are internally represented in MLRISC. |
| 16 |
\item Other information needed for performing optimizations, which |
| 17 |
include: |
| 18 |
\begin{enumerate} |
| 19 |
\item The register transfer list (RTL) that defines the |
| 20 |
operational semantics of the instruction. |
| 21 |
\item Delay slot mechanisms. |
| 22 |
\item Information for performing span dependency resolution. |
| 23 |
\item Pipeline and reservation table characteristics. |
| 24 |
\end{enumerate} |
| 25 |
\end{enumerate} |
| 26 |
|
| 27 |
Currently, item 5 is not ready for prime time. |
| 28 |
|
| 29 |
\subsubsection{Why MDGen?} |
| 30 |
MLRISC manipulates all instruction sets via a set of abstract |
| 31 |
interfaces, which allows the programmer to arbitrarily choose an |
| 32 |
instruction representation that is most convenient for a particular |
| 33 |
architecture. However, various functions that manipulate |
| 34 |
this representation must be provided by the instruction set's programmer. |
| 35 |
As the number and complexities of each optimizations grow, and as |
| 36 |
the number of architectures increases, the functions |
| 37 |
for manipulating the instructions become more numerous and complex. |
| 38 |
In order to keep the effort of developing and maintaining |
| 39 |
an instruction set manageable, |
| 40 |
the MDGen tool is developed to (partially) automate this task. |
| 41 |
|
| 42 |
\subsubsection{Syntax} |
| 43 |
|
| 44 |
MDGen's machine descriptions are written in a syntax that is very |
| 45 |
much like that of |
| 46 |
\externhref{http://cm.bell-labs.com/cm/cs/what/smlnj/sml.html}{Standard ML}. |
| 47 |
Most core SML constructs are recognized. |
| 48 |
In addition, new declaration forms specific to MDGen are |
| 49 |
used to specify architectural information. |
| 50 |
|
| 51 |
\paragraph{Reserved Words} |
| 52 |
All SML keywords are reserved words in MDGen. |
| 53 |
In addition, the following keywords are also reserved: |
| 54 |
|
| 55 |
\begin{verbatim} |
| 56 |
always architecture assembly at backwards big bits branching called |
| 57 |
candidate cell cells cellset debug delayslot dependent endian field |
| 58 |
fields formats forwards instruction internal little locations lowercase |
| 59 |
name never nodelayslot nullified opcode ordering padded pipeline predicated |
| 60 |
register rtl signed span storage superscalar unsigned uppercase |
| 61 |
verbatim version vliw when |
| 62 |
\end{verbatim} |
| 63 |
|
| 64 |
Two kinds are quotations marks are also reserved: |
| 65 |
\begin{SML} |
| 66 |
[[ ]] |
| 67 |
`` '' |
| 68 |
\end{SML} |
| 69 |
|
| 70 |
The first \sml{[[ ]]} is for describing semantics. The |
| 71 |
second \sml{`` ''} is for describing assembly syntax. |
| 72 |
|
| 73 |
\paragraph{Syntactic Sugar} |
| 74 |
|
| 75 |
MDGen recognizes the following syntactic sugar. |
| 76 |
\begin{description} |
| 77 |
\item[Record abbreviations] |
| 78 |
Record expressions such as \sml{{x=x,y=y,z=z}} |
| 79 |
can be simplified to just \sml{{x,y,z}}. |
| 80 |
\item[Binary literals] |
| 81 |
Literals in binary can be written with the prefix \sml{0b} (for integer types) |
| 82 |
or \sml{0wb} (for word types). For example, \sml{0wb101111} is the same |
| 83 |
as \sml{0wx2f} and \sml{0w79}. |
| 84 |
\item[Bit slices] |
| 85 |
A bit slice, which extracts a range of bits from a word, can be written |
| 86 |
using an \sml{at} expression. For example, \sml{w at [16..18]} |
| 87 |
means the same thing as \verb|Word32.andb(Word32.>>(w, 0w16),0w7)|, i.e. |
| 88 |
it extracts bit 16 to 18 from \sml{w}. |
| 89 |
The least significant bit the zeroth bit. |
| 90 |
|
| 91 |
In general, we can write: |
| 92 |
\begin{SML} |
| 93 |
w at [range1, range2, ..., rangen] |
| 94 |
\end{SML} |
| 95 |
to extract a sequence of slices from $w$ and concatenate them together. |
| 96 |
For example, the expression |
| 97 |
\begin{SML} |
| 98 |
0wxabcd at [0..3, 4..7, 8..11, 12..15] |
| 99 |
\end{SML} |
| 100 |
swap the 4 nybbles from the 16-bit word, and evaluates to \sml{0wxdcba}. |
| 101 |
|
| 102 |
\item[Signature] |
| 103 |
Signature declarations of the form |
| 104 |
\begin{SML} |
| 105 |
val x y z : int -> int |
| 106 |
\end{SML} |
| 107 |
can be used as a shorthand for the more verbose: |
| 108 |
\begin{SML} |
| 109 |
val x : int -> int |
| 110 |
val y : int -> int |
| 111 |
val z : int -> int |
| 112 |
\end{SML} |
| 113 |
\end{description} |
| 114 |
|
| 115 |
\subsubsection{Elaboration Semantics} |
| 116 |
|
| 117 |
Unfortunately, there is no complete formal semantics of how |
| 118 |
an MDGen specification elaborates. |
| 119 |
But generally speaking, a machine description is a just a structure |
| 120 |
(in the SML sense). Different components of this structure describe |
| 121 |
different aspects of the architecture. |
| 122 |
|
| 123 |
\paragraph{Syntactic Overloading} |
| 124 |
In general, the syntactic overloading are used heavily in MDGen. |
| 125 |
There are three types of definitions: |
| 126 |
\begin{itemize} |
| 127 |
\item Definitions that defines properties of the instruction set. |
| 128 |
\item Definitions of functions and terms that are in the RTL meta-language. |
| 129 |
The syntax of MDGen's RTL language is borrowed heavily from Lambda-RTL, |
| 130 |
which in turns is borrowed heavily from SML. |
| 131 |
\item Definitions of functions and types that are to be included in the |
| 132 |
output generated by the MDGen tool. These are usually auxiliary |
| 133 |
helper functions and definitions. |
| 134 |
\end{itemize} |
| 135 |
In general, entities of type 2, when appearing in other context, are |
| 136 |
properly meta-quoted in the semantics quotations \sml{[[ ]]}. |
| 137 |
|
| 138 |
\subsubsection{Basic Structure of A Machine Description} |
| 139 |
|
| 140 |
The machine description for an architecture are defined via |
| 141 |
an \sml{architecture} declaration, which has the following general |
| 142 |
form. |
| 143 |
|
| 144 |
\begin{SML} |
| 145 |
architecture name = |
| 146 |
struct |
| 147 |
\Term{architecture type declaration} |
| 148 |
\Term{endianess declaration} |
| 149 |
\Term{storage class declarations} |
| 150 |
\Term{locations declarations} |
| 151 |
\Term{assembly case declarations} |
| 152 |
\Term{delayslot declaration} |
| 153 |
\Term{instruction machine encoding format declarations} |
| 154 |
\Term{nested structure declarations} |
| 155 |
\Term{instruction definition} |
| 156 |
end |
| 157 |
\end{SML} |
| 158 |
|
| 159 |
\subsection{Describing the Architecture} |
| 160 |
|
| 161 |
\subsubsection{Architecture type} |
| 162 |
Architecture type declaration specifies whether the architecture is |
| 163 |
a superscalar or a VLIW/EPIC machine. Currently, this information is |
| 164 |
ignored. |
| 165 |
|
| 166 |
\begin{SML} |
| 167 |
\Term{architecture type declaration} ::= superscalar | vliw |
| 168 |
\end{SML} |
| 169 |
|
| 170 |
\subsubsection{Storage class} |
| 171 |
|
| 172 |
Storage class declarations specify various information about the |
| 173 |
registers in the architecture. For example, the Alpha has 32 general |
| 174 |
purpose registers and 32 floating point registers. In addition, MLRISC |
| 175 |
requires that each architecture specifies a (pseudo) register |
| 176 |
type\footnote{Called cellkind in MLRISC.} for |
| 177 |
holding condition codes (\sml{CC}). |
| 178 |
To specify these information in MDGen, we can say: |
| 179 |
|
| 180 |
\begin{SML} |
| 181 |
storage |
| 182 |
GP "r" = 32 cells of 64 bits in cellset called "register" |
| 183 |
assembly as (fn (30,_) => "$sp" |
| 184 |
| (r,_) => "$"^Int.toString r |
| 185 |
) |
| 186 |
| FP "f" = 32 cells of 64 bits in cellset called "floating point register" |
| 187 |
assembly as (fn (f,_) => "$f"^Int.toString f) |
| 188 |
| CC "cc" = cells of 64 bits in cellset GP called "condition code register" |
| 189 |
assembly as "cc" |
| 190 |
\end{SML} |
| 191 |
|
| 192 |
\begin{itemize} |
| 193 |
\item There are 32 64-bit general purpose registers, |
| 194 |
32 64-bit floating point registers, while \sml{CC} is not a |
| 195 |
real register type. |
| 196 |
\item Cellsets |
| 197 |
are used by MLRISC for annotating liveness information in the program. |
| 198 |
The clause \sml{in cellset} states that register type \sml{GP} |
| 199 |
and \sml{FP} are allotted their own components in the cellset, |
| 200 |
while the register type \sml{CC} are put |
| 201 |
in the same cellset component as \sml{GP}. |
| 202 |
\item The clause \sml{assembly as} specifies |
| 203 |
how each register is to be pretty printed. On the Alpha, general |
| 204 |
purpose register are pretty printed with prefix \sml{$}, while |
| 205 |
floating point registers are pretty printed with the prefix \sml{$f}. |
| 206 |
A special case is made for register 30, which is the stack pointer, and |
| 207 |
is pretty printing as \sml{$sp}. Pseudo condition code registers |
| 208 |
are pretty printed with the prefix \sml{cc}. |
| 209 |
\end{itemize} |
| 210 |
|
| 211 |
\subsubsection{Locations} |
| 212 |
|
| 213 |
Special locations in the register files can be declared using the |
| 214 |
\sml{locations} declarations. On the Alpha, GPR |
| 215 |
30 is the stack pointer, GPR 28 and floating point register 30 |
| 216 |
are used as the assembly temporaries. This special constants |
| 217 |
can be defined as follows: |
| 218 |
|
| 219 |
\begin{SML} |
| 220 |
locations |
| 221 |
stackptrR = $GP[30] |
| 222 |
and asmTmpR = $GP[28] |
| 223 |
and fasmTmp = $FP[30] |
| 224 |
\end{SML} |
| 225 |
|
| 226 |
\subsection{Specifying the Machine Encoding} |
| 227 |
\subsubsection{Endianess} |
| 228 |
|
| 229 |
The endianess declaration specifies whether the machine is little |
| 230 |
endian or big endian so that the correct machine instruction encoding |
| 231 |
functions can be generated. The general syntax of this is: |
| 232 |
|
| 233 |
\begin{SML} |
| 234 |
\Term{endianess declaration} ::= little endian | big endian |
| 235 |
\end{SML} |
| 236 |
|
| 237 |
The Alpha is little endian, so we just say: |
| 238 |
\begin{SML} |
| 239 |
little endian |
| 240 |
\end{SML} |
| 241 |
|
| 242 |
\subsubsection{Defining New Instruction Formats} |
| 243 |
|
| 244 |
How instructions are encoded are specified using |
| 245 |
\sml{instruction format} declarations. An instruction format declaration |
| 246 |
has the following syntax: |
| 247 |
\begin{SML} |
| 248 |
\Term{instruction machine encoding format declarations} ::= |
| 249 |
instruction formats n bits |
| 250 |
\Term{format}1 |
| 251 |
| \Term{format}2 |
| 252 |
| \Term{format}3 |
| 253 |
| ... |
| 254 |
| \Term{format}n-1 |
| 255 |
| \Term{format}n |
| 256 |
\end{SML} |
| 257 |
|
| 258 |
Each encoding format can be a primitive format, or a derived format. |
| 259 |
|
| 260 |
\paragraph{Primitive formats} |
| 261 |
|
| 262 |
A primitive format is simply specified by giving it a name and specifying |
| 263 |
the position, names and types of its fields. This is usually the same |
| 264 |
way it is described in a architectural reference manual. |
| 265 |
|
| 266 |
|
| 267 |
Here is how we specify some of the (32 bit) primitive instruction formats |
| 268 |
used in the Alpha. |
| 269 |
\begin{SML} |
| 270 |
instruction formats 32 bits |
| 271 |
Memory\{opc:6, ra:5, rb:GP 5, disp: signed 16\} |
| 272 |
| Jump\{opc:6=0wx1a,ra:GP 5,rb:GP 5,h:2,disp:int signed 14\} |
| 273 |
| Memory_fun\{opc:6, ra:GP 5, rb:GP 5, func:16\} |
| 274 |
| Branch\{opc:branch 6, ra:GP 5, disp:signed 21\} |
| 275 |
| Fbranch\{opc:fbranch 6, ra:FP 5, disp:signed 21\} |
| 276 |
| Operate0\{opc:6,ra:GP 5,rb:GP 5,sbz:13..15=0,_:1=0,func:5..11,rc:GP 5\} |
| 277 |
| Operate1\{opc:6,ra:GP 5,lit:signed 13..20,_:1=1,func:5..11,rc:GP 5\} |
| 278 |
\end{SML} |
| 279 |
|
| 280 |
For example, the format \sml{Memory} |
| 281 |
\begin{SML} |
| 282 |
Memory\{opc:6, ra:5, rb:GP 5, disp: signed 16\} |
| 283 |
\end{SML} |
| 284 |
has a 6-bit opcode field, a 5-bit \sml{ra} field, a 5-bit \sml{rb} |
| 285 |
field which always hold a general purpose register, and a 16-bit |
| 286 |
sign-extended displacement field. The field to the left is positioned |
| 287 |
at the most significant bits, while the field to the right is positioned |
| 288 |
at the least. The widths of these fields must add up to 32 bits. |
| 289 |
|
| 290 |
|
| 291 |
Similarly, the format \sml{Jump} |
| 292 |
\begin{SML} |
| 293 |
Jump{opc:6=0wx1a,ra:GP 5,rb:GP 5,h:2,disp:int signed 14} |
| 294 |
\end{SML} |
| 295 |
contains a 6-bit opcode field which always hold the constant \sml{0x1a}, |
| 296 |
two 5-bit fields \sml{ra} and \sml{rb} which are of type \sml{GP}, |
| 297 |
and a 14-bit sign-extended field of type integer. |
| 298 |
|
| 299 |
Each field in a primitive format has one of 5 forms: |
| 300 |
\begin{SML} |
| 301 |
\Term{name} : \Term{position} |
| 302 |
\Term{name} : \Term{position} = \Term{value} |
| 303 |
\Term{name} : \Term{type} \Term{position} |
| 304 |
\Term{name} : \Term{type} \Term{position} = \Term{value} |
| 305 |
_ : \Term{position} = \Term{value} |
| 306 |
\end{SML} |
| 307 |
where \Term{position} is either a width, or a bits range |
| 308 |
$n$\sml{..}$m$, |
| 309 |
with an optional \sml{signed} prefix. The last form, with a wild card |
| 310 |
for the field name, can be used to specify an anonymous field that |
| 311 |
always has a fixed value. |
| 312 |
|
| 313 |
|
| 314 |
By default, a field has type \sml{Word32.word}. If a type $T$ |
| 315 |
is specified, then the function \sml{emit_}$T$ is implicitly called |
| 316 |
to convert the type into the appropriate encoding. The function |
| 317 |
\sml{emit_}$T$ are generated automatically by MDGen if it is a cellkind |
| 318 |
defined by the \sml{storage} class declaration, or if it is a primitive |
| 319 |
type such as integer or boolean. |
| 320 |
There are also other ways to automatically generate this function |
| 321 |
(more on this later.) |
| 322 |
|
| 323 |
For example, the format \sml{Operate1} |
| 324 |
\begin{SML} |
| 325 |
Operate1\{opc:6,ra:GP 5,lit:signed 13..20,_:1=1,func:5..11,rc:GP 5\} |
| 326 |
\end{SML} |
| 327 |
states that bits 26 to 31 are allocated to field \sml{opc}, |
| 328 |
bits 21 to 25 are allocated to field \sml{ra}, which is of type |
| 329 |
\sml{GP}, bits 13 to 20 are allocated to field \sml{lit}, bit 12 |
| 330 |
is a single bit of value 1, etc. |
| 331 |
|
| 332 |
|
| 333 |
MDGen generates a function for each primitive format declaration of |
| 334 |
the same name that can be used for emitting the instruction. |
| 335 |
In the case of the Alpha, the following functions are generated: |
| 336 |
\begin{SML} |
| 337 |
val Memory : \{opc:Word32.word, ra:Word32.word, |
| 338 |
rb:int, disp:Word32.word\} -> unit |
| 339 |
val Jump : \{ra:int, rb:int, disp:Word32.word\} -> unit |
| 340 |
val Operate1 : \{opc:Word32.word, ra:int, lit:Word32.word, |
| 341 |
func:Word32.word, rc:int\} -> unit |
| 342 |
\end{SML} |
| 343 |
|
| 344 |
\paragraph{Derived formats} |
| 345 |
|
| 346 |
Derived formats are simply instruction formats that are defined |
| 347 |
in terms of other formats. On the alpha, we have a \sml{Operate} |
| 348 |
format that simplifies to either \sml{Operate0} or \sml{Operate1}, |
| 349 |
depending on whether the second argument is a literal or a register. |
| 350 |
\begin{SML} |
| 351 |
Operate\{opc,ra,rb,func,rc\} = |
| 352 |
(case rb of |
| 353 |
I.REGop rb => Operate0\{opc,ra,rb,func,rc\} |
| 354 |
| I.IMMop i => Operate1\{opc,ra,lit=itow i,func,rc\} |
| 355 |
| I.HILABop le => Operate1\{opc,ra,lit=High{le=le},func,rc\} |
| 356 |
| I.LOLABop le => Operate1\{opc,ra,lit=Low{le=le},func,rc\} |
| 357 |
| I.LABop le => Operate1\{opc,ra,lit=itow(LabelExp.valueOf le),func,rc\} |
| 358 |
) |
| 359 |
\end{SML} |
| 360 |
|
| 361 |
\subsubsection{Generating Encoding Functions} |
| 362 |
|
| 363 |
In MLRISC, we represent an instruction as a set of ML datatypes. |
| 364 |
Some of these datatypes represent specific fields or |
| 365 |
opcodes of the instructions. |
| 366 |
MDGen lets us to associate a machine encoding to each datatype constructor |
| 367 |
directly in the specification, and automatically generates an |
| 368 |
encoding function for these datatypes. |
| 369 |
|
| 370 |
There are two different ways of specifying an encoding. The first way |
| 371 |
is just to write the machine encoding directly next the constructor. |
| 372 |
Here's an example directly from the Alpha description: |
| 373 |
\begin{SML} |
| 374 |
structure Instruction = |
| 375 |
struct |
| 376 |
datatype branch! = (* table C-2 *) |
| 377 |
BR 0x30 |
| 378 |
| BSR 0x34 |
| 379 |
| BLBC 0x38 |
| 380 |
| BEQ 0x39 | BLT 0x3a | BLE 0x3b |
| 381 |
| BLBS 0x3c | BNE 0x3d | BGE 0x3e |
| 382 |
| BGT 0x3f |
| 383 |
|
| 384 |
datatype fbranch! = (* table C-2 *) |
| 385 |
FBEQ 0x31 | FBLT 0x32 |
| 386 |
| FBLE 0x33 | FBNE 0x35 |
| 387 |
| FBGE 0x36 | FBGT 0x37 |
| 388 |
|
| 389 |
... |
| 390 |
end |
| 391 |
\end{SML} |
| 392 |
|
| 393 |
The datatypes \sml{branch} and \sml{fbranch} represent specific |
| 394 |
branch opcodes for integer branches \sml{BRANCH}, or floating point |
| 395 |
branches \sml{FBRANCH}. On the Alpha, instruction \sml{BR} is encoded |
| 396 |
with an opcode of \sml{0x30}, instruction \sml{BSR} is encoded |
| 397 |
as \sml{0x34} etc. MDGen automatically generates two functions |
| 398 |
\begin{SML} |
| 399 |
val emit_branch : branch -> Word32.word |
| 400 |
val emit_fbranch : branch -> Word32.word |
| 401 |
\end{SML} |
| 402 |
that perform this encoding. |
| 403 |
|
| 404 |
In the specification for the instruction set, we state that the |
| 405 |
\sml{BRANCH} instruction should be encoded using format \sml{Branch}, |
| 406 |
while the \sml{FBRANCH} instruction should be encoded using |
| 407 |
format \sml{Fbranch}. |
| 408 |
\begin{SML} |
| 409 |
structure MC = |
| 410 |
struct |
| 411 |
(* Auxiliary function for computing the displacement of a label *) |
| 412 |
fun disp ... = ... |
| 413 |
... |
| 414 |
end |
| 415 |
|
| 416 |
... |
| 417 |
|
| 418 |
instruction |
| 419 |
... |
| 420 |
|
| 421 |
| BRANCH of branch * $GP * Label.label |
| 422 |
Branch\{opc=branch,ra=GP,disp=disp label\} |
| 423 |
|
| 424 |
| FBRANCH of fbranch * $FP * Label.label |
| 425 |
Fbranch\{opc=fbranch,ra=FP,disp=disp label\} |
| 426 |
|
| 427 |
| ... |
| 428 |
\end{SML} |
| 429 |
|
| 430 |
Since the primitive instructions formats \sml{Branch} and \sml{FBranch} |
| 431 |
are defined with branch and fbranch as the type in the opcode field |
| 432 |
\begin{SML} |
| 433 |
| Branch\{opc:branch 6, ra:GP 5, disp:signed 21\} |
| 434 |
| Fbranch\{opc:fbranch 6, ra:FP 5, disp:signed 21\} |
| 435 |
\end{SML} |
| 436 |
the functions \sml{emit_branch} and \sml{emit_fbranch} are implicitly |
| 437 |
called. |
| 438 |
|
| 439 |
|
| 440 |
Another way to specify an encoding is to specify a range, as |
| 441 |
in the following example: |
| 442 |
\begin{SML} |
| 443 |
datatype fload[0x20..0x23]! = LDF | LDG | LDS | LDT |
| 444 |
|
| 445 |
datatype fstore[0x24..0x27]! = STF | STG | STS | STT |
| 446 |
\end{SML} |
| 447 |
|
| 448 |
This states that \sml{LDF} should be assigned the encoding \sml{0x20}, |
| 449 |
\sml{LDG} the encoding \sml{0x21} etc. This form is useful for |
| 450 |
specifying a consecutive range. |
| 451 |
|
| 452 |
\subsubsection{Encoding Variable Length Instructions} |
| 453 |
|
| 454 |
Most architectures nowadays have fixed length encodings for instructions. |
| 455 |
There are some notatable exceptions, however. |
| 456 |
The Intel x86 architecture uses a legacy |
| 457 |
variable length encoding. Modern RISC machines developed for |
| 458 |
embedded systems may utilize space-reduction compression schemes in their |
| 459 |
instruction sets. Finally, VLIW machines usually have some form |
| 460 |
of NOP compression scheme for compacting issue packets. |
| 461 |
|
| 462 |
\subsection{Specifying the Assembly Formats} |
| 463 |
|
| 464 |
\subsubsection{Assembly Case Declaration} |
| 465 |
|
| 466 |
The assembly case declaration specifies whether the assembly should be |
| 467 |
emitted in lower case, upper case, or verbatim. If either lower case |
| 468 |
or upper case is specified, all literal strings are converted to the |
| 469 |
appropriate case. The general syntax of this declaration is: |
| 470 |
|
| 471 |
\begin{SML} |
| 472 |
\Term{assembly case declaration} ::= |
| 473 |
lowercase assembly |
| 474 |
| uppercase assembly |
| 475 |
| verbatim assembly |
| 476 |
\end{SML} |
| 477 |
|
| 478 |
\subsubsection{Assembly Annotations} |
| 479 |
|
| 480 |
Assembly output are specified in the assembly meta quotations |
| 481 |
\sml{`` ''}, or string quotations \sml{" "}. |
| 482 |
For example, here is a fragment from the Alpha description: |
| 483 |
|
| 484 |
\begin{SML} |
| 485 |
instruction |
| 486 |
... |
| 487 |
| LOAD of \{ldOp:load, r: $GP, b: $GP, d:operand, mem:Region.region\} |
| 488 |
``<ldOp>\t<r>, <d>()<mem>'' |
| 489 |
|
| 490 |
| STORE of \{stOp:store, r: $GP, b: $GP, d:operand, mem:Region.region\} |
| 491 |
``<stOp>\t<r>, <d>()<mem>'' |
| 492 |
|
| 493 |
| BRANCH of branch * $GP * Label.label |
| 494 |
``<branch>\t<GP>, <label>'' |
| 495 |
|
| 496 |
| FBRANCH of fbranch * $FP * Label.label |
| 497 |
``<fbranch>\t<FP>, <label>'' |
| 498 |
|
| 499 |
| CMOVE of \{oper:cmove, ra: $GP, rb:operand, rc: $GP\} |
| 500 |
``<oper>\t<ra>, <rb>, <rc>'' |
| 501 |
|
| 502 |
| FOPERATE of \{oper:foperate, fa: $FP, fb: $FP, fc: $FP\} |
| 503 |
``<oper>\t<fa>, <fb>, <fc>'' |
| 504 |
|
| 505 |
| ... |
| 506 |
\end{SML} |
| 507 |
|
| 508 |
All characters within the quotations \sml{`` ''} have the same |
| 509 |
interpretation as in the string quotation \sml{" "}, except when |
| 510 |
they are delimited by the \newdef{backquotes} |
| 511 |
\verb|< >|. |
| 512 |
Here's how the backquote is interpreted: |
| 513 |
\begin{itemize} |
| 514 |
\item If it is \verb|<|$x$\verb.>. and $x$ is a variable name of type $t$, |
| 515 |
and if an assembly function of type $t$ is defined, then it will be invoked |
| 516 |
to convert $x$ to the appropriate text. |
| 517 |
\item If it is \verb|<|$x$\verb.>. and $x$ is a variable name of type $t$, |
| 518 |
and if an assembly function of type $t$ is NOT defined, |
| 519 |
then the function \sml{emit_}$x$ will be called to pretty print $x$. |
| 520 |
\item If it is \verb|<|$e$\verb.>. where $e$ is a general expression, then |
| 521 |
it will be used directly. |
| 522 |
\end{itemize} |
| 523 |
|
| 524 |
\subsubsection{Generating Assembly Functions} |
| 525 |
Similar to machine encodings, we can attach assembly annotations to |
| 526 |
datatype definitions and let MDGen generate the assembly functions for us. |
| 527 |
Annotations take two forms, explicit or implicit. |
| 528 |
Explicit annotations are enclosed within assembly quotations \sml{`` ''}. |
| 529 |
|
| 530 |
|
| 531 |
For example, on the Alpha the datatype \sml{operand} is used to represent |
| 532 |
an integer operand. This datatype is defined as follows: |
| 533 |
\begin{SML} |
| 534 |
datatype operand = |
| 535 |
REGop of $GP ``<GP>'' |
| 536 |
| IMMop of int ``<int>'' |
| 537 |
| HILABop of LabelExp.labexp ``hi(<labexp>)'' |
| 538 |
| LOLABop of LabelExp.labexp ``lo(<labexp>)'' |
| 539 |
| LABop of LabelExp.labexp ``<labexp>'' |
| 540 |
| CONSTop of Constant.const ``<const>'' |
| 541 |
\end{SML} |
| 542 |
Basicaly this states that \sml{REGop r} should be pretty printed |
| 543 |
as \sml{$r}, \sml{IMMop i} |
| 544 |
as \sml{i}, \sml{HILABexp le} |
| 545 |
as \sml{hi(le)}, |
| 546 |
etc. |
| 547 |
|
| 548 |
Implicit assembly annotations are specified by simply attaching |
| 549 |
an exclamation mark at the end of the datatype name. This states |
| 550 |
that the assembly output is the same as the name of the datatype |
| 551 |
constructor\footnote{But appropriately modified by the assembly case |
| 552 |
declaration.}. For example, |
| 553 |
the datatype \sml{operate} is a listing of all integer opcodes |
| 554 |
used in MLRISC. |
| 555 |
\begin{SML} |
| 556 |
datatype operate! = (* table C-5 *) |
| 557 |
ADDL (0wx10,0wx00) | ADDQ (0wx10,0wx20) |
| 558 |
| CMPBGE(0wx10,0wx0f) | CMPEQ (0wx10,0wx2d) |
| 559 |
| CMPLE (0wx10,0wx6d) | CMPLT (0wx10,0wx4d) | CMPULE (0wx10,0wx3d) |
| 560 |
| CMPULT(0wx10,0wx1d) | SUBL (0wx10,0wx09) |
| 561 |
| SUBQ (0wx10,0wx29) |
| 562 |
| S4ADDL(0wx10,0wx02) | S4ADDQ (0wx10,0wx22) | S4SUBL (0wx10,0wx0b) |
| 563 |
| S4SUBQ(0wx10,0wx2b) | S8ADDL (0wx10,0wx12) | S8ADDQ (0wx10,0wx32) |
| 564 |
| S8SUBL(0wx10,0wx1b) | S8SUBQ (0wx10,0wx3b) |
| 565 |
|
| 566 |
| AND (0wx11,0wx00) | BIC (0wx11,0wx08) | BIS (0wx11,0wx20) |
| 567 |
| EQV (0wx11,0wx48) |
| 568 |
| ORNOT (0wx11,0wx28) | XOR (0wx11,0wx40) |
| 569 |
|
| 570 |
| EXTBL (0wx12,0wx06) | EXTLH (0wx12,0wx6a) | EXTLL(0wx12,0wx26) |
| 571 |
| EXTQH (0wx12,0wx7a) | EXTQL (0wx12,0wx36) | EXTWH(0wx12,0wx5a) |
| 572 |
| EXTWL (0wx12,0wx16) | INSBL (0wx12,0wx0b) | INSLH(0wx12,0wx67) |
| 573 |
| INSLL (0wx12,0wx2b) | INSQH (0wx12,0wx77) | INSQL(0wx12,0wx3b) |
| 574 |
| INSWH (0wx12,0wx57) | INSWL (0wx12,0wx1b) | MSKBL(0wx12,0wx02) |
| 575 |
| MSKLH (0wx12,0wx62) | MSKLL (0wx12,0wx22) | MSKQH(0wx12,0wx72) |
| 576 |
| MSKQL (0wx12,0wx32) | MSKWH (0wx12,0wx52) | MSKWL(0wx12,0wx12) |
| 577 |
| SLL (0wx12,0wx39) | SRA (0wx12,0wx3c) | SRL (0wx12,0wx34) |
| 578 |
| ZAP (0wx12,0wx30) | ZAPNOT (0wx12,0wx31) |
| 579 |
| MULL (0wx13,0wx00) | MULQ (0wx13,0wx20) |
| 580 |
| UMULH (0wx13,0wx30) |
| 581 |
| SGNXL "addl" (0wx10,0wx00) (* same as ADDL *) |
| 582 |
\end{SML} |
| 583 |
This definitions states that \sml{ADDL} should be pretty printed |
| 584 |
as \sml{addl}, \sml{ADDQ} as \sml{addq}, etc. However, the opcode |
| 585 |
\sml{SGNXL} is pretty printed as \sml{addl} since it has been explicitly |
| 586 |
overridden. |
| 587 |
|
| 588 |
\subsection{Defining the Instruction Set} |
| 589 |
|
| 590 |
How the instruction set is represented is declared using the |
| 591 |
\sml{instruction} declaration. For example, here's how the Alpha instruction |
| 592 |
set is defined: |
| 593 |
\begin{SML} |
| 594 |
instruction |
| 595 |
DEFFREG of $FP |
| 596 |
| LDA of \{r: $GP, b: $GP, d:operand\} |
| 597 |
| LDAH of \{r: $GP, b: $GP, d:operand\} |
| 598 |
| LOAD of \{ldOp:load, r: $GP, b: $GP, d:operand, mem:Region.region\} |
| 599 |
| STORE of \{stOp:store, r: $GP, b: $GP, d:operand, mem:Region.region\} |
| 600 |
| FLOAD of \{ldOp:fload, r: $FP, b: $GP, d:operand, mem:Region.region\} |
| 601 |
| FSTORE of \{stOp:fstore, r: $FP, b: $GP, d:operand, mem:Region.region\} |
| 602 |
| JMPL of \{r: $GP, b: $GP, d:int\} * Label.label list |
| 603 |
| JSR of \{r: $GP, b: $GP, d:int\} * C.cellset * C.cellset * Region.region |
| 604 |
| RET of \{r: $GP, b: $GP, d:int\} |
| 605 |
| BRANCH of branch * $GP * Label.label |
| 606 |
| FBRANCH of fbranch * $FP * Label.label |
| 607 |
| ... |
| 608 |
\end{SML} |
| 609 |
|
| 610 |
The \sml{instruction} declaration defines a datatype and specifies |
| 611 |
that this datatype is used to represent the instruction set. Generally |
| 612 |
speaking, the instruction set's designer has complete freedom in how the |
| 613 |
datatype is structured, but there are a few simple rules that she should |
| 614 |
follow: |
| 615 |
\begin{itemize} |
| 616 |
\item If a field represents a register, it should be typed |
| 617 |
with the appropriate storage types \sml{$GP}, |
| 618 |
\sml{$FP}, etc.~instead |
| 619 |
of \sml{int}. MDGen will treat its value in the correct manner; for |
| 620 |
example, during assembly emission a field declared type \sml{int} is |
| 621 |
printed as an integer, while a field declared type \sml{$GP} is displayed |
| 622 |
as a general purpose register. |
| 623 |
\item MDGen recognizes the following special |
| 624 |
types: \sml{label}, \sml{labexp}, \sml{region}, and \sml{cellset}. |
| 625 |
\end{itemize} |
| 626 |
|
| 627 |
\subsection{Specifying Instruction Semantics} |
| 628 |
MLRISC performs all optimizations at |
| 629 |
the granulariy of individual instructions, |
| 630 |
specialized to the architecture at hand. Many |
| 631 |
optimizations are possible only if the ``semantics'' of the |
| 632 |
instructions set to are properly specified. MDGen contains a |
| 633 |
\emph{register transfer language} (RTL) sub-language that let us to describe |
| 634 |
instruction semantics in a modular and succinct manner. |
| 635 |
|
| 636 |
The semantics of this RTL sub-language has been borrowed heavily from |
| 637 |
Norman Ramsey's and Jack Davidson's Lambda RTL. There |
| 638 |
are a few main differences, however: |
| 639 |
\begin{itemize} |
| 640 |
\item The syntax of our RTL language |
| 641 |
is closer to that of ML than Lambda RTL. |
| 642 |
\item Our RTL language, like that of MDGen, is tied closely to MLRISC. |
| 643 |
\end{itemize} |
| 644 |
|
| 645 |
\subsection{How to Run the Tool} |
| 646 |
|
| 647 |
\subsection{Machine Description} |
| 648 |
Here are some machine descriptions in varing degree of completion. |
| 649 |
|
| 650 |
\begin{itemize} |
| 651 |
\item \mlrischref{sparc/sparc.mdl}{Sparc} |
| 652 |
\item \mlrischref{hppa/hppa.mdl}{Hppa} |
| 653 |
\item \mlrischref{alpha/alpha.mdl}{Alpha} |
| 654 |
\item \mlrischref{ppc/ppc.mdl}{PowerPC} |
| 655 |
\item \mlrischref{x86/x86.mdl}{X86} |
| 656 |
\end{itemize} |
| 657 |
|
| 658 |
\subsection{ Syntax Highlighting Macros } |
| 659 |
|
| 660 |
\begin{itemize} |
| 661 |
\item \href{md.vim}{For vim 5.3} |
| 662 |
\end{itemize} |